Revision 94cc96ff
Added by Leszek Koltunski over 6 years ago
| src/main/java/org/distorted/examples/rubik/RubikRenderer.java | ||
|---|---|---|
| 29 | 29 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
| 30 | 30 |
import org.distorted.library.effect.MatrixEffectScale; |
| 31 | 31 |
import org.distorted.library.effect.PostprocessEffectGlow; |
| 32 |
import org.distorted.library.effect.VertexEffectSink; |
|
| 32 | 33 |
import org.distorted.library.main.Distorted; |
| 33 | 34 |
import org.distorted.library.main.DistortedEffects; |
| 34 | 35 |
import org.distorted.library.main.DistortedScreen; |
| ... | ... | |
| 45 | 46 |
|
| 46 | 47 |
class RubikRenderer implements GLSurfaceView.Renderer |
| 47 | 48 |
{
|
| 48 |
static final int NUM_CUBES = 3;
|
|
| 49 |
private static final int VERTICES = 5;
|
|
| 49 |
static final int NUM_CUBES = 6;
|
|
| 50 |
private static final int VERTICES = 10;
|
|
| 50 | 51 |
private static final int SIZE = 200; |
| 51 | 52 |
|
| 52 | 53 |
private static final float CUBE_SCREEN_RATIO = 0.5f; |
| ... | ... | |
| 64 | 65 |
private Static1D mGlowRadius; |
| 65 | 66 |
private Static4D mGlowColor; |
| 66 | 67 |
|
| 67 |
Static4D mQuatCurrent, mQuatAccumulated; |
|
| 68 |
private Static4D mQuatCurrent, mQuatAccumulated; |
|
| 69 |
private Static4D mTempCurrent, mTempAccumulated; |
|
| 68 | 70 |
int mScreenMin; |
| 69 | 71 |
|
| 70 | 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 76 | 78 |
mScreen = new DistortedScreen(); |
| 77 | 79 |
mScreen.setProjection(90.0f, 0.1f); |
| 78 | 80 |
|
| 79 |
mQuatCurrent = new Static4D( 0, 0, 0, 1); // unity quaternion |
|
| 80 |
mQuatAccumulated = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); // something semi-random that looks good |
|
| 81 |
mTempCurrent = new Static4D(0,0,0,1); |
|
| 82 |
mTempAccumulated = initializeQuat(); |
|
| 83 |
mQuatCurrent = new Static4D(0,0,0,1); |
|
| 84 |
mQuatAccumulated = initializeQuat(); |
|
| 81 | 85 |
|
| 82 | 86 |
mCubes = new MeshCubes[NUM_CUBES][NUM_CUBES][NUM_CUBES]; |
| 83 | 87 |
mEffects = new DistortedEffects[NUM_CUBES][NUM_CUBES][NUM_CUBES]; |
| 84 | 88 |
Static3D[][][] cubeVectors = new Static3D[NUM_CUBES][NUM_CUBES][NUM_CUBES]; |
| 85 | 89 |
|
| 90 |
VertexEffectSink sink = new VertexEffectSink( new Static1D(3.0f), |
|
| 91 |
new Static3D(SIZE*0.5f, SIZE*0.5f, SIZE*0.5f), |
|
| 92 |
new Static4D(0,0,0, SIZE*0.72f) ); |
|
| 86 | 93 |
mMove = new Static3D(0,0,0); |
| 87 | 94 |
mScale = new Static3D(1,1,1); |
| 88 | 95 |
mCenter= new Static3D(0,0,0); |
| ... | ... | |
| 123 | 130 |
for(int y = 0; y< NUM_CUBES; y++) |
| 124 | 131 |
for(int z = 0; z< NUM_CUBES; z++) |
| 125 | 132 |
{
|
| 126 |
tmpLeft = (x== 0 ? mapLeft :mapBlack); |
|
| 133 |
tmpLeft = (x== 0 ? mapLeft :mapBlack);
|
|
| 127 | 134 |
tmpRight = (x== NUM_CUBES -1 ? mapRight :mapBlack); |
| 128 | 135 |
tmpFront = (z== NUM_CUBES -1 ? mapFront :mapBlack); |
| 129 |
tmpBack = (z== 0 ? mapBack :mapBlack); |
|
| 136 |
tmpBack = (z== 0 ? mapBack :mapBlack);
|
|
| 130 | 137 |
tmpTop = (y== NUM_CUBES -1 ? mapTop :mapBlack); |
| 131 |
tmpBottom= (y== 0 ? mapBottom:mapBlack); |
|
| 138 |
tmpBottom= (y== 0 ? mapBottom:mapBlack);
|
|
| 132 | 139 |
|
| 133 | 140 |
mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom); |
| 134 | 141 |
|
| ... | ... | |
| 136 | 143 |
|
| 137 | 144 |
mEffects[x][y][z] = new DistortedEffects(); |
| 138 | 145 |
|
| 146 |
mEffects[x][y][z].apply(sink); |
|
| 139 | 147 |
mEffects[x][y][z].apply(move); |
| 140 | 148 |
mEffects[x][y][z].apply(scale); |
| 141 | 149 |
mEffects[x][y][z].apply(quat1); |
| ... | ... | |
| 149 | 157 |
public void onDrawFrame(GL10 glUnused) |
| 150 | 158 |
{
|
| 151 | 159 |
mScreen.render( System.currentTimeMillis() ); |
| 160 |
|
|
| 161 |
mQuatCurrent.set(mTempCurrent); |
|
| 162 |
mQuatAccumulated.set(mTempAccumulated); |
|
| 152 | 163 |
} |
| 153 | 164 |
|
| 154 | 165 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 224 | 235 |
mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]); |
| 225 | 236 |
|
| 226 | 237 |
PostprocessEffectGlow.enable(); |
| 238 |
VertexEffectSink.enable(); |
|
| 227 | 239 |
|
| 228 | 240 |
try |
| 229 | 241 |
{
|
| ... | ... | |
| 290 | 302 |
|
| 291 | 303 |
mEffects[mLastCol][mLastRow][mLastSli].apply(new PostprocessEffectGlow(mGlowRadius,mGlowColor)); |
| 292 | 304 |
} |
| 305 |
|
|
| 306 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 307 |
// Initial rotation of the cube. Something semi-random that looks good. |
|
| 308 |
|
|
| 309 |
Static4D initializeQuat() |
|
| 310 |
{
|
|
| 311 |
return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f); |
|
| 312 |
} |
|
| 313 |
|
|
| 314 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 315 |
|
|
| 316 |
void setQuatCurrent(Static4D current) |
|
| 317 |
{
|
|
| 318 |
mTempCurrent.set(current); |
|
| 319 |
} |
|
| 320 |
|
|
| 321 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 322 |
|
|
| 323 |
void setQuatAccumulated(Static4D accumulated) |
|
| 324 |
{
|
|
| 325 |
mTempAccumulated.set(accumulated); |
|
| 326 |
} |
|
| 293 | 327 |
} |
| src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java | ||
|---|---|---|
| 31 | 31 |
|
| 32 | 32 |
class RubikSurfaceView extends GLSurfaceView |
| 33 | 33 |
{
|
| 34 |
private final static int ERROR =-1;
|
|
| 34 |
private final static int NONE =-1;
|
|
| 35 | 35 |
private final static int FRONT = 0; |
| 36 | 36 |
private final static int BACK = 1; |
| 37 | 37 |
private final static int LEFT = 2; |
| ... | ... | |
| 42 | 42 |
private boolean mDragging; |
| 43 | 43 |
private int mX, mY; |
| 44 | 44 |
private int mTouchedRow, mTouchedCol, mTouchedSli; |
| 45 |
private Static4D mQuatCurrent, mQuatAccumulated; |
|
| 45 | 46 |
private RubikRenderer mRenderer; |
| 46 | 47 |
|
| 47 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 52 | 53 |
|
| 53 | 54 |
mDragging = false; |
| 54 | 55 |
mRenderer = new RubikRenderer(this); |
| 56 |
|
|
| 57 |
mQuatCurrent = new Static4D(0,0,0,1); |
|
| 58 |
mQuatAccumulated = mRenderer.initializeQuat(); |
|
| 59 |
|
|
| 55 | 60 |
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 56 | 61 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
| 57 | 62 |
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 ); |
| ... | ... | |
| 76 | 81 |
|
| 77 | 82 |
switch(action) |
| 78 | 83 |
{
|
| 79 |
case MotionEvent.ACTION_DOWN: if( faceTouched(x,y) != ERROR )
|
|
| 84 |
case MotionEvent.ACTION_DOWN: if( faceTouched(x,y) != NONE )
|
|
| 80 | 85 |
{
|
| 81 | 86 |
mRenderer.abortLastEffect(); |
| 82 | 87 |
mRenderer.applyNewEffect(mTouchedCol, mTouchedRow, mTouchedSli); |
| ... | ... | |
| 88 | 93 |
mDragging = true; |
| 89 | 94 |
} |
| 90 | 95 |
break; |
| 91 |
case MotionEvent.ACTION_MOVE: if( mDragging ) mRenderer.mQuatCurrent.set(quatFromDrag(mX-x,mY-y)); |
|
| 96 |
case MotionEvent.ACTION_MOVE: if( mDragging ) |
|
| 97 |
{
|
|
| 98 |
mQuatCurrent.set(quatFromDrag(mX-x,mY-y)); |
|
| 99 |
mRenderer.setQuatCurrent(mQuatCurrent); |
|
| 100 |
} |
|
| 92 | 101 |
break; |
| 93 | 102 |
case MotionEvent.ACTION_UP : mDragging = false; |
| 94 |
mRenderer.mQuatAccumulated.set(quatMultiply(mRenderer.mQuatCurrent, mRenderer.mQuatAccumulated)); |
|
| 95 |
mRenderer.mQuatCurrent.set(0f, 0f, 0f, 1f); |
|
| 103 |
mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated)); |
|
| 104 |
mQuatCurrent.set(0f, 0f, 0f, 1f); |
|
| 105 |
|
|
| 106 |
mRenderer.setQuatCurrent(mQuatCurrent); |
|
| 107 |
mRenderer.setQuatAccumulated(mQuatAccumulated); |
|
| 96 | 108 |
break; |
| 97 | 109 |
} |
| 98 | 110 |
|
| ... | ... | |
| 164 | 176 |
if( absY>absX && absY>absZ ) return rotatedY>0 ? TOP:BOTTOM; |
| 165 | 177 |
if( absZ>absX && absZ>absY ) return rotatedZ>0 ? FRONT:BACK; |
| 166 | 178 |
|
| 167 |
return ERROR;
|
|
| 179 |
return NONE;
|
|
| 168 | 180 |
} |
| 169 | 181 |
|
| 170 | 182 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 194 | 206 |
|
| 195 | 207 |
private Static4D rotateVector(Static4D vector) |
| 196 | 208 |
{
|
| 197 |
float qx = mRenderer.mQuatAccumulated.get1();
|
|
| 198 |
float qy = mRenderer.mQuatAccumulated.get2();
|
|
| 199 |
float qz = mRenderer.mQuatAccumulated.get3();
|
|
| 200 |
float qw = mRenderer.mQuatAccumulated.get4();
|
|
| 209 |
float qx = mQuatAccumulated.get1(); |
|
| 210 |
float qy = mQuatAccumulated.get2(); |
|
| 211 |
float qz = mQuatAccumulated.get3(); |
|
| 212 |
float qw = mQuatAccumulated.get4(); |
|
| 201 | 213 |
|
| 202 | 214 |
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw); |
| 203 | 215 |
Static4D tmp = quatMultiply(quatInverted,vector); |
| 204 | 216 |
|
| 205 |
return quatMultiply(tmp,mRenderer.mQuatAccumulated);
|
|
| 217 |
return quatMultiply(tmp,mQuatAccumulated); |
|
| 206 | 218 |
} |
| 207 | 219 |
|
| 208 | 220 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 347 | 359 |
mTouchedCol = -1; |
| 348 | 360 |
mTouchedSli = -1; |
| 349 | 361 |
|
| 350 |
return ERROR;
|
|
| 362 |
return NONE;
|
|
| 351 | 363 |
} |
| 352 | 364 |
} |
| 353 | 365 |
|
Also available in: Unified diff
Improve the Rubik App.