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