Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 6c295be1

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import android.graphics.Bitmap;
13
import android.graphics.Canvas;
14
import android.graphics.Paint;
15

    
16
import org.distorted.library.effect.EffectType;
17
import org.distorted.library.effect.FragmentEffectBrightness;
18
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
import org.distorted.library.helpers.QuatHelper;
25
import org.distorted.library.mesh.MeshBase;
26
import org.distorted.library.type.Static1D;
27
import org.distorted.library.type.Static3D;
28
import org.distorted.library.type.Static4D;
29
import org.distorted.objectlib.helpers.FactoryCubit;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class BandagedCubit
34
{
35
    private static final Static3D CENTER = new Static3D(0,0,0);
36
    private static final float[] mTmp = new float[4];
37
    private static final Static1D mAlpha = new Static1D(2.0f);
38
    private static Bitmap mBitmap;
39
    private static Static4D[] mTextureMaps;
40

    
41
    private final BandagedObject mObject;
42
    private final DistortedNode mNode;
43
    private final DistortedEffects mEffects;
44
    private final DistortedTexture mTexture;
45
    private final Static3D mMove;
46
    private final int mVariant;
47
    private final boolean mRoundCorners;
48

    
49
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
50
    private float[] mPosition;
51
    private boolean mIsAttached;
52
    private long mMarkedEffectID;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    private static void createBitmap(int[] colors)
57
      {
58
      final int NUM = colors.length;
59
      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
        paint.setColor(colors[color]);
71
        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
        int index = mObject.computeMapsIndex(mTmp);
89
        maps[i] = mTextureMaps[index];
90
        }
91

    
92
      mesh.setTextureMap(maps,0);
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// PUBLIC API
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    public BandagedCubit(BandagedObject object, float[] position, int variant, Static4D quat1,
121
                         Static4D quat2, Static3D scale, boolean roundCorners)
122
      {
123
      mObject = object;
124
      mRoundCorners = roundCorners;
125
      mPosition = position;
126
      mIsAttached = true;
127
      mMarkedEffectID = -1;
128
      mVariant = variant;
129

    
130
      computeMove(mPosition);
131
      mMove = new Static3D(0,0,0);
132
      MeshBase mesh = mObject.createMesh(mVariant,mPosition,mRoundCorners);
133

    
134
      mTexture = new DistortedTexture();
135
      if( mBitmap==null ) createBitmap(mObject.getColors());
136
      mTexture.setTextureAlreadyInverted(mBitmap);
137

    
138
      resetTextureMaps(mesh);
139

    
140
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
141
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
142
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
143
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
144

    
145
      mEffects = new DistortedEffects();
146
      mEffects.apply(scaleEffect);
147
      mEffects.apply(moveEffect);
148
      mEffects.apply(quat2Effect);
149
      mEffects.apply(quat1Effect);
150

    
151
      mNode = new DistortedNode(mTexture,mEffects,mesh);
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    public void join(float[] position, float scale)
157
      {
158
      int len1 = mPosition.length;
159
      int len2 = position.length;
160
      float[] tmpPosition = new float[len1+len2];
161
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
162
      System.arraycopy(position , 0, tmpPosition, len1, len2);
163
      mPosition = tmpPosition;
164

    
165
      computeMove(mPosition);
166
      MeshBase mesh = mObject.createMesh(mVariant,mPosition,mRoundCorners);
167
      resetTextureMaps(mesh);
168
      mNode.setMesh(mesh);
169
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    public void reset(float scale)
175
      {
176
      float x = mPosition[0];
177
      float y = mPosition[1];
178
      float z = mPosition[2];
179

    
180
      mPosition = new float[3];
181
      mPosition[0] = x;
182
      mPosition[1] = y;
183
      mPosition[2] = z;
184

    
185
      computeMove(mPosition);
186
      MeshBase mesh = mObject.createMesh(mVariant,mPosition,mRoundCorners);
187
      resetTextureMaps(mesh);
188
      mNode.setMesh(mesh);
189
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
    public void recreateBitmap()
195
      {
196
      if( mBitmap==null ) createBitmap(mObject.getColors());
197
      mTexture.setTextureAlreadyInverted(mBitmap);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    public void scaleMove(float scale)
203
      {
204
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    public void setMarked()
210
      {
211
      if( mMarkedEffectID<0 )
212
        {
213
        FragmentEffectBrightness effect = new FragmentEffectBrightness(mAlpha);
214
        mMarkedEffectID = effect.getID();
215
        mEffects.apply(effect);
216
        }
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    public void setUnmarked()
222
      {
223
      mEffects.abortByType(EffectType.FRAGMENT);
224
      mMarkedEffectID = -1;
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    public void detach()
230
      {
231
      mIsAttached = false;
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    public void attach()
237
      {
238
      mIsAttached = true;
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
    public boolean isAttached()
244
      {
245
      return mIsAttached;
246
      }
247

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

    
250
    public float[] getPosition()
251
      {
252
      return mPosition;
253
      }
254

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

    
257
    public DistortedNode getNode()
258
      {
259
      return mNode;
260
      }
261
}
262

    
(8-8/16)