1 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
bc0a685b
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
bc0a685b
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
bc0a685b
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
427ab7bf
|
Leszek Koltunski
|
|
20 |
5068fa06
|
Leszek Koltunski
|
package org.distorted.examples.quaternion;
|
21 |
427ab7bf
|
Leszek Koltunski
|
|
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 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
30 |
427ab7bf
|
Leszek Koltunski
|
|
31 |
c6526577
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
32 |
|
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
33 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
34 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
35 |
e3900503
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
36 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedScreen;
|
37 |
107e4b72
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedTexture;
|
38 |
|
|
import org.distorted.library.mesh.MeshCubes;
|
39 |
7589635e
|
Leszek Koltunski
|
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 |
427ab7bf
|
Leszek Koltunski
|
|
44 |
|
|
import android.graphics.Bitmap;
|
45 |
|
|
import android.graphics.BitmapFactory;
|
46 |
|
|
import android.opengl.GLSurfaceView;
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
|
50 |
|
|
class QuaternionRenderer implements GLSurfaceView.Renderer
|
51 |
bc0a685b
|
Leszek Koltunski
|
{
|
52 |
|
|
private static final int NUM_QUATERNIONS = 5;
|
53 |
251e574e
|
Leszek Koltunski
|
|
54 |
bc0a685b
|
Leszek Koltunski
|
private GLSurfaceView mView;
|
55 |
f6d884d5
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
56 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
57 |
c6526577
|
Leszek Koltunski
|
private Static3D mMove, mScale, mCenter;
|
58 |
|
|
|
59 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
|
|
61 |
b695a86a
|
Leszek Koltunski
|
QuaternionRenderer(GLSurfaceView v)
|
62 |
bc0a685b
|
Leszek Koltunski
|
{
|
63 |
f6d884d5
|
Leszek Koltunski
|
mView = v;
|
64 |
687263cc
|
Leszek Koltunski
|
mTexture = new DistortedTexture();
|
65 |
c6526577
|
Leszek Koltunski
|
|
66 |
698ad0a8
|
Leszek Koltunski
|
DistortedEffects effects = new DistortedEffects();
|
67 |
c6526577
|
Leszek Koltunski
|
DynamicQuat rot = new DynamicQuat();
|
68 |
b695a86a
|
Leszek Koltunski
|
|
69 |
|
|
Random rnd = new Random(System.currentTimeMillis());
|
70 |
bc0a685b
|
Leszek Koltunski
|
float x,y,z,w, len;
|
71 |
427ab7bf
|
Leszek Koltunski
|
|
72 |
bc0a685b
|
Leszek Koltunski
|
for(int i=0; i<NUM_QUATERNIONS; i++)
|
73 |
|
|
{
|
74 |
b695a86a
|
Leszek Koltunski
|
x = 2*rnd.nextFloat()-1;
|
75 |
|
|
y = 2*rnd.nextFloat()-1;
|
76 |
|
|
z = 2*rnd.nextFloat()-1;
|
77 |
|
|
w = 2*rnd.nextFloat()-1;
|
78 |
427ab7bf
|
Leszek Koltunski
|
|
79 |
bc0a685b
|
Leszek Koltunski
|
len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
|
80 |
427ab7bf
|
Leszek Koltunski
|
|
81 |
c6526577
|
Leszek Koltunski
|
rot.add(new Static4D(x/len,y/len,z/len,w/len));
|
82 |
427ab7bf
|
Leszek Koltunski
|
}
|
83 |
bc0a685b
|
Leszek Koltunski
|
|
84 |
c6526577
|
Leszek Koltunski
|
rot.setCount(0);
|
85 |
2666a48c
|
Leszek Koltunski
|
rot.setDuration(8000);
|
86 |
c6526577
|
Leszek Koltunski
|
rot.setMode(Dynamic.MODE_LOOP);
|
87 |
|
|
|
88 |
|
|
mMove = new Static3D(0,0,0);
|
89 |
|
|
mScale = new Static3D(1,1,1);
|
90 |
|
|
mCenter = new Static3D(0,0,0);
|
91 |
|
|
|
92 |
|
|
effects.apply( new MatrixEffectQuaternion(rot,mCenter) );
|
93 |
dae661e9
|
Leszek Koltunski
|
effects.apply( new MatrixEffectScale(mScale));
|
94 |
|
|
effects.apply( new MatrixEffectMove(mMove));
|
95 |
392e16fd
|
Leszek Koltunski
|
|
96 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
97 |
687263cc
|
Leszek Koltunski
|
mScreen.attach(mTexture,effects,new MeshCubes(1,1,1));
|
98 |
bc0a685b
|
Leszek Koltunski
|
}
|
99 |
427ab7bf
|
Leszek Koltunski
|
|
100 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
|
102 |
bc0a685b
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
103 |
|
|
{
|
104 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
105 |
bc0a685b
|
Leszek Koltunski
|
}
|
106 |
427ab7bf
|
Leszek Koltunski
|
|
107 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
bc0a685b
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
110 |
|
|
{
|
111 |
687263cc
|
Leszek Koltunski
|
float factor = 0.5f*(width>height ? height:width);
|
112 |
8a99c681
|
Leszek Koltunski
|
|
113 |
687263cc
|
Leszek Koltunski
|
mCenter.set(0.5f,0.5f,0.5f);
|
114 |
|
|
mMove.set( (width-factor)/2 , (height-factor)/2 , -factor/2 );
|
115 |
c6526577
|
Leszek Koltunski
|
mScale.set(factor,factor,factor);
|
116 |
427ab7bf
|
Leszek Koltunski
|
|
117 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
118 |
bc0a685b
|
Leszek Koltunski
|
}
|
119 |
427ab7bf
|
Leszek Koltunski
|
|
120 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121 |
|
|
|
122 |
bc0a685b
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
123 |
|
|
{
|
124 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
125 |
|
|
Bitmap bitmap;
|
126 |
427ab7bf
|
Leszek Koltunski
|
|
127 |
bc0a685b
|
Leszek Koltunski
|
try
|
128 |
|
|
{
|
129 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
130 |
|
|
}
|
131 |
|
|
finally
|
132 |
|
|
{
|
133 |
427ab7bf
|
Leszek Koltunski
|
try
|
134 |
|
|
{
|
135 |
bc0a685b
|
Leszek Koltunski
|
is.close();
|
136 |
427ab7bf
|
Leszek Koltunski
|
}
|
137 |
bc0a685b
|
Leszek Koltunski
|
catch(IOException e) { }
|
138 |
|
|
}
|
139 |
427ab7bf
|
Leszek Koltunski
|
|
140 |
f6d884d5
|
Leszek Koltunski
|
mTexture.setTexture(bitmap);
|
141 |
427ab7bf
|
Leszek Koltunski
|
|
142 |
bc0a685b
|
Leszek Koltunski
|
try
|
143 |
|
|
{
|
144 |
e3900503
|
Leszek Koltunski
|
DistortedLibrary.onCreate(mView.getContext());
|
145 |
bc0a685b
|
Leszek Koltunski
|
}
|
146 |
|
|
catch(Exception ex)
|
147 |
|
|
{
|
148 |
|
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
149 |
427ab7bf
|
Leszek Koltunski
|
}
|
150 |
bc0a685b
|
Leszek Koltunski
|
}
|
151 |
|
|
}
|