1 |
5b4a2d76
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
8647eae2
|
Leszek Koltunski
|
// Copyright 2019 Leszek Koltunski //
|
3 |
5b4a2d76
|
Leszek Koltunski
|
// //
|
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.opengl.GLSurfaceView;
|
23 |
|
|
|
24 |
94cc96ff
|
Leszek Koltunski
|
import org.distorted.library.effect.VertexEffectSink;
|
25 |
5b4a2d76
|
Leszek Koltunski
|
import org.distorted.library.main.Distorted;
|
26 |
|
|
import org.distorted.library.main.DistortedScreen;
|
27 |
|
|
import org.distorted.library.type.Static3D;
|
28 |
|
|
import org.distorted.library.type.Static4D;
|
29 |
|
|
|
30 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
31 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
32 |
|
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34 |
|
|
|
35 |
|
|
class RubikRenderer implements GLSurfaceView.Renderer
|
36 |
|
|
{
|
37 |
7f986357
|
Leszek Koltunski
|
private static final int NUM_CUBES = 4;
|
38 |
e4c0057e
|
Leszek Koltunski
|
private static final float CUBE_SCREEN_RATIO = 0.5f;
|
39 |
|
|
|
40 |
18709ae0
|
Leszek Koltunski
|
private RubikSurfaceView mView;
|
41 |
5b4a2d76
|
Leszek Koltunski
|
private DistortedScreen mScreen;
|
42 |
8647eae2
|
Leszek Koltunski
|
private Static3D mMove, mScale;
|
43 |
94cc96ff
|
Leszek Koltunski
|
private Static4D mQuatCurrent, mQuatAccumulated;
|
44 |
|
|
private Static4D mTempCurrent, mTempAccumulated;
|
45 |
8647eae2
|
Leszek Koltunski
|
private float mScaleFactor;
|
46 |
|
|
private boolean mFinishRotation, mFinishDragCurrent, mFinishDragAccumulated;
|
47 |
|
|
private RubikCube mCube;
|
48 |
5b4a2d76
|
Leszek Koltunski
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
|
|
|
51 |
18709ae0
|
Leszek Koltunski
|
RubikRenderer(RubikSurfaceView v)
|
52 |
5b4a2d76
|
Leszek Koltunski
|
{
|
53 |
|
|
mView = v;
|
54 |
|
|
|
55 |
|
|
mScreen = new DistortedScreen();
|
56 |
|
|
|
57 |
94cc96ff
|
Leszek Koltunski
|
mTempCurrent = new Static4D(0,0,0,1);
|
58 |
|
|
mTempAccumulated = initializeQuat();
|
59 |
|
|
mQuatCurrent = new Static4D(0,0,0,1);
|
60 |
|
|
mQuatAccumulated = initializeQuat();
|
61 |
5b4a2d76
|
Leszek Koltunski
|
|
62 |
|
|
mMove = new Static3D(0,0,0);
|
63 |
|
|
mScale = new Static3D(1,1,1);
|
64 |
8647eae2
|
Leszek Koltunski
|
|
65 |
|
|
mFinishRotation = false;
|
66 |
|
|
mFinishDragCurrent = false;
|
67 |
|
|
mFinishDragAccumulated = false;
|
68 |
|
|
|
69 |
|
|
mCube = new RubikCube(NUM_CUBES, mMove, mScale, mQuatCurrent, mQuatAccumulated);
|
70 |
5b4a2d76
|
Leszek Koltunski
|
}
|
71 |
|
|
|
72 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73 |
|
|
|
74 |
|
|
public void onDrawFrame(GL10 glUnused)
|
75 |
|
|
{
|
76 |
|
|
mScreen.render( System.currentTimeMillis() );
|
77 |
94cc96ff
|
Leszek Koltunski
|
|
78 |
8647eae2
|
Leszek Koltunski
|
if( mFinishDragCurrent )
|
79 |
|
|
{
|
80 |
|
|
mFinishDragCurrent = false;
|
81 |
|
|
mQuatCurrent.set(mTempCurrent);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
if( mFinishDragAccumulated )
|
85 |
|
|
{
|
86 |
|
|
mFinishDragAccumulated = false;
|
87 |
|
|
mQuatAccumulated.set(mTempAccumulated);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
if( mFinishRotation )
|
91 |
|
|
{
|
92 |
|
|
mFinishRotation=false;
|
93 |
|
|
mCube.finishRotationCalledOnNextRender();
|
94 |
|
|
}
|
95 |
5b4a2d76
|
Leszek Koltunski
|
}
|
96 |
|
|
|
97 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98 |
|
|
|
99 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
100 |
|
|
{
|
101 |
7f986357
|
Leszek Koltunski
|
float fovInDegrees = (width>height ? 60.0f : 90.0f);
|
102 |
|
|
|
103 |
|
|
mScreen.setProjection( fovInDegrees, 0.1f);
|
104 |
8647eae2
|
Leszek Koltunski
|
|
105 |
18709ae0
|
Leszek Koltunski
|
mView.setScreenSize(width,height);
|
106 |
7f986357
|
Leszek Koltunski
|
mView.recomputeCameraDistance(fovInDegrees*Math.PI/180);
|
107 |
5b4a2d76
|
Leszek Koltunski
|
|
108 |
8647eae2
|
Leszek Koltunski
|
float w = mCube.getWidth();
|
109 |
|
|
float h = mCube.getHeight();
|
110 |
|
|
float d = mCube.getDepth();
|
111 |
5b4a2d76
|
Leszek Koltunski
|
|
112 |
dc8979ba
|
Leszek Koltunski
|
mScaleFactor = CUBE_SCREEN_RATIO*(width>height ? height:width)/mCube.getSizeInModelSpace();
|
113 |
5b4a2d76
|
Leszek Koltunski
|
|
114 |
8647eae2
|
Leszek Koltunski
|
mMove.set( (width-mScaleFactor*w)/2 , (height-mScaleFactor*h)/2 , -mScaleFactor*d/2 );
|
115 |
|
|
mScale.set(mScaleFactor,mScaleFactor,mScaleFactor);
|
116 |
5b4a2d76
|
Leszek Koltunski
|
|
117 |
|
|
mScreen.resize(width, height);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121 |
|
|
|
122 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
123 |
|
|
{
|
124 |
8647eae2
|
Leszek Koltunski
|
mCube.createTexture();
|
125 |
5b4a2d76
|
Leszek Koltunski
|
mScreen.detachAll();
|
126 |
8647eae2
|
Leszek Koltunski
|
mCube.attachToScreen(mScreen);
|
127 |
5b4a2d76
|
Leszek Koltunski
|
|
128 |
94cc96ff
|
Leszek Koltunski
|
VertexEffectSink.enable();
|
129 |
e4c0057e
|
Leszek Koltunski
|
|
130 |
5b4a2d76
|
Leszek Koltunski
|
try
|
131 |
|
|
{
|
132 |
|
|
Distorted.onCreate(mView.getContext());
|
133 |
|
|
}
|
134 |
|
|
catch(Exception ex)
|
135 |
|
|
{
|
136 |
|
|
android.util.Log.e("Rubik", ex.getMessage() );
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
e4c0057e
|
Leszek Koltunski
|
|
140 |
8647eae2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141 |
|
|
// no this will not race with onDrawFrame
|
142 |
|
|
|
143 |
|
|
void finishRotation()
|
144 |
|
|
{
|
145 |
|
|
mFinishRotation = true;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
e4c0057e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
|
|
150 |
dc8979ba
|
Leszek Koltunski
|
float returnCubeSizeInScreenSpace()
|
151 |
e4c0057e
|
Leszek Koltunski
|
{
|
152 |
7f986357
|
Leszek Koltunski
|
return mScaleFactor*mCube.getSizeInModelSpace();
|
153 |
e4c0057e
|
Leszek Koltunski
|
}
|
154 |
|
|
|
155 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156 |
|
|
|
157 |
8647eae2
|
Leszek Koltunski
|
RubikCube getCube()
|
158 |
e4c0057e
|
Leszek Koltunski
|
{
|
159 |
8647eae2
|
Leszek Koltunski
|
return mCube;
|
160 |
e4c0057e
|
Leszek Koltunski
|
}
|
161 |
|
|
|
162 |
94cc96ff
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163 |
|
|
// Initial rotation of the cube. Something semi-random that looks good.
|
164 |
|
|
|
165 |
|
|
Static4D initializeQuat()
|
166 |
|
|
{
|
167 |
7f62afde
|
Leszek Koltunski
|
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
168 |
94cc96ff
|
Leszek Koltunski
|
}
|
169 |
|
|
|
170 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171 |
|
|
|
172 |
|
|
void setQuatCurrent(Static4D current)
|
173 |
|
|
{
|
174 |
|
|
mTempCurrent.set(current);
|
175 |
8647eae2
|
Leszek Koltunski
|
mFinishDragCurrent = true;
|
176 |
94cc96ff
|
Leszek Koltunski
|
}
|
177 |
|
|
|
178 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179 |
|
|
|
180 |
|
|
void setQuatAccumulated(Static4D accumulated)
|
181 |
|
|
{
|
182 |
|
|
mTempAccumulated.set(accumulated);
|
183 |
8647eae2
|
Leszek Koltunski
|
mFinishDragAccumulated = true;
|
184 |
94cc96ff
|
Leszek Koltunski
|
}
|
185 |
5b4a2d76
|
Leszek Koltunski
|
}
|