Project

General

Profile

« Previous | Next » 

Revision 217096bf

Added by Leszek Koltunski over 2 years ago

BandagedCreator: colorful cube.

View differences:

src/main/java/org/distorted/bandaged/BandagedCubit.java
19 19

  
20 20
package org.distorted.bandaged;
21 21

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

  
22 26
import org.distorted.library.effect.MatrixEffectMove;
23 27
import org.distorted.library.effect.MatrixEffectQuaternion;
24 28
import org.distorted.library.effect.MatrixEffectScale;
25 29
import org.distorted.library.main.DistortedEffects;
26 30
import org.distorted.library.main.DistortedNode;
27 31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.main.QuatHelper;
28 33
import org.distorted.library.mesh.MeshBase;
29 34
import org.distorted.library.type.Static3D;
30 35
import org.distorted.library.type.Static4D;
31 36
import org.distorted.objectlib.helpers.FactoryBandaged3x3Cubit;
37
import org.distorted.objectlib.helpers.FactoryCubit;
38

  
39
import static org.distorted.objectlib.main.TwistyObject.COLOR_BLUE;
40
import static org.distorted.objectlib.main.TwistyObject.COLOR_GREEN;
41
import static org.distorted.objectlib.main.TwistyObject.COLOR_ORANGE;
42
import static org.distorted.objectlib.main.TwistyObject.COLOR_RED;
43
import static org.distorted.objectlib.main.TwistyObject.COLOR_WHITE;
44
import static org.distorted.objectlib.main.TwistyObject.COLOR_YELLOW;
32 45

  
33 46
///////////////////////////////////////////////////////////////////////////////////////////////////
34 47

  
35 48
public class BandagedCubit
36 49
{
37 50
    private static final Static3D CENTER = new Static3D(0,0,0);
51
    private static Bitmap mBitmap;
52
    private static Static4D[] mTextureMaps;
53
    private static float[] mTmp = new float[4];
38 54

  
39 55
    private final DistortedNode mNode;
40 56
    private final DistortedTexture mTexture;
......
45 61
    private float[] mPosition;
46 62
    private boolean mIsAttached;
47 63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
    private static void createBitmap()
67
      {
68
      final int[] FACE_COLORS = new int[]
69
         {
70
           COLOR_YELLOW, COLOR_WHITE,
71
           COLOR_BLUE  , COLOR_GREEN,
72
           COLOR_RED   , COLOR_ORANGE
73
         };
74
      final int NUM = FACE_COLORS.length;
75
      final int SIZE= 32;
76

  
77
      mTextureMaps = new Static4D[NUM];
78

  
79
      Paint paint = new Paint();
80
      paint.setStyle(Paint.Style.FILL);
81
      mBitmap = Bitmap.createBitmap( NUM*SIZE, SIZE, Bitmap.Config.ARGB_4444);
82
      Canvas canvas = new Canvas(mBitmap);
83

  
84
      for(int color=0; color<NUM; color++)
85
        {
86
        paint.setColor(FACE_COLORS[color]);
87
        canvas.drawRect(color*SIZE, 0, (color+1)*SIZE, SIZE, paint);
88
        mTextureMaps[color] = new Static4D( ((float)color)/NUM, 0.0f, 1.0f/NUM, 1.0f );
89
        }
90
      }
91

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
// (x,y,z) ==
94
//
95
// ( 1,0,0) --> 0
96
// (-1,0,0) --> 1
97
// (0, 1,0) --> 2
98
// (0,-1,0) --> 3
99
// (0,0, 1) --> 4
100
// (0,0,-1) --> 5
101

  
102
    private int computeMapsIndex(float x, float y, float z)
103
      {
104
      int ix = x>0 ? (int)(x+0.5f) : (int)(x-0.5f);
105
      int iy = y>0 ? (int)(y+0.5f) : (int)(y-0.5f);
106
      int iz = z>0 ? (int)(z+0.5f) : (int)(z-0.5f);
107

  
108
      if( ix== 1 ) return 0;
109
      if( ix==-1 ) return 1;
110
      if( iy== 1 ) return 2;
111
      if( iy==-1 ) return 3;
112
      if( iz== 1 ) return 4;
113
      if( iz==-1 ) return 5;
114

  
115
      return 0;
116
      }
117

  
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

  
120
    private void resetTextureMaps(MeshBase mesh)
121
      {
122
      FactoryCubit fact = FactoryCubit.getInstance();
123
      int numComponents = mesh.getNumTexComponents();
124
      Static4D[] maps = new Static4D[numComponents];
125

  
126
      for(int i=0; i<numComponents; i++)
127
        {
128
        Static4D q = fact.getQuaternion(i);
129
        QuatHelper.rotateVectorByQuat(mTmp,0,0,1,0,q);
130
        int index = computeMapsIndex(mTmp[0], mTmp[1], mTmp[2]);
131
        maps[i] = mTextureMaps[index];
132
        }
133

  
134
      mesh.setTextureMap(maps,0);
135
      }
136

  
48 137
///////////////////////////////////////////////////////////////////////////////////////////////////
49 138

  
50 139
    private void computeMove(float[] position)
......
83 172
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
84 173

  
85 174
      mTexture = new DistortedTexture();
86
      mTexture.setColorARGB(color);
175
      if( mBitmap==null ) createBitmap();
176
      mTexture.setTextureAlreadyInverted(mBitmap);
177

  
178
      resetTextureMaps(mesh);
87 179

  
88 180
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
89 181
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
......
116 208

  
117 209
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
118 210
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
211
      resetTextureMaps(mesh);
119 212
      mNode.setMesh(mesh);
120 213
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
121 214
      }
......
152 245

  
153 246
    public void setTexture(int color)
154 247
      {
155
      mTexture.setColorARGB(color);
248
   //   mTexture.setColorARGB(color);
156 249
      }
157 250

  
158 251
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff