1 |
887e1853
|
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.deferredjob;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.Canvas;
|
24 |
|
|
import android.graphics.Paint;
|
25 |
|
|
import android.opengl.GLSurfaceView;
|
26 |
|
|
|
27 |
|
|
import org.distorted.library.effect.EffectType;
|
28 |
ba984444
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
29 |
887e1853
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
30 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
31 |
|
|
import org.distorted.library.effect.VertexEffectRotate;
|
32 |
|
|
import org.distorted.library.main.DistortedEffects;
|
33 |
|
|
import org.distorted.library.main.DistortedLibrary;
|
34 |
|
|
import org.distorted.library.main.DistortedScreen;
|
35 |
|
|
import org.distorted.library.main.DistortedTexture;
|
36 |
|
|
import org.distorted.library.mesh.MeshBase;
|
37 |
|
|
import org.distorted.library.mesh.MeshJoined;
|
38 |
ba984444
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshTriangles;
|
39 |
887e1853
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
40 |
|
|
import org.distorted.library.type.DynamicQuat;
|
41 |
|
|
import org.distorted.library.type.Static1D;
|
42 |
|
|
import org.distorted.library.type.Static3D;
|
43 |
|
|
import org.distorted.library.type.Static4D;
|
44 |
|
|
|
45 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
46 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
|
50 |
|
|
class DeferredJobRenderer implements GLSurfaceView.Renderer
|
51 |
|
|
{
|
52 |
|
|
private GLSurfaceView mView;
|
53 |
|
|
private DistortedTexture mTexture;
|
54 |
|
|
private DistortedScreen mScreen;
|
55 |
|
|
private DistortedEffects mEffects;
|
56 |
|
|
private Static3D mScale;
|
57 |
|
|
private MeshBase mMesh;
|
58 |
bf3b5480
|
Leszek Koltunski
|
private VertexEffectRotate mRotate;
|
59 |
|
|
private Dynamic1D mAngleDyn;
|
60 |
|
|
private Static1D mAngle;
|
61 |
887e1853
|
Leszek Koltunski
|
|
62 |
|
|
Static4D mQuat1, mQuat2;
|
63 |
|
|
int mScreenMin;
|
64 |
|
|
|
65 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66 |
|
|
|
67 |
|
|
DeferredJobRenderer(GLSurfaceView v)
|
68 |
|
|
{
|
69 |
|
|
mView = v;
|
70 |
|
|
mScreen = new DistortedScreen();
|
71 |
|
|
mScale= new Static3D(1,1,1);
|
72 |
|
|
Static3D center=new Static3D(0,0,0);
|
73 |
|
|
|
74 |
|
|
Dynamic1D sink = new Dynamic1D(5000,0.0f);
|
75 |
|
|
sink.add( new Static1D(0.5f) );
|
76 |
|
|
sink.add( new Static1D(2.0f) );
|
77 |
|
|
|
78 |
9cbe58b0
|
Leszek Koltunski
|
mQuat1 = new Static4D(0,0,0,1);
|
79 |
|
|
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
80 |
887e1853
|
Leszek Koltunski
|
|
81 |
|
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
82 |
|
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
83 |
|
|
|
84 |
|
|
quatInt1.add(mQuat1);
|
85 |
|
|
quatInt2.add(mQuat2);
|
86 |
|
|
|
87 |
bf3b5480
|
Leszek Koltunski
|
mAngle = new Static1D(0);
|
88 |
|
|
|
89 |
|
|
mAngleDyn = new Dynamic1D(2000,0.5f);
|
90 |
|
|
mAngleDyn.add(new Static1D(0));
|
91 |
|
|
mAngleDyn.add( mAngle );
|
92 |
|
|
|
93 |
|
|
mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
|
94 |
887e1853
|
Leszek Koltunski
|
|
95 |
|
|
mEffects = new DistortedEffects();
|
96 |
bf3b5480
|
Leszek Koltunski
|
mEffects.apply( mRotate );
|
97 |
887e1853
|
Leszek Koltunski
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
98 |
|
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
99 |
|
|
mEffects.apply( new MatrixEffectScale(mScale));
|
100 |
|
|
|
101 |
|
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105 |
|
|
|
106 |
|
|
public void onDrawFrame(GL10 glUnused)
|
107 |
|
|
{
|
108 |
|
|
mScreen.render( System.currentTimeMillis() );
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
|
|
|
113 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
114 |
|
|
{
|
115 |
d08bd5a3
|
Leszek Koltunski
|
final float SCALE = 0.4f;
|
116 |
887e1853
|
Leszek Koltunski
|
mScreenMin = Math.min(width, height);
|
117 |
|
|
float factor = SCALE*mScreenMin;
|
118 |
|
|
mScale.set(factor,factor,factor);
|
119 |
|
|
mScreen.resize(width, height);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123 |
|
|
|
124 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
125 |
|
|
{
|
126 |
|
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
127 |
bf3b5480
|
Leszek Koltunski
|
mTexture.setTexture( createTexture() );
|
128 |
887e1853
|
Leszek Koltunski
|
|
129 |
bf3b5480
|
Leszek Koltunski
|
if( mMesh==null ) mMesh = createMesh();
|
130 |
887e1853
|
Leszek Koltunski
|
|
131 |
|
|
mScreen.detachAll();
|
132 |
|
|
mScreen.attach(mTexture,mEffects,mMesh);
|
133 |
|
|
|
134 |
|
|
DistortedLibrary.setMax(EffectType.VERTEX, 11);
|
135 |
bf3b5480
|
Leszek Koltunski
|
VertexEffectRotate.enable();
|
136 |
887e1853
|
Leszek Koltunski
|
|
137 |
|
|
try
|
138 |
|
|
{
|
139 |
|
|
DistortedLibrary.onCreate(mView.getContext());
|
140 |
|
|
}
|
141 |
|
|
catch(Exception ex)
|
142 |
|
|
{
|
143 |
bf3b5480
|
Leszek Koltunski
|
android.util.Log.e("DeferredJob", ex.getMessage() );
|
144 |
887e1853
|
Leszek Koltunski
|
}
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148 |
|
|
|
149 |
bf3b5480
|
Leszek Koltunski
|
void apply(int number)
|
150 |
887e1853
|
Leszek Koltunski
|
{
|
151 |
eb507734
|
Leszek Koltunski
|
mRotate.setMeshAssociation(0,number);
|
152 |
bf3b5480
|
Leszek Koltunski
|
mAngle.set(360);
|
153 |
|
|
mAngleDyn.resetToBeginning();
|
154 |
887e1853
|
Leszek Koltunski
|
}
|
155 |
|
|
|
156 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157 |
|
|
|
158 |
bf3b5480
|
Leszek Koltunski
|
private MeshBase createMesh()
|
159 |
887e1853
|
Leszek Koltunski
|
{
|
160 |
ba984444
|
Leszek Koltunski
|
final int MESHES=4;
|
161 |
887e1853
|
Leszek Koltunski
|
|
162 |
ba984444
|
Leszek Koltunski
|
MeshBase[] meshes = new MeshTriangles[MESHES];
|
163 |
|
|
MatrixEffectMove[] moveEffect = new MatrixEffectMove[MESHES];
|
164 |
887e1853
|
Leszek Koltunski
|
|
165 |
ba984444
|
Leszek Koltunski
|
meshes[0] = new MeshTriangles(1);
|
166 |
eb507734
|
Leszek Koltunski
|
meshes[0].setEffectAssociation(0,1,0);
|
167 |
bf3b5480
|
Leszek Koltunski
|
|
168 |
|
|
for(int i=1; i<MESHES; i++)
|
169 |
887e1853
|
Leszek Koltunski
|
{
|
170 |
bf3b5480
|
Leszek Koltunski
|
meshes[i] = meshes[0].copy(true);
|
171 |
eb507734
|
Leszek Koltunski
|
meshes[i].setEffectAssociation(0,1,i);
|
172 |
887e1853
|
Leszek Koltunski
|
}
|
173 |
|
|
|
174 |
ba984444
|
Leszek Koltunski
|
Static4D[] textureMaps = new Static4D[MESHES];
|
175 |
5dfbbda7
|
Leszek Koltunski
|
|
176 |
|
|
for(int i=0; i<MESHES; i++)
|
177 |
|
|
{
|
178 |
ba984444
|
Leszek Koltunski
|
textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
|
179 |
5dfbbda7
|
Leszek Koltunski
|
}
|
180 |
|
|
|
181 |
|
|
MeshBase[] tmp = new MeshBase[2];
|
182 |
|
|
|
183 |
|
|
tmp[0] = new MeshJoined(meshes);
|
184 |
887e1853
|
Leszek Koltunski
|
|
185 |
ba984444
|
Leszek Koltunski
|
for(int i=0; i<MESHES; i++)
|
186 |
|
|
{
|
187 |
|
|
moveEffect[i] = new MatrixEffectMove( new Static3D(0,0,0.5f-i/(MESHES-1.0f)) );
|
188 |
|
|
tmp[0].apply(moveEffect[i],0,i);
|
189 |
|
|
}
|
190 |
887e1853
|
Leszek Koltunski
|
|
191 |
5dfbbda7
|
Leszek Koltunski
|
tmp[1] = tmp[0].copy(true);
|
192 |
|
|
|
193 |
fcb09e1f
|
Leszek Koltunski
|
tmp[0].mergeEffComponents();
|
194 |
|
|
tmp[1].mergeEffComponents();
|
195 |
5dfbbda7
|
Leszek Koltunski
|
|
196 |
eb507734
|
Leszek Koltunski
|
tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
|
197 |
|
|
tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
|
198 |
|
|
|
199 |
5dfbbda7
|
Leszek Koltunski
|
MeshBase combined = new MeshJoined(tmp);
|
200 |
|
|
|
201 |
ba984444
|
Leszek Koltunski
|
MatrixEffectMove moveL = new MatrixEffectMove( new Static3D(-0.6f,0,0) );
|
202 |
|
|
MatrixEffectMove moveR = new MatrixEffectMove( new Static3D(+0.6f,0,0) );
|
203 |
|
|
|
204 |
|
|
combined.apply(moveL,0,0);
|
205 |
|
|
combined.apply(moveR,0,1);
|
206 |
5dfbbda7
|
Leszek Koltunski
|
|
207 |
ba984444
|
Leszek Koltunski
|
combined.setTextureMap(textureMaps,0);
|
208 |
|
|
combined.setTextureMap(textureMaps,MESHES);
|
209 |
5dfbbda7
|
Leszek Koltunski
|
|
210 |
b1950186
|
Leszek Koltunski
|
|
211 |
5dfbbda7
|
Leszek Koltunski
|
return combined;
|
212 |
887e1853
|
Leszek Koltunski
|
}
|
213 |
ba984444
|
Leszek Koltunski
|
|
214 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215 |
|
|
|
216 |
|
|
private Bitmap createTexture()
|
217 |
|
|
{
|
218 |
|
|
final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
|
219 |
|
|
final int FACES=FACE_COLORS.length;
|
220 |
|
|
int SIZE = 200;
|
221 |
|
|
float STROKE = 0.05f*SIZE;
|
222 |
|
|
float OFF = STROKE/2 -1;
|
223 |
|
|
float OFF2 = 0.5f*SIZE + OFF;
|
224 |
|
|
float HEIGHT = SIZE - OFF;
|
225 |
|
|
float RADIUS = SIZE/12;
|
226 |
|
|
float ARC1_H = 0.2f*SIZE;
|
227 |
|
|
float ARC1_W = SIZE*0.5f;
|
228 |
|
|
float ARC2_W = 0.153f*SIZE;
|
229 |
|
|
float ARC2_H = 0.905f*SIZE;
|
230 |
|
|
float ARC3_W = SIZE-ARC2_W;
|
231 |
|
|
|
232 |
|
|
Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
233 |
|
|
Canvas canvas = new Canvas(result);
|
234 |
|
|
Paint paint = new Paint();
|
235 |
|
|
paint.setAntiAlias(true);
|
236 |
|
|
paint.setStrokeWidth(STROKE);
|
237 |
|
|
|
238 |
|
|
for(int i=0; i<FACES; i++)
|
239 |
|
|
{
|
240 |
|
|
paint.setColor(FACE_COLORS[i]);
|
241 |
|
|
paint.setStyle(Paint.Style.FILL);
|
242 |
|
|
|
243 |
|
|
canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
|
244 |
|
|
|
245 |
|
|
paint.setColor(0xff000000);
|
246 |
|
|
paint.setStyle(Paint.Style.STROKE);
|
247 |
|
|
|
248 |
|
|
canvas.drawLine( i*SIZE, HEIGHT, SIZE +i*SIZE, HEIGHT, paint);
|
249 |
|
|
canvas.drawLine( OFF +i*SIZE, SIZE, OFF2 +i*SIZE, 0, paint);
|
250 |
|
|
canvas.drawLine((SIZE-OFF)+i*SIZE, SIZE, (SIZE-OFF2) +i*SIZE, 0, paint);
|
251 |
|
|
|
252 |
|
|
canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
|
253 |
|
|
canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
|
254 |
|
|
canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
return result;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
887e1853
|
Leszek Koltunski
|
}
|