Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ f8c52090

1 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 44fec653 Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 217096bf Leszek Koltunski
import android.graphics.Bitmap;
13
import android.graphics.Canvas;
14
import android.graphics.Paint;
15
16 576afdf9 Leszek Koltunski
import org.distorted.library.effect.EffectType;
17 1a106eb8 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
18 da56b12f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
19
import org.distorted.library.effect.MatrixEffectQuaternion;
20
import org.distorted.library.effect.MatrixEffectScale;
21
import org.distorted.library.main.DistortedEffects;
22
import org.distorted.library.main.DistortedNode;
23
import org.distorted.library.main.DistortedTexture;
24 f3b3c087 Leszek Koltunski
import org.distorted.library.helpers.QuatHelper;
25 da56b12f Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
26 1a106eb8 Leszek Koltunski
import org.distorted.library.type.Static1D;
27 da56b12f Leszek Koltunski
import org.distorted.library.type.Static3D;
28
import org.distorted.library.type.Static4D;
29 217096bf Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
30
31 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33
public class BandagedCubit
34
{
35
    private static final Static3D CENTER = new Static3D(0,0,0);
36 1a106eb8 Leszek Koltunski
    private static final float[] mTmp = new float[4];
37
    private static final Static1D mAlpha = new Static1D(2.0f);
38 217096bf Leszek Koltunski
    private static Bitmap mBitmap;
39
    private static Static4D[] mTextureMaps;
40 da56b12f Leszek Koltunski
41 afd7e804 Leszek Koltunski
    private final BandagedObject mObject;
42 28cb1607 Leszek Koltunski
    private final DistortedNode mNode;
43 1a106eb8 Leszek Koltunski
    private final DistortedEffects mEffects;
44 3b8f5220 Leszek Koltunski
    private final DistortedTexture mTexture;
45 550db260 Leszek Koltunski
    private final Static3D mMove;
46 6c295be1 Leszek Koltunski
    private final int mVariant;
47 5d5ed376 Leszek Koltunski
    private final boolean mRoundCorners;
48 550db260 Leszek Koltunski
49
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
50 28cb1607 Leszek Koltunski
    private float[] mPosition;
51 903041cd Leszek Koltunski
    private boolean mIsAttached;
52 1a106eb8 Leszek Koltunski
    private long mMarkedEffectID;
53 da56b12f Leszek Koltunski
54 217096bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 afd7e804 Leszek Koltunski
    private static void createBitmap(int[] colors)
57 217096bf Leszek Koltunski
      {
58 afd7e804 Leszek Koltunski
      final int NUM = colors.length;
59 217096bf Leszek Koltunski
      final int SIZE= 32;
60
61
      mTextureMaps = new Static4D[NUM];
62
63
      Paint paint = new Paint();
64
      paint.setStyle(Paint.Style.FILL);
65
      mBitmap = Bitmap.createBitmap( NUM*SIZE, SIZE, Bitmap.Config.ARGB_4444);
66
      Canvas canvas = new Canvas(mBitmap);
67
68
      for(int color=0; color<NUM; color++)
69
        {
70 afd7e804 Leszek Koltunski
        paint.setColor(colors[color]);
71 217096bf Leszek Koltunski
        canvas.drawRect(color*SIZE, 0, (color+1)*SIZE, SIZE, paint);
72
        mTextureMaps[color] = new Static4D( ((float)color)/NUM, 0.0f, 1.0f/NUM, 1.0f );
73
        }
74
      }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78
    private void resetTextureMaps(MeshBase mesh)
79
      {
80
      FactoryCubit fact = FactoryCubit.getInstance();
81
      int numComponents = mesh.getNumTexComponents();
82
      Static4D[] maps = new Static4D[numComponents];
83
84
      for(int i=0; i<numComponents; i++)
85
        {
86
        Static4D q = fact.getQuaternion(i);
87
        QuatHelper.rotateVectorByQuat(mTmp,0,0,1,0,q);
88 afd7e804 Leszek Koltunski
        int index = mObject.computeMapsIndex(mTmp);
89 217096bf Leszek Koltunski
        maps[i] = mTextureMaps[index];
90
        }
91
92
      mesh.setTextureMap(maps,0);
93
      }
94
95 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97
    private void computeMove(float[] position)
98
      {
99
      int numCenters = position.length/3;
100
      mUnscaledX=0.0f;
101
      mUnscaledY=0.0f;
102
      mUnscaledZ=0.0f;
103
104
      for(int center=0; center<numCenters; center++)
105
        {
106
        mUnscaledX += position[3*center  ];
107
        mUnscaledY += position[3*center+1];
108
        mUnscaledZ += position[3*center+2];
109
        }
110
111
      mUnscaledX /= numCenters;
112
      mUnscaledY /= numCenters;
113
      mUnscaledZ /= numCenters;
114
      }
115
116 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// PUBLIC API
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120 6c295be1 Leszek Koltunski
    public BandagedCubit(BandagedObject object, float[] position, int variant, Static4D quat1,
121 826abd80 Leszek Koltunski
                         Static4D quat2, Static3D scale, boolean roundCorners)
122 da56b12f Leszek Koltunski
      {
123 afd7e804 Leszek Koltunski
      mObject = object;
124 5d5ed376 Leszek Koltunski
      mRoundCorners = roundCorners;
125 da56b12f Leszek Koltunski
      mPosition = position;
126 903041cd Leszek Koltunski
      mIsAttached = true;
127 1a106eb8 Leszek Koltunski
      mMarkedEffectID = -1;
128 6c295be1 Leszek Koltunski
      mVariant = variant;
129 da56b12f Leszek Koltunski
130 550db260 Leszek Koltunski
      computeMove(mPosition);
131
      mMove = new Static3D(0,0,0);
132 f8c52090 Leszek Koltunski
133
      MeshBase mesh = mObject.createMesh(mPosition,mRoundCorners);
134 da56b12f Leszek Koltunski
135 3b8f5220 Leszek Koltunski
      mTexture = new DistortedTexture();
136 afd7e804 Leszek Koltunski
      if( mBitmap==null ) createBitmap(mObject.getColors());
137 3b8f5220 Leszek Koltunski
      mTexture.setTextureAlreadyInverted(mBitmap);
138 217096bf Leszek Koltunski
139
      resetTextureMaps(mesh);
140 da56b12f Leszek Koltunski
141 77efd5ad Leszek Koltunski
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
142 a76cc4f4 Leszek Koltunski
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
143
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
144 28cb1607 Leszek Koltunski
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
145 da56b12f Leszek Koltunski
146 1a106eb8 Leszek Koltunski
      mEffects = new DistortedEffects();
147
      mEffects.apply(scaleEffect);
148
      mEffects.apply(moveEffect);
149
      mEffects.apply(quat2Effect);
150
      mEffects.apply(quat1Effect);
151 550db260 Leszek Koltunski
152 3b8f5220 Leszek Koltunski
      mNode = new DistortedNode(mTexture,mEffects,mesh);
153 550db260 Leszek Koltunski
      }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 28cb1607 Leszek Koltunski
    public void join(float[] position, float scale)
158 550db260 Leszek Koltunski
      {
159
      int len1 = mPosition.length;
160
      int len2 = position.length;
161 28cb1607 Leszek Koltunski
      float[] tmpPosition = new float[len1+len2];
162
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
163
      System.arraycopy(position , 0, tmpPosition, len1, len2);
164
      mPosition = tmpPosition;
165 6c295be1 Leszek Koltunski
166
      computeMove(mPosition);
167 f8c52090 Leszek Koltunski
      MeshBase mesh = mObject.createMesh(mPosition,mRoundCorners);
168 217096bf Leszek Koltunski
      resetTextureMaps(mesh);
169 28cb1607 Leszek Koltunski
      mNode.setMesh(mesh);
170 550db260 Leszek Koltunski
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
171
      }
172
173 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
    public void reset(float scale)
176
      {
177 16663e27 Leszek Koltunski
      float x = mPosition[0];
178
      float y = mPosition[1];
179
      float z = mPosition[2];
180 e0b71e6e Leszek Koltunski
181
      mPosition = new float[3];
182 16663e27 Leszek Koltunski
      mPosition[0] = x;
183
      mPosition[1] = y;
184
      mPosition[2] = z;
185 e0b71e6e Leszek Koltunski
186
      computeMove(mPosition);
187 f8c52090 Leszek Koltunski
      MeshBase mesh = mObject.createMesh(mPosition,mRoundCorners);
188 33bfd7c9 Leszek Koltunski
      resetTextureMaps(mesh);
189 e0b71e6e Leszek Koltunski
      mNode.setMesh(mesh);
190
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
191
      }
192
193 3b8f5220 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195
    public void recreateBitmap()
196
      {
197 afd7e804 Leszek Koltunski
      if( mBitmap==null ) createBitmap(mObject.getColors());
198 3b8f5220 Leszek Koltunski
      mTexture.setTextureAlreadyInverted(mBitmap);
199
      }
200
201 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
    public void scaleMove(float scale)
204
      {
205
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
206 da56b12f Leszek Koltunski
      }
207
208 c279ea6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210 1a106eb8 Leszek Koltunski
    public void setMarked()
211 c279ea6d Leszek Koltunski
      {
212 3b8f5220 Leszek Koltunski
      if( mMarkedEffectID<0 )
213
        {
214
        FragmentEffectBrightness effect = new FragmentEffectBrightness(mAlpha);
215
        mMarkedEffectID = effect.getID();
216
        mEffects.apply(effect);
217
        }
218 1a106eb8 Leszek Koltunski
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
    public void setUnmarked()
223
      {
224 576afdf9 Leszek Koltunski
      mEffects.abortByType(EffectType.FRAGMENT);
225
      mMarkedEffectID = -1;
226 c279ea6d Leszek Koltunski
      }
227
228 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
    public void detach()
231
      {
232
      mIsAttached = false;
233
      }
234
235 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
    public void attach()
238
      {
239
      mIsAttached = true;
240
      }
241
242 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
    public boolean isAttached()
245
      {
246
      return mIsAttached;
247
      }
248
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
251
    public float[] getPosition()
252
      {
253
      return mPosition;
254
      }
255
256 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258
    public DistortedNode getNode()
259
      {
260
      return mNode;
261
      }
262
}