Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 3b8f5220

1 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 217096bf Leszek Koltunski
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
26 1a106eb8 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
27 da56b12f Leszek Koltunski
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 217096bf Leszek Koltunski
import org.distorted.library.main.QuatHelper;
34 da56b12f Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
35 1a106eb8 Leszek Koltunski
import org.distorted.library.type.Static1D;
36 da56b12f Leszek Koltunski
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38 16663e27 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryBandagedCubit;
39 217096bf Leszek Koltunski
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 da56b12f Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
public class BandagedCubit
51
{
52
    private static final Static3D CENTER = new Static3D(0,0,0);
53 1a106eb8 Leszek Koltunski
    private static final float[] mTmp = new float[4];
54
    private static final Static1D mAlpha = new Static1D(2.0f);
55 217096bf Leszek Koltunski
    private static Bitmap mBitmap;
56
    private static Static4D[] mTextureMaps;
57 da56b12f Leszek Koltunski
58 28cb1607 Leszek Koltunski
    private final DistortedNode mNode;
59 1a106eb8 Leszek Koltunski
    private final DistortedEffects mEffects;
60 3b8f5220 Leszek Koltunski
    private final DistortedTexture mTexture;
61 550db260 Leszek Koltunski
    private final Static3D mMove;
62 5d5ed376 Leszek Koltunski
    private final boolean mRoundCorners;
63 826abd80 Leszek Koltunski
    private final int mX, mY, mZ;
64 550db260 Leszek Koltunski
65
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
66 28cb1607 Leszek Koltunski
    private float[] mPosition;
67 903041cd Leszek Koltunski
    private boolean mIsAttached;
68 1a106eb8 Leszek Koltunski
    private long mMarkedEffectID;
69 da56b12f Leszek Koltunski
70 217096bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
    private static void createBitmap()
73
      {
74
      final int[] FACE_COLORS = new int[]
75
         {
76
           COLOR_YELLOW, COLOR_WHITE,
77
           COLOR_BLUE  , COLOR_GREEN,
78 e5c9982e Leszek Koltunski
           COLOR_RED   , COLOR_ORANGE
79 217096bf Leszek Koltunski
         };
80
      final int NUM = FACE_COLORS.length;
81
      final int SIZE= 32;
82
83
      mTextureMaps = new Static4D[NUM];
84
85
      Paint paint = new Paint();
86
      paint.setStyle(Paint.Style.FILL);
87
      mBitmap = Bitmap.createBitmap( NUM*SIZE, SIZE, Bitmap.Config.ARGB_4444);
88
      Canvas canvas = new Canvas(mBitmap);
89
90
      for(int color=0; color<NUM; color++)
91
        {
92
        paint.setColor(FACE_COLORS[color]);
93
        canvas.drawRect(color*SIZE, 0, (color+1)*SIZE, SIZE, paint);
94
        mTextureMaps[color] = new Static4D( ((float)color)/NUM, 0.0f, 1.0f/NUM, 1.0f );
95
        }
96
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// (x,y,z) ==
100
//
101
// ( 1,0,0) --> 0
102
// (-1,0,0) --> 1
103
// (0, 1,0) --> 2
104
// (0,-1,0) --> 3
105
// (0,0, 1) --> 4
106
// (0,0,-1) --> 5
107
108
    private int computeMapsIndex(float x, float y, float z)
109
      {
110
      int ix = x>0 ? (int)(x+0.5f) : (int)(x-0.5f);
111
      int iy = y>0 ? (int)(y+0.5f) : (int)(y-0.5f);
112
      int iz = z>0 ? (int)(z+0.5f) : (int)(z-0.5f);
113
114
      if( ix== 1 ) return 0;
115
      if( ix==-1 ) return 1;
116
      if( iy== 1 ) return 2;
117
      if( iy==-1 ) return 3;
118
      if( iz== 1 ) return 4;
119
      if( iz==-1 ) return 5;
120
121
      return 0;
122
      }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
    private void resetTextureMaps(MeshBase mesh)
127
      {
128
      FactoryCubit fact = FactoryCubit.getInstance();
129
      int numComponents = mesh.getNumTexComponents();
130
      Static4D[] maps = new Static4D[numComponents];
131
132
      for(int i=0; i<numComponents; i++)
133
        {
134
        Static4D q = fact.getQuaternion(i);
135
        QuatHelper.rotateVectorByQuat(mTmp,0,0,1,0,q);
136
        int index = computeMapsIndex(mTmp[0], mTmp[1], mTmp[2]);
137
        maps[i] = mTextureMaps[index];
138
        }
139
140
      mesh.setTextureMap(maps,0);
141
      }
142
143 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
    private void computeMove(float[] position)
146
      {
147
      int numCenters = position.length/3;
148
      mUnscaledX=0.0f;
149
      mUnscaledY=0.0f;
150
      mUnscaledZ=0.0f;
151
152
      for(int center=0; center<numCenters; center++)
153
        {
154
        mUnscaledX += position[3*center  ];
155
        mUnscaledY += position[3*center+1];
156
        mUnscaledZ += position[3*center+2];
157
        }
158
159
      mUnscaledX /= numCenters;
160
      mUnscaledY /= numCenters;
161
      mUnscaledZ /= numCenters;
162
      }
163
164 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// PUBLIC API
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 826abd80 Leszek Koltunski
    public BandagedCubit(float[] position, int x, int y, int z, Static4D quat1,
169
                         Static4D quat2, Static3D scale, boolean roundCorners)
170 da56b12f Leszek Koltunski
      {
171 826abd80 Leszek Koltunski
      mX = x;
172
      mY = y;
173
      mZ = z;
174 5d5ed376 Leszek Koltunski
      mRoundCorners = roundCorners;
175 da56b12f Leszek Koltunski
      mPosition = position;
176 903041cd Leszek Koltunski
      mIsAttached = true;
177 1a106eb8 Leszek Koltunski
      mMarkedEffectID = -1;
178 da56b12f Leszek Koltunski
179 550db260 Leszek Koltunski
      computeMove(mPosition);
180
      mMove = new Static3D(0,0,0);
181
182 16663e27 Leszek Koltunski
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
183 826abd80 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
184 da56b12f Leszek Koltunski
185 3b8f5220 Leszek Koltunski
      mTexture = new DistortedTexture();
186 217096bf Leszek Koltunski
      if( mBitmap==null ) createBitmap();
187 3b8f5220 Leszek Koltunski
      mTexture.setTextureAlreadyInverted(mBitmap);
188 217096bf Leszek Koltunski
189
      resetTextureMaps(mesh);
190 da56b12f Leszek Koltunski
191 77efd5ad Leszek Koltunski
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
192 a76cc4f4 Leszek Koltunski
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
193
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
194 28cb1607 Leszek Koltunski
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
195 da56b12f Leszek Koltunski
196 1a106eb8 Leszek Koltunski
      mEffects = new DistortedEffects();
197
      mEffects.apply(scaleEffect);
198
      mEffects.apply(moveEffect);
199
      mEffects.apply(quat2Effect);
200
      mEffects.apply(quat1Effect);
201 550db260 Leszek Koltunski
202 3b8f5220 Leszek Koltunski
      mNode = new DistortedNode(mTexture,mEffects,mesh);
203 550db260 Leszek Koltunski
      }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207 28cb1607 Leszek Koltunski
    public void join(float[] position, float scale)
208 550db260 Leszek Koltunski
      {
209
      int len1 = mPosition.length;
210
      int len2 = position.length;
211
212 28cb1607 Leszek Koltunski
      float[] tmpPosition = new float[len1+len2];
213 da56b12f Leszek Koltunski
214 28cb1607 Leszek Koltunski
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
215
      System.arraycopy(position , 0, tmpPosition, len1, len2);
216 550db260 Leszek Koltunski
217 28cb1607 Leszek Koltunski
      computeMove(tmpPosition);
218
      mPosition = tmpPosition;
219 550db260 Leszek Koltunski
220 16663e27 Leszek Koltunski
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
221 826abd80 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
222 217096bf Leszek Koltunski
      resetTextureMaps(mesh);
223 28cb1607 Leszek Koltunski
      mNode.setMesh(mesh);
224 550db260 Leszek Koltunski
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
225
      }
226
227 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229
    public void reset(float scale)
230
      {
231 16663e27 Leszek Koltunski
      float x = mPosition[0];
232
      float y = mPosition[1];
233
      float z = mPosition[2];
234 e0b71e6e Leszek Koltunski
235
      mPosition = new float[3];
236 16663e27 Leszek Koltunski
      mPosition[0] = x;
237
      mPosition[1] = y;
238
      mPosition[2] = z;
239 e0b71e6e Leszek Koltunski
240
      computeMove(mPosition);
241
242 16663e27 Leszek Koltunski
      FactoryBandagedCubit factory = FactoryBandagedCubit.getInstance();
243 826abd80 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,mX,mY,mZ,false,mRoundCorners);
244 33bfd7c9 Leszek Koltunski
      resetTextureMaps(mesh);
245 e0b71e6e Leszek Koltunski
      mNode.setMesh(mesh);
246
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
247
      }
248
249 3b8f5220 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
250
251
    public void recreateBitmap()
252
      {
253
      if( mBitmap==null ) createBitmap();
254
      mTexture.setTextureAlreadyInverted(mBitmap);
255
      }
256
257 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
    public void scaleMove(float scale)
260
      {
261
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
262 da56b12f Leszek Koltunski
      }
263
264 c279ea6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 1a106eb8 Leszek Koltunski
    public void setMarked()
267 c279ea6d Leszek Koltunski
      {
268 3b8f5220 Leszek Koltunski
      if( mMarkedEffectID<0 )
269
        {
270
        FragmentEffectBrightness effect = new FragmentEffectBrightness(mAlpha);
271
        mMarkedEffectID = effect.getID();
272
        mEffects.apply(effect);
273
        }
274 1a106eb8 Leszek Koltunski
      }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278
    public void setUnmarked()
279
      {
280 3b8f5220 Leszek Koltunski
      if( mMarkedEffectID>=0 )
281
        {
282
        mEffects.abortById(mMarkedEffectID);
283
        mMarkedEffectID = -1;
284
        }
285 c279ea6d Leszek Koltunski
      }
286
287 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
    public void detach()
290
      {
291
      mIsAttached = false;
292
      }
293
294 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
    public void attach()
297
      {
298
      mIsAttached = true;
299
      }
300
301 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
    public boolean isAttached()
304
      {
305
      return mIsAttached;
306
      }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
    public float[] getPosition()
311
      {
312
      return mPosition;
313
      }
314
315 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317
    public DistortedNode getNode()
318
      {
319
      return mNode;
320
      }
321
}