Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 826abd80

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

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

    
26
import org.distorted.library.effect.FragmentEffectBrightness;
27
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedNode;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.main.QuatHelper;
34
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.objectlib.helpers.FactoryBandagedCubit;
39
import org.distorted.objectlib.helpers.FactoryCubit;
40

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class BandagedCubit
51
{
52
    private static final Static3D CENTER = new Static3D(0,0,0);
53
    private static final float[] mTmp = new float[4];
54
    private static final Static1D mAlpha = new Static1D(2.0f);
55
    private static Bitmap mBitmap;
56
    private static Static4D[] mTextureMaps;
57

    
58
    private final DistortedNode mNode;
59
    private final DistortedEffects mEffects;
60
    private final Static3D mMove;
61
    private final boolean mRoundCorners;
62
    private final int mX, mY, mZ;
63

    
64
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
65
    private float[] mPosition;
66
    private boolean mIsAttached;
67
    private long mMarkedEffectID;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    private static void createBitmap()
72
      {
73
      final int[] FACE_COLORS = new int[]
74
         {
75
           COLOR_YELLOW, COLOR_WHITE,
76
           COLOR_BLUE  , COLOR_GREEN,
77
           COLOR_RED   , COLOR_ORANGE
78
         };
79
      final int NUM = FACE_COLORS.length;
80
      final int SIZE= 32;
81

    
82
      mTextureMaps = new Static4D[NUM];
83

    
84
      Paint paint = new Paint();
85
      paint.setStyle(Paint.Style.FILL);
86
      mBitmap = Bitmap.createBitmap( NUM*SIZE, SIZE, Bitmap.Config.ARGB_4444);
87
      Canvas canvas = new Canvas(mBitmap);
88

    
89
      for(int color=0; color<NUM; color++)
90
        {
91
        paint.setColor(FACE_COLORS[color]);
92
        canvas.drawRect(color*SIZE, 0, (color+1)*SIZE, SIZE, paint);
93
        mTextureMaps[color] = new Static4D( ((float)color)/NUM, 0.0f, 1.0f/NUM, 1.0f );
94
        }
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// (x,y,z) ==
99
//
100
// ( 1,0,0) --> 0
101
// (-1,0,0) --> 1
102
// (0, 1,0) --> 2
103
// (0,-1,0) --> 3
104
// (0,0, 1) --> 4
105
// (0,0,-1) --> 5
106

    
107
    private int computeMapsIndex(float x, float y, float z)
108
      {
109
      int ix = x>0 ? (int)(x+0.5f) : (int)(x-0.5f);
110
      int iy = y>0 ? (int)(y+0.5f) : (int)(y-0.5f);
111
      int iz = z>0 ? (int)(z+0.5f) : (int)(z-0.5f);
112

    
113
      if( ix== 1 ) return 0;
114
      if( ix==-1 ) return 1;
115
      if( iy== 1 ) return 2;
116
      if( iy==-1 ) return 3;
117
      if( iz== 1 ) return 4;
118
      if( iz==-1 ) return 5;
119

    
120
      return 0;
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    private void resetTextureMaps(MeshBase mesh)
126
      {
127
      FactoryCubit fact = FactoryCubit.getInstance();
128
      int numComponents = mesh.getNumTexComponents();
129
      Static4D[] maps = new Static4D[numComponents];
130

    
131
      for(int i=0; i<numComponents; i++)
132
        {
133
        Static4D q = fact.getQuaternion(i);
134
        QuatHelper.rotateVectorByQuat(mTmp,0,0,1,0,q);
135
        int index = computeMapsIndex(mTmp[0], mTmp[1], mTmp[2]);
136
        maps[i] = mTextureMaps[index];
137
        }
138

    
139
      mesh.setTextureMap(maps,0);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    private void computeMove(float[] position)
145
      {
146
      int numCenters = position.length/3;
147
      mUnscaledX=0.0f;
148
      mUnscaledY=0.0f;
149
      mUnscaledZ=0.0f;
150

    
151
      for(int center=0; center<numCenters; center++)
152
        {
153
        mUnscaledX += position[3*center  ];
154
        mUnscaledY += position[3*center+1];
155
        mUnscaledZ += position[3*center+2];
156
        }
157

    
158
      mUnscaledX /= numCenters;
159
      mUnscaledY /= numCenters;
160
      mUnscaledZ /= numCenters;
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// PUBLIC API
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    public BandagedCubit(float[] position, int x, int y, int z, Static4D quat1,
168
                         Static4D quat2, Static3D scale, boolean roundCorners)
169
      {
170
      mX = x;
171
      mY = y;
172
      mZ = z;
173
      mRoundCorners = roundCorners;
174
      mPosition = position;
175
      mIsAttached = true;
176
      mMarkedEffectID = -1;
177

    
178
      computeMove(mPosition);
179
      mMove = new Static3D(0,0,0);
180

    
181
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
182
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
183

    
184
      DistortedTexture texture = new DistortedTexture();
185
      if( mBitmap==null ) createBitmap();
186
      texture.setTextureAlreadyInverted(mBitmap);
187

    
188
      resetTextureMaps(mesh);
189

    
190
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
191
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
192
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
193
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
194

    
195
      mEffects = new DistortedEffects();
196
      mEffects.apply(scaleEffect);
197
      mEffects.apply(moveEffect);
198
      mEffects.apply(quat2Effect);
199
      mEffects.apply(quat1Effect);
200

    
201
      mNode = new DistortedNode(texture,mEffects,mesh);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    public void join(float[] position, float scale)
207
      {
208
      int len1 = mPosition.length;
209
      int len2 = position.length;
210

    
211
      float[] tmpPosition = new float[len1+len2];
212

    
213
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
214
      System.arraycopy(position , 0, tmpPosition, len1, len2);
215

    
216
      computeMove(tmpPosition);
217
      mPosition = tmpPosition;
218

    
219
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
220
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
221
      resetTextureMaps(mesh);
222
      mNode.setMesh(mesh);
223
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    public void reset(float scale)
229
      {
230
      float x = mPosition[0];
231
      float y = mPosition[1];
232
      float z = mPosition[2];
233

    
234
      mPosition = new float[3];
235
      mPosition[0] = x;
236
      mPosition[1] = y;
237
      mPosition[2] = z;
238

    
239
      computeMove(mPosition);
240

    
241
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
242
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
243
      resetTextureMaps(mesh);
244
      mNode.setMesh(mesh);
245
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
246
      }
247

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

    
250
    public void scaleMove(float scale)
251
      {
252
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    public void setMarked()
258
      {
259
      FragmentEffectBrightness effect = new FragmentEffectBrightness(mAlpha);
260
      mMarkedEffectID = effect.getID();
261
      mEffects.apply(effect);
262
      }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
    public void setUnmarked()
267
      {
268
      if( mMarkedEffectID>=0 ) mEffects.abortById(mMarkedEffectID);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public void detach()
274
      {
275
      mIsAttached = false;
276
      }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    public void attach()
281
      {
282
      mIsAttached = true;
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    public boolean isAttached()
288
      {
289
      return mIsAttached;
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
    public float[] getPosition()
295
      {
296
      return mPosition;
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    public DistortedNode getNode()
302
      {
303
      return mNode;
304
      }
305
}
306

    
(8-8/13)