1
|
|
2
|
package org.distorted.examples.cubes;
|
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.EffectTypes;
|
13
|
import org.distorted.library.InterpolatorQuat;
|
14
|
import org.distorted.library.DistortedCubes;
|
15
|
import org.distorted.library.Float4D;
|
16
|
import org.distorted.library.Float3D;
|
17
|
import org.distorted.library.Distorted;
|
18
|
|
19
|
import android.graphics.Bitmap;
|
20
|
import android.graphics.BitmapFactory;
|
21
|
import android.opengl.GLES20;
|
22
|
import android.opengl.GLSurfaceView;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
class CubesRenderer implements GLSurfaceView.Renderer
|
27
|
{
|
28
|
private static final int SIZE = 100;
|
29
|
|
30
|
private int mCols, mRows;
|
31
|
|
32
|
private GLSurfaceView mView;
|
33
|
private DistortedCubes mCubes;
|
34
|
private InterpolatorQuat mQuatInt1, mQuatInt2;
|
35
|
|
36
|
Float4D mQuat1, mQuat2;
|
37
|
int mScreenMin;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public CubesRenderer(GLSurfaceView v)
|
42
|
{
|
43
|
mView = v;
|
44
|
|
45
|
String shape = CubesActivity.getShape();
|
46
|
|
47
|
mCols = CubesActivity.getCols();
|
48
|
mRows = shape.length() / mCols;
|
49
|
|
50
|
mCubes = new DistortedCubes( mCols, shape, SIZE);
|
51
|
|
52
|
mQuat1 = new Float4D(0,0,0,1); // unity
|
53
|
mQuat2 = new Float4D(0,0,0,1); // quaternions
|
54
|
|
55
|
mQuatInt1 = new InterpolatorQuat();
|
56
|
mQuatInt2 = new InterpolatorQuat();
|
57
|
mQuatInt1.setDuration(0);
|
58
|
mQuatInt2.setDuration(0);
|
59
|
mQuatInt1.setCount(0.5f);
|
60
|
mQuatInt2.setCount(0.5f);
|
61
|
|
62
|
mQuatInt1.add(mQuat1);
|
63
|
mQuatInt2.add(mQuat2);
|
64
|
}
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
public void onDrawFrame(GL10 glUnused)
|
69
|
{
|
70
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
71
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
72
|
|
73
|
mCubes.draw(System.currentTimeMillis());
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
79
|
{
|
80
|
mScreenMin = width<height ? width:height;
|
81
|
|
82
|
mCubes.abortEffects(EffectTypes.MATRIX);
|
83
|
|
84
|
if( mRows/mCols > height/width )
|
85
|
{
|
86
|
int w = (height*mRows)/mCols;
|
87
|
mCubes.move((width-w)/2 ,0, 0);
|
88
|
mCubes.scale((float)height/(mRows*SIZE));
|
89
|
}
|
90
|
else
|
91
|
{
|
92
|
int h = (width*mRows)/mCols;
|
93
|
mCubes.move(0 ,(height-h)/2, 0);
|
94
|
mCubes.scale((float)width/(mCols*SIZE));
|
95
|
}
|
96
|
|
97
|
Float3D center = new Float3D(mCols*SIZE/2,mRows*SIZE/2, 0);
|
98
|
|
99
|
mCubes.quaternion(center, mQuatInt1);
|
100
|
mCubes.quaternion(center, mQuatInt2);
|
101
|
|
102
|
Distorted.onSurfaceChanged(width, height);
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
108
|
{
|
109
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
110
|
Bitmap bitmap;
|
111
|
|
112
|
try
|
113
|
{
|
114
|
bitmap = BitmapFactory.decodeStream(is);
|
115
|
}
|
116
|
finally
|
117
|
{
|
118
|
try
|
119
|
{
|
120
|
is.close();
|
121
|
}
|
122
|
catch(IOException e) { }
|
123
|
}
|
124
|
|
125
|
mCubes.setBitmap(bitmap);
|
126
|
|
127
|
try
|
128
|
{
|
129
|
Distorted.onSurfaceCreated(mView.getContext());
|
130
|
}
|
131
|
catch(Exception ex)
|
132
|
{
|
133
|
android.util.Log.e("Cubes", ex.getMessage() );
|
134
|
}
|
135
|
}
|
136
|
}
|