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.effects3d;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLSurfaceView;
|
25
|
|
26
|
import org.distorted.examples.R;
|
27
|
import org.distorted.library.main.Distorted;
|
28
|
import org.distorted.library.main.DistortedEffects;
|
29
|
import org.distorted.library.main.DistortedNode;
|
30
|
import org.distorted.library.main.DistortedScreen;
|
31
|
import org.distorted.library.main.DistortedTexture;
|
32
|
import org.distorted.library.EffectNames;
|
33
|
import org.distorted.library.main.MeshFlat;
|
34
|
import org.distorted.library.main.MeshObject;
|
35
|
import org.distorted.library.EffectTypes;
|
36
|
import org.distorted.library.type.Dynamic3D;
|
37
|
import org.distorted.library.type.DynamicQuat;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
import org.distorted.library.type.Static4D;
|
40
|
|
41
|
import java.io.IOException;
|
42
|
import java.io.InputStream;
|
43
|
|
44
|
import javax.microedition.khronos.egl.EGLConfig;
|
45
|
import javax.microedition.khronos.opengles.GL10;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
class Effects3DRenderer implements GLSurfaceView.Renderer
|
50
|
{
|
51
|
private static final float FOV = 70.0f;
|
52
|
private static final float NEAR = 0.1f;
|
53
|
|
54
|
private GLSurfaceView mView;
|
55
|
private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
|
56
|
private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
|
57
|
private DistortedScreen mScreen;
|
58
|
private DistortedNode mCenterNode, mRegionNode;
|
59
|
private int mObjWidth, mObjHeight, mObjDepth;
|
60
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
61
|
|
62
|
private Dynamic3D mCenterInter, mRegionInter;
|
63
|
private Static3D mCenterPoint, mRegionPoint;
|
64
|
private Dynamic3D mRegionScaleInter;
|
65
|
private Static3D mRegionScalePoint;
|
66
|
|
67
|
private float mFactorObj, mFactorReg;
|
68
|
|
69
|
Static4D mQuat1, mQuat2;
|
70
|
int mScreenMin;
|
71
|
private boolean mShowingCenter=false;
|
72
|
private boolean mShowingRegion=false;
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
Effects3DRenderer(GLSurfaceView v)
|
77
|
{
|
78
|
mView = v;
|
79
|
|
80
|
Effects3DActivity act = (Effects3DActivity)v.getContext();
|
81
|
|
82
|
mObjectTexture = act.getTexture();
|
83
|
mObjectEffects = act.getEffects();
|
84
|
mBackgroundTexture = new DistortedTexture(100,100);
|
85
|
mCenterTexture = new DistortedTexture(100,100);
|
86
|
mRegionTexture = new DistortedTexture(100,100);
|
87
|
mBackgroundEffects = new DistortedEffects();
|
88
|
mCenterEffects = new DistortedEffects();
|
89
|
mRegionEffects = new DistortedEffects();
|
90
|
|
91
|
MeshObject meshO = act.getMesh();
|
92
|
MeshFlat quad = new MeshFlat(1,1);
|
93
|
|
94
|
mObjWidth = mObjectTexture.getWidth();
|
95
|
mObjHeight= mObjectTexture.getHeight();
|
96
|
mObjDepth = mObjectTexture.getDepth(meshO);
|
97
|
|
98
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
99
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
100
|
|
101
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
102
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
103
|
|
104
|
mQuatInt1.add(mQuat1);
|
105
|
mQuatInt2.add(mQuat2);
|
106
|
|
107
|
mCenterInter= new Dynamic3D();
|
108
|
mCenterPoint= new Static3D(0,0,0);
|
109
|
mCenterInter.add(mCenterPoint);
|
110
|
|
111
|
mRegionInter= new Dynamic3D();
|
112
|
mRegionPoint= new Static3D(0,0,0);
|
113
|
mRegionInter.add(mRegionPoint);
|
114
|
|
115
|
mRegionScaleInter = new Dynamic3D();
|
116
|
mRegionScalePoint = new Static3D(0,0,0);
|
117
|
mRegionScaleInter.add(mRegionScalePoint);
|
118
|
|
119
|
mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
|
120
|
mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
|
121
|
|
122
|
mScreen = new DistortedScreen(mView);
|
123
|
mScreen.setProjection(FOV, NEAR);
|
124
|
mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
|
125
|
mScreen.attach(mObjectTexture , mObjectEffects , meshO);
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
void showRegionAndCenter(boolean showRegion, boolean showCenter)
|
131
|
{
|
132
|
if( mShowingCenter!=showCenter )
|
133
|
{
|
134
|
if( showCenter ) mScreen.attach(mCenterNode);
|
135
|
else mScreen.detach(mCenterNode);
|
136
|
|
137
|
mShowingCenter = showCenter;
|
138
|
}
|
139
|
|
140
|
if( mShowingRegion!=showRegion )
|
141
|
{
|
142
|
if( showRegion ) mScreen.attach(mRegionNode);
|
143
|
else mScreen.detach(mRegionNode);
|
144
|
|
145
|
mShowingRegion = showRegion;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
void setCenter(float x, float y, float z)
|
152
|
{
|
153
|
mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
void setRegion(float x, float y, float r)
|
159
|
{
|
160
|
mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
|
161
|
mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
|
162
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public void onDrawFrame(GL10 glUnused)
|
168
|
{
|
169
|
mScreen.render(System.currentTimeMillis());
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
175
|
{
|
176
|
mScreenMin = width<height ? width:height;
|
177
|
|
178
|
mObjectEffects.abortEffects(EffectTypes.MATRIX);
|
179
|
mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
|
180
|
mCenterEffects.abortEffects(EffectTypes.MATRIX);
|
181
|
mRegionEffects.abortEffects(EffectTypes.MATRIX);
|
182
|
|
183
|
float factorCen;
|
184
|
int centerSize = mCenterTexture.getWidth();
|
185
|
int regionSize = mRegionTexture.getWidth();
|
186
|
|
187
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
188
|
{
|
189
|
mFactorObj = (0.80f*height)/mObjHeight;
|
190
|
factorCen = (0.08f*height)/centerSize;
|
191
|
}
|
192
|
else
|
193
|
{
|
194
|
mFactorObj = (0.80f*width)/mObjWidth;
|
195
|
factorCen = (0.08f*width)/centerSize;
|
196
|
}
|
197
|
|
198
|
Effects3DActivity act = (Effects3DActivity)mView.getContext();
|
199
|
mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
|
200
|
mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
|
201
|
|
202
|
mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
|
203
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
204
|
|
205
|
Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
|
206
|
|
207
|
mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
|
208
|
mObjectEffects.scale(mFactorObj);
|
209
|
mObjectEffects.quaternion(mQuatInt1, rotateObj);
|
210
|
mObjectEffects.quaternion(mQuatInt2, rotateObj);
|
211
|
|
212
|
Static3D rotateCen = new Static3D(width/2,height/2, 0);
|
213
|
|
214
|
mCenterEffects.quaternion(mQuatInt1, rotateCen);
|
215
|
mCenterEffects.quaternion(mQuatInt2, rotateCen);
|
216
|
|
217
|
mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
|
218
|
(height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15) );
|
219
|
mCenterEffects.move(mCenterInter);
|
220
|
mCenterEffects.scale(factorCen);
|
221
|
|
222
|
mRegionEffects.quaternion(mQuatInt1, rotateCen);
|
223
|
mRegionEffects.quaternion(mQuatInt2, rotateCen);
|
224
|
|
225
|
mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
|
226
|
(height-mFactorObj*mObjHeight)/2 , 12) );
|
227
|
mRegionEffects.move(mCenterInter);
|
228
|
mRegionEffects.move(mRegionInter);
|
229
|
mRegionEffects.scale(mRegionScaleInter);
|
230
|
mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
|
231
|
|
232
|
int backgroundSize = mBackgroundTexture.getWidth();
|
233
|
float factorBackX = ((float)width)/backgroundSize;
|
234
|
float factorBackY = ((float)height)/backgroundSize;
|
235
|
|
236
|
// quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
|
237
|
mBackgroundEffects.move(new Static3D( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360))) );
|
238
|
mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
|
239
|
|
240
|
mScreen.resize(width, height);
|
241
|
}
|
242
|
|
243
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244
|
|
245
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
246
|
{
|
247
|
Effects3DActivity act = (Effects3DActivity)mView.getContext();
|
248
|
|
249
|
InputStream isB = act.getResources().openRawResource(R.raw.water);
|
250
|
InputStream isC = act.getResources().openRawResource(R.raw.center);
|
251
|
InputStream isR = act.getResources().openRawResource(R.raw.region);
|
252
|
|
253
|
Bitmap bitmapB,bitmapC,bitmapR;
|
254
|
|
255
|
try
|
256
|
{
|
257
|
bitmapB = BitmapFactory.decodeStream(isB);
|
258
|
bitmapC = BitmapFactory.decodeStream(isC);
|
259
|
bitmapR = BitmapFactory.decodeStream(isR);
|
260
|
}
|
261
|
finally
|
262
|
{
|
263
|
try
|
264
|
{
|
265
|
isB.close();
|
266
|
isC.close();
|
267
|
isR.close();
|
268
|
}
|
269
|
catch(IOException e) { }
|
270
|
}
|
271
|
|
272
|
mObjectTexture.setTexture( act.getBitmap() );
|
273
|
mBackgroundTexture.setTexture(bitmapB);
|
274
|
mCenterTexture.setTexture(bitmapC);
|
275
|
mRegionTexture.setTexture(bitmapR);
|
276
|
|
277
|
EffectTypes type;
|
278
|
|
279
|
for( EffectNames name : EffectNames.values() )
|
280
|
{
|
281
|
type = name.getType();
|
282
|
|
283
|
if( type== EffectTypes.VERTEX || type==EffectTypes.FRAGMENT )
|
284
|
{
|
285
|
DistortedEffects.enableEffect(name);
|
286
|
}
|
287
|
}
|
288
|
|
289
|
try
|
290
|
{
|
291
|
Distorted.onCreate(mView.getContext());
|
292
|
}
|
293
|
catch(Exception ex)
|
294
|
{
|
295
|
android.util.Log.e("Effects3D", ex.getMessage() );
|
296
|
}
|
297
|
}
|
298
|
}
|