Revision ea9b68db
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/examples/meshjoin/MeshJoinRenderer.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.graphics.Bitmap; |
23 | 23 |
import android.graphics.BitmapFactory; |
24 |
import android.graphics.Canvas; |
|
25 |
import android.graphics.Paint; |
|
24 | 26 |
import android.opengl.GLSurfaceView; |
25 | 27 |
|
26 | 28 |
import org.distorted.examples.R; |
... | ... | |
29 | 31 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
30 | 32 |
import org.distorted.library.effect.MatrixEffectRotate; |
31 | 33 |
import org.distorted.library.effect.MatrixEffectScale; |
34 |
import org.distorted.library.effect.VertexEffectSink; |
|
32 | 35 |
import org.distorted.library.main.DistortedEffects; |
33 | 36 |
import org.distorted.library.main.DistortedLibrary; |
34 | 37 |
import org.distorted.library.main.DistortedScreen; |
35 | 38 |
import org.distorted.library.main.DistortedTexture; |
36 | 39 |
import org.distorted.library.mesh.MeshBase; |
37 | 40 |
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 | 41 |
import org.distorted.library.mesh.MeshTriangles; |
42 |
import org.distorted.library.type.Dynamic1D; |
|
42 | 43 |
import org.distorted.library.type.DynamicQuat; |
43 | 44 |
import org.distorted.library.type.Static1D; |
44 | 45 |
import org.distorted.library.type.Static3D; |
... | ... | |
73 | 74 |
mScale= new Static3D(1,1,1); |
74 | 75 |
Static3D center=new Static3D(0,0,0); |
75 | 76 |
|
77 |
Dynamic1D sink = new Dynamic1D(5000,0.0f); |
|
78 |
sink.add( new Static1D(0.5f) ); |
|
79 |
sink.add( new Static1D(2.0f) ); |
|
80 |
|
|
76 | 81 |
mQuat1 = new Static4D(0,0,0,1); // unity |
77 | 82 |
mQuat2 = new Static4D(0,0,0,1); // quaternions |
78 | 83 |
|
... | ... | |
86 | 91 |
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) ); |
87 | 92 |
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) ); |
88 | 93 |
mEffects.apply( new MatrixEffectScale(mScale)); |
94 |
mEffects.apply( new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) ) ); |
|
95 |
|
|
96 |
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
|
89 | 97 |
} |
90 | 98 |
|
91 | 99 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
99 | 107 |
|
100 | 108 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
101 | 109 |
{ |
102 |
final float SCALE = 0.6f;
|
|
110 |
final float SCALE = 0.7f;
|
|
103 | 111 |
mScreenMin = width<height ? width:height; |
104 | 112 |
float factor = SCALE*mScreenMin; |
105 | 113 |
mScale.set(factor,factor,factor); |
... | ... | |
110 | 118 |
|
111 | 119 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
112 | 120 |
{ |
113 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
|
121 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.pyraminx);
|
|
114 | 122 |
Bitmap bitmap; |
115 | 123 |
|
116 | 124 |
try |
... | ... | |
127 | 135 |
} |
128 | 136 |
|
129 | 137 |
if( mTexture==null ) mTexture = new DistortedTexture(); |
130 |
mTexture.setTexture(bitmap);
|
|
138 |
mTexture.setTexture( createTetrahedronTexture(bitmap) );
|
|
131 | 139 |
|
132 | 140 |
if( mMesh==null ) mMesh = createJoinedTetrahedron(); |
133 | 141 |
|
134 |
// mMesh.setShowNormals(true);
|
|
142 |
//mMesh.setShowNormals(true);
|
|
135 | 143 |
|
136 | 144 |
mScreen.detachAll(); |
137 | 145 |
mScreen.attach(mTexture,mEffects,mMesh); |
138 | 146 |
|
147 |
VertexEffectSink.enable(); |
|
148 |
|
|
139 | 149 |
try |
140 | 150 |
{ |
141 | 151 |
DistortedLibrary.onCreate(mView.getContext()); |
... | ... | |
146 | 156 |
} |
147 | 157 |
} |
148 | 158 |
|
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 | 159 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
172 | 160 |
|
173 |
private MeshBase createJoinedSphereAndTriangle()
|
|
161 |
private Bitmap createTetrahedronTexture(Bitmap input)
|
|
174 | 162 |
{ |
175 |
MeshBase[] meshes = new MeshBase[2];
|
|
176 |
meshes[0] = new MeshSphere(5);
|
|
177 |
meshes[1] = new MeshTriangles(5);
|
|
163 |
final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
|
|
164 |
final int FACES=FACE_COLORS.length;
|
|
165 |
int height = input.getHeight();
|
|
178 | 166 |
|
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)); |
|
167 |
Bitmap result = Bitmap.createBitmap(FACES*height,height, Bitmap.Config.ARGB_8888); |
|
168 |
Canvas canvas = new Canvas(result); |
|
169 |
Paint paint = new Paint(); |
|
170 |
paint.setStyle(Paint.Style.FILL); |
|
182 | 171 |
|
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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
172 |
for(int i=0; i<FACES; i++) |
|
173 |
{ |
|
174 |
paint.setColor(FACE_COLORS[i]); |
|
175 |
canvas.drawRect(i*height,0,(i+1)*height,height,paint); |
|
176 |
canvas.drawBitmap(input,i*height,0,null); |
|
177 |
} |
|
195 | 178 |
|
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 | 179 |
return result; |
243 | 180 |
} |
244 | 181 |
|
... | ... | |
251 | 188 |
final int MESHES=4; |
252 | 189 |
|
253 | 190 |
MeshBase[] meshes = new MeshTriangles[MESHES]; |
254 |
for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(5);
|
|
191 |
for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(10);
|
|
255 | 192 |
|
256 | 193 |
MatrixEffect[] effects0 = new MatrixEffect[3]; |
257 | 194 |
effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) ); |
... | ... | |
279 | 216 |
axis.set2(SQ3/2); |
280 | 217 |
meshes[3].apply(effects1); |
281 | 218 |
|
282 |
return new MeshJoined(meshes); |
|
219 |
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; |
|
283 | 227 |
} |
284 | 228 |
} |
Also available in: Unified diff
Progress with MeshJoin app.
Fix rotating in some apps.