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