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.DistortedEffects;
|
32
|
import org.distorted.library.DistortedScreen;
|
33
|
import org.distorted.library.EffectTypes;
|
34
|
import org.distorted.library.type.Dynamic;
|
35
|
import org.distorted.library.type.DynamicQuat;
|
36
|
import org.distorted.library.MeshCubes;
|
37
|
import org.distorted.library.DistortedTexture;
|
38
|
import org.distorted.library.type.Static4D;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
import org.distorted.library.Distorted;
|
41
|
|
42
|
import android.graphics.Bitmap;
|
43
|
import android.graphics.BitmapFactory;
|
44
|
import android.opengl.GLES30;
|
45
|
import android.opengl.GLSurfaceView;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
class QuaternionRenderer implements GLSurfaceView.Renderer
|
50
|
{
|
51
|
private static final int NUM_QUATERNIONS = 5;
|
52
|
|
53
|
private GLSurfaceView mView;
|
54
|
private DistortedTexture mTexture;
|
55
|
private DistortedEffects mEffects;
|
56
|
private DistortedScreen mScreen;
|
57
|
private DynamicQuat mRot;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
QuaternionRenderer(GLSurfaceView v)
|
62
|
{
|
63
|
mView = v;
|
64
|
mTexture = new DistortedTexture(1,1);
|
65
|
mEffects = new DistortedEffects();
|
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
|
mScreen = new DistortedScreen();
|
88
|
mScreen.attach(mTexture,mEffects,new MeshCubes(1,1,false));
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public void onDrawFrame(GL10 glUnused)
|
94
|
{
|
95
|
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
|
96
|
mScreen.render( System.currentTimeMillis() );
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
102
|
{
|
103
|
float scaleFactor = width>height ? height/3:width/3;
|
104
|
|
105
|
mEffects.abortEffects(EffectTypes.MATRIX);
|
106
|
mEffects.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
|
107
|
mEffects.scale(scaleFactor);
|
108
|
mEffects.quaternion( mRot, new Static3D( (float)mTexture.getWidth()/2, (float)mTexture.getHeight()/2 , 0) );
|
109
|
|
110
|
mScreen.resize(width, height);
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
116
|
{
|
117
|
GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
118
|
|
119
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
120
|
Bitmap bitmap;
|
121
|
|
122
|
try
|
123
|
{
|
124
|
bitmap = BitmapFactory.decodeStream(is);
|
125
|
}
|
126
|
finally
|
127
|
{
|
128
|
try
|
129
|
{
|
130
|
is.close();
|
131
|
}
|
132
|
catch(IOException e) { }
|
133
|
}
|
134
|
|
135
|
mTexture.setTexture(bitmap);
|
136
|
|
137
|
try
|
138
|
{
|
139
|
Distorted.onCreate(mView.getContext());
|
140
|
}
|
141
|
catch(Exception ex)
|
142
|
{
|
143
|
android.util.Log.e("Quaternion", ex.getMessage() );
|
144
|
}
|
145
|
}
|
146
|
}
|