1 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
76a81b6a
|
Leszek Koltunski
|
package org.distorted.examples.effects3d;
|
21 |
08f92d82
|
Leszek Koltunski
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.BitmapFactory;
|
24 |
|
|
import android.opengl.GLSurfaceView;
|
25 |
|
|
|
26 |
|
|
import org.distorted.examples.R;
|
27 |
06366d12
|
Leszek Koltunski
|
import org.distorted.library.effect.EffectName;
|
28 |
|
|
import org.distorted.library.effect.EffectType;
|
29 |
885b9cca
|
leszek
|
import org.distorted.library.effect.FragmentEffectAlpha;
|
30 |
|
|
import org.distorted.library.effect.FragmentEffectBrightness;
|
31 |
|
|
import org.distorted.library.effect.FragmentEffectChroma;
|
32 |
|
|
import org.distorted.library.effect.FragmentEffectContrast;
|
33 |
|
|
import org.distorted.library.effect.FragmentEffectSaturation;
|
34 |
06366d12
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
35 |
|
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
36 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
37 |
885b9cca
|
leszek
|
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 |
01782e85
|
Leszek Koltunski
|
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 |
08f92d82
|
Leszek Koltunski
|
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 |
76a81b6a
|
Leszek Koltunski
|
class Effects3DRenderer implements GLSurfaceView.Renderer
|
62 |
08f92d82
|
Leszek Koltunski
|
{
|
63 |
af662543
|
leszek
|
private static final float FOV = 70.0f;
|
64 |
|
|
private static final float NEAR = 0.1f;
|
65 |
|
|
|
66 |
08f92d82
|
Leszek Koltunski
|
private GLSurfaceView mView;
|
67 |
f6d884d5
|
Leszek Koltunski
|
private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
|
68 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
69 |
fe59d375
|
Leszek Koltunski
|
private DistortedNode mCenterNode, mRegionNode;
|
70 |
06366d12
|
Leszek Koltunski
|
private int mObjWidth, mObjHeight;
|
71 |
|
|
private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
|
72 |
fec27f16
|
Leszek Koltunski
|
private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
|
73 |
|
|
private boolean mShowingCenter=false;
|
74 |
|
|
private boolean mShowingRegion=false;
|
75 |
6f779cd4
|
Leszek Koltunski
|
private float mFactorObj, mFactorReg;
|
76 |
950511ed
|
Leszek Koltunski
|
|
77 |
833685d0
|
Leszek Koltunski
|
Static4D mQuat1, mQuat2;
|
78 |
d2337a3a
|
Leszek Koltunski
|
int mScreenMin;
|
79 |
833685d0
|
Leszek Koltunski
|
|
80 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
|
|
|
82 |
4f8ca8ff
|
Leszek Koltunski
|
Effects3DRenderer(GLSurfaceView v)
|
83 |
08f92d82
|
Leszek Koltunski
|
{
|
84 |
|
|
mView = v;
|
85 |
d40cfeb2
|
Leszek Koltunski
|
|
86 |
e8b6aa95
|
Leszek Koltunski
|
Effects3DActivity act = (Effects3DActivity)v.getContext();
|
87 |
|
|
|
88 |
06366d12
|
Leszek Koltunski
|
mRotateCen = new Static3D(0,0,0);
|
89 |
|
|
mMoveObject = new Static3D(0,0,0);
|
90 |
fec27f16
|
Leszek Koltunski
|
mScaleObject = new Static3D(1,1,1);
|
91 |
06366d12
|
Leszek Koltunski
|
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 |
f6d884d5
|
Leszek Koltunski
|
mObjectTexture = act.getTexture();
|
98 |
7451c98a
|
Leszek Koltunski
|
mBackgroundTexture = new DistortedTexture(100,100);
|
99 |
|
|
mCenterTexture = new DistortedTexture(100,100);
|
100 |
|
|
mRegionTexture = new DistortedTexture(100,100);
|
101 |
06366d12
|
Leszek Koltunski
|
|
102 |
|
|
DistortedEffects objectEffects = act.getEffects();
|
103 |
|
|
DistortedEffects backgroundEffects = new DistortedEffects();
|
104 |
|
|
DistortedEffects centerEffects = new DistortedEffects();
|
105 |
|
|
DistortedEffects regionEffects = new DistortedEffects();
|
106 |
f6d884d5
|
Leszek Koltunski
|
|
107 |
aac5c562
|
leszek
|
MeshObject meshO = act.getMesh();
|
108 |
|
|
MeshFlat quad = new MeshFlat(1,1);
|
109 |
|
|
|
110 |
f6d884d5
|
Leszek Koltunski
|
mObjWidth = mObjectTexture.getWidth();
|
111 |
|
|
mObjHeight= mObjectTexture.getHeight();
|
112 |
06366d12
|
Leszek Koltunski
|
int objDepth = mObjectTexture.getDepth(meshO);
|
113 |
d40cfeb2
|
Leszek Koltunski
|
|
114 |
833685d0
|
Leszek Koltunski
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
115 |
|
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
116 |
|
|
|
117 |
24991bc2
|
Leszek Koltunski
|
mCenterPoint= new Static3D(0,0,0);
|
118 |
|
|
mRegionPoint= new Static3D(0,0,0);
|
119 |
6f779cd4
|
Leszek Koltunski
|
mRegionScalePoint = new Static3D(0,0,0);
|
120 |
392e16fd
|
Leszek Koltunski
|
|
121 |
06366d12
|
Leszek Koltunski
|
mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
|
122 |
|
|
mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
|
123 |
fe59d375
|
Leszek Koltunski
|
|
124 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
125 |
af662543
|
leszek
|
mScreen.setProjection(FOV, NEAR);
|
126 |
06366d12
|
Leszek Koltunski
|
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 |
fec27f16
|
Leszek Koltunski
|
objectEffects.apply( new MatrixEffectScale(mScaleObject) );
|
141 |
06366d12
|
Leszek Koltunski
|
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 |
f338550a
|
leszek
|
}
|
162 |
|
|
|
163 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164 |
|
|
|
165 |
0ab55f0c
|
Leszek Koltunski
|
void showRegionAndCenter(boolean showRegion, boolean showCenter)
|
166 |
f338550a
|
leszek
|
{
|
167 |
0ab55f0c
|
Leszek Koltunski
|
if( mShowingCenter!=showCenter )
|
168 |
98c04ab8
|
leszek
|
{
|
169 |
0ab55f0c
|
Leszek Koltunski
|
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 |
98c04ab8
|
leszek
|
}
|
182 |
950511ed
|
Leszek Koltunski
|
}
|
183 |
|
|
|
184 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185 |
|
|
|
186 |
334c13fa
|
Leszek Koltunski
|
void setCenter(float x, float y, float z)
|
187 |
950511ed
|
Leszek Koltunski
|
{
|
188 |
334c13fa
|
Leszek Koltunski
|
mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
|
189 |
08f92d82
|
Leszek Koltunski
|
}
|
190 |
|
|
|
191 |
6f779cd4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192 |
|
|
|
193 |
4f8ca8ff
|
Leszek Koltunski
|
void setRegion(float x, float y, float r)
|
194 |
6f779cd4
|
Leszek Koltunski
|
{
|
195 |
f6d884d5
|
Leszek Koltunski
|
mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
|
196 |
fec27f16
|
Leszek Koltunski
|
mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
|
197 |
9ae05f6c
|
Leszek Koltunski
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
198 |
6f779cd4
|
Leszek Koltunski
|
}
|
199 |
|
|
|
200 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201 |
56cbe1cf
|
Leszek Koltunski
|
|
202 |
|
|
public void onDrawFrame(GL10 glUnused)
|
203 |
08f92d82
|
Leszek Koltunski
|
{
|
204 |
fe59d375
|
Leszek Koltunski
|
mScreen.render(System.currentTimeMillis());
|
205 |
08f92d82
|
Leszek Koltunski
|
}
|
206 |
|
|
|
207 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208 |
56cbe1cf
|
Leszek Koltunski
|
|
209 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
210 |
08f92d82
|
Leszek Koltunski
|
{
|
211 |
833685d0
|
Leszek Koltunski
|
mScreenMin = width<height ? width:height;
|
212 |
752c6b57
|
Leszek Koltunski
|
|
213 |
950511ed
|
Leszek Koltunski
|
float factorCen;
|
214 |
f6d884d5
|
Leszek Koltunski
|
int centerSize = mCenterTexture.getWidth();
|
215 |
|
|
int regionSize = mRegionTexture.getWidth();
|
216 |
6f779cd4
|
Leszek Koltunski
|
|
217 |
261fe5bd
|
Leszek Koltunski
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
218 |
d40cfeb2
|
Leszek Koltunski
|
{
|
219 |
950511ed
|
Leszek Koltunski
|
mFactorObj = (0.80f*height)/mObjHeight;
|
220 |
|
|
factorCen = (0.08f*height)/centerSize;
|
221 |
261fe5bd
|
Leszek Koltunski
|
}
|
222 |
d40cfeb2
|
Leszek Koltunski
|
else
|
223 |
|
|
{
|
224 |
950511ed
|
Leszek Koltunski
|
mFactorObj = (0.80f*width)/mObjWidth;
|
225 |
|
|
factorCen = (0.08f*width)/centerSize;
|
226 |
d40cfeb2
|
Leszek Koltunski
|
}
|
227 |
|
|
|
228 |
76a81b6a
|
Leszek Koltunski
|
Effects3DActivity act = (Effects3DActivity)mView.getContext();
|
229 |
fec27f16
|
Leszek Koltunski
|
mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
|
230 |
|
|
mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
|
231 |
17600407
|
Leszek Koltunski
|
mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
|
232 |
9ae05f6c
|
Leszek Koltunski
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
233 |
06366d12
|
Leszek Koltunski
|
mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
|
234 |
|
|
mRotateCen.set(width/2,height/2, 0);
|
235 |
fec27f16
|
Leszek Koltunski
|
mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
|
236 |
06366d12
|
Leszek Koltunski
|
mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
|
237 |
|
|
(height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
|
238 |
|
|
mScaleCenter.set(factorCen,factorCen,factorCen);
|
239 |
|
|
mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
|
240 |
fce25d04
|
leszek
|
|
241 |
|
|
int backgroundSize = mBackgroundTexture.getWidth();
|
242 |
|
|
float factorBackX = ((float)width)/backgroundSize;
|
243 |
|
|
float factorBackY = ((float)height)/backgroundSize;
|
244 |
|
|
|
245 |
|
|
// quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
|
246 |
06366d12
|
Leszek Koltunski
|
mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
|
247 |
|
|
mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
|
248 |
fce25d04
|
leszek
|
|
249 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
250 |
08f92d82
|
Leszek Koltunski
|
}
|
251 |
|
|
|
252 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253 |
56cbe1cf
|
Leszek Koltunski
|
|
254 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
255 |
08f92d82
|
Leszek Koltunski
|
{
|
256 |
76a81b6a
|
Leszek Koltunski
|
Effects3DActivity act = (Effects3DActivity)mView.getContext();
|
257 |
bcc8e016
|
Leszek Koltunski
|
|
258 |
f6d884d5
|
Leszek Koltunski
|
InputStream isB = act.getResources().openRawResource(R.raw.water);
|
259 |
|
|
InputStream isC = act.getResources().openRawResource(R.raw.center);
|
260 |
|
|
InputStream isR = act.getResources().openRawResource(R.raw.region);
|
261 |
71d8ad03
|
Leszek Koltunski
|
|
262 |
f6d884d5
|
Leszek Koltunski
|
Bitmap bitmapB,bitmapC,bitmapR;
|
263 |
08f92d82
|
Leszek Koltunski
|
|
264 |
|
|
try
|
265 |
|
|
{
|
266 |
f6d884d5
|
Leszek Koltunski
|
bitmapB = BitmapFactory.decodeStream(isB);
|
267 |
|
|
bitmapC = BitmapFactory.decodeStream(isC);
|
268 |
|
|
bitmapR = BitmapFactory.decodeStream(isR);
|
269 |
71d8ad03
|
Leszek Koltunski
|
}
|
270 |
08f92d82
|
Leszek Koltunski
|
finally
|
271 |
|
|
{
|
272 |
|
|
try
|
273 |
|
|
{
|
274 |
f6d884d5
|
Leszek Koltunski
|
isB.close();
|
275 |
|
|
isC.close();
|
276 |
|
|
isR.close();
|
277 |
9167cfd4
|
Leszek Koltunski
|
}
|
278 |
08f92d82
|
Leszek Koltunski
|
catch(IOException e) { }
|
279 |
|
|
}
|
280 |
|
|
|
281 |
f6d884d5
|
Leszek Koltunski
|
mObjectTexture.setTexture( act.getBitmap() );
|
282 |
|
|
mBackgroundTexture.setTexture(bitmapB);
|
283 |
|
|
mCenterTexture.setTexture(bitmapC);
|
284 |
|
|
mRegionTexture.setTexture(bitmapR);
|
285 |
950511ed
|
Leszek Koltunski
|
|
286 |
885b9cca
|
leszek
|
VertexEffectDeform.enable();
|
287 |
|
|
VertexEffectDistort.enable();
|
288 |
|
|
VertexEffectPinch.enable();
|
289 |
|
|
VertexEffectSink.enable();
|
290 |
|
|
VertexEffectSwirl.enable();
|
291 |
|
|
VertexEffectWave.enable();
|
292 |
|
|
|
293 |
|
|
FragmentEffectAlpha.enable();
|
294 |
|
|
FragmentEffectBrightness.enable();
|
295 |
|
|
FragmentEffectChroma.enable();
|
296 |
|
|
FragmentEffectContrast.enable();
|
297 |
|
|
FragmentEffectSaturation.enable();
|
298 |
6637d0f2
|
Leszek Koltunski
|
|
299 |
08f92d82
|
Leszek Koltunski
|
try
|
300 |
|
|
{
|
301 |
76f9798b
|
Leszek Koltunski
|
Distorted.onCreate(mView.getContext());
|
302 |
08f92d82
|
Leszek Koltunski
|
}
|
303 |
|
|
catch(Exception ex)
|
304 |
|
|
{
|
305 |
76a81b6a
|
Leszek Koltunski
|
android.util.Log.e("Effects3D", ex.getMessage() );
|
306 |
08f92d82
|
Leszek Koltunski
|
}
|
307 |
|
|
}
|
308 |
56cbe1cf
|
Leszek Koltunski
|
}
|