Project

General

Profile

« Previous | Next » 

Revision 42aa970f

Added by Leszek Koltunski almost 4 years ago

Skeleton of a new App: MeshFile

View differences:

src/main/AndroidManifest.xml
61 61
        <activity android:name=".predeform.PredeformActivity2"/>
62 62
        <activity android:name=".deferredjob.DeferredJobActivity"/>
63 63
        <activity android:name=".singlemesh.SingleMeshActivity"/>
64
        <activity android:name=".meshfile.MeshFileActivity"/>
64 65
    </application>
65 66
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
74 74
import org.distorted.examples.meshjoin.MeshJoinActivity;
75 75
import org.distorted.examples.predeform.PredeformActivity;
76 76
import org.distorted.examples.singlemesh.SingleMeshActivity;
77
import org.distorted.examples.meshfile.MeshFileActivity;
77 78

  
78 79
///////////////////////////////////////////////////////////////////////////////////////////////////
79 80

  
......
125 126
    PREDEFORM         (R.drawable.icon_example_predeform       , R.string.example_predeform           , R.string.example_predeform_subtitle           ,            PredeformActivity.class),
126 127
    DEFERREDJOB       (R.drawable.icon_example_deferredjob     , R.string.example_deferredjob           , R.string.example_deferredjob_subtitle           ,            DeferredJobActivity.class),
127 128
    SINGLEMESH        (R.drawable.icon_example_singlemesh      , R.string.example_singlemesh           , R.string.example_singlemesh_subtitle           ,            SingleMeshActivity.class),
129
    MESHFILE          (R.drawable.icon_example_wip             , R.string.example_meshfile           , R.string.example_meshfile_subtitle           ,            MeshFileActivity.class),
128 130
    ;
129 131

  
130 132
    final int icon, title, subtitle;
src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.meshfile;
21

  
22
import android.app.Activity;
23
import android.opengl.GLSurfaceView;
24
import android.os.Bundle;
25

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

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
public class MeshFileActivity extends Activity
32
{
33
    @Override
34
    protected void onCreate(Bundle icicle) 
35
      {
36
      super.onCreate(icicle);
37
      setContentView(R.layout.meshfilelayout);
38
      }
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
    
42
    @Override
43
    protected void onPause() 
44
      {
45
      GLSurfaceView view = this.findViewById(R.id.meshfileSurfaceView);
46
      view.onPause();
47
      DistortedLibrary.onPause();
48
      super.onPause();
49
      }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
    
53
    @Override
54
    protected void onResume() 
55
      {
56
      super.onResume();
57
      GLSurfaceView view = this.findViewById(R.id.meshfileSurfaceView);
58
      view.onResume();
59
      }
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onDestroy() 
65
      {
66
      DistortedLibrary.onDestroy();
67
      super.onDestroy();
68
      }
69
}
src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.meshfile;
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
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDeform;
32
import org.distorted.library.effect.VertexEffectMove;
33
import org.distorted.library.effect.VertexEffectRotate;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshJoined;
41
import org.distorted.library.mesh.MeshRectangles;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.DynamicQuat;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

  
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

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

  
53
class MeshFileRenderer implements GLSurfaceView.Renderer
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
    private GLSurfaceView mView;
106
    private DistortedTexture mTexture;
107
    private DistortedScreen mScreen;
108
    private DistortedEffects mEffects;
109
    private Static3D mScale;
110
    private MeshBase mMesh;
111
    private VertexEffectRotate mRotate;
112
    private Dynamic1D mAngleDyn;
113
    private Static1D mAngle;
114
    private Static3D mAxis;
115

  
116
    Static4D mQuat1, mQuat2;
117
    int mScreenMin;
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

  
121
    MeshFileRenderer(GLSurfaceView v)
122
      {
123
      mView = v;
124
      mScreen = new DistortedScreen();
125
      mScale= new Static3D(1,1,1);
126
      Static3D center=new Static3D(0,0,0);
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
      mQuat1 = new Static4D(0,0,0,1);
133
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
134

  
135
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
136
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
137

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

  
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
      mEffects = new DistortedEffects();
151
      mEffects.apply( mRotate );
152
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
153
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
154
      mEffects.apply( new MatrixEffectScale(mScale));
155

  
156
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
157
      }
158

  
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
   
161
    public void onDrawFrame(GL10 glUnused) 
162
      {
163
      mScreen.render( System.currentTimeMillis() );
164
      }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
169
      {
170
      final float SCALE = 0.3f;
171
      mScreenMin = Math.min(width, height);
172
      float factor = SCALE*mScreenMin;
173
      mScale.set(factor,factor,factor);
174
      mScreen.resize(width, height);
175
      }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180
      {
181
      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

  
192
      try
193
        {
194
        DistortedLibrary.onCreate(mView.getContext());
195
        }
196
      catch(Exception ex)
197
        {
198
        android.util.Log.e("DeferredJob", ex.getMessage() );
199
        }
200
      }
201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

  
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()
220
      {
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);
231

  
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;
246
      }
247

  
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

  
250
     MeshBase createCubitMesh()
251
      {
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
311

  
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;
342
      }
343

  
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

  
346
    private MeshBase createMesh()
347
      {
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);
374

  
375
      return result;
376
      }
377
}
src/main/java/org/distorted/examples/meshfile/MeshFileSurfaceView.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.meshfile;
21

  
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.util.AttributeSet;
27
import android.view.MotionEvent;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
class MeshFileSurfaceView extends GLSurfaceView
32
{
33
    private final static int DIRECTION_SENSITIVITY=  12;
34
    private int mX, mY;
35
    private MeshFileRenderer mRenderer;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
    public MeshFileSurfaceView(Context context, AttributeSet attrs)
40
      {
41
      super(context,attrs);
42

  
43
      mX = -1;
44
      mY = -1;
45

  
46
      if(!isInEditMode())
47
        {
48
        mRenderer = new MeshFileRenderer(this);
49
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
50
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
51
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
52
        setRenderer(mRenderer);
53
        }
54
      }
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

  
58
    public MeshFileRenderer getRenderer()
59
      {
60
      return mRenderer;
61
      }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
    private void resetQuats()
66
      {
67
      float qx = mRenderer.mQuat1.get0();
68
      float qy = mRenderer.mQuat1.get1();
69
      float qz = mRenderer.mQuat1.get2();
70
      float qw = mRenderer.mQuat1.get3();
71

  
72
      float rx = mRenderer.mQuat2.get0();
73
      float ry = mRenderer.mQuat2.get1();
74
      float rz = mRenderer.mQuat2.get2();
75
      float rw = mRenderer.mQuat2.get3();
76

  
77
       // This is quaternion multiplication. (tx,ty,tz,tw)
78
       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
79
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
80
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
81
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
82
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
83

  
84
       // The point of this is so that there are always
85
       // exactly 2 quaternions: Quat1 representing the rotation
86
       // accumulating only since the last screen touch, and Quat2
87
       // which remembers the combined effect of all previous
88
       // swipes.
89
       // We cannot be accumulating an ever-growing list of quaternions
90
       // and add a new one every time user swipes the screen - there
91
       // is a limited number of slots in the EffectQueueMatrix!
92
       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
93
       mRenderer.mQuat2.set(tx, ty, tz, tw);
94
       }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
    @Override public boolean onTouchEvent(MotionEvent event) 
99
      {
100
      int action = event.getAction();
101
      int x = (int)event.getX();
102
      int y = (int)event.getY();
103
           
104
      switch(action)
105
         {
106
         case MotionEvent.ACTION_DOWN: mX = x;
107
                                       mY = y;
108
                                       break;
109
                                       
110
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
111
                                         {
112
                                         float px = mY-y;
113
                                         float py = mX-x;
114
                                         float pz = 0;
115
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
116
                                         
117
                                         if( plen>0 )
118
                                           {
119
                                           px /= plen;
120
                                           py /= plen;
121
                                           pz /= plen;
122

  
123
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
124
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
125
                                         
126
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
127
                                           }
128
                                         }
129

  
130
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
131
                                         {
132
                                         mX = x;
133
                                         mY = y;
134
                                         resetQuats();
135
                                         }
136

  
137
                                       break;
138
                                       
139
         case MotionEvent.ACTION_UP  : mX = -1;
140
                                       mY = -1;
141
                                       resetQuats();
142
                                       break;
143
         }
144
             
145
      return true;
146
      }
147
         
148
}
149

  
src/main/res/layout/meshfilelayout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="fill_parent"
5
    android:orientation="vertical" >
6

  
7
    <org.distorted.examples.meshfile.MeshFileSurfaceView
8
        android:id="@+id/meshfileSurfaceView"
9
        android:layout_width="fill_parent"
10
        android:layout_height="0dp"
11
        android:layout_weight="1" />
12

  
13
    <LinearLayout
14
        android:orientation="horizontal"
15
        android:layout_width="match_parent"
16
        android:layout_height="wrap_content">
17

  
18
        <Spinner
19
            android:layout_width="0dp"
20
            android:layout_height="wrap_content"
21
            android:layout_weight="0.5"
22
            android:id="@+id/meshfileSpinner"
23
            />
24

  
25
        <Button
26
            android:id="@+id/meshfileButton"
27
            android:layout_width="100dp"
28
            android:layout_height="fill_parent"
29
            android:gravity="center_vertical|center"
30
            android:text="@string/open"
31
            android:onClick="Open"
32
            android:textAppearance="?android:attr/textAppearanceMedium"
33
            android:layout_gravity="center_vertical"/>
34

  
35
    </LinearLayout>
36

  
37
</LinearLayout>
src/main/res/values/strings.xml
87 87
    <string name="association">Sink Association</string>
88 88
    <string name="rotate_left">Rotate Left</string>
89 89
    <string name="rotate_right">Rotate Right</string>
90
    <string name="open">Open</string>
90 91

  
91 92
    <string name="quality0">Highest</string>
92 93
    <string name="quality1">High</string>
......
204 205
    <string name="example_deferredjob_subtitle">Create an advanced mesh in steps, using deferred mesh jobs: apply vertex effects, copy meshes, join them, merge their components.</string>
205 206
    <string name="example_singlemesh">Single Mesh Rubik Cube</string>
206 207
    <string name="example_singlemesh_subtitle">Use the new MeshJoined + MeshBase.apply() to create a single, movable Mesh representing a 2x2x2 RubikCube.</string>
208
    <string name="example_meshfile">Mesh from File</string>
209
    <string name="example_meshfile_subtitle">Explore Distorted\'s own Mesh format, dmesh. Open .dmesh files and explore their contents.</string>
207 210

  
208 211
    <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
209 212
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff