Project

General

Profile

« Previous | Next » 

Revision bf3b5480

Added by Leszek Koltunski almost 4 years ago

Progress with Deferred Jobs.

View differences:

src/main/java/org/distorted/examples/deferredjob/DeferredJobRenderer.java
31 31
import org.distorted.library.effect.VertexEffectMove;
32 32
import org.distorted.library.effect.VertexEffectRotate;
33 33
import org.distorted.library.effect.VertexEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35 34
import org.distorted.library.main.DistortedEffects;
36 35
import org.distorted.library.main.DistortedLibrary;
37 36
import org.distorted.library.main.DistortedScreen;
38 37
import org.distorted.library.main.DistortedTexture;
39 38
import org.distorted.library.mesh.MeshBase;
40 39
import org.distorted.library.mesh.MeshJoined;
40
import org.distorted.library.mesh.MeshRectangles;
41 41
import org.distorted.library.mesh.MeshTriangles;
42 42
import org.distorted.library.type.Dynamic1D;
43 43
import org.distorted.library.type.DynamicQuat;
......
58 58
    private DistortedEffects mEffects;
59 59
    private Static3D mScale;
60 60
    private MeshBase mMesh;
61
    private int mSinkAssociation;
62
    private VertexEffectSink mSink;
61
    private VertexEffectRotate mRotate;
62
    private Dynamic1D mAngleDyn;
63
    private Static1D mAngle;
63 64

  
64 65
    Static4D mQuat1, mQuat2;
65 66
    int mScreenMin;
......
86 87
      quatInt1.add(mQuat1);
87 88
      quatInt2.add(mQuat2);
88 89

  
89
      mSinkAssociation = 0;
90
      mSink = new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) );
91
      mSink.setMeshAssociation(-1,mSinkAssociation);
90
      mAngle = new Static1D(0);
91

  
92
      mAngleDyn = new Dynamic1D(2000,0.5f);
93
      mAngleDyn.add(new Static1D(0));
94
      mAngleDyn.add( mAngle );
95

  
96
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
92 97

  
93 98
      mEffects = new DistortedEffects();
99
      mEffects.apply( mRotate );
94 100
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
95 101
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
96 102
      mEffects.apply( new MatrixEffectScale(mScale));
97
      mEffects.apply( mSink );
98 103

  
99 104
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
100 105
      }
......
110 115
    
111 116
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
112 117
      {
113
      final float SCALE = 0.7f;
118
      final float SCALE = 0.5f;
114 119
      mScreenMin = Math.min(width, height);
115 120
      float factor = SCALE*mScreenMin;
116 121
      mScale.set(factor,factor,factor);
......
122 127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123 128
      {
124 129
      if( mTexture==null ) mTexture = new DistortedTexture();
125
      mTexture.setTexture( createTetrahedronTexture() );
130
      mTexture.setTexture( createTexture() );
126 131

  
127
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
132
      if( mMesh==null ) mMesh = createMesh();
128 133

  
129 134
      mScreen.detachAll();
130 135
      mScreen.attach(mTexture,mEffects,mMesh);
131 136

  
132 137
      DistortedLibrary.setMax(EffectType.VERTEX, 11);
133
      VertexEffectSink.enable();
138
      VertexEffectRotate.enable();
134 139

  
135 140
      try
136 141
        {
......
138 143
        }
139 144
      catch(Exception ex)
140 145
        {
141
        android.util.Log.e("MeshJoin", ex.getMessage() );
146
        android.util.Log.e("DeferredJob", ex.getMessage() );
142 147
        }
143 148
      }
144 149

  
145 150
///////////////////////////////////////////////////////////////////////////////////////////////////
146 151

  
147
    void setChecked(int number, boolean checked)
152
    void apply(int number)
148 153
      {
149
      int n = (0x1 << number);
150

  
151
      if( checked ) mSinkAssociation |= n;
152
      else          mSinkAssociation &= (15-n);
153

  
154
      mSink.setMeshAssociation(-1,mSinkAssociation);
154
      mAngle.set(360);
155
      mAngleDyn.resetToBeginning();
155 156
      }
156 157

  
157 158
///////////////////////////////////////////////////////////////////////////////////////////////////
158 159

  
159
    private Bitmap createTetrahedronTexture()
160
    private Bitmap createTexture()
160 161
      {
161
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
162
      final int[] FACE_COLORS = new int[] { 0xffff0000, 0xff00ff00 };
162 163
      final int FACES=FACE_COLORS.length;
163 164
      int SIZE = 200;
164
      float STROKE = 0.05f*SIZE;
165
      float OFF = STROKE/2 -1;
166
      float OFF2 = 0.5f*SIZE + OFF;
167
      float HEIGHT = SIZE - OFF;
168
      float RADIUS = SIZE/12;
169
      float ARC1_H = 0.2f*SIZE;
170
      float ARC1_W = SIZE*0.5f;
171
      float ARC2_W = 0.153f*SIZE;
172
      float ARC2_H = 0.905f*SIZE;
173
      float ARC3_W = SIZE-ARC2_W;
174 165

  
175 166
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
176 167
      Canvas canvas = new Canvas(result);
177 168
      Paint paint = new Paint();
178
      paint.setAntiAlias(true);
179
      paint.setStrokeWidth(STROKE);
169
      paint.setStyle(Paint.Style.FILL);
180 170

  
181 171
      for(int i=0; i<FACES; i++)
182 172
        {
183 173
        paint.setColor(FACE_COLORS[i]);
184
        paint.setStyle(Paint.Style.FILL);
185

  
186 174
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
187

  
188
        paint.setColor(0xff000000);
189
        paint.setStyle(Paint.Style.STROKE);
190

  
191
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
192
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
193
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
194

  
195
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
196
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
197
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
198 175
        }
199 176

  
200 177
      return result;
......
202 179

  
203 180
///////////////////////////////////////////////////////////////////////////////////////////////////
204 181

  
205
    private MeshBase createJoinedTetrahedron()
182
    private MeshBase createMesh()
206 183
      {
207
      final float SQ2 = (float)Math.sqrt(2);
208
      final float SQ3 = (float)Math.sqrt(3);
209
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
210
      final int MESHES=4;
184
      final int MESHES=2;
211 185

  
212
      int association = 1;
213
      MeshBase[] meshes = new MeshTriangles[MESHES];
186
      MeshBase[] meshes = new MeshRectangles[MESHES];
214 187

  
215
      for(int i=0; i<MESHES; i++)
188
      meshes[0] = new MeshRectangles(2,2);
189

  
190
      for(int i=1; i<MESHES; i++)
216 191
        {
217
        meshes[i] = new MeshTriangles(5);
218
        meshes[i].setEffectAssociation(0,association);
219
        association <<= 1;
192
        meshes[i] = meshes[0].copy(true);
220 193
        }
221 194

  
222 195
      Static4D[] textureMaps = new Static4D[MESHES];
223
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
196
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.5f,0.0f,0.5f,1.0f);
224 197
      MeshBase result = new MeshJoined(meshes);
225 198
      result.setTextureMap(textureMaps);
226 199

  
227
      Static3D a0 = new Static3D(         0,        1,       0 );
228
      Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
229
      Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
230
      Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
231

  
232
      float tetraHeight = SQ2*SQ3/3;
233
      float d1 = 0.75f*tetraHeight;
234
      float d2 =-0.10f*tetraHeight;
235
      float d3 = 0.20f*tetraHeight;
236

  
237
      Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
238
      Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
239
      Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
240
      Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
241

  
242
      Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
243
      Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
244
      Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
245
      Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
246

  
247
      Static4D dReg  = new Static4D(0,0,0,d3);
248
      Static1D dRad  = new Static1D(1);
249

  
250
      Static1D angle  = new Static1D(angleFaces);
251
      Static3D axis1  = new Static3D(  -1, 0,      0);
252
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
253
      Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
254
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
255
      Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
256

  
257
      VertexEffectScale   effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
258
      VertexEffectRotate  effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
259
      VertexEffectMove    effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
260
      VertexEffectRotate  effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
261
      VertexEffectRotate  effect5 = new VertexEffectRotate( angle, axis1, center1 );
262
      VertexEffectRotate  effect6 = new VertexEffectRotate( angle, axis2, center2 );
263
      VertexEffectRotate  effect7 = new VertexEffectRotate( angle, axis3, center2 );
264

  
265
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
266
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
267
      VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
268
      VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
269

  
270
      effect4.setMeshAssociation(-1,14);  // apply to mesh[1], [2] and [3]
271
      effect5.setMeshAssociation(-1, 2);  // apply only to mesh[1]
272
      effect6.setMeshAssociation(-1, 4);  // apply only to mesh[2]
273
      effect7.setMeshAssociation(-1, 8);  // apply only to mesh[3]
200
      VertexEffectMove   effect0 = new VertexEffectMove  ( new Static3D(0,0,0.5f) );
201
      VertexEffectRotate effect1 = new VertexEffectRotate( new Static1D(180), new Static3D(1,0,0), new Static3D(0,0,0) );
202

  
203
      effect1.setMeshAssociation(1,0);  // apply only to mesh[1]
274 204

  
205
      result.apply(effect0);
275 206
      result.apply(effect1);
276
      result.apply(effect2);
277
      result.apply(effect3);
278
      result.apply(effect4);
279
      result.apply(effect5);
280
      result.apply(effect6);
281
      result.apply(effect7);
282
      result.apply(effect8);
283
      result.apply(effect9);
284
      result.apply(effect10);
285
      result.apply(effect11);
286 207

  
287 208
      return result;
288 209
      }

Also available in: Unified diff