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