Project

General

Profile

« Previous | Next » 

Revision 411c6285

Added by Leszek Koltunski over 4 years ago

Make RubikCube more abstract.

View differences:

src/main/java/org/distorted/object/RubikCube.java
19 19

  
20 20
package org.distorted.object;
21 21

  
22
import android.graphics.Bitmap;
23 22
import android.graphics.Canvas;
24 23
import android.graphics.Paint;
25 24

  
25
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectRotate;
26 28
import org.distorted.library.main.DistortedEffects;
27 29
import org.distorted.library.main.DistortedTexture;
28 30
import org.distorted.library.mesh.MeshBase;
29
import org.distorted.library.mesh.MeshCubes;
31
import org.distorted.library.mesh.MeshJoined;
30 32
import org.distorted.library.mesh.MeshRectangles;
33
import org.distorted.library.type.Static1D;
31 34
import org.distorted.library.type.Static3D;
32 35
import org.distorted.library.type.Static4D;
33 36

  
......
35 38

  
36 39
class RubikCube extends RubikObject
37 40
{
41
  private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
42
  private static final float[] LEGAL = new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
43

  
44
  // axisXright (right-YELLOW) axisXleft (left-WHITE) axisYright (top-BLUE) axisYleft (bottom-GREEN) axisZright (front-RED) axisZleft (back-BROWN)
45
  private static final int[] FACE_COLORS   = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
46

  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

  
49
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
50
    {
51
    super(size,quatCur,quatAcc,texture,mesh,effects);
52
    }
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
38 56
  int[][] getCubitPositions(int size)
39 57
    {
40 58
    int[][] tmp = new int[getNumCubits(size)][3];
......
73 91

  
74 92
  float[] getLegalQuats()
75 93
    {
76
    final float SQ2 = 0.5f*((float)Math.sqrt(2));
77
    return new float[] { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
94
    return LEGAL;
78 95
    }
79 96

  
80 97
///////////////////////////////////////////////////////////////////////////////////////////////////
81 98

  
82
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
99
  int getNumFaces()
83 100
    {
84
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
85
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
86
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
101
    return FACE_COLORS.length;
102
    }
87 103

  
88
    return false;
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

  
106
  void createFaceTexture(Canvas canvas, Paint paint, int face)
107
    {
108
    final int S = TEXTURE_HEIGHT;
109
    final int R = TEXTURE_HEIGHT/10;
110
    final int M = TEXTURE_HEIGHT/20;
111

  
112
    paint.setColor(FACE_COLORS[face]);
113
    canvas.drawRoundRect( (face*S+M), M, (face*S+M) + (S-2*M), M + (S-2*M), R, R, paint);
89 114
    }
90 115

  
91 116
///////////////////////////////////////////////////////////////////////////////////////////////////
92 117

  
93
  RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
118
  MeshBase createCubitMesh(int vertices)
94 119
    {
95
    super(size,quatCur,quatAcc,texture,mesh,effects);
120
    final int MESHES=6;
121

  
122
    Static3D axisY  = new Static3D(0,1,0);
123
    Static3D axisX  = new Static3D(1,0,0);
124
    Static3D center = new Static3D(0,0,0);
125
    Static1D angle  = new Static1D(0);
126

  
127
    MatrixEffect[] effectsY = new MatrixEffect[2];
128
    effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
129
    effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
130

  
131
    MeshBase[] meshes = new MeshRectangles[MESHES];
132
    for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(vertices,vertices);
133

  
134
    angle.set(0);
135
    meshes[4].apply(effectsY);  // front
136
    angle.set(90);
137
    meshes[0].apply(effectsY);  // right
138
    angle.set(180);
139
    meshes[5].apply(effectsY);  // back
140
    angle.set(270);
141
    meshes[1].apply(effectsY);  // left
142

  
143
    MatrixEffect[] effectsX = new MatrixEffect[2];
144
    effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
145
    effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
146

  
147
    angle.set( 90);
148
    meshes[3].apply(effectsX);  // bottom
149
    angle.set(-90);
150
    meshes[2].apply(effectsX);  // top
151

  
152
    return new MeshJoined(meshes);
96 153
    }
97 154

  
98 155
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// PUBLIC API
100 156

  
101
  public void createTexture()
157
  boolean belongsToRotation( Static3D currentPosition, Static3D axis, int row)
102 158
    {
103
    Bitmap bitmap;
104

  
105
    final int S = 128;
106
    final int W = 3*S;
107
    final int H = 2*S;
108
    final int R = S/10;
109
    final int M = S/20;
110

  
111
    Paint paint = new Paint();
112
    bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
113
    Canvas canvas = new Canvas(bitmap);
114

  
115
    paint.setAntiAlias(true);
116
    paint.setTextAlign(Paint.Align.CENTER);
117
    paint.setStyle(Paint.Style.FILL);
118

  
119
    // 3x2 bitmap = 6 squares:
120
    //
121
    // RED     GREEN   BLUE
122
    // YELLOW  WHITE   BROWN
123

  
124
    paint.setColor(0xff000000);                                  // BLACK BACKGROUND
125
    canvas.drawRect(0, 0, W, H, paint);                          //
126

  
127
    paint.setColor(0xffff0000);                                  // RED
128
    canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
129
    paint.setColor(0xff00ff00);                                  // GREEN
130
    canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
131
    paint.setColor(0xff0000ff);                                  // BLUE
132
    canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
133
    paint.setColor(0xffffff00);                                  // YELLOW
134
    canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
135
    paint.setColor(0xffffffff);                                  // WHITE
136
    canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
137
    paint.setColor(0xffb5651d);                                  // BROWN
138
    canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
139

  
140
    mTexture.setTexture(bitmap);
159
    if( axis.get0()!=0 ) return currentPosition.get0()==row;
160
    if( axis.get1()!=0 ) return currentPosition.get1()==row;
161
    if( axis.get2()!=0 ) return currentPosition.get2()==row;
162

  
163
    return false;
141 164
    }
142 165

  
143 166
///////////////////////////////////////////////////////////////////////////////////////////////////
......
146 169

  
147 170
  static
148 171
    {
149
    // 3x2 bitmap = 6 squares:
150
    //
151
    // RED     GREEN   BLUE
152
    // YELLOW  WHITE   BROWN
153

  
154
    final float ze = 0.0f;
155
    final float ot = 1.0f/3.0f;
156
    final float tt = 2.0f/3.0f;
157
    final float oh = 1.0f/2.0f;
158
    final float of = 1.0f/40.0f;
159

  
160
    mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
161
    mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
162
    mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
163
    mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
164
    mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
165
    mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
166

  
167
    mapBlack = new Static4D(ze,ze, ze+of,ze+of);
172
    // axisXright (right-YELLOW) axisXleft (left-WHITE) axisYright (top-BLUE) axisYleft (bottom-GREEN) axisZright (front-RED) axisZleft (back-BROWN)
173

  
174
    mapRight = new Static4D( 0*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
175
    mapLeft  = new Static4D( 1*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
176
    mapTop   = new Static4D( 2*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
177
    mapBottom= new Static4D( 3*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
178
    mapFront = new Static4D( 4*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
179
    mapBack  = new Static4D( 5*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
180
    mapBlack = new Static4D( 6*(1/7.0f), 0.0f, 1/7.0f, 1.0f);
168 181
    }
169 182

  
170 183
///////////////////////////////////////////////////////////////////////////////////////////////////
171 184

  
172
  MeshBase createCubitMesh(int vertices, int x, int y, int z)
185
  void textureCubitMesh(MeshBase mesh, int x, int y, int z)
173 186
    {
174
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
175 187
    Static4D tmpRight = (x== mSize-1 ? mapRight :mapBlack);
176
    Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
177
    Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
188
    Static4D tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
178 189
    Static4D tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
179 190
    Static4D tmpBottom= (y==       0 ? mapBottom:mapBlack);
191
    Static4D tmpFront = (z== mSize-1 ? mapFront :mapBlack);
192
    Static4D tmpBack  = (z==       0 ? mapBack  :mapBlack);
193

  
194
    Static4D[] maps = new Static4D[] { tmpRight, tmpLeft, tmpTop, tmpBottom, tmpFront, tmpBack};
180 195

  
181
    return new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
196
    mesh.setTextureMap(maps);
182 197
    }
183 198
}
src/main/java/org/distorted/object/RubikObject.java
20 20
package org.distorted.object;
21 21

  
22 22
import android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
23 26

  
24 27
import org.distorted.library.effect.Effect;
25 28
import org.distorted.library.effect.MatrixEffectQuaternion;
......
42 45

  
43 46
public abstract class RubikObject extends DistortedNode
44 47
  {
48
  static final int TEXTURE_HEIGHT = 128;
45 49
  static final float OBJECT_SCREEN_RATIO = 0.5f;
46 50
  final float[] LEGAL_QUATS;
47 51

  
......
65 69

  
66 70
///////////////////////////////////////////////////////////////////////////////////////////////////
67 71

  
68
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
72
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture, MeshRectangles nodeMesh, DistortedEffects nodeEffects)
69 73
    {
70
    super(texture,effects,mesh);
74
    super(nodeTexture,nodeEffects,nodeMesh);
71 75

  
72 76
    resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
73 77

  
......
94 98
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, center);
95 99

  
96 100
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
97
    effects.apply(nodeScaleEffect);
101
    nodeEffects.apply(nodeScaleEffect);
98 102

  
99 103
    mCubits = new Cubit[NUM_CUBITS];
100 104
    mTexture = new DistortedTexture();
......
108 112
      int y = positions[i][1];
109 113
      int z = positions[i][2];
110 114

  
111
      mCubits[i] = new Cubit( this ,createCubitMesh(vertices,x,y,z), new Static3D(x,y,z) );
115
      MeshBase cubit = createCubitMesh(vertices);
116
      textureCubitMesh(cubit,x,y,z);
117

  
118
      mCubits[i] = new Cubit( this , cubit, new Static3D(x,y,z) );
112 119
      attach(mCubits[i].mNode);
113 120
      }
114 121
    }
......
139 146
      }
140 147
    }
141 148

  
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// the getFaceColors + final black in a horizontal strip.
151

  
152
  public void createTexture()
153
    {
154
    Bitmap bitmap;
155

  
156
    final int numColors = getNumFaces();
157

  
158
    Paint paint = new Paint();
159
    bitmap = Bitmap.createBitmap( (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
160
    Canvas canvas = new Canvas(bitmap);
161

  
162
    paint.setAntiAlias(true);
163
    paint.setTextAlign(Paint.Align.CENTER);
164
    paint.setStyle(Paint.Style.FILL);
165

  
166
    paint.setColor(0xff000000);
167
    canvas.drawRect(0, 0, (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
168

  
169
    for(int i=0; i<numColors; i++)
170
      {
171
      createFaceTexture(canvas,paint,i);
172
      }
173

  
174
    mTexture.setTexture(bitmap);
175
    }
176

  
142 177
///////////////////////////////////////////////////////////////////////////////////////////////////
143 178

  
144 179
  public int getSize()
......
329 364
  abstract int getNumCubits(int size);
330 365
  abstract int[][] getCubitPositions(int size);
331 366
  abstract float[] getLegalQuats();
332
  abstract boolean belongsToRotation(Static3D position, Static3D axis, int row);
333
  abstract MeshBase createCubitMesh(int vertices, int x, int y, int z);
367
  abstract int getNumFaces();
368
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face);
369
  abstract MeshBase createCubitMesh(int vertices);
334 370

  
335
  public abstract void createTexture();
371
  abstract boolean belongsToRotation(Static3D position, Static3D axis, int row);
372
  abstract void textureCubitMesh(MeshBase mesh, int x, int y, int z);
336 373
  }

Also available in: Unified diff