Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 30f337e5

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.meshjoin;
21

    
22
import android.content.res.Resources;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.opengl.GLSurfaceView;
27

    
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectDeform;
31
import org.distorted.library.effect.VertexEffectMove;
32
import org.distorted.library.effect.VertexEffectRotate;
33
import org.distorted.library.effect.VertexEffectScale;
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.MeshTriangle;
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 java.io.InputStream;
49

    
50
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class MeshJoinRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56
{
57
    private final Resources mResources;
58
    private final DistortedScreen mScreen;
59
    private final DistortedEffects mEffects;
60
    private final Static3D mScale;
61
    private final VertexEffectSink mSink;
62

    
63
    private DistortedTexture mTexture;
64
    private MeshBase mMesh;
65
    private int mSinkAssociation;
66

    
67
    Static4D mQuat1, mQuat2;
68
    int mScreenMin;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    MeshJoinRenderer(GLSurfaceView v)
73
      {
74
      mResources = v.getResources();
75

    
76
      mScreen = new DistortedScreen();
77
      mScale= new Static3D(1,1,1);
78
      Static3D center=new Static3D(0,0,0);
79

    
80
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
81
      sink.add( new Static1D(0.5f) );
82
      sink.add( new Static1D(2.0f) );
83

    
84
      mQuat1 = new Static4D(0,0,0,1);  // unity
85
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
86

    
87
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
88
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
89

    
90
      quatInt1.add(mQuat1);
91
      quatInt2.add(mQuat2);
92

    
93
      mSinkAssociation = 0;
94
      mSink = new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) );
95
      mSink.setMeshAssociation(mSinkAssociation,-1);
96

    
97
      mEffects = new DistortedEffects();
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
99
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
100
      mEffects.apply( new MatrixEffectScale(mScale));
101
      mEffects.apply( mSink );
102

    
103
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
   
108
    public void onDrawFrame(GL10 glUnused) 
109
      {
110
      mScreen.render( System.currentTimeMillis() );
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
116
      {
117
      final float SCALE = 0.7f;
118
      mScreenMin = Math.min(width, height);
119
      float factor = SCALE*mScreenMin;
120
      mScale.set(factor,factor,factor);
121
      mScreen.resize(width, height);
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
127
      {
128
      if( mTexture==null ) mTexture = new DistortedTexture();
129
      mTexture.setTexture( createTetrahedronTexture() );
130

    
131
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
132

    
133
      mScreen.detachAll();
134
      mScreen.attach(mTexture,mEffects,mMesh);
135

    
136
      VertexEffectSink.enable();
137

    
138
      DistortedLibrary.onSurfaceCreated(this);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
    void setChecked(int number, boolean checked)
144
      {
145
      int n = (0x1 << number);
146

    
147
      if( checked ) mSinkAssociation |= n;
148
      else          mSinkAssociation &= (15-n);
149

    
150
      mSink.setMeshAssociation(mSinkAssociation,-1);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

    
171
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
172
      Canvas canvas = new Canvas(result);
173
      Paint paint = new Paint();
174
      paint.setAntiAlias(true);
175
      paint.setStrokeWidth(STROKE);
176

    
177
      for(int i=0; i<FACES; i++)
178
        {
179
        paint.setColor(FACE_COLORS[i]);
180
        paint.setStyle(Paint.Style.FILL);
181

    
182
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
183

    
184
        paint.setColor(0xff000000);
185
        paint.setStyle(Paint.Style.STROKE);
186

    
187
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
188
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
189
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
190

    
191
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
192
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
193
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
194
        }
195

    
196
      return result;
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    private MeshBase createJoinedTetrahedron()
202
      {
203
      final float SQ2 = (float)Math.sqrt(2);
204
      final float SQ3 = (float)Math.sqrt(3);
205
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
206
      final int MESHES=4;
207

    
208
      int andAssoc = 1;
209
      MeshBase[] meshes = new MeshTriangle[MESHES];
210

    
211
      for(int i=0; i<MESHES; i++)
212
        {
213
        meshes[i] = new MeshTriangle(5);
214
        meshes[i].setEffectAssociation(0,andAssoc,0);
215
        andAssoc <<= 1;
216
        }
217

    
218
      Static4D[] textureMaps = new Static4D[MESHES];
219
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
220
      MeshBase result = new MeshJoined(meshes);
221
      result.setTextureMap(textureMaps,0);
222

    
223
      Static3D a0 = new Static3D(         0,        1,       0 );
224
      Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
225
      Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
226
      Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
227

    
228
      float tetraHeight = SQ2*SQ3/3;
229
      float d1 = 0.75f*tetraHeight;
230
      float d2 =-0.10f*tetraHeight;
231
      float d3 = 0.20f*tetraHeight;
232

    
233
      Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
234
      Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
235
      Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
236
      Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
237

    
238
      Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
239
      Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
240
      Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
241
      Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
242

    
243
      Static4D dReg  = new Static4D(0,0,0,d3);
244
      Static1D dRad  = new Static1D(1);
245

    
246
      Static1D angle  = new Static1D(angleFaces);
247
      Static3D axis1  = new Static3D(  -1, 0,      0);
248
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
249
      Static3D axis3  = new Static3D(0.5f, 0,  SQ3/2);
250
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
251
      Static3D center2= new Static3D(0,-SQ3*SQ2/12, SQ3/3);
252

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

    
261
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
262
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
263
      VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
264
      VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
265

    
266
      effect4.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
267
      effect5.setMeshAssociation( 2,-1);  // apply only to mesh[1]
268
      effect6.setMeshAssociation( 4,-1);  // apply only to mesh[2]
269
      effect7.setMeshAssociation( 8,-1);  // apply only to mesh[3]
270

    
271
      result.apply(effect1);
272
      result.apply(effect2);
273
      result.apply(effect3);
274
      result.apply(effect4);
275
      result.apply(effect5);
276
      result.apply(effect6);
277
      result.apply(effect7);
278
      result.apply(effect8);
279
      result.apply(effect9);
280
      result.apply(effect10);
281
      result.apply(effect11);
282

    
283
      return result;
284
      }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
    public void distortedException(Exception ex)
289
      {
290
      android.util.Log.e("MeshJoin", ex.getMessage() );
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
    public InputStream localFile(int fileID)
296
      {
297
      return mResources.openRawResource(fileID);
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    public void logMessage(String message)
303
      {
304
      android.util.Log.e("MeshJoin", message );
305
      }
306
}
(2-2/3)