Project

General

Profile

« Previous | Next » 

Revision 758729b5

Added by Leszek Koltunski over 4 years ago

Progres with SingleMesh; bugfix in MeshBase.apply(Matrix)

View differences:

src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.java
25 25
import android.opengl.GLSurfaceView;
26 26

  
27 27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.MatrixEffectMove;
28 29
import org.distorted.library.effect.MatrixEffectQuaternion;
29 30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDeform;
30 32
import org.distorted.library.effect.VertexEffectMove;
31 33
import org.distorted.library.effect.VertexEffectRotate;
34
import org.distorted.library.effect.VertexEffectSink;
32 35
import org.distorted.library.main.DistortedEffects;
33 36
import org.distorted.library.main.DistortedLibrary;
34 37
import org.distorted.library.main.DistortedScreen;
......
49 52

  
50 53
class SingleMeshRenderer implements GLSurfaceView.Renderer
51 54
{
55
    private static final float DIST = 0.5f;
56

  
57
    private static final int[] FACE_COLORS = new int[]
58
         {
59
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
60
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
61
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
62
         };
63

  
64
    private static final Static3D[] CUBIT_MOVES = new Static3D[]
65
         {
66
           new Static3D(-DIST,-DIST,-DIST),
67
           new Static3D(-DIST,-DIST,+DIST),
68
           new Static3D(-DIST,+DIST,-DIST),
69
           new Static3D(-DIST,+DIST,+DIST),
70
           new Static3D(+DIST,-DIST,-DIST),
71
           new Static3D(+DIST,-DIST,+DIST),
72
           new Static3D(+DIST,+DIST,-DIST),
73
           new Static3D(+DIST,+DIST,+DIST),
74
         };
75

  
52 76
    private GLSurfaceView mView;
53 77
    private DistortedTexture mTexture;
54 78
    private DistortedScreen mScreen;
......
131 155
      mScreen.detachAll();
132 156
      mScreen.attach(mTexture,mEffects,mMesh);
133 157

  
134
      DistortedLibrary.setMax(EffectType.VERTEX, 11);
158
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
135 159
      VertexEffectRotate.enable();
136 160

  
137 161
      try
......
157 181

  
158 182
    private Bitmap createTexture()
159 183
      {
160
      final int[] FACE_COLORS = new int[] { 0xffff0000, 0xff00ff00 };
161
      final int FACES=FACE_COLORS.length;
162
      int SIZE = 200;
184
      final int NUM_FACES = 6;
185
      final int TEXTURE_HEIGHT = 200;
186
      final int INTERIOR_COLOR = 0xff000000;
187
      final float R = TEXTURE_HEIGHT*0.10f;
188
      final float M = TEXTURE_HEIGHT*0.05f;
163 189

  
164
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
165
      Canvas canvas = new Canvas(result);
190
      Bitmap bitmap;
166 191
      Paint paint = new Paint();
192
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
193
      Canvas canvas = new Canvas(bitmap);
194

  
195
      paint.setAntiAlias(true);
196
      paint.setTextAlign(Paint.Align.CENTER);
167 197
      paint.setStyle(Paint.Style.FILL);
168 198

  
169
      for(int i=0; i<FACES; i++)
199
      paint.setColor(INTERIOR_COLOR);
200
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
201

  
202
      for(int i=0; i<NUM_FACES; i++)
170 203
        {
171 204
        paint.setColor(FACE_COLORS[i]);
172
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
205
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
173 206
        }
174 207

  
175
      return result;
208
      return bitmap;
176 209
      }
177 210

  
178 211
///////////////////////////////////////////////////////////////////////////////////////////////////
179 212

  
180
    private MeshBase createMesh()
213
     MeshBase createCubitMesh()
181 214
      {
182
      final int MESHES=2;
183

  
215
      final int MESHES=6;
216
      int association = 1;
184 217
      MeshBase[] meshes = new MeshRectangles[MESHES];
185

  
186
      meshes[0] = new MeshRectangles(1,1);
187
      meshes[0].setEffectAssociation(0,1,0);
218
      meshes[0] = new MeshRectangles(10,10);
219
      meshes[0].setEffectAssociation(0,association,0);
188 220

  
189 221
      for(int i=1; i<MESHES; i++)
190 222
        {
223
        association <<=1;
191 224
        meshes[i] = meshes[0].copy(true);
192
        meshes[i].setEffectAssociation(0,1,i);
225
        meshes[i].setEffectAssociation(0,association,0);
193 226
        }
194 227

  
195
      Static4D[] lTextureMaps = new Static4D[MESHES];
196
      Static4D[] rTextureMaps = new Static4D[MESHES];
197

  
198
      for(int i=0; i<MESHES; i++)
199
        {
200
        lTextureMaps[i] = new Static4D(0.0f,0.0f,0.5f,1.0f);
201
        rTextureMaps[i] = new Static4D(0.5f,0.0f,0.5f,1.0f);
202
        }
203

  
204
      MeshBase[] tmp = new MeshBase[2];
205

  
206
      tmp[0] = new MeshJoined(meshes);
207
      tmp[0].setTextureMap(lTextureMaps);
208

  
209
      VertexEffectMove   effect0 = new VertexEffectMove  ( new Static3D(0,0,0.5f) );
210
      VertexEffectRotate effect1 = new VertexEffectRotate( new Static1D(180), new Static3D(1,0,0), new Static3D(0,0,0) );
211

  
212
      effect0.setMeshAssociation(1,0);  // apply only to Components whose andAssoc has the least significant bit set, i.e.
213
                                        // to both meshes[0] and meshes[1]
214
      effect1.setMeshAssociation(0,1);  // apply only to Components whose equAssoc is equal to 1, i.e. only to mesh[1]
215

  
216
      tmp[0].apply(effect0);
217
      tmp[0].apply(effect1);
218

  
219
      tmp[1] = tmp[0].copy(true);
220
      tmp[1].setTextureMap(rTextureMaps);
228
      MeshBase mesh = new MeshJoined(meshes);
229

  
230
      Static3D axisY   = new Static3D(0,1,0);
231
      Static3D axisX   = new Static3D(1,0,0);
232
      Static3D center  = new Static3D(0,0,0);
233
      Static1D angle90 = new Static1D(90);
234
      Static1D angle180= new Static1D(180);
235
      Static1D angle270= new Static1D(270);
236

  
237
      float d1 = 1.0f;
238
      float d2 =-0.05f;
239
      float d3 = 0.12f;
240

  
241
      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
242
      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
243
      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
244
      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
245
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
246
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
247
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
248
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
249

  
250
      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
251
      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
252
      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
253
      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
254
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
255
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
256
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
257
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
258

  
259
      Static4D dReg  = new Static4D(0,0,0,d3);
260
      Static1D dRad  = new Static1D(1);
261

  
262
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
263
      effect0.setMeshAssociation(63,-1);  // all 6 sides
264
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
265
      effect1.setMeshAssociation(32,-1);  // back
266
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
267
      effect2.setMeshAssociation( 8,-1);  // bottom
268
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
269
      effect3.setMeshAssociation( 4,-1);  // top
270
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
271
      effect4.setMeshAssociation( 2,-1);  // left
272
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
273
      effect5.setMeshAssociation( 1,-1);  // right
274

  
275
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
276
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
277
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
278
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
279
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
280
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
281
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
282
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
283

  
284
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
285

  
286
      mesh.apply(effect0);
287
      mesh.apply(effect1);
288
      mesh.apply(effect2);
289
      mesh.apply(effect3);
290
      mesh.apply(effect4);
291
      mesh.apply(effect5);
292
      mesh.apply(effect6);
293
      mesh.apply(effect7);
294
      mesh.apply(effect8);
295
      mesh.apply(effect9);
296
      mesh.apply(effect10);
297
      mesh.apply(effect11);
298
      mesh.apply(effect12);
299
      mesh.apply(effect13);
300
      mesh.apply(effect14);
301

  
302
      mesh.mergeEffComponents();
303

  
304
      return mesh;
305
      }
221 306

  
222
      tmp[0].mergeEffComponents();
223
      tmp[1].mergeEffComponents();
307
///////////////////////////////////////////////////////////////////////////////////////////////////
224 308

  
225
      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
226
      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
309
    private MeshBase createMesh()
310
      {
311
      final int NUM_CUBITS = CUBIT_MOVES.length;
312
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
227 313

  
228
      MeshBase combined = new MeshJoined(tmp);
314
      cubits[0] = createCubitMesh();
229 315

  
230
      VertexEffectMove moveL = new VertexEffectMove  ( new Static3D(-0.6f,0,0) );
231
      VertexEffectMove moveR = new VertexEffectMove  ( new Static3D(+0.6f,0,0) );
316
      for(int i=1; i<NUM_CUBITS; i++)
317
        {
318
        cubits[i] = cubits[0].copy(true);
319
        }
232 320

  
233
      moveL.setMeshAssociation(0,0);  // apply only to tmp[0]
234
      moveR.setMeshAssociation(0,1);  // apply only to tmp[1]
321
      for(int i=0; i<NUM_CUBITS; i++)
322
        {
323
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
324
        }
235 325

  
236
      combined.apply(moveL);
237
      combined.apply(moveR);
326
      MeshBase result = new MeshJoined(cubits);
238 327

  
239
      return combined;
328
      return result;
240 329
      }
241 330
}

Also available in: Unified diff