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 |
b62eb334
|
Leszek Koltunski
|
import org.distorted.library.message.EffectListener;
|
28 |
|
|
import org.distorted.library.message.EffectMessage;
|
29 |
5b4a2d76
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
30 |
|
|
import org.distorted.library.type.Static4D;
|
31 |
|
|
|
32 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
33 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
34 |
|
|
|
35 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
|
37 |
b62eb334
|
Leszek Koltunski
|
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
|
38 |
5b4a2d76
|
Leszek Koltunski
|
{
|
39 |
e4c0057e
|
Leszek Koltunski
|
private static final float CUBE_SCREEN_RATIO = 0.5f;
|
40 |
8a40abf4
|
Leszek Koltunski
|
private static final float CAMERA_DISTANCE = 0.6f; // 0.6 of the length of max(scrHeight,scrWidth)
|
41 |
e4c0057e
|
Leszek Koltunski
|
|
42 |
18709ae0
|
Leszek Koltunski
|
private RubikSurfaceView mView;
|
43 |
5b4a2d76
|
Leszek Koltunski
|
private DistortedScreen mScreen;
|
44 |
8647eae2
|
Leszek Koltunski
|
private Static3D mMove, mScale;
|
45 |
94cc96ff
|
Leszek Koltunski
|
private Static4D mQuatCurrent, mQuatAccumulated;
|
46 |
|
|
private Static4D mTempCurrent, mTempAccumulated;
|
47 |
483ae94e
|
Leszek Koltunski
|
private float mCubeSizeInScreenSpace;
|
48 |
b62eb334
|
Leszek Koltunski
|
private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
|
49 |
d4374cd3
|
Leszek Koltunski
|
private boolean mCanRotate;
|
50 |
8647eae2
|
Leszek Koltunski
|
private RubikCube mCube;
|
51 |
5b4a2d76
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
|
54 |
18709ae0
|
Leszek Koltunski
|
RubikRenderer(RubikSurfaceView v)
|
55 |
5b4a2d76
|
Leszek Koltunski
|
{
|
56 |
|
|
mView = v;
|
57 |
|
|
|
58 |
|
|
mScreen = new DistortedScreen();
|
59 |
|
|
|
60 |
94cc96ff
|
Leszek Koltunski
|
mTempCurrent = new Static4D(0,0,0,1);
|
61 |
|
|
mTempAccumulated = initializeQuat();
|
62 |
|
|
mQuatCurrent = new Static4D(0,0,0,1);
|
63 |
|
|
mQuatAccumulated = initializeQuat();
|
64 |
5b4a2d76
|
Leszek Koltunski
|
|
65 |
|
|
mMove = new Static3D(0,0,0);
|
66 |
|
|
mScale = new Static3D(1,1,1);
|
67 |
8647eae2
|
Leszek Koltunski
|
|
68 |
|
|
mFinishRotation = false;
|
69 |
b62eb334
|
Leszek Koltunski
|
mRemoveRotation = false;
|
70 |
8647eae2
|
Leszek Koltunski
|
mFinishDragCurrent = false;
|
71 |
|
|
mFinishDragAccumulated = false;
|
72 |
|
|
|
73 |
d4374cd3
|
Leszek Koltunski
|
mCanRotate = true;
|
74 |
|
|
|
75 |
5d8a1d51
|
Leszek Koltunski
|
mCube = new RubikCube( RubikActivity.DEFAULT_CUBE_SIZE, mMove, mScale, mQuatCurrent, mQuatAccumulated);
|
76 |
5b4a2d76
|
Leszek Koltunski
|
}
|
77 |
|
|
|
78 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79 |
b62eb334
|
Leszek Koltunski
|
// various things are done here delayed, 'after the next render' as not to be done mid-render and
|
80 |
|
|
// cause artifacts.
|
81 |
|
|
|
82 |
5b4a2d76
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
83 |
|
|
{
|
84 |
|
|
mScreen.render( System.currentTimeMillis() );
|
85 |
94cc96ff
|
Leszek Koltunski
|
|
86 |
8647eae2
|
Leszek Koltunski
|
if( mFinishDragCurrent )
|
87 |
|
|
{
|
88 |
|
|
mFinishDragCurrent = false;
|
89 |
|
|
mQuatCurrent.set(mTempCurrent);
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
if( mFinishDragAccumulated )
|
93 |
|
|
{
|
94 |
|
|
mFinishDragAccumulated = false;
|
95 |
|
|
mQuatAccumulated.set(mTempAccumulated);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
if( mFinishRotation )
|
99 |
|
|
{
|
100 |
d4374cd3
|
Leszek Koltunski
|
mCanRotate = false;
|
101 |
8647eae2
|
Leszek Koltunski
|
mFinishRotation=false;
|
102 |
b62eb334
|
Leszek Koltunski
|
mCube.finishRotationCalledOnNextRender(this);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
if( mRemoveRotation )
|
106 |
|
|
{
|
107 |
|
|
mRemoveRotation=false;
|
108 |
|
|
mCube.removeRotationCalledOnNextRender(this);
|
109 |
d4374cd3
|
Leszek Koltunski
|
mCanRotate = true;
|
110 |
8647eae2
|
Leszek Koltunski
|
}
|
111 |
5b4a2d76
|
Leszek Koltunski
|
}
|
112 |
|
|
|
113 |
b62eb334
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114 |
|
|
// EffectListener. The library sends a message to us when it's time to call 'removeRotation'
|
115 |
|
|
|
116 |
|
|
public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
|
117 |
|
|
{
|
118 |
|
|
switch(em)
|
119 |
|
|
{
|
120 |
|
|
case EFFECT_FINISHED: mRemoveRotation = true; break;
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
5b4a2d76
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
|
126 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
127 |
|
|
{
|
128 |
483ae94e
|
Leszek Koltunski
|
float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
|
129 |
|
|
float fovInDegrees = computeFOV(cameraDistance,height);
|
130 |
7f986357
|
Leszek Koltunski
|
|
131 |
|
|
mScreen.setProjection( fovInDegrees, 0.1f);
|
132 |
18709ae0
|
Leszek Koltunski
|
mView.setScreenSize(width,height);
|
133 |
483ae94e
|
Leszek Koltunski
|
mView.setCameraDist(cameraDistance);
|
134 |
5b4a2d76
|
Leszek Koltunski
|
|
135 |
483ae94e
|
Leszek Koltunski
|
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(width>height ? height:width);
|
136 |
|
|
float texSize = mCube.getTextureSize();
|
137 |
|
|
float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
|
138 |
5b4a2d76
|
Leszek Koltunski
|
|
139 |
483ae94e
|
Leszek Koltunski
|
mMove.set( (width-scaleFactor*texSize)/2 , (height-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
|
140 |
|
|
mScale.set(scaleFactor,scaleFactor,scaleFactor);
|
141 |
5b4a2d76
|
Leszek Koltunski
|
|
142 |
|
|
mScreen.resize(width, height);
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146 |
|
|
|
147 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
148 |
|
|
{
|
149 |
8647eae2
|
Leszek Koltunski
|
mCube.createTexture();
|
150 |
5b4a2d76
|
Leszek Koltunski
|
mScreen.detachAll();
|
151 |
8647eae2
|
Leszek Koltunski
|
mCube.attachToScreen(mScreen);
|
152 |
5b4a2d76
|
Leszek Koltunski
|
|
153 |
94cc96ff
|
Leszek Koltunski
|
VertexEffectSink.enable();
|
154 |
e4c0057e
|
Leszek Koltunski
|
|
155 |
5b4a2d76
|
Leszek Koltunski
|
try
|
156 |
|
|
{
|
157 |
|
|
Distorted.onCreate(mView.getContext());
|
158 |
|
|
}
|
159 |
|
|
catch(Exception ex)
|
160 |
|
|
{
|
161 |
|
|
android.util.Log.e("Rubik", ex.getMessage() );
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
e4c0057e
|
Leszek Koltunski
|
|
165 |
483ae94e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166 |
|
|
|
167 |
|
|
private float computeFOV(float cameraDistance, int screenHeight)
|
168 |
|
|
{
|
169 |
|
|
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
|
170 |
b62eb334
|
Leszek Koltunski
|
return (float)(2*halfFOVInRadians*(180/Math.PI));
|
171 |
483ae94e
|
Leszek Koltunski
|
}
|
172 |
|
|
|
173 |
8647eae2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174 |
|
|
// no this will not race with onDrawFrame
|
175 |
|
|
|
176 |
|
|
void finishRotation()
|
177 |
|
|
{
|
178 |
|
|
mFinishRotation = true;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
e4c0057e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
|
|
|
183 |
dc8979ba
|
Leszek Koltunski
|
float returnCubeSizeInScreenSpace()
|
184 |
e4c0057e
|
Leszek Koltunski
|
{
|
185 |
483ae94e
|
Leszek Koltunski
|
return mCubeSizeInScreenSpace;
|
186 |
e4c0057e
|
Leszek Koltunski
|
}
|
187 |
|
|
|
188 |
d4374cd3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189 |
|
|
|
190 |
|
|
boolean canRotate()
|
191 |
|
|
{
|
192 |
|
|
return mCanRotate;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
e4c0057e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196 |
|
|
|
197 |
8647eae2
|
Leszek Koltunski
|
RubikCube getCube()
|
198 |
e4c0057e
|
Leszek Koltunski
|
{
|
199 |
8647eae2
|
Leszek Koltunski
|
return mCube;
|
200 |
e4c0057e
|
Leszek Koltunski
|
}
|
201 |
|
|
|
202 |
94cc96ff
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203 |
|
|
// Initial rotation of the cube. Something semi-random that looks good.
|
204 |
|
|
|
205 |
|
|
Static4D initializeQuat()
|
206 |
|
|
{
|
207 |
7f62afde
|
Leszek Koltunski
|
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
208 |
94cc96ff
|
Leszek Koltunski
|
}
|
209 |
|
|
|
210 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211 |
|
|
|
212 |
|
|
void setQuatCurrent(Static4D current)
|
213 |
|
|
{
|
214 |
|
|
mTempCurrent.set(current);
|
215 |
8647eae2
|
Leszek Koltunski
|
mFinishDragCurrent = true;
|
216 |
94cc96ff
|
Leszek Koltunski
|
}
|
217 |
|
|
|
218 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219 |
|
|
|
220 |
|
|
void setQuatAccumulated(Static4D accumulated)
|
221 |
|
|
{
|
222 |
|
|
mTempAccumulated.set(accumulated);
|
223 |
8647eae2
|
Leszek Koltunski
|
mFinishDragAccumulated = true;
|
224 |
94cc96ff
|
Leszek Koltunski
|
}
|
225 |
5b4a2d76
|
Leszek Koltunski
|
}
|