Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCubit.java @ 550db260

1 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 org.distorted.library.effect.MatrixEffectMove;
23
import org.distorted.library.effect.MatrixEffectQuaternion;
24
import org.distorted.library.effect.MatrixEffectScale;
25
import org.distorted.library.main.DistortedEffects;
26
import org.distorted.library.main.DistortedNode;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshBase;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31
import org.distorted.objectlib.helpers.FactoryBandaged3x3Cubit;
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35
public class BandagedCubit
36
{
37
    private static final Static3D CENTER = new Static3D(0,0,0);
38
39 550db260 Leszek Koltunski
    private DistortedNode mNode, mNewNode;
40 da56b12f Leszek Koltunski
    private final DistortedTexture mTexture;
41
    private final DistortedEffects mEffects;
42 550db260 Leszek Koltunski
    private final Static3D mMove;
43
44
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
45
    private float[] mPosition, mNewPosition;
46 903041cd Leszek Koltunski
    private boolean mIsAttached;
47 da56b12f Leszek Koltunski
48 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
    private void computeMove(float[] position)
51
      {
52
      int numCenters = position.length/3;
53
      mUnscaledX=0.0f;
54
      mUnscaledY=0.0f;
55
      mUnscaledZ=0.0f;
56
57
      for(int center=0; center<numCenters; center++)
58
        {
59
        mUnscaledX += position[3*center  ];
60
        mUnscaledY += position[3*center+1];
61
        mUnscaledZ += position[3*center+2];
62
        }
63
64
      mUnscaledX /= numCenters;
65
      mUnscaledY /= numCenters;
66
      mUnscaledZ /= numCenters;
67
      }
68
69 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// PUBLIC API
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73 550db260 Leszek Koltunski
    public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D moveY, Static3D scale, int color)
74 da56b12f Leszek Koltunski
      {
75
      mPosition = position;
76 903041cd Leszek Koltunski
      mIsAttached = true;
77 da56b12f Leszek Koltunski
78 550db260 Leszek Koltunski
      computeMove(mPosition);
79
      mMove = new Static3D(0,0,0);
80
81 da56b12f Leszek Koltunski
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
82 550db260 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition);
83 da56b12f Leszek Koltunski
84
      mTexture = new DistortedTexture();
85 c279ea6d Leszek Koltunski
      mTexture.setColorARGB(color);
86 da56b12f Leszek Koltunski
87 77efd5ad Leszek Koltunski
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
88 a76cc4f4 Leszek Koltunski
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
89
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
90 550db260 Leszek Koltunski
      MatrixEffectMove moveSEffect = new MatrixEffectMove(mMove);
91
      MatrixEffectMove moveYEffect = new MatrixEffectMove(moveY);
92 da56b12f Leszek Koltunski
93
      mEffects = new DistortedEffects();
94
      mEffects.apply(scaleEffect);
95 550db260 Leszek Koltunski
      mEffects.apply(moveSEffect);
96 a76cc4f4 Leszek Koltunski
      mEffects.apply(quat2Effect);
97
      mEffects.apply(quat1Effect);
98 550db260 Leszek Koltunski
      mEffects.apply(moveYEffect);
99
100
      mNode = new DistortedNode(mTexture,mEffects,mesh);
101
      }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
    public void prepareJoin(float[] position)
106
      {
107
      int len1 = mPosition.length;
108
      int len2 = position.length;
109
110
      mNewPosition = new float[len1+len2];
111 da56b12f Leszek Koltunski
112 550db260 Leszek Koltunski
      System.arraycopy(mPosition, 0, mNewPosition,    0, len1);
113
      System.arraycopy(position , 0, mNewPosition, len1, len2);
114
115
/*
116
int l= mPosition.length/3;
117
String s="";
118
for(int i=0; i<l; i++) s += (mPosition[3*i]+" "+mPosition[3*i+1]+" "+mPosition[3*i+2]+"\n");
119
android.util.Log.e("D", "pos after joining: \n"+s);
120
*/
121
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
122
      MeshBase mesh = factory.createMesh(mNewPosition);
123
      mNewNode = new DistortedNode(mTexture,mEffects,mesh);
124
125
      computeMove(mNewPosition);
126
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
    public void swapNodes(float scale)
131
      {
132
      mPosition = mNewPosition;
133
      mNode = mNewNode;
134
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
135
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
    public void scaleMove(float scale)
140
      {
141
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
142 da56b12f Leszek Koltunski
      }
143
144 c279ea6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    public void setTexture(int color)
147
      {
148
      mTexture.setColorARGB(color);
149
      }
150
151 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    public void detach()
154
      {
155
      mIsAttached = false;
156
      }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
    public boolean isAttached()
161
      {
162
      return mIsAttached;
163
      }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
    public float[] getPosition()
168
      {
169
      return mPosition;
170
      }
171
172 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
    public DistortedNode getNode()
175
      {
176
      return mNode;
177
      }
178
}