Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 462b983d

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