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 |
|
|
package org.distorted.examples.vertex3d;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.BitmapFactory;
|
24 |
|
|
import android.opengl.GLES20;
|
25 |
|
|
import android.opengl.GLSurfaceView;
|
26 |
|
|
|
27 |
|
|
import org.distorted.examples.R;
|
28 |
|
|
import org.distorted.library.Distorted;
|
29 |
794e6c4f
|
Leszek Koltunski
|
import org.distorted.library.DistortedBitmap;
|
30 |
08f92d82
|
Leszek Koltunski
|
import org.distorted.library.DistortedCubes;
|
31 |
261fe5bd
|
Leszek Koltunski
|
import org.distorted.library.DistortedObject;
|
32 |
08f92d82
|
Leszek Koltunski
|
import org.distorted.library.EffectNames;
|
33 |
|
|
import org.distorted.library.EffectTypes;
|
34 |
e2d8ea1d
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
35 |
794e6c4f
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic2D;
|
36 |
08f92d82
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
37 |
833685d0
|
Leszek Koltunski
|
import org.distorted.library.type.DynamicQuat;
|
38 |
e2d8ea1d
|
Leszek Koltunski
|
import org.distorted.library.type.Static1D;
|
39 |
|
|
import org.distorted.library.type.Static2D;
|
40 |
08f92d82
|
Leszek Koltunski
|
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 Vertex3DRenderer implements GLSurfaceView.Renderer
|
52 |
|
|
{
|
53 |
d2337a3a
|
Leszek Koltunski
|
private static final int SIZE = 100;
|
54 |
794e6c4f
|
Leszek Koltunski
|
|
55 |
08f92d82
|
Leszek Koltunski
|
private GLSurfaceView mView;
|
56 |
d2337a3a
|
Leszek Koltunski
|
private DistortedObject mObject;
|
57 |
71d8ad03
|
Leszek Koltunski
|
private DistortedBitmap mCenter;
|
58 |
d2337a3a
|
Leszek Koltunski
|
private float mFactorCen, mFactorObj;
|
59 |
261fe5bd
|
Leszek Koltunski
|
|
60 |
|
|
private int mObjWidth, mObjHeight;
|
61 |
|
|
|
62 |
833685d0
|
Leszek Koltunski
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
63 |
08f92d82
|
Leszek Koltunski
|
|
64 |
d2337a3a
|
Leszek Koltunski
|
private EffectNames[] order;
|
65 |
794e6c4f
|
Leszek Koltunski
|
|
66 |
d2337a3a
|
Leszek Koltunski
|
private Dynamic2D mCenterInter;
|
67 |
|
|
private Dynamic3D mDeformInter, mDistortInter, mMoveInter;
|
68 |
|
|
private Dynamic1D mSinkInter, mSwirlInter;
|
69 |
08f92d82
|
Leszek Koltunski
|
|
70 |
d2337a3a
|
Leszek Koltunski
|
private Static2D mCenterPoint;
|
71 |
|
|
private Static3D mDeformPoint, mDistortPoint, mMovePoint;
|
72 |
|
|
private Static1D mSinkPoint, mSwirlPoint;
|
73 |
08f92d82
|
Leszek Koltunski
|
|
74 |
833685d0
|
Leszek Koltunski
|
Static4D mQuat1, mQuat2;
|
75 |
d2337a3a
|
Leszek Koltunski
|
int mScreenMin;
|
76 |
833685d0
|
Leszek Koltunski
|
|
77 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78 |
|
|
|
79 |
d2337a3a
|
Leszek Koltunski
|
public void setDeform(float x, float y, float z)
|
80 |
08f92d82
|
Leszek Koltunski
|
{
|
81 |
e2d8ea1d
|
Leszek Koltunski
|
mDeformPoint.set(x, y, z);
|
82 |
08f92d82
|
Leszek Koltunski
|
}
|
83 |
|
|
|
84 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
d2337a3a
|
Leszek Koltunski
|
public void setDistort(float x, float y, float z)
|
87 |
08f92d82
|
Leszek Koltunski
|
{
|
88 |
e2d8ea1d
|
Leszek Koltunski
|
mDistortPoint.set(x, y, z);
|
89 |
08f92d82
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92 |
|
|
|
93 |
d2337a3a
|
Leszek Koltunski
|
public void setSink(float s)
|
94 |
08f92d82
|
Leszek Koltunski
|
{
|
95 |
e2d8ea1d
|
Leszek Koltunski
|
mSinkPoint.set(s);
|
96 |
08f92d82
|
Leszek Koltunski
|
}
|
97 |
|
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
d2337a3a
|
Leszek Koltunski
|
public void setSwirl(float s)
|
101 |
08f92d82
|
Leszek Koltunski
|
{
|
102 |
e2d8ea1d
|
Leszek Koltunski
|
mSwirlPoint.set(s);
|
103 |
08f92d82
|
Leszek Koltunski
|
}
|
104 |
|
|
|
105 |
794e6c4f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
d2337a3a
|
Leszek Koltunski
|
public void setCenter(float x, float y)
|
108 |
794e6c4f
|
Leszek Koltunski
|
{
|
109 |
d2337a3a
|
Leszek Koltunski
|
x = (0.012f*x-0.1f)*mObjWidth;
|
110 |
|
|
y = (0.012f*y-0.1f)*mObjHeight;
|
111 |
|
|
|
112 |
794e6c4f
|
Leszek Koltunski
|
mCenterPoint.set(x,y);
|
113 |
d2337a3a
|
Leszek Koltunski
|
mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
|
114 |
794e6c4f
|
Leszek Koltunski
|
}
|
115 |
|
|
|
116 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117 |
|
|
|
118 |
d2337a3a
|
Leszek Koltunski
|
public void setOrder(EffectNames[] effects)
|
119 |
08f92d82
|
Leszek Koltunski
|
{
|
120 |
|
|
order = effects;
|
121 |
e2d8ea1d
|
Leszek Koltunski
|
setVertexEffects();
|
122 |
08f92d82
|
Leszek Koltunski
|
}
|
123 |
|
|
|
124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
|
126 |
d2337a3a
|
Leszek Koltunski
|
public void setVertexEffects()
|
127 |
08f92d82
|
Leszek Koltunski
|
{
|
128 |
261fe5bd
|
Leszek Koltunski
|
mObject.abortEffects(EffectTypes.VERTEX);
|
129 |
794e6c4f
|
Leszek Koltunski
|
|
130 |
08f92d82
|
Leszek Koltunski
|
for( int i=0; i<=order.length-1 ; i++ )
|
131 |
|
|
{
|
132 |
|
|
switch(order[i])
|
133 |
|
|
{
|
134 |
794e6c4f
|
Leszek Koltunski
|
case DEFORM : mObject.deform( mDeformInter , mCenterInter) ; break;
|
135 |
|
|
case DISTORT: mObject.distort(mDistortInter, mCenterInter) ; break;
|
136 |
|
|
case SINK : mObject.sink( mSinkInter , mCenterInter) ; break;
|
137 |
|
|
case SWIRL : mObject.swirl( mSwirlInter , mCenterInter) ; break;
|
138 |
08f92d82
|
Leszek Koltunski
|
}
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143 |
|
|
|
144 |
|
|
public Vertex3DRenderer(GLSurfaceView v)
|
145 |
|
|
{
|
146 |
|
|
mView = v;
|
147 |
d40cfeb2
|
Leszek Koltunski
|
|
148 |
50ac40a6
|
Leszek Koltunski
|
mObject = ((Vertex3DActivity)v.getContext()).getObject();
|
149 |
71d8ad03
|
Leszek Koltunski
|
mCenter = new DistortedBitmap(SIZE, SIZE, 1);
|
150 |
d40cfeb2
|
Leszek Koltunski
|
|
151 |
261fe5bd
|
Leszek Koltunski
|
mObjWidth = mObject.getWidth();
|
152 |
|
|
mObjHeight= mObject.getHeight();
|
153 |
d40cfeb2
|
Leszek Koltunski
|
|
154 |
e2d8ea1d
|
Leszek Koltunski
|
mCenterPoint = new Static2D(0,0);
|
155 |
2bf60c29
|
Leszek Koltunski
|
mMovePoint = new Static3D(0,0,0);
|
156 |
e2d8ea1d
|
Leszek Koltunski
|
mDeformPoint = new Static3D(0,0,0);
|
157 |
|
|
mDistortPoint= new Static3D(1,1,1);
|
158 |
|
|
mSwirlPoint = new Static1D(0);
|
159 |
|
|
mSinkPoint = new Static1D(1);
|
160 |
|
|
|
161 |
794e6c4f
|
Leszek Koltunski
|
mCenterInter = new Dynamic2D();
|
162 |
e2d8ea1d
|
Leszek Koltunski
|
mDeformInter = new Dynamic3D();
|
163 |
|
|
mDistortInter = new Dynamic3D();
|
164 |
2bf60c29
|
Leszek Koltunski
|
mMoveInter = new Dynamic3D();
|
165 |
e2d8ea1d
|
Leszek Koltunski
|
mSwirlInter = new Dynamic1D();
|
166 |
|
|
mSinkInter = new Dynamic1D();
|
167 |
|
|
|
168 |
794e6c4f
|
Leszek Koltunski
|
mCenterInter.add(mCenterPoint);
|
169 |
2bf60c29
|
Leszek Koltunski
|
mMoveInter.add(mMovePoint);
|
170 |
e2d8ea1d
|
Leszek Koltunski
|
mDeformInter.add(mDeformPoint);
|
171 |
|
|
mDistortInter.add(mDistortPoint);
|
172 |
|
|
mSwirlInter.add(mSwirlPoint);
|
173 |
|
|
mSinkInter.add(mSinkPoint);
|
174 |
833685d0
|
Leszek Koltunski
|
|
175 |
|
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
176 |
|
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
177 |
|
|
|
178 |
|
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
179 |
|
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
180 |
|
|
|
181 |
|
|
mQuatInt1.add(mQuat1);
|
182 |
|
|
mQuatInt2.add(mQuat2);
|
183 |
08f92d82
|
Leszek Koltunski
|
}
|
184 |
|
|
|
185 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186 |
|
|
|
187 |
|
|
public void onDrawFrame(GL10 glUnused)
|
188 |
|
|
{
|
189 |
|
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
190 |
|
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
191 |
71d8ad03
|
Leszek Koltunski
|
|
192 |
|
|
long time = System.currentTimeMillis();
|
193 |
|
|
|
194 |
|
|
mObject.draw(time);
|
195 |
|
|
mCenter.draw(time);
|
196 |
08f92d82
|
Leszek Koltunski
|
}
|
197 |
|
|
|
198 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199 |
|
|
|
200 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
201 |
|
|
{
|
202 |
833685d0
|
Leszek Koltunski
|
mScreenMin = width<height ? width:height;
|
203 |
|
|
|
204 |
261fe5bd
|
Leszek Koltunski
|
mObject.abortEffects(EffectTypes.MATRIX);
|
205 |
71d8ad03
|
Leszek Koltunski
|
mCenter.abortEffects(EffectTypes.MATRIX);
|
206 |
|
|
|
207 |
d2337a3a
|
Leszek Koltunski
|
int centerSize = mCenter.getWidth();
|
208 |
d40cfeb2
|
Leszek Koltunski
|
|
209 |
261fe5bd
|
Leszek Koltunski
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
210 |
d40cfeb2
|
Leszek Koltunski
|
{
|
211 |
d2337a3a
|
Leszek Koltunski
|
mFactorObj = (0.70f*height)/mObjHeight;
|
212 |
|
|
mFactorCen = (0.15f*height)/centerSize;
|
213 |
261fe5bd
|
Leszek Koltunski
|
}
|
214 |
d40cfeb2
|
Leszek Koltunski
|
else
|
215 |
|
|
{
|
216 |
d2337a3a
|
Leszek Koltunski
|
mFactorObj = (0.70f*width)/mObjWidth;
|
217 |
|
|
mFactorCen = (0.15f*width)/centerSize;
|
218 |
d40cfeb2
|
Leszek Koltunski
|
}
|
219 |
|
|
|
220 |
d2337a3a
|
Leszek Koltunski
|
Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
|
221 |
|
|
float cX = act.getCenterX();
|
222 |
|
|
float cY = act.getCenterY();
|
223 |
|
|
|
224 |
|
|
cX = (0.012f*cX-0.1f)*mObjWidth;
|
225 |
|
|
cY = (0.012f*cY-0.1f)*mObjHeight;
|
226 |
|
|
|
227 |
|
|
mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
|
228 |
|
|
|
229 |
2bf60c29
|
Leszek Koltunski
|
Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
|
230 |
|
|
|
231 |
d2337a3a
|
Leszek Koltunski
|
mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
|
232 |
|
|
mObject.scale(mFactorObj);
|
233 |
2bf60c29
|
Leszek Koltunski
|
mObject.quaternion(mQuatInt1, rotateObj);
|
234 |
|
|
mObject.quaternion(mQuatInt2, rotateObj);
|
235 |
833685d0
|
Leszek Koltunski
|
|
236 |
2bf60c29
|
Leszek Koltunski
|
Static3D rotateCen = new Static3D(width/2,height/2, 0);
|
237 |
833685d0
|
Leszek Koltunski
|
|
238 |
2bf60c29
|
Leszek Koltunski
|
mCenter.quaternion(mQuatInt1, rotateCen);
|
239 |
|
|
mCenter.quaternion(mQuatInt2, rotateCen);
|
240 |
d2337a3a
|
Leszek Koltunski
|
mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
|
241 |
|
|
(height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
|
242 |
2bf60c29
|
Leszek Koltunski
|
mCenter.move(mMoveInter);
|
243 |
d2337a3a
|
Leszek Koltunski
|
mCenter.scale(mFactorCen);
|
244 |
71d8ad03
|
Leszek Koltunski
|
|
245 |
e2d8ea1d
|
Leszek Koltunski
|
setVertexEffects();
|
246 |
08f92d82
|
Leszek Koltunski
|
|
247 |
|
|
Distorted.onSurfaceChanged(width, height);
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251 |
|
|
|
252 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
253 |
|
|
{
|
254 |
bcc8e016
|
Leszek Koltunski
|
Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
|
255 |
|
|
|
256 |
|
|
InputStream is1 = act.getResources().openRawResource(act.getBitmap());
|
257 |
|
|
InputStream is2 = act.getResources().openRawResource(R.raw.center);
|
258 |
71d8ad03
|
Leszek Koltunski
|
|
259 |
|
|
Bitmap bitmap1,bitmap2;
|
260 |
08f92d82
|
Leszek Koltunski
|
|
261 |
|
|
try
|
262 |
|
|
{
|
263 |
71d8ad03
|
Leszek Koltunski
|
bitmap1 = BitmapFactory.decodeStream(is1);
|
264 |
|
|
bitmap2 = BitmapFactory.decodeStream(is2);
|
265 |
|
|
}
|
266 |
08f92d82
|
Leszek Koltunski
|
finally
|
267 |
|
|
{
|
268 |
|
|
try
|
269 |
|
|
{
|
270 |
71d8ad03
|
Leszek Koltunski
|
is1.close();
|
271 |
|
|
is2.close();
|
272 |
08f92d82
|
Leszek Koltunski
|
}
|
273 |
|
|
catch(IOException e) { }
|
274 |
|
|
}
|
275 |
|
|
|
276 |
71d8ad03
|
Leszek Koltunski
|
mObject.setBitmap(bitmap1);
|
277 |
|
|
mCenter.setBitmap(bitmap2);
|
278 |
|
|
|
279 |
08f92d82
|
Leszek Koltunski
|
try
|
280 |
|
|
{
|
281 |
|
|
Distorted.onSurfaceCreated(mView.getContext());
|
282 |
|
|
}
|
283 |
|
|
catch(Exception ex)
|
284 |
|
|
{
|
285 |
|
|
android.util.Log.e("Vertex3D", ex.getMessage() );
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
}
|