Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 77e9d967

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