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.generic;
|
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.effect.Effect;
|
28
|
import org.distorted.library.effect.EffectType;
|
29
|
import org.distorted.library.effect.MatrixEffectMove;
|
30
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
31
|
import org.distorted.library.effect.MatrixEffectScale;
|
32
|
import org.distorted.library.main.DistortedLibrary;
|
33
|
import org.distorted.library.main.DistortedEffects;
|
34
|
import org.distorted.library.main.DistortedNode;
|
35
|
import org.distorted.library.main.DistortedScreen;
|
36
|
import org.distorted.library.main.DistortedTexture;
|
37
|
import org.distorted.library.mesh.MeshBase;
|
38
|
import org.distorted.library.mesh.MeshQuad;
|
39
|
import org.distorted.library.mesh.MeshSphere;
|
40
|
import org.distorted.library.type.Static3D;
|
41
|
import org.distorted.library.type.Static4D;
|
42
|
|
43
|
import java.io.IOException;
|
44
|
import java.io.InputStream;
|
45
|
|
46
|
import javax.microedition.khronos.egl.EGLConfig;
|
47
|
import javax.microedition.khronos.opengles.GL10;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class GenericRenderer implements GLSurfaceView.Renderer
|
52
|
{
|
53
|
private static final float FOV = 30.0f;
|
54
|
private static final float NEAR = 0.1f;
|
55
|
|
56
|
private GLSurfaceView mView;
|
57
|
private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
|
58
|
private DistortedEffects mObjectEffects;
|
59
|
private DistortedScreen mScreen;
|
60
|
private MeshQuad mRegionQuad, mCenterQuad, mBackgroundQuad;
|
61
|
private DistortedNode mCenterNode, mRegionNode;
|
62
|
private float mObjWidth, mObjHeight, mObjDepth;
|
63
|
private Static3D mCenterPoint, mRegionPoint, mScaleRegion;
|
64
|
private Static3D mMoveObject, mScaleObject, mScaleCenter, mMoveBackground, mScaleBackground;
|
65
|
private boolean mShowingCenter=false;
|
66
|
private boolean mShowingRegion=false;
|
67
|
private float mFactorObj;
|
68
|
private int mWidth;
|
69
|
private float mFactor;
|
70
|
|
71
|
Static4D mQuat1, mQuat2;
|
72
|
int mScreenMin;
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
GenericRenderer(GLSurfaceView v)
|
77
|
{
|
78
|
mView = v;
|
79
|
|
80
|
GenericActivity2 act = (GenericActivity2)v.getContext();
|
81
|
|
82
|
mMoveObject = new Static3D(0,0,0);
|
83
|
mScaleObject = new Static3D(1,1,1);
|
84
|
mScaleCenter = new Static3D(1,1,1);
|
85
|
mMoveBackground = new Static3D(0,0,0);
|
86
|
mScaleBackground= new Static3D(1,1,1);
|
87
|
|
88
|
mObjectTexture = act.getTexture();
|
89
|
mBackgroundTexture = new DistortedTexture();
|
90
|
mCenterTexture = new DistortedTexture();
|
91
|
mRegionTexture = new DistortedTexture();
|
92
|
|
93
|
mFactorObj = 1.0f;
|
94
|
|
95
|
mObjectEffects = act.getEffects();
|
96
|
DistortedEffects backgroundEffects = new DistortedEffects();
|
97
|
DistortedEffects centerEffects = new DistortedEffects();
|
98
|
DistortedEffects regionEffects = new DistortedEffects();
|
99
|
|
100
|
MeshBase mesh = act.getMesh();
|
101
|
mRegionQuad = new MeshQuad();
|
102
|
mCenterQuad = new MeshQuad();
|
103
|
mBackgroundQuad = new MeshQuad();
|
104
|
|
105
|
mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
|
106
|
|
107
|
mObjWidth = act.getWidth();
|
108
|
mObjHeight= act.getHeight();
|
109
|
mObjDepth = act.getDepth();
|
110
|
|
111
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
112
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
113
|
|
114
|
mCenterPoint= new Static3D(0,0,0);
|
115
|
mRegionPoint= new Static3D(0,0,0);
|
116
|
mScaleRegion = new Static3D(0,0,0);
|
117
|
|
118
|
mCenterNode = new DistortedNode(mCenterTexture, centerEffects, mCenterQuad);
|
119
|
mRegionNode = new DistortedNode(mRegionTexture, regionEffects, mRegionQuad);
|
120
|
|
121
|
mScreen = new DistortedScreen();
|
122
|
mScreen.setProjection(FOV, NEAR);
|
123
|
mScreen.attach(mBackgroundTexture, backgroundEffects, mBackgroundQuad );
|
124
|
mScreen.attach(mObjectTexture , mObjectEffects , mesh );
|
125
|
|
126
|
Static3D rotateCen = new Static3D(0,0,0);
|
127
|
|
128
|
MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, rotateCen);
|
129
|
MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, rotateCen);
|
130
|
MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
|
131
|
|
132
|
centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
|
133
|
centerEffects.apply( centerMove );
|
134
|
centerEffects.apply( new MatrixEffectMove(new Static3D(0,0,12)) );
|
135
|
centerEffects.apply(quat2cen);
|
136
|
centerEffects.apply(quat1cen);
|
137
|
|
138
|
regionEffects.apply( new MatrixEffectScale(mScaleRegion) );
|
139
|
regionEffects.apply( centerMove );
|
140
|
regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
|
141
|
regionEffects.apply( new MatrixEffectMove(new Static3D(0,0,10)) );
|
142
|
regionEffects.apply(quat2cen);
|
143
|
regionEffects.apply(quat1cen);
|
144
|
|
145
|
resetMatrixEffects();
|
146
|
|
147
|
// quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
|
148
|
backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
|
149
|
backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
void resetMatrixEffects()
|
155
|
{
|
156
|
Static3D rotateObj = new Static3D(0,0,0);
|
157
|
|
158
|
MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1, rotateObj);
|
159
|
MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2, rotateObj);
|
160
|
|
161
|
mObjectEffects.apply(quat2obj);
|
162
|
mObjectEffects.apply(quat1obj);
|
163
|
mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
|
164
|
mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
|
165
|
|
166
|
mQuat1.set(0,0,0,1);
|
167
|
mQuat2.set(0,0,0,1);
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
|
172
|
void showRegionAndCenter(boolean showRegion, boolean showCenter)
|
173
|
{
|
174
|
if( mShowingCenter!=showCenter )
|
175
|
{
|
176
|
if( showCenter )
|
177
|
{
|
178
|
if( mShowingRegion ) mScreen.detach(mRegionNode);
|
179
|
mScreen.attach(mCenterNode);
|
180
|
if( mShowingRegion ) mScreen.attach(mRegionNode);
|
181
|
}
|
182
|
else
|
183
|
{
|
184
|
mScreen.detach(mCenterNode);
|
185
|
}
|
186
|
|
187
|
mShowingCenter = showCenter;
|
188
|
}
|
189
|
|
190
|
if( mShowingRegion!=showRegion )
|
191
|
{
|
192
|
if( showRegion ) mScreen.attach(mRegionNode);
|
193
|
else mScreen.detach(mRegionNode);
|
194
|
|
195
|
mShowingRegion = showRegion;
|
196
|
}
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
|
201
|
public int getWidth()
|
202
|
{
|
203
|
return mWidth;
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
void setCenter(float x, float y, float z)
|
209
|
{
|
210
|
mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
void setRegion(float x, float y, float z, float r)
|
216
|
{
|
217
|
float factorReg = 2*mFactorObj*r/mRegionQuad.getStretchX();
|
218
|
mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
|
219
|
mScaleRegion.set(factorReg,factorReg,factorReg);
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
void useOIT(boolean use)
|
225
|
{
|
226
|
mScreen.setOrderIndependentTransparency(use);
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
|
231
|
public void onDrawFrame(GL10 glUnused)
|
232
|
{
|
233
|
mScreen.render(System.currentTimeMillis());
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
239
|
{
|
240
|
mWidth = width;
|
241
|
mScreenMin = width<height ? width:height;
|
242
|
|
243
|
float factorCen;
|
244
|
float centerSize = mCenterQuad.getStretchX();
|
245
|
|
246
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
247
|
{
|
248
|
mFactorObj = (mFactor*height)/mObjHeight;
|
249
|
factorCen = (0.08f *height)/centerSize;
|
250
|
}
|
251
|
else
|
252
|
{
|
253
|
mFactorObj = (mFactor*width)/mObjWidth;
|
254
|
factorCen = (0.08f *width)/centerSize;
|
255
|
}
|
256
|
|
257
|
setCenter(0,0,0);
|
258
|
setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
|
259
|
|
260
|
mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
|
261
|
mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
|
262
|
mScaleCenter.set(factorCen,factorCen,factorCen);
|
263
|
|
264
|
float backgroundSize = mBackgroundQuad.getStretchX();
|
265
|
float factorBackX = ((float)width)/backgroundSize;
|
266
|
float factorBackY = ((float)height)/backgroundSize;
|
267
|
|
268
|
// quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
|
269
|
mMoveBackground.set( 0,0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
|
270
|
mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
|
271
|
|
272
|
mScreen.resize(width, height);
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276
|
|
277
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
278
|
{
|
279
|
GenericActivity2 act = (GenericActivity2)mView.getContext();
|
280
|
|
281
|
InputStream isB = act.getResources().openRawResource(R.raw.water);
|
282
|
InputStream isC = act.getResources().openRawResource(R.raw.center);
|
283
|
InputStream isR = act.getResources().openRawResource(R.raw.region);
|
284
|
|
285
|
Bitmap bitmapB,bitmapC,bitmapR;
|
286
|
|
287
|
try
|
288
|
{
|
289
|
bitmapB = BitmapFactory.decodeStream(isB);
|
290
|
bitmapC = BitmapFactory.decodeStream(isC);
|
291
|
bitmapR = BitmapFactory.decodeStream(isR);
|
292
|
}
|
293
|
finally
|
294
|
{
|
295
|
try
|
296
|
{
|
297
|
isB.close();
|
298
|
isC.close();
|
299
|
isR.close();
|
300
|
}
|
301
|
catch(IOException e) { }
|
302
|
}
|
303
|
|
304
|
mObjectTexture.setTexture( act.getBitmap() );
|
305
|
mBackgroundTexture.setTexture(bitmapB);
|
306
|
mCenterTexture.setTexture(bitmapC);
|
307
|
mRegionTexture.setTexture(bitmapR);
|
308
|
|
309
|
DistortedLibrary.setMax(EffectType.VERTEX ,10);
|
310
|
DistortedLibrary.setMax(EffectType.FRAGMENT,10);
|
311
|
|
312
|
Effect.enableEffects(EffectType.VERTEX);
|
313
|
Effect.enableEffects(EffectType.FRAGMENT);
|
314
|
Effect.enableEffects(EffectType.POSTPROCESS);
|
315
|
|
316
|
try
|
317
|
{
|
318
|
DistortedLibrary.onCreate(mView.getContext());
|
319
|
}
|
320
|
catch(Exception ex)
|
321
|
{
|
322
|
android.util.Log.e("Effects3D", ex.getMessage() );
|
323
|
}
|
324
|
}
|
325
|
}
|