Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 8982b894

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
import android.graphics.Bitmap;
23 ea9b68db Leszek Koltunski
import android.graphics.Canvas;
24
import android.graphics.Paint;
25 508ee98f Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27 dd19d6e0 Leszek Koltunski
import org.distorted.library.effect.EffectType;
28 508ee98f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30 dd19d6e0 Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
31
import org.distorted.library.effect.VertexEffectRotate;
32
import org.distorted.library.effect.VertexEffectScale;
33 ea9b68db Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
34 508ee98f Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshBase;
39 37320052 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
40 b3090c52 Leszek Koltunski
import org.distorted.library.mesh.MeshTriangles;
41 ea9b68db Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
42 508ee98f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
43 37320052 Leszek Koltunski
import org.distorted.library.type.Static1D;
44 508ee98f Leszek Koltunski
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
class MeshJoinRenderer implements GLSurfaceView.Renderer
53
{
54
    private GLSurfaceView mView;
55
    private DistortedTexture mTexture;
56
    private DistortedScreen mScreen;
57
    private DistortedEffects mEffects;
58 ba9ae2c8 Leszek Koltunski
    private Static3D mScale;
59 508ee98f Leszek Koltunski
    private MeshBase mMesh;
60
61
    Static4D mQuat1, mQuat2;
62
    int mScreenMin;
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
    MeshJoinRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69
      mScreen = new DistortedScreen();
70
      mScale= new Static3D(1,1,1);
71 ba9ae2c8 Leszek Koltunski
      Static3D center=new Static3D(0,0,0);
72 508ee98f Leszek Koltunski
73 ea9b68db Leszek Koltunski
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
74
      sink.add( new Static1D(0.5f) );
75
      sink.add( new Static1D(2.0f) );
76
77 508ee98f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
78
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
79
80
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
81
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
82
83
      quatInt1.add(mQuat1);
84
      quatInt2.add(mQuat2);
85
86
      mEffects = new DistortedEffects();
87 ba9ae2c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
88
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
89 508ee98f Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale));
90 ea9b68db Leszek Koltunski
      mEffects.apply( new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) ) );
91
92
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
93 508ee98f Leszek Koltunski
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
   
97
    public void onDrawFrame(GL10 glUnused) 
98
      {
99
      mScreen.render( System.currentTimeMillis() );
100
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
105
      {
106 ea9b68db Leszek Koltunski
      final float SCALE = 0.7f;
107 dd19d6e0 Leszek Koltunski
      mScreenMin = Math.min(width, height);
108 37320052 Leszek Koltunski
      float factor = SCALE*mScreenMin;
109 508ee98f Leszek Koltunski
      mScale.set(factor,factor,factor);
110
      mScreen.resize(width, height);
111
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116
      {
117
      if( mTexture==null ) mTexture = new DistortedTexture();
118 f793bd9d Leszek Koltunski
      mTexture.setTexture( createTetrahedronTexture() );
119 508ee98f Leszek Koltunski
120 33bd5234 Leszek Koltunski
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
121 508ee98f Leszek Koltunski
122
      mScreen.detachAll();
123
      mScreen.attach(mTexture,mEffects,mMesh);
124
125 dd19d6e0 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 7);
126 da412a40 Leszek Koltunski
      VertexEffectSink.enable();
127 ea9b68db Leszek Koltunski
128 508ee98f Leszek Koltunski
      try
129
        {
130
        DistortedLibrary.onCreate(mView.getContext());
131
        }
132
      catch(Exception ex)
133
        {
134
        android.util.Log.e("MeshJoin", ex.getMessage() );
135
        }
136
      }
137
138 8982b894 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    void setChecked(int number, boolean checked)
141
      {
142
      android.util.Log.e("renderer", "Checkbox "+number+" checked: "+checked);
143
      }
144
145 33bd5234 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147 f793bd9d Leszek Koltunski
    private Bitmap createTetrahedronTexture()
148 33bd5234 Leszek Koltunski
      {
149 ea9b68db Leszek Koltunski
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
150
      final int FACES=FACE_COLORS.length;
151 a8a553a5 Leszek Koltunski
      int SIZE = 200;
152 77e9d967 Leszek Koltunski
      float STROKE = 0.05f*SIZE;
153
      float OFF = STROKE/2 -1;
154 a8a553a5 Leszek Koltunski
      float OFF2 = 0.5f*SIZE + OFF;
155 77e9d967 Leszek Koltunski
      float HEIGHT = SIZE - OFF;
156
      float RADIUS = SIZE/12;
157 a8a553a5 Leszek Koltunski
      float ARC1_H = 0.2f*SIZE;
158 f793bd9d Leszek Koltunski
      float ARC1_W = SIZE*0.5f;
159 a8a553a5 Leszek Koltunski
      float ARC2_W = 0.153f*SIZE;
160
      float ARC2_H = 0.905f*SIZE;
161
      float ARC3_W = SIZE-ARC2_W;
162 f793bd9d Leszek Koltunski
163
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
164 ea9b68db Leszek Koltunski
      Canvas canvas = new Canvas(result);
165
      Paint paint = new Paint();
166 f793bd9d Leszek Koltunski
      paint.setAntiAlias(true);
167
      paint.setStrokeWidth(STROKE);
168 b3090c52 Leszek Koltunski
169 ea9b68db Leszek Koltunski
      for(int i=0; i<FACES; i++)
170
        {
171
        paint.setColor(FACE_COLORS[i]);
172 f793bd9d Leszek Koltunski
        paint.setStyle(Paint.Style.FILL);
173
174
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
175
176
        paint.setColor(0xff000000);
177
        paint.setStyle(Paint.Style.STROKE);
178
179
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
180
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
181
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
182
183
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
184
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
185
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
186 ea9b68db Leszek Koltunski
        }
187 33bd5234 Leszek Koltunski
188 23c35a5d Leszek Koltunski
      return result;
189 508ee98f Leszek Koltunski
      }
190 33bd5234 Leszek Koltunski
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193
    private MeshBase createJoinedTetrahedron()
194
      {
195 40223bbb Leszek Koltunski
      final float SQ2 = (float)Math.sqrt(2);
196 33bd5234 Leszek Koltunski
      final float SQ3 = (float)Math.sqrt(3);
197
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
198
      final int MESHES=4;
199
200 dd19d6e0 Leszek Koltunski
      int association = 1;
201 33bd5234 Leszek Koltunski
      MeshBase[] meshes = new MeshTriangles[MESHES];
202 ea9b68db Leszek Koltunski
203 dd19d6e0 Leszek Koltunski
      for(int i=0; i<MESHES; i++)
204
        {
205
        meshes[i] = new MeshTriangles(10);
206
        meshes[i].setEffectAssociation(0,association);
207
        association <<= 1;
208
        }
209 ea9b68db Leszek Koltunski
210 dd19d6e0 Leszek Koltunski
      Static4D[] textureMaps = new Static4D[MESHES];
211
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
212 ea9b68db Leszek Koltunski
      MeshBase result = new MeshJoined(meshes);
213
      result.setTextureMap(textureMaps);
214
215 dd19d6e0 Leszek Koltunski
      Static1D angle  = new Static1D(angleFaces);
216
      Static3D axis1  = new Static3D(  -1, 0,      0);
217
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
218
      Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
219
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
220
      Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
221
222
      VertexEffectScale  effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
223
      VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
224
      VertexEffectMove   effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
225
      VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
226
      VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 );
227
      VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 );
228
      VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 );
229
230
      effect1.setMeshAssociation(15);  // apply to all 4 meshes
231
      effect2.setMeshAssociation(15);  // apply to all 4 meshes
232
      effect3.setMeshAssociation(15);  // apply to all 4 meshes
233
      effect4.setMeshAssociation(14);  // apply to mesh[1], [2] and [3]
234
      effect5.setMeshAssociation( 2);  // apply only to mesh[1]
235 8982b894 Leszek Koltunski
      effect6.setMeshAssociation( 4);  // apply only to mesh[2]
236 da412a40 Leszek Koltunski
      effect7.setMeshAssociation( 8);  // apply only to mesh[3]
237 dd19d6e0 Leszek Koltunski
238
      result.apply(effect1);
239
      result.apply(effect2);
240
      result.apply(effect3);
241
      result.apply(effect4);
242
      result.apply(effect5);
243
      result.apply(effect6);
244
      result.apply(effect7);
245
246 ea9b68db Leszek Koltunski
      return result;
247 33bd5234 Leszek Koltunski
      }
248 508ee98f Leszek Koltunski
}