Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / bandaged / BandagedCubit.java @ 6612cbb4

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.objectlib.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 boolean mRoundCorners;
47

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// PUBLIC API
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
128
      computeMove(mPosition);
129
      mMove = new Static3D(0,0,0);
130

    
131
      MeshBase mesh = mObject.createMesh(mPosition,mRoundCorners);
132

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

    
137
      resetTextureMaps(mesh);
138

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

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

    
(1-1/8)