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