Project

General

Profile

« Previous | Next » 

Revision 71c7624f

Added by Leszek Koltunski over 4 years ago

Progress with MeshFile

View differences:

src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java
22 22
import android.app.Activity;
23 23
import android.opengl.GLSurfaceView;
24 24
import android.os.Bundle;
25
import android.view.View;
26
import android.widget.AdapterView;
27
import android.widget.ArrayAdapter;
28
import android.widget.Spinner;
25 29

  
26 30
import org.distorted.examples.R;
27 31
import org.distorted.library.main.DistortedLibrary;
28 32

  
29 33
///////////////////////////////////////////////////////////////////////////////////////////////////
30 34

  
31
public class MeshFileActivity extends Activity
35
public class MeshFileActivity extends Activity implements AdapterView.OnItemSelectedListener
32 36
{
37
    private String mName;
38
    private String[] mNames = new String[] { "deferredjob",
39
                                             "meshjoin"   ,
40
                                             "predeform"  ,
41
                                             "singlemesh" ,
42
                                             "cube2"      ,
43
                                             "cube3"      ,
44
                                             "cube4"      ,
45
                                             "cube5"      ,
46
                                             "pyra3"      ,
47
                                             "pyra4"      ,
48
                                             "pyra5"
49
                                           };
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
33 53
    @Override
34 54
    protected void onCreate(Bundle icicle) 
35 55
      {
36 56
      super.onCreate(icicle);
37 57
      setContentView(R.layout.meshfilelayout);
58

  
59
      Spinner meshSpinner  = findViewById(R.id.meshfileSpinner);
60
      meshSpinner.setOnItemSelectedListener(this);
61

  
62
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mNames);
63
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
64
      meshSpinner.setAdapter(adapterBitmap);
38 65
      }
39 66

  
40 67
///////////////////////////////////////////////////////////////////////////////////////////////////
......
66 93
      DistortedLibrary.onDestroy();
67 94
      super.onDestroy();
68 95
      }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
    public void Open(View v)
100
      {
101
      MeshFileSurfaceView view = this.findViewById(R.id.meshfileSurfaceView);
102
      MeshFileRenderer renderer = view.getRenderer();
103

  
104
      renderer.open(mName);
105
      }
106

  
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

  
109
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
110
      {
111
      if( parent.getId()==R.id.meshfileSpinner )
112
        {
113
        mName = mNames[pos];
114
        }
115
      }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
    public void onNothingSelected(AdapterView<?> parent)
120
      {
121
      }
69 122
}
src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
52 52

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

  
57
    private static Static3D[] AXIS = new Static3D[]
58
         {
59
           new Static3D(1,0,0),
60
           new Static3D(0,1,0),
61
           new Static3D(0,0,1)
62
         };
63

  
64
    private static final int[] FACE_COLORS = new int[]
65
         {
66
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
67
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
68
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
69
         };
70

  
71
    private static final int NUM_FACES = FACE_COLORS.length;
72

  
73
    private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
74
    private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
75
    private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
76
    private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
77
    private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
78
    private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
79
    private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
80

  
81
    private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
82
         {
83
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
84
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
85
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
86
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
87
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
88
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
89
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
90
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
91
         };
92

  
93
    private static final Static3D[] CUBIT_MOVES = new Static3D[]
94
         {
95
           new Static3D(-DIST,-DIST,-DIST),
96
           new Static3D(-DIST,-DIST,+DIST),
97
           new Static3D(-DIST,+DIST,-DIST),
98
           new Static3D(-DIST,+DIST,+DIST),
99
           new Static3D(+DIST,-DIST,-DIST),
100
           new Static3D(+DIST,-DIST,+DIST),
101
           new Static3D(+DIST,+DIST,-DIST),
102
           new Static3D(+DIST,+DIST,+DIST),
103
         };
104

  
105 55
    private GLSurfaceView mView;
106 56
    private DistortedTexture mTexture;
107 57
    private DistortedScreen mScreen;
108 58
    private DistortedEffects mEffects;
109 59
    private Static3D mScale;
110 60
    private MeshBase mMesh;
111
    private VertexEffectRotate mRotate;
112
    private Dynamic1D mAngleDyn;
113
    private Static1D mAngle;
114
    private Static3D mAxis;
115 61

  
116 62
    Static4D mQuat1, mQuat2;
117 63
    int mScreenMin;
......
125 71
      mScale= new Static3D(1,1,1);
126 72
      Static3D center=new Static3D(0,0,0);
127 73

  
128
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
129
      sink.add( new Static1D(0.5f) );
130
      sink.add( new Static1D(2.0f) );
131

  
132 74
      mQuat1 = new Static4D(0,0,0,1);
133 75
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
134 76

  
......
138 80
      quatInt1.add(mQuat1);
139 81
      quatInt2.add(mQuat2);
140 82

  
141
      mAngle = new Static1D(0);
142
      mAxis  = new Static3D(1,0,0);
143

  
144
      mAngleDyn = new Dynamic1D(2000,0.5f);
145
      mAngleDyn.add(new Static1D(0));
146
      mAngleDyn.add( mAngle );
147

  
148
      mRotate = new VertexEffectRotate( mAngleDyn, mAxis, new Static3D(0,0,0) );
149

  
150 83
      mEffects = new DistortedEffects();
151
      mEffects.apply( mRotate );
152 84
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
153 85
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
154 86
      mEffects.apply( new MatrixEffectScale(mScale));
......
179 111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180 112
      {
181 113
      if( mTexture==null ) mTexture = new DistortedTexture();
182
      mTexture.setTexture( createTexture() );
183

  
184
      if( mMesh==null ) mMesh = createMesh();
185

  
186
      mScreen.detachAll();
187
      mScreen.attach(mTexture,mEffects,mMesh);
188

  
189
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
190
      VertexEffectRotate.enable();
191 114

  
192 115
      try
193 116
        {
......
195 118
        }
196 119
      catch(Exception ex)
197 120
        {
198
        android.util.Log.e("DeferredJob", ex.getMessage() );
121
        android.util.Log.e("MeshFile", ex.getMessage() );
199 122
        }
200 123
      }
201 124

  
202 125
///////////////////////////////////////////////////////////////////////////////////////////////////
203 126

  
204
    void apply(int andAssoc, int axisIndex)
205
      {
206
      mRotate.setMeshAssociation(andAssoc,-1);
207

  
208
      mAngle.set(360);
209

  
210
      mAxis.set0(AXIS[axisIndex].get0());
211
      mAxis.set1(AXIS[axisIndex].get1());
212
      mAxis.set2(AXIS[axisIndex].get2());
213

  
214
      mAngleDyn.resetToBeginning();
215
      }
216

  
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

  
219
    private Bitmap createTexture()
127
    void open(String name)
220 128
      {
221
      final int NUM_FACES = 6;
222
      final int TEXTURE_HEIGHT = 200;
223
      final int INTERIOR_COLOR = 0xff000000;
224
      final float R = TEXTURE_HEIGHT*0.10f;
225
      final float M = TEXTURE_HEIGHT*0.05f;
226

  
227
      Bitmap bitmap;
228
      Paint paint = new Paint();
229
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
230
      Canvas canvas = new Canvas(bitmap);
129
      mTexture.setTexture( createTexture(name) );
130
      mMesh = createMesh(name);
231 131

  
232
      paint.setAntiAlias(true);
233
      paint.setTextAlign(Paint.Align.CENTER);
234
      paint.setStyle(Paint.Style.FILL);
235

  
236
      paint.setColor(INTERIOR_COLOR);
237
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
238

  
239
      for(int i=0; i<NUM_FACES; i++)
240
        {
241
        paint.setColor(FACE_COLORS[i]);
242
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
243
        }
244

  
245
      return bitmap;
132
      mScreen.detachAll();
133
      mScreen.attach(mTexture,mEffects,mMesh);
246 134
      }
247 135

  
248 136
///////////////////////////////////////////////////////////////////////////////////////////////////
249 137

  
250
     MeshBase createCubitMesh()
138
    private Bitmap createTexture(String name)
251 139
      {
252
      final int MESHES=6;
253
      int association = 1;
254
      MeshBase[] meshes = new MeshRectangles[MESHES];
255
      meshes[0] = new MeshRectangles(10,10);
256
      meshes[0].setEffectAssociation(0,association,0);
257

  
258
      for(int i=1; i<MESHES; i++)
259
        {
260
        association <<=1;
261
        meshes[i] = meshes[0].copy(true);
262
        meshes[i].setEffectAssociation(0,association,0);
263
        }
264

  
265
      MeshBase mesh = new MeshJoined(meshes);
266

  
267
      Static3D axisY   = new Static3D(0,1,0);
268
      Static3D axisX   = new Static3D(1,0,0);
269
      Static3D center  = new Static3D(0,0,0);
270
      Static1D angle90 = new Static1D(90);
271
      Static1D angle180= new Static1D(180);
272
      Static1D angle270= new Static1D(270);
273

  
274
      float d1 = 1.0f;
275
      float d2 =-0.05f;
276
      float d3 = 0.12f;
277

  
278
      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
279
      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
280
      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
281
      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
282
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
283
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
284
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
285
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
286

  
287
      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
288
      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
289
      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
290
      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
291
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
292
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
293
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
294
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
295

  
296
      Static4D dReg  = new Static4D(0,0,0,d3);
297
      Static1D dRad  = new Static1D(1);
298

  
299
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
300
      effect0.setMeshAssociation(63,-1);  // all 6 sides
301
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
302
      effect1.setMeshAssociation(32,-1);  // back
303
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
304
      effect2.setMeshAssociation( 8,-1);  // bottom
305
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
306
      effect3.setMeshAssociation( 4,-1);  // top
307
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
308
      effect4.setMeshAssociation( 2,-1);  // left
309
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
310
      effect5.setMeshAssociation( 1,-1);  // right
140
      // TODO
311 141

  
312
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
313
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
314
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
315
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
316
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
317
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
318
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
319
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
320

  
321
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
322

  
323
      mesh.apply(effect0);
324
      mesh.apply(effect1);
325
      mesh.apply(effect2);
326
      mesh.apply(effect3);
327
      mesh.apply(effect4);
328
      mesh.apply(effect5);
329
      mesh.apply(effect6);
330
      mesh.apply(effect7);
331
      mesh.apply(effect8);
332
      mesh.apply(effect9);
333
      mesh.apply(effect10);
334
      mesh.apply(effect11);
335
      mesh.apply(effect12);
336
      mesh.apply(effect13);
337
      mesh.apply(effect14);
338

  
339
      mesh.mergeEffComponents();
340

  
341
      return mesh;
142
      return null;
342 143
      }
343 144

  
344 145
///////////////////////////////////////////////////////////////////////////////////////////////////
345 146

  
346
    private MeshBase createMesh()
147
    private MeshBase createMesh(String name)
347 148
      {
348
      final int NUM_CUBITS = CUBIT_MOVES.length;
349
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
350

  
351
      cubits[NUM_CUBITS-1] = createCubitMesh();   // NUM_CUBITS-1 (or anything non-zero!)
352

  
353
      for(int i=0; i<NUM_CUBITS-1; i++)
354
        {
355
        cubits[i] = cubits[NUM_CUBITS-1].copy(true);
356
        }
357

  
358
      for(int i=0; i<NUM_CUBITS; i++)
359
        {
360
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
361
        cubits[i].setTextureMap(TEXTURE_MAP[i],0);
362
        }
363

  
364
      MeshBase result = new MeshJoined(cubits);
365

  
366
      result.setEffectAssociation( 0, (1<<4) + (1<<2) + 1, 0);
367
      result.setEffectAssociation( 1, (1<<4) + (1<<2) + 2, 0);
368
      result.setEffectAssociation( 2, (1<<4) + (2<<2) + 1, 0);
369
      result.setEffectAssociation( 3, (1<<4) + (2<<2) + 2, 0);
370
      result.setEffectAssociation( 4, (2<<4) + (1<<2) + 1, 0);
371
      result.setEffectAssociation( 5, (2<<4) + (1<<2) + 2, 0);
372
      result.setEffectAssociation( 6, (2<<4) + (2<<2) + 1, 0);
373
      result.setEffectAssociation( 7, (2<<4) + (2<<2) + 2, 0);
149
      // TODO
374 150

  
375
      return result;
151
      return null;
376 152
      }
377 153
}
src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.java
125 125
      mScale= new Static3D(1,1,1);
126 126
      Static3D center=new Static3D(0,0,0);
127 127

  
128
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
129
      sink.add( new Static1D(0.5f) );
130
      sink.add( new Static1D(2.0f) );
131

  
132 128
      mQuat1 = new Static4D(0,0,0,1);
133 129
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
134 130

  

Also available in: Unified diff