Project

General

Profile

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

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

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.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.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectMove;
31
import org.distorted.library.effect.VertexEffectRotate;
32
import org.distorted.library.effect.VertexEffectScale;
33
import org.distorted.library.effect.VertexEffectSink;
34
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
import org.distorted.library.mesh.MeshJoined;
40
import org.distorted.library.mesh.MeshTriangles;
41
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.DynamicQuat;
43
import org.distorted.library.type.Static1D;
44
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
    private Static3D mScale;
59
    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
      Static3D center=new Static3D(0,0,0);
72

    
73
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
74
      sink.add( new Static1D(0.5f) );
75
      sink.add( new Static1D(2.0f) );
76

    
77
      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
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
88
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
89
      mEffects.apply( new MatrixEffectScale(mScale));
90
      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
      }
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
      final float SCALE = 0.7f;
107
      mScreenMin = Math.min(width, height);
108
      float factor = SCALE*mScreenMin;
109
      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
      mTexture.setTexture( createTetrahedronTexture() );
119

    
120
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
121

    
122
      mScreen.detachAll();
123
      mScreen.attach(mTexture,mEffects,mMesh);
124

    
125
      DistortedLibrary.setMax(EffectType.VERTEX, 7);
126

    
127
      try
128
        {
129
        DistortedLibrary.onCreate(mView.getContext());
130
        }
131
      catch(Exception ex)
132
        {
133
        android.util.Log.e("MeshJoin", ex.getMessage() );
134
        }
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    private Bitmap createTetrahedronTexture()
140
      {
141
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
142
      final int FACES=FACE_COLORS.length;
143
      int SIZE = 200;
144
      float STROKE = 0.05f*SIZE;
145
      float OFF = STROKE/2 -1;
146
      float OFF2 = 0.5f*SIZE + OFF;
147
      float HEIGHT = SIZE - OFF;
148
      float RADIUS = SIZE/12;
149
      float ARC1_H = 0.2f*SIZE;
150
      float ARC1_W = SIZE*0.5f;
151
      float ARC2_W = 0.153f*SIZE;
152
      float ARC2_H = 0.905f*SIZE;
153
      float ARC3_W = SIZE-ARC2_W;
154

    
155
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
156
      Canvas canvas = new Canvas(result);
157
      Paint paint = new Paint();
158
      paint.setAntiAlias(true);
159
      paint.setStrokeWidth(STROKE);
160

    
161
      for(int i=0; i<FACES; i++)
162
        {
163
        paint.setColor(FACE_COLORS[i]);
164
        paint.setStyle(Paint.Style.FILL);
165

    
166
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
167

    
168
        paint.setColor(0xff000000);
169
        paint.setStyle(Paint.Style.STROKE);
170

    
171
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
172
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
173
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
174

    
175
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
176
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
177
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
178
        }
179

    
180
      return result;
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    private MeshBase createJoinedTetrahedron()
186
      {
187
      final float SQ2 = (float)Math.sqrt(2);
188
      final float SQ3 = (float)Math.sqrt(3);
189
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
190
      final int MESHES=4;
191

    
192
      int association = 1;
193
      MeshBase[] meshes = new MeshTriangles[MESHES];
194

    
195
      for(int i=0; i<MESHES; i++)
196
        {
197
        meshes[i] = new MeshTriangles(10);
198
        meshes[i].setEffectAssociation(0,association);
199
        association <<= 1;
200
        }
201

    
202
      Static4D[] textureMaps = new Static4D[MESHES];
203
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
204
      MeshBase result = new MeshJoined(meshes);
205
      result.setTextureMap(textureMaps);
206

    
207
      Static1D angle  = new Static1D(angleFaces);
208
      Static3D axis1  = new Static3D(  -1, 0,      0);
209
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
210
      Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
211
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
212
      Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
213

    
214
      VertexEffectScale  effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
215
      VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
216
      VertexEffectMove   effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
217
      VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
218
      VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 );
219
      VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 );
220
      VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 );
221

    
222
      effect1.setMeshAssociation(15);  // apply to all 4 meshes
223
      effect2.setMeshAssociation(15);  // apply to all 4 meshes
224
      effect3.setMeshAssociation(15);  // apply to all 4 meshes
225
      effect4.setMeshAssociation(14);  // apply to mesh[1], [2] and [3]
226
      effect5.setMeshAssociation( 2);  // apply only to mesh[1]
227
      effect6.setMeshAssociation( 0);  // apply onlt to mesh[2]
228
      effect7.setMeshAssociation( 0);  // apply only to mesh[3]
229

    
230
      result.apply(effect1);
231
      result.apply(effect2);
232
      result.apply(effect3);
233
      result.apply(effect4);
234
      result.apply(effect5);
235
      result.apply(effect6);
236
      result.apply(effect7);
237

    
238
      return result;
239
      }
240
}
(2-2/3)