1 |
5b4a2d76
|
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 |
|
|
|
20 |
|
|
package org.distorted.examples.rubik;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.BitmapFactory;
|
24 |
|
|
import android.opengl.GLSurfaceView;
|
25 |
|
|
|
26 |
|
|
import org.distorted.examples.R;
|
27 |
|
|
import org.distorted.library.effect.MatrixEffectMove;
|
28 |
|
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
29 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
30 |
|
|
import org.distorted.library.effect.VertexEffect;
|
31 |
|
|
import org.distorted.library.effect.VertexEffectDistort;
|
32 |
|
|
import org.distorted.library.main.Distorted;
|
33 |
|
|
import org.distorted.library.main.DistortedEffects;
|
34 |
|
|
import org.distorted.library.main.DistortedScreen;
|
35 |
|
|
import org.distorted.library.main.DistortedTexture;
|
36 |
|
|
import org.distorted.library.mesh.MeshCubes;
|
37 |
|
|
import org.distorted.library.mesh.MeshFlat;
|
38 |
|
|
import org.distorted.library.type.Dynamic3D;
|
39 |
|
|
import org.distorted.library.type.Static3D;
|
40 |
|
|
import org.distorted.library.type.Static4D;
|
41 |
|
|
|
42 |
|
|
import java.io.IOException;
|
43 |
|
|
import java.io.InputStream;
|
44 |
|
|
|
45 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
46 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
|
|
|
50 |
|
|
class RubikRenderer implements GLSurfaceView.Renderer
|
51 |
|
|
{
|
52 |
|
|
private static final int CUBE_SIZE = 3;
|
53 |
|
|
private static final int VERTICES = 5;
|
54 |
|
|
private static final int SIZE = 200;
|
55 |
|
|
|
56 |
|
|
private GLSurfaceView mView;
|
57 |
|
|
private DistortedTexture mTexture;
|
58 |
|
|
private DistortedScreen mScreen;
|
59 |
|
|
private Static3D mMove, mScale, mCenter;
|
60 |
|
|
private MeshCubes[][][] mCubes;
|
61 |
|
|
private DistortedEffects[][][] mEffects;
|
62 |
|
|
|
63 |
|
|
Static4D mQuat1, mQuat2;
|
64 |
|
|
int mScreenMin;
|
65 |
|
|
|
66 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67 |
|
|
|
68 |
|
|
RubikRenderer(GLSurfaceView v)
|
69 |
|
|
{
|
70 |
|
|
mView = v;
|
71 |
|
|
|
72 |
|
|
mScreen = new DistortedScreen();
|
73 |
|
|
|
74 |
|
|
mQuat1 = new Static4D( 0, 0, 0, 1); // unity quaternion
|
75 |
|
|
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); // something semi-random that looks good
|
76 |
|
|
|
77 |
|
|
mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
78 |
|
|
mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
79 |
|
|
Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
|
80 |
|
|
|
81 |
|
|
mMove = new Static3D(0,0,0);
|
82 |
|
|
mScale = new Static3D(1,1,1);
|
83 |
|
|
mCenter= new Static3D(0,0,0);
|
84 |
|
|
|
85 |
|
|
MatrixEffectMove move = new MatrixEffectMove(mMove);
|
86 |
|
|
MatrixEffectScale scale = new MatrixEffectScale(mScale);
|
87 |
|
|
MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, mCenter);
|
88 |
|
|
MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, mCenter);
|
89 |
|
|
|
90 |
|
|
for(int x=0; x<CUBE_SIZE; x++)
|
91 |
|
|
for(int y=0; y<CUBE_SIZE; y++)
|
92 |
|
|
for(int z=0; z<CUBE_SIZE; z++)
|
93 |
|
|
{
|
94 |
|
|
mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES);
|
95 |
|
|
|
96 |
|
|
cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
|
97 |
|
|
|
98 |
|
|
mEffects[x][y][z] = new DistortedEffects();
|
99 |
|
|
|
100 |
|
|
mEffects[x][y][z].apply(move);
|
101 |
|
|
mEffects[x][y][z].apply(scale);
|
102 |
|
|
mEffects[x][y][z].apply(quat1);
|
103 |
|
|
mEffects[x][y][z].apply(quat2);
|
104 |
|
|
mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109 |
|
|
|
110 |
|
|
public void onDrawFrame(GL10 glUnused)
|
111 |
|
|
{
|
112 |
|
|
mScreen.render( System.currentTimeMillis() );
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116 |
|
|
|
117 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
118 |
|
|
{
|
119 |
|
|
mScreenMin = width<height ? width:height;
|
120 |
|
|
|
121 |
|
|
float w = mTexture.getWidth();
|
122 |
|
|
float h = mTexture.getHeight();
|
123 |
|
|
float d = mTexture.getDepth(mCubes[0][0][0]);
|
124 |
|
|
|
125 |
|
|
float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
|
126 |
|
|
|
127 |
|
|
mCenter.set(w/2,h/2,d/2);
|
128 |
|
|
mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , -factor*d/2 );
|
129 |
|
|
mScale.set(factor,factor,factor);
|
130 |
|
|
|
131 |
|
|
mScreen.resize(width, height);
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
|
|
|
136 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
137 |
|
|
{
|
138 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
139 |
|
|
Bitmap bitmap;
|
140 |
|
|
|
141 |
|
|
try
|
142 |
|
|
{
|
143 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
144 |
|
|
}
|
145 |
|
|
finally
|
146 |
|
|
{
|
147 |
|
|
try
|
148 |
|
|
{
|
149 |
|
|
is.close();
|
150 |
|
|
}
|
151 |
|
|
catch(IOException e) { }
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
if( mTexture==null ) mTexture = new DistortedTexture(SIZE,SIZE);
|
155 |
|
|
mTexture.setTexture(bitmap);
|
156 |
|
|
|
157 |
|
|
mScreen.detachAll();
|
158 |
|
|
|
159 |
|
|
for(int x=0; x<CUBE_SIZE; x++)
|
160 |
|
|
for(int y=0; y<CUBE_SIZE; y++)
|
161 |
|
|
for(int z=0; z<CUBE_SIZE; z++)
|
162 |
|
|
mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
|
163 |
|
|
|
164 |
|
|
try
|
165 |
|
|
{
|
166 |
|
|
Distorted.onCreate(mView.getContext());
|
167 |
|
|
}
|
168 |
|
|
catch(Exception ex)
|
169 |
|
|
{
|
170 |
|
|
android.util.Log.e("Rubik", ex.getMessage() );
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
}
|