Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 33bd5234

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.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.MatrixEffect;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectRotate;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshJoined;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.mesh.MeshSphere;
41
import org.distorted.library.mesh.MeshTriangles;
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 java.io.IOException;
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
56
{
57
    private GLSurfaceView mView;
58
    private DistortedTexture mTexture;
59
    private DistortedScreen mScreen;
60
    private DistortedEffects mEffects;
61
    private Static3D mScale;
62
    private MeshBase mMesh;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
76
      mQuat1 = new Static4D(0,0,0,1);  // unity
77
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
78

    
79
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
80
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
81

    
82
      quatInt1.add(mQuat1);
83
      quatInt2.add(mQuat2);
84

    
85
      mEffects = new DistortedEffects();
86
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
87
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
88
      mEffects.apply( new MatrixEffectScale(mScale));
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
   
93
    public void onDrawFrame(GL10 glUnused) 
94
      {
95
      mScreen.render( System.currentTimeMillis() );
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102
      final float SCALE = 0.6f;
103
      mScreenMin = width<height ? width:height;
104
      float factor = SCALE*mScreenMin;
105
      mScale.set(factor,factor,factor);
106
      mScreen.resize(width, height);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
      {
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
114
      Bitmap bitmap;
115

    
116
      try
117
        {
118
        bitmap = BitmapFactory.decodeStream(is);
119
        }
120
      finally
121
        {
122
        try
123
          {
124
          is.close();
125
          }
126
        catch(IOException e) { }
127
        }
128

    
129
      if( mTexture==null ) mTexture = new DistortedTexture();
130
      mTexture.setTexture(bitmap);
131

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

    
134
     // mMesh.setShowNormals(true);
135

    
136
      mScreen.detachAll();
137
      mScreen.attach(mTexture,mEffects,mMesh);
138

    
139
      try
140
        {
141
        DistortedLibrary.onCreate(mView.getContext());
142
        }
143
      catch(Exception ex)
144
        {
145
        android.util.Log.e("MeshJoin", ex.getMessage() );
146
        }
147
      }
148

    
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    private MeshBase createJoinedDoubleQuad()
153
      {
154
      MeshBase[] meshes = new MeshBase[2];
155
      meshes[0] = new MeshQuad();
156
      meshes[1] = new MeshQuad();
157

    
158
      MatrixEffect[] effects0 = new MatrixEffect[1];
159
      effects0[0] = new MatrixEffectMove( new Static3D(0,+0.6f,0));
160

    
161
      meshes[0].apply(effects0);
162

    
163
      MatrixEffect[] effects1 = new MatrixEffect[1];
164
      effects1[0] = new MatrixEffectMove( new Static3D(0,-0.6f,0));
165

    
166
      meshes[1].apply(effects1);
167

    
168
      return new MeshJoined(meshes);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    private MeshBase createJoinedSphereAndTriangle()
174
      {
175
      MeshBase[] meshes = new MeshBase[2];
176
      meshes[0] = new MeshSphere(5);
177
      meshes[1] = new MeshTriangles(5);
178

    
179
      MatrixEffect[] effects0 = new MatrixEffect[2];
180
      effects0[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
181
      effects0[1] = new MatrixEffectMove( new Static3D(+0.25f,0,0));
182

    
183
      meshes[0].apply(effects0);
184

    
185
      MatrixEffect[] effects1 = new MatrixEffect[2];
186
      effects1[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
187
      effects1[1] = new MatrixEffectMove( new Static3D(-0.25f,0,0));
188

    
189
      meshes[1].apply(effects1);
190

    
191
      return new MeshJoined(meshes);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    private MeshBase createJoinedCube()
197
      {
198
      final int MESHES=6;
199

    
200
      Static3D axisY  = new Static3D(0,1,0);
201
      Static3D axisX  = new Static3D(1,0,0);
202
      Static3D center = new Static3D(0,0,0);
203
      Static1D angle  = new Static1D(0);
204

    
205
      MatrixEffect[] effectsY = new MatrixEffect[2];
206
      effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
207
      effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
208

    
209
      MeshBase[] meshes = new MeshRectangles[MESHES];
210
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(5,5);
211

    
212
      angle.set(0);
213
      meshes[0].apply(effectsY);
214
      angle.set(90);
215
      meshes[1].apply(effectsY);
216
      angle.set(180);
217
      meshes[2].apply(effectsY);
218
      angle.set(270);
219
      meshes[3].apply(effectsY);
220

    
221
      MatrixEffect[] effectsX = new MatrixEffect[2];
222
      effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
223
      effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
224

    
225
      angle.set( 90);
226
      meshes[4].apply(effectsX);
227
      angle.set(-90);
228
      meshes[5].apply(effectsX);
229

    
230
      MeshJoined result = new MeshJoined(meshes);
231

    
232
      Static4D[] maps = new Static4D[MESHES];
233

    
234
      maps[0] = new Static4D(3.0f/8, 3.0f/8, 2.0f/8, 2.0f/8);
235
      maps[1] = new Static4D(5.0f/8, 1.0f/8, 2.0f/8, 2.0f/8);
236
      maps[2] = new Static4D(1.0f/8, 5.0f/8, 2.0f/8, 2.0f/8);
237
      maps[3] = new Static4D(1.0f/8, 3.0f/8, 2.0f/8, 2.0f/8);
238
      maps[4] = new Static4D(3.0f/8, 1.0f/8, 2.0f/8, 2.0f/8);
239
      maps[5] = new Static4D(1.0f/8, 1.0f/8, 2.0f/8, 2.0f/8);
240

    
241
      result.setTextureMap(maps);
242
      return result;
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    private MeshBase createJoinedTetrahedron()
248
      {
249
      final float SQ3 = (float)Math.sqrt(3);
250
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
251
      final int MESHES=4;
252

    
253
      MeshBase[] meshes = new MeshTriangles[MESHES];
254
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(5);
255

    
256
      MatrixEffect[] effects0 = new MatrixEffect[3];
257
      effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
258
      effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
259
      effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3/6,SQ3/12) );
260

    
261
      meshes[0].apply(effects0);
262

    
263
      Static1D angle = new Static1D(angleFaces);
264
      Static3D axis  = new Static3D(-1,0,0);
265
      Static3D center= new Static3D(0,-SQ3/6,-SQ3/6);
266

    
267
      MatrixEffect[] effects1 = new MatrixEffect[5];
268
      effects1[0] = effects0[0];
269
      effects1[1] = effects0[1];
270
      effects1[2] = effects0[2];
271
      effects1[3] = new MatrixEffectRotate( new Static1D(180), new Static3D(0,0,1), center );
272
      effects1[4] = new MatrixEffectRotate( angle, axis, center );
273
      meshes[1].apply(effects1);
274

    
275
      axis.set(0.5f,0,-SQ3/2);
276
      center.set2(SQ3/3);
277
      meshes[2].apply(effects1);
278

    
279
      axis.set2(SQ3/2);
280
      meshes[3].apply(effects1);
281

    
282
      return new MeshJoined(meshes);
283
      }
284
}
(2-2/3)