Project

General

Profile

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

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

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