Project

General

Profile

« Previous | Next » 

Revision f18e8fae

Added by Leszek Koltunski about 4 years ago

Simplify drawing textures.

View differences:

src/main/java/org/distorted/main/RubikPreRender.java
121 121

  
122 122
      if( mScreenWidth!=0 )
123 123
        {
124
        mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
124
        mNewObject.recomputeScaleFactor(mScreenWidth);
125 125
        }
126 126

  
127 127
      mIsSolved = mNewObject.isSolved();
......
361 361
    if( mNewObject!=null )
362 362
      {
363 363
      mNewObject.createTexture();
364
      mNewObject.recomputeScaleFactor(width,height);
364
      mNewObject.recomputeScaleFactor(width);
365 365
      }
366 366

  
367 367
    mScreenHeight = height;
src/main/java/org/distorted/objects/TwistyCube.java
127 127
// paint the square with upper-right corner at (left,top) and side length 'side' with texture
128 128
// for face 'face'.
129 129

  
130
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
130
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
131 131
    {
132
    final float R = side*0.10f;
133
    final float M = side*0.05f;
132
    final float R = TEXTURE_HEIGHT*0.10f;
133
    final float M = TEXTURE_HEIGHT*0.05f;
134 134

  
135 135
    paint.setColor(FACE_COLORS[face]);
136
    canvas.drawRoundRect( left+M, top+M, left+side-M, top+side-M, R, R, paint);
136
    canvas.drawRoundRect( left+M, M, left+TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
137 137
    }
138 138

  
139 139
///////////////////////////////////////////////////////////////////////////////////////////////////
......
210 210

  
211 211
  int getFaceColor(int cubit, int cubitface, int size)
212 212
    {
213
    boolean belongs = isOnFace(cubit, cubitface/2, cubitface%2==0 ? size-1:0 );
214
    return belongs ? cubitface : NUM_FACES;
213
    float diff = CUBITS[cubit].mRotationRow[cubitface/2] - (cubitface%2==0 ? size-1:0);
214
    return diff*diff < 0.0001f ? cubitface : NUM_FACES;
215 215
    }
216 216

  
217 217
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyDiamond.java
246 246

  
247 247
///////////////////////////////////////////////////////////////////////////////////////////////////
248 248

  
249
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
249
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
250 250
    {
251
    float STROKE = 0.044f*side;
251
    float STROKE = 0.044f*TEXTURE_HEIGHT;
252 252
    float OFF = STROKE/2 -1;
253
    float OFF2 = 0.5f*side + OFF;
254
    float HEIGHT = side - OFF;
255
    float RADIUS = side/12.0f;
256
    float ARC1_H = 0.2f*side;
257
    float ARC1_W = side*0.5f;
258
    float ARC2_W = 0.153f*side;
259
    float ARC2_H = 0.905f*side;
260
    float ARC3_W = side-ARC2_W;
253
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
254
    float HEIGHT = TEXTURE_HEIGHT - OFF;
255
    float RADIUS = TEXTURE_HEIGHT/12.0f;
256
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
257
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
258
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
259
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
260
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
261 261

  
262 262
    float M = SQ3/2;
263
    float D = (M/2 - 0.51f)*side;
263
    float D = (M/2 - 0.51f)*TEXTURE_HEIGHT;
264 264

  
265 265
    paint.setAntiAlias(true);
266 266
    paint.setStrokeWidth(STROKE);
267 267
    paint.setColor(FACE_COLORS[face]);
268 268
    paint.setStyle(Paint.Style.FILL);
269 269

  
270
    canvas.drawRect(left,top,left+side,top+side,paint);
270
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
271 271

  
272 272
    paint.setColor(INTERIOR_COLOR);
273 273
    paint.setStyle(Paint.Style.STROKE);
274 274

  
275
    canvas.drawLine(           left, M*HEIGHT+D,  side       +left, M*HEIGHT+D, paint);
276
    canvas.drawLine(      OFF +left, M*side  +D,       OFF2  +left,          D, paint);
277
    canvas.drawLine((side-OFF)+left, M*side  +D, (side-OFF2) +left,          D, paint);
275
    canvas.drawLine(                     left,         M*HEIGHT+D,  TEXTURE_HEIGHT       +left, M*HEIGHT+D, paint);
276
    canvas.drawLine(                OFF +left, M*TEXTURE_HEIGHT+D,                 OFF2  +left,          D, paint);
277
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+left, M*TEXTURE_HEIGHT+D, (TEXTURE_HEIGHT-OFF2) +left,          D, paint);
278 278

  
279 279
    canvas.drawArc( ARC1_W-RADIUS+left, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+left, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
280 280
    canvas.drawArc( ARC2_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
src/main/java/org/distorted/objects/TwistyDino.java
206 206

  
207 207
///////////////////////////////////////////////////////////////////////////////////////////////////
208 208

  
209
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
209
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
210 210
    {
211
    float STROKE = 0.04f*side;
211
    float STROKE = 0.04f*TEXTURE_HEIGHT;
212 212
    float L= left;
213
    float H= 0.333f*side;
214
    float LEN = 0.5f*side;
213
    float H= 0.333f*TEXTURE_HEIGHT;
214
    float LEN = 0.5f*TEXTURE_HEIGHT;
215 215

  
216 216
    paint.setAntiAlias(true);
217 217
    paint.setStrokeWidth(STROKE);
218 218
    paint.setColor(FACE_COLORS[face]);
219 219
    paint.setStyle(Paint.Style.FILL);
220 220

  
221
    canvas.drawRect(left,top,left+side,top+side,paint);
221
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
222 222

  
223 223
    paint.setColor(INTERIOR_COLOR);
224 224
    paint.setStyle(Paint.Style.STROKE);
......
227 227
    canvas.drawLine( L      , H,  L+  LEN, H+LEN, paint);
228 228
    canvas.drawLine( L+2*LEN, H,  L+  LEN, H+LEN, paint);
229 229

  
230
    float S1 = 0.150f*side;
231
    float S2 = 0.090f*side;
230
    float S1 = 0.150f*TEXTURE_HEIGHT;
231
    float S2 = 0.090f*TEXTURE_HEIGHT;
232 232
    float X  = 0.7f*S2;
233 233
    float Y  = 0.2f*S1;
234 234

  
235
    float LA = left+0.500f*side;
235
    float LA = left+0.500f*TEXTURE_HEIGHT;
236 236
    float RA = left;
237
    float TA = 0.333f*side;
238
    float BA = 0.833f*side;
237
    float TA = 0.333f*TEXTURE_HEIGHT;
238
    float BA = 0.833f*TEXTURE_HEIGHT;
239 239

  
240
    canvas.drawArc( RA+X        , TA     , RA+X+S2  , TA+S2, 135,135, false, paint);
241
    canvas.drawArc( RA+side-S2-X, TA     , RA+side-X, TA+S2, 270,135, false, paint);
242
    canvas.drawArc( LA-S1/2     , BA-S1-Y, LA+S1/2  , BA-Y ,  45, 90, false, paint);
240
    canvas.drawArc( RA+X                  , TA     , RA+X+S2            , TA+S2, 135,135, false, paint);
241
    canvas.drawArc( RA+TEXTURE_HEIGHT-S2-X, TA     , RA+TEXTURE_HEIGHT-X, TA+S2, 270,135, false, paint);
242
    canvas.drawArc( LA-S1/2               , BA-S1-Y, LA+S1/2            , BA-Y ,  45, 90, false, paint);
243 243
    }
244 244

  
245 245
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objects/TwistyHelicopter.java
285 285

  
286 286
///////////////////////////////////////////////////////////////////////////////////////////////////
287 287

  
288
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
288
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
289 289
    {
290
    float STROKE = 0.035f*side;
291
    float L= left+0.125f*side;
292
    float H= 0.375f*side;
293
    float LEN = 0.5f*side;
290
    float STROKE = 0.035f*TEXTURE_HEIGHT;
291
    float L= left+0.125f*TEXTURE_HEIGHT;
292
    float H= 0.375f*TEXTURE_HEIGHT;
293
    float LEN = 0.5f*TEXTURE_HEIGHT;
294 294

  
295 295
    paint.setAntiAlias(true);
296 296
    paint.setStrokeWidth(STROKE);
297 297
    paint.setColor(FACE_COLORS[face]);
298 298
    paint.setStyle(Paint.Style.FILL);
299 299

  
300
    canvas.drawRect(left,top,left+side,top+side,paint);
300
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
301 301

  
302 302
    paint.setColor(INTERIOR_COLOR);
303 303
    paint.setStyle(Paint.Style.STROKE);
......
306 306
    canvas.drawLine( L    , H,  L+LEN, H+LEN, paint);
307 307
    canvas.drawLine( L+LEN, H,  L+LEN, H+LEN, paint);
308 308

  
309
    float S1 = 0.125f*side;
310
    float S2 = 0.070f*side;
309
    float S1 = 0.125f*TEXTURE_HEIGHT;
310
    float S2 = 0.070f*TEXTURE_HEIGHT;
311 311
    float X  = 0.7f*S2;
312 312

  
313
    float LA = left+0.625f*side;
314
    float RA = left+0.125f*side;
315
    float TA = 0.375f*side;
316
    float BA = 0.875f*side;
313
    float LA = left+0.625f*TEXTURE_HEIGHT;
314
    float RA = left+0.125f*TEXTURE_HEIGHT;
315
    float TA = 0.375f*TEXTURE_HEIGHT;
316
    float BA = 0.875f*TEXTURE_HEIGHT;
317 317

  
318 318
    canvas.drawArc( LA-S1, TA     , LA     , TA+S1, 270, 90, false, paint);
319 319
    canvas.drawArc( RA+X , TA     , RA+X+S2, TA+S2, 135,135, false, paint);
src/main/java/org/distorted/objects/TwistyObject.java
73 73
  private static final Static3D CENTER = new Static3D(0,0,0);
74 74
  static final int INTERIOR_COLOR = 0xff000000;
75 75
  private static final int POST_ROTATION_MILLISEC = 500;
76
  private static final int TEXTURE_HEIGHT = 256;
76
  static final int TEXTURE_HEIGHT = 256;
77 77

  
78 78
  final Static3D[] ROTATION_AXIS;
79 79
  final Static4D[] QUATS;
......
340 340
    }
341 341

  
342 342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
344
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
345
// away from the face of the Pyraminx shouldn't be textured.
346 343

  
347
  boolean isOnFace( int cubit, int axis, int row)
344
  void drawRoundedPolygon()
348 345
    {
349
    final float MAX_ERROR = 0.0001f;
350
    float diff = CUBITS[cubit].mRotationRow[axis] - row;
351
    return diff*diff < MAX_ERROR;
346

  
352 347
    }
353 348

  
354 349
///////////////////////////////////////////////////////////////////////////////////////////////////
......
409 404

  
410 405
    for(int i=0; i<NUM_TEXTURES; i++)
411 406
      {
412
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
407
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT);
413 408
      }
414 409

  
415 410
    mTexture.setTexture(bitmap);
......
438 433

  
439 434
///////////////////////////////////////////////////////////////////////////////////////////////////
440 435

  
441
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
436
  public void recomputeScaleFactor(int scrWidth)
442 437
    {
443 438
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
444 439
    }
......
700 695
  abstract int getNumStickerTypes();
701 696
  abstract int getNumCubitFaces();
702 697
  abstract MeshBase createCubitMesh(int cubit);
703
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
698
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left);
704 699
  abstract int getFaceColor(int cubit, int cubitface, int size);
705 700
  abstract float returnMultiplier();
706 701
  abstract float[] getRowChances();
src/main/java/org/distorted/objects/TwistyPyraminx.java
234 234

  
235 235
///////////////////////////////////////////////////////////////////////////////////////////////////
236 236

  
237
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
237
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
238 238
    {
239
    float STROKE = 0.044f*side;
239
    float STROKE = 0.044f*TEXTURE_HEIGHT;
240 240
    float OFF = STROKE/2 -1;
241
    float OFF2 = 0.5f*side + OFF;
242
    float HEIGHT = side - OFF;
243
    float RADIUS = side/12.0f;
244
    float ARC1_H = 0.2f*side;
245
    float ARC1_W = side*0.5f;
246
    float ARC2_W = 0.153f*side;
247
    float ARC2_H = 0.905f*side;
248
    float ARC3_W = side-ARC2_W;
241
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
242
    float HEIGHT = TEXTURE_HEIGHT - OFF;
243
    float RADIUS = TEXTURE_HEIGHT/12.0f;
244
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
245
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
246
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
247
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
248
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
249 249

  
250 250
    float M = SQ3/2;
251
    float D = (M/2 - 0.51f)*side;
251
    float D = (M/2 - 0.51f)*TEXTURE_HEIGHT;
252 252

  
253 253
    paint.setAntiAlias(true);
254 254
    paint.setStrokeWidth(STROKE);
255 255
    paint.setColor(FACE_COLORS[face]);
256 256
    paint.setStyle(Paint.Style.FILL);
257 257

  
258
    canvas.drawRect(left,top,left+side,top+side,paint);
258
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
259 259

  
260 260
    paint.setColor(INTERIOR_COLOR);
261 261
    paint.setStyle(Paint.Style.STROKE);
262 262

  
263
    canvas.drawLine(           left, M*HEIGHT+D,  side       +left, M*HEIGHT+D, paint);
264
    canvas.drawLine(      OFF +left, M*side  +D,       OFF2  +left,          D, paint);
265
    canvas.drawLine((side-OFF)+left, M*side  +D, (side-OFF2) +left,          D, paint);
263
    canvas.drawLine(                     left, M*HEIGHT        +D,  TEXTURE_HEIGHT       +left, M*HEIGHT+D, paint);
264
    canvas.drawLine(                OFF +left, M*TEXTURE_HEIGHT+D,                 OFF2  +left,          D, paint);
265
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+left, M*TEXTURE_HEIGHT+D, (TEXTURE_HEIGHT-OFF2) +left,          D, paint);
266 266

  
267 267
    canvas.drawArc( ARC1_W-RADIUS+left, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+left, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
268 268
    canvas.drawArc( ARC2_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
src/main/java/org/distorted/objects/TwistySkewb.java
247 247

  
248 248
///////////////////////////////////////////////////////////////////////////////////////////////////
249 249

  
250
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
250
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left)
251 251
    {
252 252
    int COLORS = FACE_COLORS.length;
253 253

  
254 254
    if( face<COLORS )
255 255
      {
256
      float STROKE = 0.035f*side;
257
      float L= left+0.125f*side;
258
      float H= 0.375f*side;
259
      float LEN = 0.5f*side;
256
      float STROKE = 0.035f*TEXTURE_HEIGHT;
257
      float L= left+0.125f*TEXTURE_HEIGHT;
258
      float H= 0.375f*TEXTURE_HEIGHT;
259
      float LEN = 0.5f*TEXTURE_HEIGHT;
260 260

  
261 261
      paint.setAntiAlias(true);
262 262
      paint.setStrokeWidth(STROKE);
263 263
      paint.setColor(FACE_COLORS[face]);
264 264
      paint.setStyle(Paint.Style.FILL);
265 265

  
266
      canvas.drawRect(left,top,left+side,top+side,paint);
266
      canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
267 267

  
268 268
      paint.setColor(INTERIOR_COLOR);
269 269
      paint.setStyle(Paint.Style.STROKE);
......
272 272
      canvas.drawLine( L    , H,  L+LEN, H+LEN, paint);
273 273
      canvas.drawLine( L+LEN, H,  L+LEN, H+LEN, paint);
274 274

  
275
      float S1 = 0.125f*side;
276
      float S2 = 0.070f*side;
275
      float S1 = 0.125f*TEXTURE_HEIGHT;
276
      float S2 = 0.070f*TEXTURE_HEIGHT;
277 277
      float X  = 0.7f*S2;
278 278

  
279
      float LA = left+0.625f*side;
280
      float RA = left+0.125f*side;
281
      float TA = 0.375f*side;
282
      float BA = 0.875f*side;
279
      float LA = left+0.625f*TEXTURE_HEIGHT;
280
      float RA = left+0.125f*TEXTURE_HEIGHT;
281
      float TA = 0.375f*TEXTURE_HEIGHT;
282
      float BA = 0.875f*TEXTURE_HEIGHT;
283 283

  
284 284
      canvas.drawArc( LA-S1, TA     , LA     , TA+S1, 270, 90, false, paint);
285 285
      canvas.drawArc( RA+X , TA     , RA+X+S2, TA+S2, 135,135, false, paint);
......
287 287
      }
288 288
    else
289 289
      {
290
      final float R = (SQ2/2)*side*0.10f;
291
      final float M = side*(0.5f-SQ2/4+0.018f);
290
      final float R = (SQ2/2)*TEXTURE_HEIGHT*0.10f;
291
      final float M = TEXTURE_HEIGHT*(0.5f-SQ2/4+0.018f);
292 292

  
293 293
      paint.setColor(FACE_COLORS[face-COLORS]);
294 294
      paint.setStyle(Paint.Style.FILL);
295
      canvas.drawRoundRect( left+M, top+M, left+side-M, top+side-M, R, R, paint);
295
      canvas.drawRoundRect( left+M, M, left+TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
296 296
      }
297 297
    }
298 298

  

Also available in: Unified diff