Project

General

Profile

Download (9.82 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / deferredjob / DeferredJobRenderer.java @ 1c41c4c9

1 887e1853 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.deferredjob;
21
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.library.effect.EffectType;
28 ba984444 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
29 887e1853 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31 38b31baa Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
32 887e1853 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.mesh.MeshJoined;
39 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshTriangle;
40 887e1853 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.DynamicQuat;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 061449ed Leszek Koltunski
class DeferredJobRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
52 887e1853 Leszek Koltunski
{
53
    private GLSurfaceView mView;
54
    private DistortedTexture mTexture;
55
    private DistortedScreen mScreen;
56
    private DistortedEffects mEffects;
57
    private Static3D mScale;
58
    private MeshBase mMesh;
59 bf3b5480 Leszek Koltunski
    private VertexEffectRotate mRotate;
60
    private Dynamic1D mAngleDyn;
61
    private Static1D mAngle;
62 887e1853 Leszek Koltunski
63
    Static4D mQuat1, mQuat2;
64
    int mScreenMin;
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
    DeferredJobRenderer(GLSurfaceView v)
69
      {
70
      mView = v;
71
      mScreen = new DistortedScreen();
72
      mScale= new Static3D(1,1,1);
73
      Static3D center=new Static3D(0,0,0);
74
75
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
76
      sink.add( new Static1D(0.5f) );
77
      sink.add( new Static1D(2.0f) );
78
79 9cbe58b0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
80
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
81 887e1853 Leszek Koltunski
82
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
83
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
84
85
      quatInt1.add(mQuat1);
86
      quatInt2.add(mQuat2);
87
88 bf3b5480 Leszek Koltunski
      mAngle = new Static1D(0);
89
90
      mAngleDyn = new Dynamic1D(2000,0.5f);
91
      mAngleDyn.add(new Static1D(0));
92
      mAngleDyn.add( mAngle );
93
94
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
95 887e1853 Leszek Koltunski
96
      mEffects = new DistortedEffects();
97 bf3b5480 Leszek Koltunski
      mEffects.apply( mRotate );
98 887e1853 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
99
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
100
      mEffects.apply( new MatrixEffectScale(mScale));
101
102
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
103
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
    public void onDrawFrame(GL10 glUnused) 
108
      {
109
      mScreen.render( System.currentTimeMillis() );
110
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
115
      {
116 d08bd5a3 Leszek Koltunski
      final float SCALE = 0.4f;
117 887e1853 Leszek Koltunski
      mScreenMin = Math.min(width, height);
118
      float factor = SCALE*mScreenMin;
119
      mScale.set(factor,factor,factor);
120
      mScreen.resize(width, height);
121
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126
      {
127
      if( mTexture==null ) mTexture = new DistortedTexture();
128 bf3b5480 Leszek Koltunski
      mTexture.setTexture( createTexture() );
129 887e1853 Leszek Koltunski
130 bf3b5480 Leszek Koltunski
      if( mMesh==null ) mMesh = createMesh();
131 887e1853 Leszek Koltunski
132
      mScreen.detachAll();
133
      mScreen.attach(mTexture,mEffects,mMesh);
134
135
      DistortedLibrary.setMax(EffectType.VERTEX, 11);
136 bf3b5480 Leszek Koltunski
      VertexEffectRotate.enable();
137 887e1853 Leszek Koltunski
138 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this);
139 061449ed Leszek Koltunski
      }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
    public void distortedException(Exception ex)
144
      {
145
      android.util.Log.e("DeferredJob", ex.getMessage() );
146 887e1853 Leszek Koltunski
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 bf3b5480 Leszek Koltunski
    void apply(int number)
151 887e1853 Leszek Koltunski
      {
152 eb507734 Leszek Koltunski
      mRotate.setMeshAssociation(0,number);
153 bf3b5480 Leszek Koltunski
      mAngle.set(360);
154
      mAngleDyn.resetToBeginning();
155 887e1853 Leszek Koltunski
      }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159 bf3b5480 Leszek Koltunski
    private MeshBase createMesh()
160 887e1853 Leszek Koltunski
      {
161 ba984444 Leszek Koltunski
      final int MESHES=4;
162 887e1853 Leszek Koltunski
163 f2085b96 Leszek Koltunski
      MeshBase[] meshes = new MeshTriangle[MESHES];
164 38b31baa Leszek Koltunski
      VertexEffectMove[] moveEffect = new VertexEffectMove[MESHES];
165 887e1853 Leszek Koltunski
166 f2085b96 Leszek Koltunski
      meshes[0] = new MeshTriangle(1);
167 eb507734 Leszek Koltunski
      meshes[0].setEffectAssociation(0,1,0);
168 bf3b5480 Leszek Koltunski
169
      for(int i=1; i<MESHES; i++)
170 887e1853 Leszek Koltunski
        {
171 bf3b5480 Leszek Koltunski
        meshes[i] = meshes[0].copy(true);
172 eb507734 Leszek Koltunski
        meshes[i].setEffectAssociation(0,1,i);
173 887e1853 Leszek Koltunski
        }
174
175 ba984444 Leszek Koltunski
      Static4D[] textureMaps = new Static4D[MESHES];
176 5dfbbda7 Leszek Koltunski
177
      for(int i=0; i<MESHES; i++)
178
        {
179 ba984444 Leszek Koltunski
        textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
180 5dfbbda7 Leszek Koltunski
        }
181
182
      MeshBase[] tmp = new MeshBase[2];
183
184
      tmp[0] = new MeshJoined(meshes);
185 887e1853 Leszek Koltunski
186 ba984444 Leszek Koltunski
      for(int i=0; i<MESHES; i++)
187
        {
188 38b31baa Leszek Koltunski
        moveEffect[i] = new VertexEffectMove( new Static3D(0,0,0.5f-i/(MESHES-1.0f)) );
189
        moveEffect[i].setMeshAssociation(0,i);
190
        tmp[0].apply(moveEffect[i]);
191 ba984444 Leszek Koltunski
        }
192 887e1853 Leszek Koltunski
193 5dfbbda7 Leszek Koltunski
      tmp[1] = tmp[0].copy(true);
194
195 fcb09e1f Leszek Koltunski
      tmp[0].mergeEffComponents();
196
      tmp[1].mergeEffComponents();
197 5dfbbda7 Leszek Koltunski
198 eb507734 Leszek Koltunski
      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
199
      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
200
201 5dfbbda7 Leszek Koltunski
      MeshBase combined = new MeshJoined(tmp);
202
203 ba984444 Leszek Koltunski
      MatrixEffectMove moveL = new MatrixEffectMove( new Static3D(-0.6f,0,0) );
204
      MatrixEffectMove moveR = new MatrixEffectMove( new Static3D(+0.6f,0,0) );
205
206
      combined.apply(moveL,0,0);
207
      combined.apply(moveR,0,1);
208 5dfbbda7 Leszek Koltunski
209 ba984444 Leszek Koltunski
      combined.setTextureMap(textureMaps,0);
210
      combined.setTextureMap(textureMaps,MESHES);
211 5dfbbda7 Leszek Koltunski
212 38b31baa Leszek Koltunski
      combined.setComponentCenter(0,-0.6f,0,0);
213
      combined.setComponentCenter(1,+0.6f,0,0);
214 b1950186 Leszek Koltunski
215 5dfbbda7 Leszek Koltunski
      return combined;
216 887e1853 Leszek Koltunski
      }
217 ba984444 Leszek Koltunski
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
    private Bitmap createTexture()
221
      {
222
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
223
      final int FACES=FACE_COLORS.length;
224
      int SIZE = 200;
225
      float STROKE = 0.05f*SIZE;
226
      float OFF = STROKE/2 -1;
227
      float OFF2 = 0.5f*SIZE + OFF;
228
      float HEIGHT = SIZE - OFF;
229
      float RADIUS = SIZE/12;
230
      float ARC1_H = 0.2f*SIZE;
231
      float ARC1_W = SIZE*0.5f;
232
      float ARC2_W = 0.153f*SIZE;
233
      float ARC2_H = 0.905f*SIZE;
234
      float ARC3_W = SIZE-ARC2_W;
235
236
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
237
      Canvas canvas = new Canvas(result);
238
      Paint paint = new Paint();
239
      paint.setAntiAlias(true);
240
      paint.setStrokeWidth(STROKE);
241
242
      for(int i=0; i<FACES; i++)
243
        {
244
        paint.setColor(FACE_COLORS[i]);
245
        paint.setStyle(Paint.Style.FILL);
246
247
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
248
249
        paint.setColor(0xff000000);
250
        paint.setStyle(Paint.Style.STROKE);
251
252
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
253
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
254
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
255
256
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
257
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
258
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
259
        }
260
261
      return result;
262
      }
263
264 887e1853 Leszek Koltunski
}