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.GridCubes;
|
35
|
import org.distorted.library.DistortedTexture;
|
36
|
import org.distorted.library.DistortedEffectQueues;
|
37
|
import org.distorted.library.type.Static4D;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
import org.distorted.library.Distorted;
|
40
|
|
41
|
import android.graphics.Bitmap;
|
42
|
import android.graphics.BitmapFactory;
|
43
|
import android.opengl.GLES20;
|
44
|
import android.opengl.GLSurfaceView;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
class QuaternionRenderer implements GLSurfaceView.Renderer
|
49
|
{
|
50
|
private static final int NUM_QUATERNIONS = 5;
|
51
|
|
52
|
private GLSurfaceView mView;
|
53
|
private DistortedTexture mTexture;
|
54
|
private DistortedEffectQueues mQueues;
|
55
|
private GridCubes mGrid;
|
56
|
private DynamicQuat mRot;
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
QuaternionRenderer(GLSurfaceView v)
|
61
|
{
|
62
|
mView = v;
|
63
|
mGrid = new GridCubes(1,1,false);
|
64
|
mTexture = new DistortedTexture(1,1,1);
|
65
|
mQueues = new DistortedEffectQueues();
|
66
|
mRot = new DynamicQuat();
|
67
|
|
68
|
Random rnd = new Random(System.currentTimeMillis());
|
69
|
float x,y,z,w, len;
|
70
|
|
71
|
for(int i=0; i<NUM_QUATERNIONS; i++)
|
72
|
{
|
73
|
x = 2*rnd.nextFloat()-1;
|
74
|
y = 2*rnd.nextFloat()-1;
|
75
|
z = 2*rnd.nextFloat()-1;
|
76
|
w = 2*rnd.nextFloat()-1;
|
77
|
|
78
|
len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
|
79
|
|
80
|
mRot.add(new Static4D(x/len,y/len,z/len,w/len));
|
81
|
}
|
82
|
|
83
|
mRot.setCount(0);
|
84
|
mRot.setDuration(8000);
|
85
|
mRot.setMode(Dynamic.MODE_LOOP);
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
public void onDrawFrame(GL10 glUnused)
|
91
|
{
|
92
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
93
|
mQueues.draw(System.currentTimeMillis(), mTexture,mGrid);
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
99
|
{
|
100
|
float scaleFactor = width>height ? height/3:width/3;
|
101
|
|
102
|
mQueues.abortEffects(EffectTypes.MATRIX);
|
103
|
mQueues.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
|
104
|
mQueues.scale(scaleFactor);
|
105
|
mQueues.quaternion( mRot, new Static3D(0.5f,0.5f,0) );
|
106
|
|
107
|
Distorted.onSurfaceChanged(width, height);
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
113
|
{
|
114
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
115
|
|
116
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
117
|
Bitmap bitmap;
|
118
|
|
119
|
try
|
120
|
{
|
121
|
bitmap = BitmapFactory.decodeStream(is);
|
122
|
}
|
123
|
finally
|
124
|
{
|
125
|
try
|
126
|
{
|
127
|
is.close();
|
128
|
}
|
129
|
catch(IOException e) { }
|
130
|
}
|
131
|
|
132
|
mTexture.setTexture(bitmap);
|
133
|
|
134
|
try
|
135
|
{
|
136
|
Distorted.onSurfaceCreated(mView.getContext());
|
137
|
}
|
138
|
catch(Exception ex)
|
139
|
{
|
140
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
141
|
}
|
142
|
}
|
143
|
}
|