Revision fcc0e034
Added by Leszek Koltunski over 3 years ago
src/main/java/org/distorted/objects/RubikDino.java | ||
---|---|---|
23 | 23 |
import android.graphics.Canvas; |
24 | 24 |
import android.graphics.Paint; |
25 | 25 |
|
26 |
import org.distorted.library.effect.MatrixEffectMove; |
|
27 | 26 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
28 |
import org.distorted.library.effect.MatrixEffectRotate; |
|
29 |
import org.distorted.library.effect.MatrixEffectScale; |
|
30 | 27 |
import org.distorted.library.effect.VertexEffectDeform; |
31 | 28 |
import org.distorted.library.effect.VertexEffectMove; |
32 | 29 |
import org.distorted.library.effect.VertexEffectRotate; |
... | ... | |
52 | 49 |
{ |
53 | 50 |
private static final float SQ2 = (float)Math.sqrt(2); |
54 | 51 |
private static final float SQ3 = (float)Math.sqrt(3); |
55 |
private static final float ANGLE_FACES = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron |
|
56 | 52 |
|
57 | 53 |
// the four rotation axis of a RubikDino. Must be normalized. |
58 | 54 |
static final Static3D[] ROT_AXIS = new Static3D[] |
... | ... | |
112 | 108 |
new Static3D(-1.5f, 0.0f,-1.5f ) |
113 | 109 |
}; |
114 | 110 |
|
115 |
private static final int[] mFaceMap = {2,4, 4,0, 3,4, 4,1,
|
|
116 |
0,2, 0,3, 1,3, 1,2,
|
|
117 |
2,5, 5,0, 3,5, 5,1 };
|
|
111 |
private static final int[] mFaceMap = {4,2, 0,4, 4,3, 1,4,
|
|
112 |
2,0, 3,0, 3,1, 2,1,
|
|
113 |
5,2, 0,5, 5,3, 1,5 };
|
|
118 | 114 |
|
119 | 115 |
private static MeshBase mMesh; |
120 | 116 |
|
... | ... | |
128 | 124 |
|
129 | 125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
130 | 126 |
|
131 |
private MeshBase createTetrahedronMesh()
|
|
127 |
private void createBasicMesh()
|
|
132 | 128 |
{ |
129 |
final float ANGLE = (float)((180/Math.PI)*(Math.atan(SQ2))); |
|
130 |
|
|
133 | 131 |
final int MESHES=4; |
134 | 132 |
|
135 |
int association = 1; |
|
133 |
float D = 0.02f; |
|
134 |
float E = 0.5f*SQ2; |
|
135 |
float F = 0.5f; |
|
136 | 136 |
|
137 |
float D = 0.005f; |
|
138 |
float E = 0.5f - D*SQ2; |
|
139 |
float F = 0.5f - D*SQ2*SQ3; |
|
140 |
float[] bands = { 1.0f ,-D, |
|
141 |
1.0f-D/2,-D*0.55f, |
|
142 |
1.0f-D ,-D*0.25f, |
|
143 |
1.0f-2*D,+D*0.25f, |
|
144 |
0.50f, 0.040f, |
|
145 |
0.0f, 0.045f }; |
|
137 |
float[] bands0 = { 1.0f , 0, |
|
138 |
1.0f-2*D, D*0.25f, |
|
139 |
1.0f-4*D, D*0.35f, |
|
140 |
1.0f-8*D, D*0.6f, |
|
141 |
0.60f , D*1.0f, |
|
142 |
0.30f , D*1.375f, |
|
143 |
0.0f , D*1.4f }; |
|
146 | 144 |
|
147 |
float[] vertices = { -F,-E, +F,-E, 0.0f,E-D*SQ2};
|
|
145 |
float[] vertices0 = { -F,F/3, 0,-2*F/3, +F,F/3 };
|
|
148 | 146 |
|
149 | 147 |
MeshBase[] meshes = new MeshPolygon[MESHES]; |
150 |
meshes[0] = new MeshPolygon(vertices, bands, 2, 3); |
|
151 |
meshes[0].setEffectAssociation(0,association,0); |
|
148 |
meshes[0] = new MeshPolygon(vertices0, bands0, 2, 5); |
|
149 |
meshes[0].setEffectAssociation(0,1,0); |
|
150 |
meshes[1] = meshes[0].copy(true); |
|
151 |
meshes[1].setEffectAssociation(0,2,0); |
|
152 | 152 |
|
153 |
for(int i=1; i<MESHES; i++) |
|
154 |
{ |
|
155 |
association <<= 1; |
|
156 |
meshes[i] = meshes[0].copy(true); |
|
157 |
meshes[i].setEffectAssociation(0,association,0); |
|
158 |
} |
|
153 |
float[] bands1 = { 1.0f , 0, |
|
154 |
0.50f , 0.10f, |
|
155 |
0.0f , 0.20f }; |
|
156 |
|
|
157 |
float[] vertices1 = { -E/2,-E*(SQ3/6), E/2,-E*(SQ3/6), 0,E*(SQ3/3) }; |
|
159 | 158 |
|
160 |
MeshBase result = new MeshJoined(meshes); |
|
159 |
meshes[2] = new MeshPolygon(vertices1, bands1, 1, 2); |
|
160 |
meshes[2].setEffectAssociation(0,4,0); |
|
161 |
meshes[3] = meshes[2].copy(true); |
|
162 |
meshes[3].setEffectAssociation(0,8,0); |
|
161 | 163 |
|
162 |
Static3D a0 = new Static3D( 0, 1, 0 ); |
|
163 |
Static3D a1 = new Static3D( 0, -1.0f/3, 2*SQ2/3 ); |
|
164 |
Static3D a2 = new Static3D(-SQ2*SQ3/3, -1.0f/3, -SQ2/3 ); |
|
165 |
Static3D a3 = new Static3D( SQ2*SQ3/3, -1.0f/3, -SQ2/3 ); |
|
164 |
mMesh = new MeshJoined(meshes); |
|
166 | 165 |
|
167 |
float tetraHeight = SQ2*SQ3/3; |
|
168 |
float d1 = 0.75f*tetraHeight; |
|
169 |
float d2 =-0.10f*tetraHeight; |
|
170 |
float d3 =-0.05f*tetraHeight; |
|
171 |
float d4 = 0.15f*tetraHeight; |
|
166 |
Static3D a0 = new Static3D( 0,-3*F, 0 ); |
|
167 |
Static3D a1 = new Static3D( 0, 0, -3*F ); |
|
168 |
Static3D a2 = new Static3D( -3*F, 0, 0 ); |
|
169 |
Static3D a3 = new Static3D( +3*F, 0, 0 ); |
|
170 |
|
|
171 |
Static3D v0 = new Static3D( 0,-3*F/2, 3*F/2 ); |
|
172 |
Static3D v1 = new Static3D( 0, 3*F/2,-3*F/2 ); |
|
173 |
Static3D v2 = new Static3D( -3*F, 3*F/2, 3*F/2 ); |
|
174 |
Static3D v3 = new Static3D( +3*F, 3*F/2, 3*F/2 ); |
|
175 |
|
|
176 |
float d1 = 1.0f; |
|
177 |
float d2 =-0.10f; |
|
178 |
float d3 =-0.10f; |
|
179 |
float d4 = 0.40f; |
|
172 | 180 |
|
173 | 181 |
Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() ); |
174 | 182 |
Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() ); |
175 | 183 |
Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() ); |
176 | 184 |
Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() ); |
177 | 185 |
|
178 |
Static3D dVec0 = new Static3D( d3*a0.get0(), d3*a0.get1(), d3*a0.get2() );
|
|
179 |
Static3D dVec1 = new Static3D( d3*a1.get0(), d3*a1.get1(), d3*a1.get2() );
|
|
180 |
Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
|
|
181 |
Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
|
|
186 |
Static3D dVec0 = new Static3D( d3*v0.get0(), d3*v0.get1(), d3*v0.get2() );
|
|
187 |
Static3D dVec1 = new Static3D( d3*v1.get0(), d3*v1.get1(), d3*v1.get2() );
|
|
188 |
Static3D dVec2 = new Static3D( d2*v2.get0(), d2*v2.get1(), d2*v2.get2() );
|
|
189 |
Static3D dVec3 = new Static3D( d2*v3.get0(), d2*v3.get1(), d2*v3.get2() );
|
|
182 | 190 |
|
183 | 191 |
Static4D dReg = new Static4D(0,0,0,d4); |
184 | 192 |
Static1D dRad = new Static1D(1); |
185 | 193 |
|
186 |
Static1D angle = new Static1D(ANGLE_FACES); |
|
187 |
Static3D axis1 = new Static3D( -1, 0, 0); |
|
188 |
Static3D axis2 = new Static3D(0.5f, 0, -SQ3/2); |
|
189 |
Static3D axis3 = new Static3D(0.5f, 0, +SQ3/2); |
|
190 |
Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6); |
|
191 |
Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3); |
|
192 |
|
|
193 |
VertexEffectScale effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) ); |
|
194 |
VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) ); |
|
195 |
VertexEffectMove effect3 = new VertexEffectMove ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) ); |
|
196 |
VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 ); |
|
197 |
VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 ); |
|
198 |
VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 ); |
|
199 |
VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 ); |
|
200 |
|
|
201 |
VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg); |
|
202 |
VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg); |
|
203 |
VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg); |
|
204 |
VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg); |
|
205 |
|
|
206 |
effect4.setMeshAssociation(14,-1); // apply to mesh[1], [2] and [3] |
|
207 |
effect5.setMeshAssociation( 2,-1); // apply only to mesh[1] |
|
208 |
effect6.setMeshAssociation( 4,-1); // apply only to mesh[2] |
|
209 |
effect7.setMeshAssociation( 8,-1); // apply only to mesh[3] |
|
210 |
|
|
211 |
result.apply(effect1); |
|
212 |
result.apply(effect2); |
|
213 |
result.apply(effect3); |
|
214 |
result.apply(effect4); |
|
215 |
result.apply(effect5); |
|
216 |
result.apply(effect6); |
|
217 |
result.apply(effect7); |
|
218 |
result.apply(effect8); |
|
219 |
result.apply(effect9); |
|
220 |
result.apply(effect10); |
|
221 |
result.apply(effect11); |
|
222 |
|
|
223 |
result.mergeEffComponents(); |
|
224 |
|
|
225 |
return result; |
|
226 |
} |
|
227 |
|
|
228 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
229 |
|
|
230 |
private void createBasicMesh() |
|
231 |
{ |
|
232 |
mMesh = createTetrahedronMesh(); |
|
233 |
|
|
234 |
Static3D axis = new Static3D(1,0,0); |
|
235 |
Static3D cent = new Static3D(0,0,0); |
|
236 |
|
|
237 |
MatrixEffectMove moveEffect = new MatrixEffectMove ( new Static3D(0.0f,SQ3*SQ2/12,SQ3/6) ); |
|
238 |
MatrixEffectRotate rot1Effect = new MatrixEffectRotate( new Static1D(180+ANGLE_FACES/2), axis, cent); |
|
239 |
MatrixEffectScale scalEffect = new MatrixEffectScale ( new Static3D(3.0f, 3*SQ2/2, 1.5f) ); |
|
240 |
MatrixEffectRotate rot2Effect = new MatrixEffectRotate( new Static1D(-45), axis, cent); |
|
241 |
|
|
242 |
mMesh.apply(moveEffect, 0xffffffff, 0); |
|
243 |
mMesh.apply(rot1Effect, 0xffffffff, 0); |
|
244 |
mMesh.apply(scalEffect, 0xffffffff, 0); |
|
245 |
mMesh.apply(rot2Effect, 0xffffffff, 0); |
|
194 |
Static1D angle1 = new Static1D(+ANGLE); |
|
195 |
Static1D angle2 = new Static1D(-ANGLE); |
|
196 |
|
|
197 |
Static3D axisX = new Static3D(1,0,0); |
|
198 |
Static3D axisY = new Static3D(0,1,0); |
|
199 |
Static3D axisZ = new Static3D(0,-1,1); |
|
200 |
|
|
201 |
Static3D center0= new Static3D(0,0,0); |
|
202 |
Static3D center1= new Static3D(0,-3*F,0); |
|
203 |
|
|
204 |
VertexEffectScale effect0 = new VertexEffectScale ( new Static3D(3,3,3) ); |
|
205 |
VertexEffectMove effect1 = new VertexEffectMove ( new Static3D(0,-F,0) ); |
|
206 |
VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), axisX, center0 ); |
|
207 |
VertexEffectScale effect3 = new VertexEffectScale ( new Static3D(1,-1,1) ); |
|
208 |
VertexEffectMove effect4 = new VertexEffectMove ( new Static3D(3*E/2,E*(SQ3/2)-3*F,0) ); |
|
209 |
VertexEffectRotate effect5 = new VertexEffectRotate( new Static1D(+90), axisY, center1 ); |
|
210 |
VertexEffectScale effect6 = new VertexEffectScale ( new Static3D(-1,1,1) ); |
|
211 |
VertexEffectRotate effect7 = new VertexEffectRotate( new Static1D( 45), axisX, center1 ); |
|
212 |
VertexEffectRotate effect8 = new VertexEffectRotate( angle1 , axisZ, center1 ); |
|
213 |
VertexEffectRotate effect9 = new VertexEffectRotate( angle2 , axisZ, center1 ); |
|
214 |
|
|
215 |
VertexEffectDeform effect10= new VertexEffectDeform(dVec0, dRad, dCen0, dReg); |
|
216 |
VertexEffectDeform effect11= new VertexEffectDeform(dVec1, dRad, dCen1, dReg); |
|
217 |
VertexEffectDeform effect12= new VertexEffectDeform(dVec2, dRad, dCen2, dReg); |
|
218 |
VertexEffectDeform effect13= new VertexEffectDeform(dVec3, dRad, dCen3, dReg); |
|
219 |
|
|
220 |
effect0.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3 |
|
221 |
effect1.setMeshAssociation( 3,-1); // apply to meshes 0,1 |
|
222 |
effect2.setMeshAssociation( 2,-1); // apply to mesh 1 |
|
223 |
effect3.setMeshAssociation( 2,-1); // apply to mesh 0 |
|
224 |
effect4.setMeshAssociation(12,-1); // apply to meshes 2,3 |
|
225 |
effect5.setMeshAssociation(12,-1); // apply to meshes 2,3 |
|
226 |
effect6.setMeshAssociation( 8,-1); // apply to mesh 3 |
|
227 |
effect7.setMeshAssociation(12,-1); // apply to meshes 2,3 |
|
228 |
effect8.setMeshAssociation( 4,-1); // apply to mesh 2 |
|
229 |
effect9.setMeshAssociation( 8,-1); // apply to mesh 3 |
|
230 |
effect10.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3 |
|
231 |
effect11.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3 |
|
232 |
effect12.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3 |
|
233 |
effect13.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3 |
|
234 |
|
|
235 |
mMesh.apply(effect0); |
|
236 |
mMesh.apply(effect1); |
|
237 |
mMesh.apply(effect2); |
|
238 |
mMesh.apply(effect3); |
|
239 |
mMesh.apply(effect4); |
|
240 |
mMesh.apply(effect5); |
|
241 |
mMesh.apply(effect6); |
|
242 |
mMesh.apply(effect7); |
|
243 |
mMesh.apply(effect8); |
|
244 |
mMesh.apply(effect9); |
|
245 |
mMesh.apply(effect10); |
|
246 |
mMesh.apply(effect11); |
|
247 |
mMesh.apply(effect12); |
|
248 |
mMesh.apply(effect13); |
|
249 |
|
|
250 |
mMesh.mergeEffComponents(); |
|
246 | 251 |
} |
247 | 252 |
|
248 | 253 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
316 | 321 |
|
317 | 322 |
void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side) |
318 | 323 |
{ |
319 |
float STROKE = 0.046f*side; |
|
320 |
float OFF = STROKE/2 -1; |
|
321 |
float OFF2 = 0.5f*side + OFF; |
|
322 |
float HEIGHT = side - OFF; |
|
323 |
float RADIUS = side/12.0f; |
|
324 |
float ARC1_H = 0.2f*side; |
|
325 |
float ARC1_W = side*0.5f; |
|
326 |
float ARC2_W = 0.153f*side; |
|
327 |
float ARC2_H = 0.905f*side; |
|
328 |
float ARC3_W = side-ARC2_W; |
|
324 |
float STROKE = 0.04f*side; |
|
325 |
float L= left; |
|
326 |
float H= 0.333f*side; |
|
327 |
float LEN = 0.5f*side; |
|
329 | 328 |
|
330 | 329 |
paint.setAntiAlias(true); |
331 | 330 |
paint.setStrokeWidth(STROKE); |
... | ... | |
337 | 336 |
paint.setColor(INTERIOR_COLOR); |
338 | 337 |
paint.setStyle(Paint.Style.STROKE); |
339 | 338 |
|
340 |
canvas.drawLine( left, HEIGHT, side +left, HEIGHT, paint); |
|
341 |
canvas.drawLine( OFF +left, side , OFF2 +left, 0, paint); |
|
342 |
canvas.drawLine((side-OFF)+left, side , (side-OFF2) +left, 0, paint); |
|
339 |
canvas.drawLine( L , H, L+2*LEN, H , paint); |
|
340 |
canvas.drawLine( L , H, L+ LEN, H+LEN, paint); |
|
341 |
canvas.drawLine( L+2*LEN, H, L+ LEN, H+LEN, paint); |
|
342 |
|
|
343 |
float S1 = 0.150f*side; |
|
344 |
float S2 = 0.090f*side; |
|
345 |
float X = 0.7f*S2; |
|
346 |
float Y = 0.2f*S1; |
|
347 |
|
|
348 |
float LA = left+0.500f*side; |
|
349 |
float RA = left; |
|
350 |
float TA = 0.333f*side; |
|
351 |
float BA = 0.833f*side; |
|
343 | 352 |
|
344 |
canvas.drawArc( ARC1_W-RADIUS+left, ARC1_H-RADIUS, ARC1_W+RADIUS+left, ARC1_H+RADIUS, 225, 90, false, paint);
|
|
345 |
canvas.drawArc( ARC2_W-RADIUS+left, ARC2_H-RADIUS, ARC2_W+RADIUS+left, ARC2_H+RADIUS, 105, 90, false, paint);
|
|
346 |
canvas.drawArc( ARC3_W-RADIUS+left, ARC2_H-RADIUS, ARC3_W+RADIUS+left, ARC2_H+RADIUS, 345, 90, false, paint);
|
|
353 |
canvas.drawArc( RA+X , TA , RA+X+S2 , TA+S2, 135,135, false, paint);
|
|
354 |
canvas.drawArc( RA+side-S2-X, TA , RA+side-X, TA+S2, 270,135, false, paint);
|
|
355 |
canvas.drawArc( LA-S1/2 , BA-S1-Y, LA+S1/2 , BA-Y , 45, 90, false, paint);
|
|
347 | 356 |
} |
348 | 357 |
|
349 | 358 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Correct the Dino mesh.