Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 811ffcf5

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
    private int mSinkAssociation;
61
    private VertexEffectSink mSink;
62

    
63
    Static4D mQuat1, mQuat2;
64
    int mScreenMin;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
    MeshJoinRenderer(GLSurfaceView v)
69
      {
70
      mView = v;
71
      mScreen = new DistortedScreen();
72
      mScale= new Static3D(1,1,1);
73
      Static3D center=new Static3D(0,0,0);
74

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

    
79
      mQuat1 = new Static4D(0,0,0,1);  // unity
80
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
81

    
82
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
83
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
84

    
85
      quatInt1.add(mQuat1);
86
      quatInt2.add(mQuat2);
87

    
88
      mSinkAssociation = 15;
89
      mSink = new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) );
90
      mSink.setMeshAssociation(mSinkAssociation);
91

    
92
      mEffects = new DistortedEffects();
93
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
94
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
95
      mEffects.apply( new MatrixEffectScale(mScale));
96
      mEffects.apply( mSink );
97

    
98
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
    public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      final float SCALE = 0.7f;
113
      mScreenMin = Math.min(width, height);
114
      float factor = SCALE*mScreenMin;
115
      mScale.set(factor,factor,factor);
116
      mScreen.resize(width, height);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
122
      {
123
      if( mTexture==null ) mTexture = new DistortedTexture();
124
      mTexture.setTexture( createTetrahedronTexture() );
125

    
126
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
127

    
128
      mScreen.detachAll();
129
      mScreen.attach(mTexture,mEffects,mMesh);
130

    
131
      DistortedLibrary.setMax(EffectType.VERTEX, 7);
132
      VertexEffectSink.enable();
133

    
134
      try
135
        {
136
        DistortedLibrary.onCreate(mView.getContext());
137
        }
138
      catch(Exception ex)
139
        {
140
        android.util.Log.e("MeshJoin", ex.getMessage() );
141
        }
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    void setChecked(int number, boolean checked)
147
      {
148
      int n = (0x1 << number);
149

    
150
      if( checked ) mSinkAssociation |= n;
151
      else          mSinkAssociation &= (15-n);
152

    
153
      mSink.setMeshAssociation(mSinkAssociation);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

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

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

    
185
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
186

    
187
        paint.setColor(0xff000000);
188
        paint.setStyle(Paint.Style.STROKE);
189

    
190
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
191
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
192
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
193

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

    
199
      return result;
200
      }
201

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

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

    
211
      int association = 1;
212
      MeshBase[] meshes = new MeshTriangles[MESHES];
213

    
214
      for(int i=0; i<MESHES; i++)
215
        {
216
        meshes[i] = new MeshTriangles(10);
217
        meshes[i].setEffectAssociation(0,association);
218
        association <<= 1;
219
        }
220

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

    
226
      Static1D angle  = new Static1D(angleFaces);
227
      Static3D axis1  = new Static3D(  -1, 0,      0);
228
      Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
229
      Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
230
      Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
231
      Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
232

    
233
      VertexEffectScale  effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
234
      VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
235
      VertexEffectMove   effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
236
      VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
237
      VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 );
238
      VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 );
239
      VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 );
240

    
241
      effect1.setMeshAssociation(15);  // apply to all 4 meshes
242
      effect2.setMeshAssociation(15);  // apply to all 4 meshes
243
      effect3.setMeshAssociation(15);  // apply to all 4 meshes
244
      effect4.setMeshAssociation(14);  // apply to mesh[1], [2] and [3]
245
      effect5.setMeshAssociation( 2);  // apply only to mesh[1]
246
      effect6.setMeshAssociation( 4);  // apply only to mesh[2]
247
      effect7.setMeshAssociation( 8);  // apply only to mesh[3]
248

    
249
      result.apply(effect1);
250
      result.apply(effect2);
251
      result.apply(effect3);
252
      result.apply(effect4);
253
      result.apply(effect5);
254
      result.apply(effect6);
255
      result.apply(effect7);
256

    
257
      return result;
258
      }
259
}
(2-2/3)