Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ dc10a48d

1 508ee98f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 508ee98f Leszek Koltunski
import android.graphics.Bitmap;
27 ea9b68db Leszek Koltunski
import android.graphics.Canvas;
28
import android.graphics.Paint;
29 508ee98f Leszek Koltunski
import android.opengl.GLSurfaceView;
30
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33 0339e04e Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
34 dd19d6e0 Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.effect.VertexEffectScale;
37 ea9b68db Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
38 508ee98f Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
43 37320052 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
44 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshTriangle;
45 ea9b68db Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
46 508ee98f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
47 37320052 Leszek Koltunski
import org.distorted.library.type.Static1D;
48 508ee98f Leszek Koltunski
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50
51 dc10a48d Leszek Koltunski
import java.io.InputStream;
52
53 508ee98f Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
54
import javax.microedition.khronos.opengles.GL10;
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 dc10a48d Leszek Koltunski
class MeshJoinRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
59 508ee98f Leszek Koltunski
{
60 dc10a48d Leszek Koltunski
    private final GLSurfaceView mView;
61
    private final Resources mResources;
62
    private final DistortedScreen mScreen;
63
    private final DistortedEffects mEffects;
64
    private final Static3D mScale;
65
    private final VertexEffectSink mSink;
66
67 508ee98f Leszek Koltunski
    private DistortedTexture mTexture;
68
    private MeshBase mMesh;
69 811ffcf5 Leszek Koltunski
    private int mSinkAssociation;
70 508ee98f Leszek Koltunski
71
    Static4D mQuat1, mQuat2;
72
    int mScreenMin;
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
    MeshJoinRenderer(GLSurfaceView v)
77
      {
78
      mView = v;
79 dc10a48d Leszek Koltunski
      mResources = v.getResources();
80
81 508ee98f Leszek Koltunski
      mScreen = new DistortedScreen();
82
      mScale= new Static3D(1,1,1);
83 ba9ae2c8 Leszek Koltunski
      Static3D center=new Static3D(0,0,0);
84 508ee98f Leszek Koltunski
85 ea9b68db Leszek Koltunski
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
86
      sink.add( new Static1D(0.5f) );
87
      sink.add( new Static1D(2.0f) );
88
89 508ee98f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
90
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
91
92
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
93
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
94
95
      quatInt1.add(mQuat1);
96
      quatInt2.add(mQuat2);
97
98 d7829514 Leszek Koltunski
      mSinkAssociation = 0;
99 811ffcf5 Leszek Koltunski
      mSink = new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) );
100 eb507734 Leszek Koltunski
      mSink.setMeshAssociation(mSinkAssociation,-1);
101 811ffcf5 Leszek Koltunski
102 508ee98f Leszek Koltunski
      mEffects = new DistortedEffects();
103 ba9ae2c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
104
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
105 508ee98f Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale));
106 811ffcf5 Leszek Koltunski
      mEffects.apply( mSink );
107 ea9b68db Leszek Koltunski
108
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
109 508ee98f Leszek Koltunski
      }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113
    public void onDrawFrame(GL10 glUnused) 
114
      {
115
      mScreen.render( System.currentTimeMillis() );
116
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
121
      {
122 ea9b68db Leszek Koltunski
      final float SCALE = 0.7f;
123 dd19d6e0 Leszek Koltunski
      mScreenMin = Math.min(width, height);
124 37320052 Leszek Koltunski
      float factor = SCALE*mScreenMin;
125 508ee98f Leszek Koltunski
      mScale.set(factor,factor,factor);
126
      mScreen.resize(width, height);
127
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132
      {
133
      if( mTexture==null ) mTexture = new DistortedTexture();
134 f793bd9d Leszek Koltunski
      mTexture.setTexture( createTetrahedronTexture() );
135 508ee98f Leszek Koltunski
136 33bd5234 Leszek Koltunski
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
137 508ee98f Leszek Koltunski
138
      mScreen.detachAll();
139
      mScreen.attach(mTexture,mEffects,mMesh);
140
141 da412a40 Leszek Koltunski
      VertexEffectSink.enable();
142 ea9b68db Leszek Koltunski
143 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
144 508ee98f Leszek Koltunski
      }
145
146 8982b894 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
    void setChecked(int number, boolean checked)
149
      {
150 811ffcf5 Leszek Koltunski
      int n = (0x1 << number);
151
152
      if( checked ) mSinkAssociation |= n;
153
      else          mSinkAssociation &= (15-n);
154
155 eb507734 Leszek Koltunski
      mSink.setMeshAssociation(mSinkAssociation,-1);
156 8982b894 Leszek Koltunski
      }
157
158 33bd5234 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 f793bd9d Leszek Koltunski
    private Bitmap createTetrahedronTexture()
161 33bd5234 Leszek Koltunski
      {
162 ea9b68db Leszek Koltunski
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
163
      final int FACES=FACE_COLORS.length;
164 a8a553a5 Leszek Koltunski
      int SIZE = 200;
165 77e9d967 Leszek Koltunski
      float STROKE = 0.05f*SIZE;
166
      float OFF = STROKE/2 -1;
167 a8a553a5 Leszek Koltunski
      float OFF2 = 0.5f*SIZE + OFF;
168 77e9d967 Leszek Koltunski
      float HEIGHT = SIZE - OFF;
169
      float RADIUS = SIZE/12;
170 a8a553a5 Leszek Koltunski
      float ARC1_H = 0.2f*SIZE;
171 f793bd9d Leszek Koltunski
      float ARC1_W = SIZE*0.5f;
172 a8a553a5 Leszek Koltunski
      float ARC2_W = 0.153f*SIZE;
173
      float ARC2_H = 0.905f*SIZE;
174
      float ARC3_W = SIZE-ARC2_W;
175 f793bd9d Leszek Koltunski
176
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
177 ea9b68db Leszek Koltunski
      Canvas canvas = new Canvas(result);
178
      Paint paint = new Paint();
179 f793bd9d Leszek Koltunski
      paint.setAntiAlias(true);
180
      paint.setStrokeWidth(STROKE);
181 b3090c52 Leszek Koltunski
182 ea9b68db Leszek Koltunski
      for(int i=0; i<FACES; i++)
183
        {
184
        paint.setColor(FACE_COLORS[i]);
185 f793bd9d Leszek Koltunski
        paint.setStyle(Paint.Style.FILL);
186
187
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
188
189
        paint.setColor(0xff000000);
190
        paint.setStyle(Paint.Style.STROKE);
191
192
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
193
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
194
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
195
196
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
197
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
198
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
199 ea9b68db Leszek Koltunski
        }
200 33bd5234 Leszek Koltunski
201 23c35a5d Leszek Koltunski
      return result;
202 508ee98f Leszek Koltunski
      }
203 33bd5234 Leszek Koltunski
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206
    private MeshBase createJoinedTetrahedron()
207
      {
208 40223bbb Leszek Koltunski
      final float SQ2 = (float)Math.sqrt(2);
209 33bd5234 Leszek Koltunski
      final float SQ3 = (float)Math.sqrt(3);
210
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
211
      final int MESHES=4;
212
213 eb507734 Leszek Koltunski
      int andAssoc = 1;
214 f2085b96 Leszek Koltunski
      MeshBase[] meshes = new MeshTriangle[MESHES];
215 ea9b68db Leszek Koltunski
216 dd19d6e0 Leszek Koltunski
      for(int i=0; i<MESHES; i++)
217
        {
218 f2085b96 Leszek Koltunski
        meshes[i] = new MeshTriangle(5);
219 eb507734 Leszek Koltunski
        meshes[i].setEffectAssociation(0,andAssoc,0);
220
        andAssoc <<= 1;
221 dd19d6e0 Leszek Koltunski
        }
222 ea9b68db Leszek Koltunski
223 dd19d6e0 Leszek Koltunski
      Static4D[] textureMaps = new Static4D[MESHES];
224
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
225 ea9b68db Leszek Koltunski
      MeshBase result = new MeshJoined(meshes);
226 96345c94 Leszek Koltunski
      result.setTextureMap(textureMaps,0);
227 ea9b68db Leszek Koltunski
228 0339e04e Leszek Koltunski
      Static3D a0 = new Static3D(         0,        1,       0 );
229
      Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
230
      Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
231
      Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
232
233
      float tetraHeight = SQ2*SQ3/3;
234
      float d1 = 0.75f*tetraHeight;
235
      float d2 =-0.10f*tetraHeight;
236
      float d3 = 0.20f*tetraHeight;
237
238
      Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
239
      Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
240
      Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
241
      Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
242
243
      Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
244
      Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
245
      Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
246
      Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
247
248
      Static4D dReg  = new Static4D(0,0,0,d3);
249
      Static1D dRad  = new Static1D(1);
250
251 dd19d6e0 Leszek Koltunski
      Static1D angle  = new Static1D(angleFaces);
252
      Static3D axis1  = new Static3D(  -1, 0,      0);
253
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
254 dc10a48d Leszek Koltunski
      Static3D axis3  = new Static3D(0.5f, 0,  SQ3/2);
255 dd19d6e0 Leszek Koltunski
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
256 dc10a48d Leszek Koltunski
      Static3D center2= new Static3D(0,-SQ3*SQ2/12, SQ3/3);
257 dd19d6e0 Leszek Koltunski
258 0339e04e Leszek Koltunski
      VertexEffectScale   effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
259
      VertexEffectRotate  effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
260
      VertexEffectMove    effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
261
      VertexEffectRotate  effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
262
      VertexEffectRotate  effect5 = new VertexEffectRotate( angle, axis1, center1 );
263
      VertexEffectRotate  effect6 = new VertexEffectRotate( angle, axis2, center2 );
264
      VertexEffectRotate  effect7 = new VertexEffectRotate( angle, axis3, center2 );
265
266
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
267
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
268
      VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
269
      VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
270 dd19d6e0 Leszek Koltunski
271 eb507734 Leszek Koltunski
      effect4.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
272
      effect5.setMeshAssociation( 2,-1);  // apply only to mesh[1]
273
      effect6.setMeshAssociation( 4,-1);  // apply only to mesh[2]
274
      effect7.setMeshAssociation( 8,-1);  // apply only to mesh[3]
275 dd19d6e0 Leszek Koltunski
276
      result.apply(effect1);
277
      result.apply(effect2);
278
      result.apply(effect3);
279
      result.apply(effect4);
280
      result.apply(effect5);
281
      result.apply(effect6);
282
      result.apply(effect7);
283 0339e04e Leszek Koltunski
      result.apply(effect8);
284
      result.apply(effect9);
285
      result.apply(effect10);
286
      result.apply(effect11);
287 dd19d6e0 Leszek Koltunski
288 ea9b68db Leszek Koltunski
      return result;
289 33bd5234 Leszek Koltunski
      }
290 dc10a48d Leszek Koltunski
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
    public void distortedException(Exception ex)
294
      {
295
      android.util.Log.e("MeshJoin", ex.getMessage() );
296
      }
297
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300
    public int openGlVersion()
301
      {
302
      Context context = mView.getContext();
303
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
304
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
305
      int glESversion = configurationInfo.reqGlEsVersion;
306
      int major = glESversion >> 16;
307
      int minor = glESversion & 0xff;
308
309
      return 100*major + 10*minor;
310
      }
311
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314
    public InputStream localFile(int fileID)
315
      {
316
      return mResources.openRawResource(fileID);
317
      }
318
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
    public void logMessage(String message)
322
      {
323
      android.util.Log.e("MeshJoin", message );
324
      }
325 508ee98f Leszek Koltunski
}