1
|
|
2
|
package org.distorted.examples.effects3d;
|
3
|
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStream;
|
6
|
|
7
|
import javax.microedition.khronos.egl.EGLConfig;
|
8
|
import javax.microedition.khronos.opengles.GL10;
|
9
|
|
10
|
import org.distorted.examples.R;
|
11
|
|
12
|
import org.distorted.library.DistortedCubes;
|
13
|
import org.distorted.library.EffectTypes;
|
14
|
import org.distorted.library.Float3D;
|
15
|
import org.distorted.library.Float4D;
|
16
|
import org.distorted.library.Interpolator3D;
|
17
|
import org.distorted.library.Interpolator4D;
|
18
|
import org.distorted.library.Distorted;
|
19
|
|
20
|
import android.graphics.Bitmap;
|
21
|
import android.graphics.BitmapFactory;
|
22
|
import android.opengl.GLES20;
|
23
|
import android.opengl.GLSurfaceView;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
class Effects3DRenderer implements GLSurfaceView.Renderer
|
28
|
{
|
29
|
private static final int SIZE = 100;
|
30
|
|
31
|
private GLSurfaceView mView;
|
32
|
private static DistortedCubes mCube;
|
33
|
|
34
|
private static int[] order;
|
35
|
|
36
|
private static Interpolator3D mMoveInter, mScaleInter, mShearInter;
|
37
|
private static Interpolator4D mRotateInter;
|
38
|
|
39
|
private static Float3D mZeroPoint, mMovePoint, mScalePoint, mShearPoint;
|
40
|
private static Float4D mRotatePoint;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public static void setMove(float x, float y, float z)
|
45
|
{
|
46
|
mMovePoint.set(x, y, z);
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
public static void setScale(float x, float y, float z)
|
52
|
{
|
53
|
mScalePoint.set(x, y, z);
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
public static void setRotate(float a, float x, float y, float z)
|
59
|
{
|
60
|
mRotatePoint.set(a, x, y, z);
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
public static void setShear(float x, float y, float z)
|
66
|
{
|
67
|
mShearPoint.set(x, y, z);
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
public static void setOrder(int[] effects)
|
73
|
{
|
74
|
order = effects;
|
75
|
setMatrixEffects();
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
public static void setMatrixEffects()
|
81
|
{
|
82
|
mCube.abortEffects(EffectTypes.MATRIX);
|
83
|
|
84
|
for( int i=0; i<=order.length-1 ; i++ )
|
85
|
{
|
86
|
switch(order[i])
|
87
|
{
|
88
|
case Effects3DActivity.MOVE : mCube.move(mMoveInter) ; break;
|
89
|
case Effects3DActivity.SCALE : mCube.scale(mScaleInter) ; break;
|
90
|
case Effects3DActivity.ROTATE: mCube.rotate(mZeroPoint,mRotateInter); break;
|
91
|
case Effects3DActivity.SHEAR : mCube.shear(mZeroPoint,mShearInter) ; break;
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
public Effects3DRenderer(GLSurfaceView v)
|
99
|
{
|
100
|
mView = v;
|
101
|
mCube = new DistortedCubes( 1, "1", SIZE);
|
102
|
|
103
|
mZeroPoint = new Float3D(0,0,0);
|
104
|
mMovePoint = new Float3D(0,0,0);
|
105
|
mScalePoint = new Float3D(1,1,1);
|
106
|
mShearPoint = new Float3D(0,0,0);
|
107
|
mRotatePoint= new Float4D(1,0,0,0);
|
108
|
|
109
|
mMoveInter = new Interpolator3D();
|
110
|
mScaleInter = new Interpolator3D();
|
111
|
mShearInter = new Interpolator3D();
|
112
|
mRotateInter= new Interpolator4D();
|
113
|
|
114
|
mMoveInter.add(mMovePoint);
|
115
|
mScaleInter.add(mScalePoint);
|
116
|
mShearInter.add(mShearPoint);
|
117
|
mRotateInter.add(mRotatePoint);
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
public void onDrawFrame(GL10 glUnused)
|
123
|
{
|
124
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
125
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
126
|
|
127
|
mCube.draw(System.currentTimeMillis());
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
133
|
{
|
134
|
setMatrixEffects();
|
135
|
|
136
|
Distorted.onSurfaceChanged(width, height);
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
|
141
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
142
|
{
|
143
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
144
|
Bitmap bitmap;
|
145
|
|
146
|
try
|
147
|
{
|
148
|
bitmap = BitmapFactory.decodeStream(is);
|
149
|
}
|
150
|
finally
|
151
|
{
|
152
|
try
|
153
|
{
|
154
|
is.close();
|
155
|
}
|
156
|
catch(IOException e) { }
|
157
|
}
|
158
|
|
159
|
mCube.setBitmap(bitmap);
|
160
|
|
161
|
try
|
162
|
{
|
163
|
Distorted.onSurfaceCreated(mView.getContext());
|
164
|
}
|
165
|
catch(Exception ex)
|
166
|
{
|
167
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
168
|
}
|
169
|
}
|
170
|
}
|