1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
29
|
import org.distorted.library.effect.MatrixEffectScale;
|
30
|
import org.distorted.library.effect.VertexEffectDeform;
|
31
|
import org.distorted.library.effect.VertexEffectMove;
|
32
|
import org.distorted.library.effect.VertexEffectRotate;
|
33
|
import org.distorted.library.effect.VertexEffectScale;
|
34
|
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
|
import org.distorted.library.mesh.MeshJoined;
|
40
|
import org.distorted.library.mesh.MeshRectangles;
|
41
|
import org.distorted.library.mesh.MeshTriangles;
|
42
|
import org.distorted.library.type.Dynamic1D;
|
43
|
import org.distorted.library.type.DynamicQuat;
|
44
|
import org.distorted.library.type.Static1D;
|
45
|
import org.distorted.library.type.Static3D;
|
46
|
import org.distorted.library.type.Static4D;
|
47
|
|
48
|
import javax.microedition.khronos.egl.EGLConfig;
|
49
|
import javax.microedition.khronos.opengles.GL10;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
class DeferredJobRenderer implements GLSurfaceView.Renderer
|
54
|
{
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedTexture mTexture;
|
57
|
private DistortedScreen mScreen;
|
58
|
private DistortedEffects mEffects;
|
59
|
private Static3D mScale;
|
60
|
private MeshBase mMesh;
|
61
|
private VertexEffectRotate mRotate;
|
62
|
private Dynamic1D mAngleDyn;
|
63
|
private Static1D mAngle;
|
64
|
|
65
|
Static4D mQuat1, mQuat2;
|
66
|
int mScreenMin;
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
DeferredJobRenderer(GLSurfaceView v)
|
71
|
{
|
72
|
mView = v;
|
73
|
mScreen = new DistortedScreen();
|
74
|
mScale= new Static3D(1,1,1);
|
75
|
Static3D center=new Static3D(0,0,0);
|
76
|
|
77
|
Dynamic1D sink = new Dynamic1D(5000,0.0f);
|
78
|
sink.add( new Static1D(0.5f) );
|
79
|
sink.add( new Static1D(2.0f) );
|
80
|
|
81
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
82
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
83
|
|
84
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
85
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
86
|
|
87
|
quatInt1.add(mQuat1);
|
88
|
quatInt2.add(mQuat2);
|
89
|
|
90
|
mAngle = new Static1D(0);
|
91
|
|
92
|
mAngleDyn = new Dynamic1D(2000,0.5f);
|
93
|
mAngleDyn.add(new Static1D(0));
|
94
|
mAngleDyn.add( mAngle );
|
95
|
|
96
|
mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
|
97
|
|
98
|
mEffects = new DistortedEffects();
|
99
|
mEffects.apply( mRotate );
|
100
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
101
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
102
|
mEffects.apply( new MatrixEffectScale(mScale));
|
103
|
|
104
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
public void onDrawFrame(GL10 glUnused)
|
110
|
{
|
111
|
mScreen.render( System.currentTimeMillis() );
|
112
|
}
|
113
|
|
114
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115
|
|
116
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
117
|
{
|
118
|
final float SCALE = 0.5f;
|
119
|
mScreenMin = Math.min(width, height);
|
120
|
float factor = SCALE*mScreenMin;
|
121
|
mScale.set(factor,factor,factor);
|
122
|
mScreen.resize(width, height);
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
128
|
{
|
129
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
130
|
mTexture.setTexture( createTexture() );
|
131
|
|
132
|
if( mMesh==null ) mMesh = createMesh();
|
133
|
|
134
|
mScreen.detachAll();
|
135
|
mScreen.attach(mTexture,mEffects,mMesh);
|
136
|
|
137
|
DistortedLibrary.setMax(EffectType.VERTEX, 11);
|
138
|
VertexEffectRotate.enable();
|
139
|
|
140
|
try
|
141
|
{
|
142
|
DistortedLibrary.onCreate(mView.getContext());
|
143
|
}
|
144
|
catch(Exception ex)
|
145
|
{
|
146
|
android.util.Log.e("DeferredJob", ex.getMessage() );
|
147
|
}
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
void apply(int number)
|
153
|
{
|
154
|
mAngle.set(360);
|
155
|
mAngleDyn.resetToBeginning();
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
private Bitmap createTexture()
|
161
|
{
|
162
|
final int[] FACE_COLORS = new int[] { 0xffff0000, 0xff00ff00 };
|
163
|
final int FACES=FACE_COLORS.length;
|
164
|
int SIZE = 200;
|
165
|
|
166
|
Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
167
|
Canvas canvas = new Canvas(result);
|
168
|
Paint paint = new Paint();
|
169
|
paint.setStyle(Paint.Style.FILL);
|
170
|
|
171
|
for(int i=0; i<FACES; i++)
|
172
|
{
|
173
|
paint.setColor(FACE_COLORS[i]);
|
174
|
canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
|
175
|
}
|
176
|
|
177
|
return result;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
private MeshBase createMesh()
|
183
|
{
|
184
|
final int MESHES=2;
|
185
|
|
186
|
MeshBase[] meshes = new MeshRectangles[MESHES];
|
187
|
|
188
|
meshes[0] = new MeshRectangles(2,2);
|
189
|
|
190
|
for(int i=1; i<MESHES; i++)
|
191
|
{
|
192
|
meshes[i] = meshes[0].copy(true);
|
193
|
}
|
194
|
|
195
|
Static4D[] lTextureMaps = new Static4D[MESHES];
|
196
|
Static4D[] rTextureMaps = new Static4D[MESHES];
|
197
|
|
198
|
for(int i=0; i<MESHES; i++)
|
199
|
{
|
200
|
lTextureMaps[i] = new Static4D(0.0f,0.0f,0.5f,1.0f);
|
201
|
rTextureMaps[i] = new Static4D(0.5f,0.0f,0.5f,1.0f);
|
202
|
}
|
203
|
|
204
|
MeshBase[] tmp = new MeshBase[2];
|
205
|
|
206
|
tmp[0] = new MeshJoined(meshes);
|
207
|
tmp[0].setTextureMap(lTextureMaps);
|
208
|
|
209
|
VertexEffectMove effect0 = new VertexEffectMove ( new Static3D(0,0,0.5f) );
|
210
|
VertexEffectRotate effect1 = new VertexEffectRotate( new Static1D(180), new Static3D(1,0,0), new Static3D(0,0,0) );
|
211
|
|
212
|
effect1.setMeshAssociation(1,0); // apply only to mesh[1]
|
213
|
|
214
|
tmp[0].apply(effect0);
|
215
|
tmp[0].apply(effect1);
|
216
|
|
217
|
tmp[1] = tmp[0].copy(true);
|
218
|
tmp[1].setTextureMap(rTextureMaps);
|
219
|
|
220
|
tmp[0].mergeComponents();
|
221
|
tmp[1].mergeComponents();
|
222
|
|
223
|
MeshBase combined = new MeshJoined(tmp);
|
224
|
|
225
|
VertexEffectMove moveL = new VertexEffectMove ( new Static3D(-1,0,0) );
|
226
|
VertexEffectMove moveR = new VertexEffectMove ( new Static3D(+1,0,0) );
|
227
|
|
228
|
moveL.setMeshAssociation(0,0);
|
229
|
moveR.setMeshAssociation(1,0);
|
230
|
|
231
|
return combined;
|
232
|
}
|
233
|
}
|