Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 1d4592a2

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.FactoryBandaged3x3Cubit;
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
import static org.distorted.objectlib.main.TwistyObject.COLOR_INTERNAL;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
59
    private final DistortedNode mNode;
60
    private final DistortedEffects mEffects;
61
    private final Static3D mMove;
62
    private final boolean mRoundCorners;
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
           COLOR_INTERNAL
79
         };
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// PUBLIC API
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D scale, boolean roundCorners)
169
      {
170
      mRoundCorners = roundCorners;
171
      mPosition = position;
172
      mIsAttached = true;
173
      mMarkedEffectID = -1;
174

    
175
      computeMove(mPosition);
176
      mMove = new Static3D(0,0,0);
177

    
178
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
179
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
180

    
181
      DistortedTexture texture = new DistortedTexture();
182
      if( mBitmap==null ) createBitmap();
183
      texture.setTextureAlreadyInverted(mBitmap);
184

    
185
      resetTextureMaps(mesh);
186

    
187
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
188
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
189
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
190
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
191

    
192
      mEffects = new DistortedEffects();
193
      mEffects.apply(scaleEffect);
194
      mEffects.apply(moveEffect);
195
      mEffects.apply(quat2Effect);
196
      mEffects.apply(quat1Effect);
197

    
198
      mNode = new DistortedNode(texture,mEffects,mesh);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public void join(float[] position, float scale)
204
      {
205
      int len1 = mPosition.length;
206
      int len2 = position.length;
207

    
208
      float[] tmpPosition = new float[len1+len2];
209

    
210
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
211
      System.arraycopy(position , 0, tmpPosition, len1, len2);
212

    
213
      computeMove(tmpPosition);
214
      mPosition = tmpPosition;
215

    
216
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
217
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
218
      resetTextureMaps(mesh);
219
      mNode.setMesh(mesh);
220
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    public void reset(float scale)
226
      {
227
      float x0 = mPosition[0];
228
      float x1 = mPosition[1];
229
      float x2 = mPosition[2];
230

    
231
      mPosition = new float[3];
232
      mPosition[0] = x0;
233
      mPosition[1] = x1;
234
      mPosition[2] = x2;
235

    
236
      computeMove(mPosition);
237

    
238
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
239
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
240
      resetTextureMaps(mesh);
241
      mNode.setMesh(mesh);
242
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public void scaleMove(float scale)
248
      {
249
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public void setMarked()
255
      {
256
      FragmentEffectBrightness effect = new FragmentEffectBrightness(mAlpha);
257
      mMarkedEffectID = effect.getID();
258
      mEffects.apply(effect);
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public void setUnmarked()
264
      {
265
      if( mMarkedEffectID>=0 ) mEffects.abortById(mMarkedEffectID);
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public void detach()
271
      {
272
      mIsAttached = false;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public void attach()
278
      {
279
      mIsAttached = true;
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public boolean isAttached()
285
      {
286
      return mIsAttached;
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    public float[] getPosition()
292
      {
293
      return mPosition;
294
      }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    public DistortedNode getNode()
299
      {
300
      return mNode;
301
      }
302
}
303

    
(8-8/13)