1
|
|
2
|
package org.distorted.examples.quaternion;
|
3
|
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStream;
|
6
|
import java.util.Random;
|
7
|
|
8
|
import javax.microedition.khronos.egl.EGLConfig;
|
9
|
import javax.microedition.khronos.opengles.GL10;
|
10
|
|
11
|
import org.distorted.examples.R;
|
12
|
|
13
|
import org.distorted.library.Interpolator;
|
14
|
import org.distorted.library.InterpolatorQuat;
|
15
|
import org.distorted.library.DistortedCubes;
|
16
|
import org.distorted.library.Float4D;
|
17
|
import org.distorted.library.Float3D;
|
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 QuaternionRenderer implements GLSurfaceView.Renderer
|
28
|
{
|
29
|
private static final int NUM_QUATERNIONS = 5;
|
30
|
private static final int SIZE = 100;
|
31
|
|
32
|
private GLSurfaceView mView;
|
33
|
private DistortedCubes mCube;
|
34
|
private Random mRnd = new Random(System.currentTimeMillis());
|
35
|
private InterpolatorQuat mRot;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public QuaternionRenderer(GLSurfaceView v)
|
40
|
{
|
41
|
mView = v;
|
42
|
mCube = new DistortedCubes( 3, "000010000", SIZE);
|
43
|
|
44
|
mRot = new InterpolatorQuat();
|
45
|
|
46
|
float x,y,z,w, len;
|
47
|
|
48
|
for(int i=0; i<NUM_QUATERNIONS; i++)
|
49
|
{
|
50
|
x = 2*mRnd.nextFloat()-1;
|
51
|
y = 2*mRnd.nextFloat()-1;
|
52
|
z = 2*mRnd.nextFloat()-1;
|
53
|
w = 2*mRnd.nextFloat()-1;
|
54
|
|
55
|
len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
|
56
|
|
57
|
mRot.add(new Float4D(x/len,y/len,z/len,w/len));
|
58
|
}
|
59
|
|
60
|
mRot.setCount(0);
|
61
|
mRot.setDuration(8000);
|
62
|
mRot.setMode(Interpolator.MODE_LOOP);
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
public void onDrawFrame(GL10 glUnused)
|
68
|
{
|
69
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
70
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
71
|
|
72
|
mCube.draw(System.currentTimeMillis());
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
78
|
{
|
79
|
mCube.abortAllEffects(Distorted.TYPE_MATR);
|
80
|
|
81
|
if( width > height )
|
82
|
{
|
83
|
mCube.move((width-height)/2 ,0, 0);
|
84
|
mCube.scale(height/(3.0f*SIZE));
|
85
|
}
|
86
|
else
|
87
|
{
|
88
|
mCube.move(0 ,(height-width)/2, 0);
|
89
|
mCube.scale(width/(3.0f*SIZE));
|
90
|
}
|
91
|
|
92
|
mCube.quaternion( new Float3D(3*SIZE/2,3*SIZE/2,0), mRot);
|
93
|
|
94
|
Distorted.onSurfaceChanged(width, height);
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
100
|
{
|
101
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
102
|
Bitmap bitmap;
|
103
|
|
104
|
try
|
105
|
{
|
106
|
bitmap = BitmapFactory.decodeStream(is);
|
107
|
}
|
108
|
finally
|
109
|
{
|
110
|
try
|
111
|
{
|
112
|
is.close();
|
113
|
}
|
114
|
catch(IOException e) { }
|
115
|
}
|
116
|
|
117
|
mCube.setBitmap(bitmap);
|
118
|
|
119
|
try
|
120
|
{
|
121
|
Distorted.onSurfaceCreated(mView.getContext());
|
122
|
}
|
123
|
catch(Exception ex)
|
124
|
{
|
125
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
126
|
}
|
127
|
}
|
128
|
}
|