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 |
dd19d6e0
|
Leszek Koltunski
|
import org.distorted.library.effect.EffectType;
|
28 |
508ee98f
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
29 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
30 |
dd19d6e0
|
Leszek Koltunski
|
import org.distorted.library.effect.VertexEffectMove;
|
31 |
|
|
import org.distorted.library.effect.VertexEffectRotate;
|
32 |
|
|
import org.distorted.library.effect.VertexEffectScale;
|
33 |
ea9b68db
|
Leszek Koltunski
|
import org.distorted.library.effect.VertexEffectSink;
|
34 |
508ee98f
|
Leszek Koltunski
|
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 |
37320052
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshJoined;
|
40 |
b3090c52
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshTriangles;
|
41 |
ea9b68db
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
42 |
508ee98f
|
Leszek Koltunski
|
import org.distorted.library.type.DynamicQuat;
|
43 |
37320052
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
44 |
508ee98f
|
Leszek Koltunski
|
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 |
ba9ae2c8
|
Leszek Koltunski
|
private Static3D mScale;
|
59 |
508ee98f
|
Leszek Koltunski
|
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 |
ba9ae2c8
|
Leszek Koltunski
|
Static3D center=new Static3D(0,0,0);
|
72 |
508ee98f
|
Leszek Koltunski
|
|
73 |
ea9b68db
|
Leszek Koltunski
|
Dynamic1D sink = new Dynamic1D(5000,0.0f);
|
74 |
|
|
sink.add( new Static1D(0.5f) );
|
75 |
|
|
sink.add( new Static1D(2.0f) );
|
76 |
|
|
|
77 |
508ee98f
|
Leszek Koltunski
|
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 |
ba9ae2c8
|
Leszek Koltunski
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
88 |
|
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
89 |
508ee98f
|
Leszek Koltunski
|
mEffects.apply( new MatrixEffectScale(mScale));
|
90 |
ea9b68db
|
Leszek Koltunski
|
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 |
508ee98f
|
Leszek Koltunski
|
}
|
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 |
ea9b68db
|
Leszek Koltunski
|
final float SCALE = 0.7f;
|
107 |
dd19d6e0
|
Leszek Koltunski
|
mScreenMin = Math.min(width, height);
|
108 |
37320052
|
Leszek Koltunski
|
float factor = SCALE*mScreenMin;
|
109 |
508ee98f
|
Leszek Koltunski
|
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 |
f793bd9d
|
Leszek Koltunski
|
mTexture.setTexture( createTetrahedronTexture() );
|
119 |
508ee98f
|
Leszek Koltunski
|
|
120 |
33bd5234
|
Leszek Koltunski
|
if( mMesh==null ) mMesh = createJoinedTetrahedron();
|
121 |
508ee98f
|
Leszek Koltunski
|
|
122 |
|
|
mScreen.detachAll();
|
123 |
|
|
mScreen.attach(mTexture,mEffects,mMesh);
|
124 |
|
|
|
125 |
dd19d6e0
|
Leszek Koltunski
|
DistortedLibrary.setMax(EffectType.VERTEX, 7);
|
126 |
ea9b68db
|
Leszek Koltunski
|
|
127 |
508ee98f
|
Leszek Koltunski
|
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 |
33bd5234
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138 |
|
|
|
139 |
f793bd9d
|
Leszek Koltunski
|
private Bitmap createTetrahedronTexture()
|
140 |
33bd5234
|
Leszek Koltunski
|
{
|
141 |
ea9b68db
|
Leszek Koltunski
|
final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
|
142 |
|
|
final int FACES=FACE_COLORS.length;
|
143 |
a8a553a5
|
Leszek Koltunski
|
int SIZE = 200;
|
144 |
77e9d967
|
Leszek Koltunski
|
float STROKE = 0.05f*SIZE;
|
145 |
|
|
float OFF = STROKE/2 -1;
|
146 |
a8a553a5
|
Leszek Koltunski
|
float OFF2 = 0.5f*SIZE + OFF;
|
147 |
77e9d967
|
Leszek Koltunski
|
float HEIGHT = SIZE - OFF;
|
148 |
|
|
float RADIUS = SIZE/12;
|
149 |
a8a553a5
|
Leszek Koltunski
|
float ARC1_H = 0.2f*SIZE;
|
150 |
f793bd9d
|
Leszek Koltunski
|
float ARC1_W = SIZE*0.5f;
|
151 |
a8a553a5
|
Leszek Koltunski
|
float ARC2_W = 0.153f*SIZE;
|
152 |
|
|
float ARC2_H = 0.905f*SIZE;
|
153 |
|
|
float ARC3_W = SIZE-ARC2_W;
|
154 |
f793bd9d
|
Leszek Koltunski
|
|
155 |
|
|
Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
156 |
ea9b68db
|
Leszek Koltunski
|
Canvas canvas = new Canvas(result);
|
157 |
|
|
Paint paint = new Paint();
|
158 |
f793bd9d
|
Leszek Koltunski
|
paint.setAntiAlias(true);
|
159 |
|
|
paint.setStrokeWidth(STROKE);
|
160 |
b3090c52
|
Leszek Koltunski
|
|
161 |
ea9b68db
|
Leszek Koltunski
|
for(int i=0; i<FACES; i++)
|
162 |
|
|
{
|
163 |
|
|
paint.setColor(FACE_COLORS[i]);
|
164 |
f793bd9d
|
Leszek Koltunski
|
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 |
ea9b68db
|
Leszek Koltunski
|
}
|
179 |
33bd5234
|
Leszek Koltunski
|
|
180 |
23c35a5d
|
Leszek Koltunski
|
return result;
|
181 |
508ee98f
|
Leszek Koltunski
|
}
|
182 |
33bd5234
|
Leszek Koltunski
|
|
183 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184 |
|
|
|
185 |
|
|
private MeshBase createJoinedTetrahedron()
|
186 |
|
|
{
|
187 |
40223bbb
|
Leszek Koltunski
|
final float SQ2 = (float)Math.sqrt(2);
|
188 |
33bd5234
|
Leszek Koltunski
|
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 |
dd19d6e0
|
Leszek Koltunski
|
int association = 1;
|
193 |
33bd5234
|
Leszek Koltunski
|
MeshBase[] meshes = new MeshTriangles[MESHES];
|
194 |
ea9b68db
|
Leszek Koltunski
|
|
195 |
dd19d6e0
|
Leszek Koltunski
|
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 |
ea9b68db
|
Leszek Koltunski
|
|
202 |
dd19d6e0
|
Leszek Koltunski
|
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 |
ea9b68db
|
Leszek Koltunski
|
MeshBase result = new MeshJoined(meshes);
|
205 |
|
|
result.setTextureMap(textureMaps);
|
206 |
|
|
|
207 |
dd19d6e0
|
Leszek Koltunski
|
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 |
ea9b68db
|
Leszek Koltunski
|
return result;
|
239 |
33bd5234
|
Leszek Koltunski
|
}
|
240 |
508ee98f
|
Leszek Koltunski
|
}
|