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.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
|
import org.distorted.library.DistortedCubes;
|
30
|
import org.distorted.library.EffectNames;
|
31
|
import org.distorted.library.EffectTypes;
|
32
|
import org.distorted.library.type.Dynamic1D;
|
33
|
import org.distorted.library.type.Dynamic3D;
|
34
|
import org.distorted.library.type.Dynamic4D;
|
35
|
import org.distorted.library.type.DynamicQuat;
|
36
|
import org.distorted.library.type.Static1D;
|
37
|
import org.distorted.library.type.Static2D;
|
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 Vertex3DRenderer implements GLSurfaceView.Renderer
|
50
|
{
|
51
|
private static final int SIZE = 100;
|
52
|
|
53
|
private GLSurfaceView mView;
|
54
|
private static DistortedCubes mCube;
|
55
|
private int mCols, mRows;
|
56
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
57
|
|
58
|
private static EffectNames[] order;
|
59
|
|
60
|
private static Dynamic3D mDeformInter, mDistortInter;
|
61
|
private static Dynamic1D mSinkInter, mSwirlInter;
|
62
|
|
63
|
private static Static2D mCenterPoint;
|
64
|
private static Static3D mDeformPoint, mDistortPoint;
|
65
|
private static Static1D mSinkPoint, mSwirlPoint;
|
66
|
|
67
|
Static4D mQuat1, mQuat2;
|
68
|
int mScreenMin;
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
public static void setDeform(float x, float y, float z)
|
73
|
{
|
74
|
mDeformPoint.set(x, y, z);
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
public static void setDistort(float x, float y, float z)
|
80
|
{
|
81
|
mDistortPoint.set(x, y, z);
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
public static void setSink(float s)
|
87
|
{
|
88
|
mSinkPoint.set(s);
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public static void setSwirl(float s)
|
94
|
{
|
95
|
mSwirlPoint.set(s);
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
public static void setOrder(EffectNames[] effects)
|
101
|
{
|
102
|
order = effects;
|
103
|
setVertexEffects();
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public static void setVertexEffects()
|
109
|
{
|
110
|
mCube.abortEffects(EffectTypes.VERTEX);
|
111
|
|
112
|
for( int i=0; i<=order.length-1 ; i++ )
|
113
|
{
|
114
|
switch(order[i])
|
115
|
{
|
116
|
case DEFORM : mCube.deform( mDeformInter , mCenterPoint) ; break;
|
117
|
case DISTORT: mCube.distort(mDistortInter, mCenterPoint) ; break;
|
118
|
case SINK : mCube.sink( mSinkInter , mCenterPoint) ; break;
|
119
|
case SWIRL : mCube.swirl( mSwirlInter , mCenterPoint) ; break;
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
public Vertex3DRenderer(GLSurfaceView v)
|
127
|
{
|
128
|
mView = v;
|
129
|
|
130
|
String shape = Vertex3DActivity.getShape();
|
131
|
|
132
|
mCols = Vertex3DActivity.getCols();
|
133
|
mRows = shape.length() / mCols;
|
134
|
|
135
|
mCube = new DistortedCubes( mCols, shape, SIZE);
|
136
|
|
137
|
mCenterPoint = new Static2D(0,0);
|
138
|
mDeformPoint = new Static3D(0,0,0);
|
139
|
mDistortPoint= new Static3D(1,1,1);
|
140
|
mSwirlPoint = new Static1D(0);
|
141
|
mSinkPoint = new Static1D(1);
|
142
|
|
143
|
mDeformInter = new Dynamic3D();
|
144
|
mDistortInter = new Dynamic3D();
|
145
|
mSwirlInter = new Dynamic1D();
|
146
|
mSinkInter = new Dynamic1D();
|
147
|
|
148
|
mDeformInter.add(mDeformPoint);
|
149
|
mDistortInter.add(mDistortPoint);
|
150
|
mSwirlInter.add(mSwirlPoint);
|
151
|
mSinkInter.add(mSinkPoint);
|
152
|
|
153
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
154
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
155
|
|
156
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
157
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
158
|
|
159
|
mQuatInt1.add(mQuat1);
|
160
|
mQuatInt2.add(mQuat2);
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
public void onDrawFrame(GL10 glUnused)
|
166
|
{
|
167
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
168
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
169
|
|
170
|
mCube.draw(System.currentTimeMillis());
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
176
|
{
|
177
|
mScreenMin = width<height ? width:height;
|
178
|
|
179
|
mCube.abortEffects(EffectTypes.MATRIX);
|
180
|
float factor;
|
181
|
|
182
|
if( width*mRows > height*mCols ) // screen is more 'horizontal' than the shape
|
183
|
{
|
184
|
factor = height/((mRows+2)*SIZE);
|
185
|
}
|
186
|
else
|
187
|
{
|
188
|
factor = width/((mCols+2)*SIZE);
|
189
|
}
|
190
|
|
191
|
mCube.scale(factor);
|
192
|
mCube.move( new Static3D( (width-factor*mCols*SIZE)/2 , (height-factor*mRows*SIZE)/2 , 0) );
|
193
|
|
194
|
Static3D center = new Static3D(mCols*SIZE/2,mRows*SIZE/2, 0);
|
195
|
|
196
|
mCube.quaternion(mQuatInt1, center);
|
197
|
mCube.quaternion(mQuatInt2, center);
|
198
|
|
199
|
setVertexEffects();
|
200
|
|
201
|
Distorted.onSurfaceChanged(width, height);
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
207
|
{
|
208
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
209
|
Bitmap bitmap;
|
210
|
|
211
|
try
|
212
|
{
|
213
|
bitmap = BitmapFactory.decodeStream(is);
|
214
|
}
|
215
|
finally
|
216
|
{
|
217
|
try
|
218
|
{
|
219
|
is.close();
|
220
|
}
|
221
|
catch(IOException e) { }
|
222
|
}
|
223
|
|
224
|
mCube.setBitmap(bitmap);
|
225
|
|
226
|
try
|
227
|
{
|
228
|
Distorted.onSurfaceCreated(mView.getContext());
|
229
|
}
|
230
|
catch(Exception ex)
|
231
|
{
|
232
|
android.util.Log.e("Vertex3D", ex.getMessage() );
|
233
|
}
|
234
|
}
|
235
|
}
|