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.quaternion;
|
21
|
|
22
|
import java.io.IOException;
|
23
|
import java.io.InputStream;
|
24
|
import java.util.Random;
|
25
|
|
26
|
import javax.microedition.khronos.egl.EGLConfig;
|
27
|
import javax.microedition.khronos.opengles.GL10;
|
28
|
|
29
|
import org.distorted.examples.R;
|
30
|
|
31
|
import org.distorted.library.EffectTypes;
|
32
|
import org.distorted.library.type.Dynamic;
|
33
|
import org.distorted.library.type.DynamicQuat;
|
34
|
import org.distorted.library.DistortedCubes;
|
35
|
import org.distorted.library.type.Static4D;
|
36
|
import org.distorted.library.type.Static3D;
|
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 QuaternionRenderer implements GLSurfaceView.Renderer
|
47
|
{
|
48
|
private static final int NUM_QUATERNIONS = 5;
|
49
|
private static final int SIZE = 100;
|
50
|
|
51
|
private GLSurfaceView mView;
|
52
|
private DistortedCubes mCube;
|
53
|
|
54
|
private DynamicQuat mRot;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
QuaternionRenderer(GLSurfaceView v)
|
59
|
{
|
60
|
mView = v;
|
61
|
mCube = new DistortedCubes( 3, "000010000", SIZE);
|
62
|
|
63
|
mRot = new DynamicQuat();
|
64
|
|
65
|
Random rnd = new Random(System.currentTimeMillis());
|
66
|
float x,y,z,w, len;
|
67
|
|
68
|
for(int i=0; i<NUM_QUATERNIONS; i++)
|
69
|
{
|
70
|
x = 2*rnd.nextFloat()-1;
|
71
|
y = 2*rnd.nextFloat()-1;
|
72
|
z = 2*rnd.nextFloat()-1;
|
73
|
w = 2*rnd.nextFloat()-1;
|
74
|
|
75
|
len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
|
76
|
|
77
|
mRot.add(new Static4D(x/len,y/len,z/len,w/len));
|
78
|
}
|
79
|
|
80
|
mRot.setCount(0);
|
81
|
mRot.setDuration(8000);
|
82
|
mRot.setMode(Dynamic.MODE_LOOP);
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
public void onDrawFrame(GL10 glUnused)
|
88
|
{
|
89
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
90
|
mCube.draw(System.currentTimeMillis());
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
96
|
{
|
97
|
mCube.abortEffects(EffectTypes.MATRIX);
|
98
|
|
99
|
if( width > height )
|
100
|
{
|
101
|
mCube.move( new Static3D((width-height)/2,0,0) );
|
102
|
mCube.scale(height/(3.0f*SIZE));
|
103
|
}
|
104
|
else
|
105
|
{
|
106
|
mCube.move( new Static3D(0,(height-width)/2,0) );
|
107
|
mCube.scale(width/(3.0f*SIZE));
|
108
|
}
|
109
|
|
110
|
mCube.quaternion( mRot, new Static3D(3*SIZE/2,3*SIZE/2,0) );
|
111
|
|
112
|
Distorted.onSurfaceChanged(width, height);
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
118
|
{
|
119
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
120
|
|
121
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
122
|
Bitmap bitmap;
|
123
|
|
124
|
try
|
125
|
{
|
126
|
bitmap = BitmapFactory.decodeStream(is);
|
127
|
}
|
128
|
finally
|
129
|
{
|
130
|
try
|
131
|
{
|
132
|
is.close();
|
133
|
}
|
134
|
catch(IOException e) { }
|
135
|
}
|
136
|
|
137
|
mCube.setBitmap(bitmap);
|
138
|
|
139
|
try
|
140
|
{
|
141
|
Distorted.onSurfaceCreated(mView.getContext());
|
142
|
}
|
143
|
catch(Exception ex)
|
144
|
{
|
145
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
146
|
}
|
147
|
}
|
148
|
}
|