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.effect.MatrixEffectMove;
|
32
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
33
|
import org.distorted.library.effect.MatrixEffectScale;
|
34
|
import org.distorted.library.main.DistortedEffects;
|
35
|
import org.distorted.library.main.DistortedScreen;
|
36
|
import org.distorted.library.main.DistortedTexture;
|
37
|
import org.distorted.library.mesh.MeshBase;
|
38
|
import org.distorted.library.mesh.MeshCubes;
|
39
|
import org.distorted.library.type.Dynamic;
|
40
|
import org.distorted.library.type.DynamicQuat;
|
41
|
import org.distorted.library.type.Static4D;
|
42
|
import org.distorted.library.type.Static3D;
|
43
|
import org.distorted.library.main.Distorted;
|
44
|
|
45
|
import android.graphics.Bitmap;
|
46
|
import android.graphics.BitmapFactory;
|
47
|
import android.opengl.GLSurfaceView;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class QuaternionRenderer implements GLSurfaceView.Renderer
|
52
|
{
|
53
|
private static final int NUM_QUATERNIONS = 5;
|
54
|
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedTexture mTexture;
|
57
|
private MeshBase mMesh;
|
58
|
private DistortedScreen mScreen;
|
59
|
private Static3D mMove, mScale, mCenter;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
QuaternionRenderer(GLSurfaceView v)
|
64
|
{
|
65
|
mView = v;
|
66
|
mTexture = new DistortedTexture(1,1);
|
67
|
mMesh = new MeshCubes(1,1,1);
|
68
|
|
69
|
DistortedEffects effects = new DistortedEffects();
|
70
|
DynamicQuat rot = new DynamicQuat();
|
71
|
|
72
|
Random rnd = new Random(System.currentTimeMillis());
|
73
|
float x,y,z,w, len;
|
74
|
|
75
|
for(int i=0; i<NUM_QUATERNIONS; i++)
|
76
|
{
|
77
|
x = 2*rnd.nextFloat()-1;
|
78
|
y = 2*rnd.nextFloat()-1;
|
79
|
z = 2*rnd.nextFloat()-1;
|
80
|
w = 2*rnd.nextFloat()-1;
|
81
|
|
82
|
len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
|
83
|
|
84
|
rot.add(new Static4D(x/len,y/len,z/len,w/len));
|
85
|
}
|
86
|
|
87
|
rot.setCount(0);
|
88
|
rot.setDuration(8000);
|
89
|
rot.setMode(Dynamic.MODE_LOOP);
|
90
|
|
91
|
mMove = new Static3D(0,0,0);
|
92
|
mScale = new Static3D(1,1,1);
|
93
|
mCenter = new Static3D(0,0,0);
|
94
|
|
95
|
effects.apply( new MatrixEffectMove(mMove));
|
96
|
effects.apply(new MatrixEffectScale(mScale));
|
97
|
effects.apply( new MatrixEffectQuaternion(rot,mCenter) );
|
98
|
|
99
|
mScreen = new DistortedScreen();
|
100
|
mScreen.attach(mTexture,effects,mMesh);
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
public void onDrawFrame(GL10 glUnused)
|
106
|
{
|
107
|
mScreen.render( System.currentTimeMillis() );
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
113
|
{
|
114
|
float w = mTexture.getWidth();
|
115
|
float h = mTexture.getHeight();
|
116
|
float d = mTexture.getDepth(mMesh);
|
117
|
|
118
|
float factor = 0.5f*(width>height ? height/h:width/w);
|
119
|
|
120
|
mCenter.set(w/2,h/2,d/2);
|
121
|
mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , 0 );
|
122
|
mScale.set(factor,factor,factor);
|
123
|
|
124
|
mScreen.resize(width, height);
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
130
|
{
|
131
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
132
|
Bitmap bitmap;
|
133
|
|
134
|
try
|
135
|
{
|
136
|
bitmap = BitmapFactory.decodeStream(is);
|
137
|
}
|
138
|
finally
|
139
|
{
|
140
|
try
|
141
|
{
|
142
|
is.close();
|
143
|
}
|
144
|
catch(IOException e) { }
|
145
|
}
|
146
|
|
147
|
mTexture.setTexture(bitmap);
|
148
|
|
149
|
try
|
150
|
{
|
151
|
Distorted.onCreate(mView.getContext());
|
152
|
}
|
153
|
catch(Exception ex)
|
154
|
{
|
155
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
156
|
}
|
157
|
}
|
158
|
}
|