Project

General

Profile

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

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

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 28cb1607 Leszek Koltunski
    private final DistortedNode mNode;
40 da56b12f Leszek Koltunski
    private final DistortedTexture mTexture;
41 550db260 Leszek Koltunski
    private final Static3D mMove;
42 5d5ed376 Leszek Koltunski
    private final boolean mRoundCorners;
43 550db260 Leszek Koltunski
44
    private float mUnscaledX, mUnscaledY, mUnscaledZ;
45 28cb1607 Leszek Koltunski
    private float[] mPosition;
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 5d5ed376 Leszek Koltunski
    public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, Static3D scale, int color, boolean roundCorners)
74 da56b12f Leszek Koltunski
      {
75 5d5ed376 Leszek Koltunski
      mRoundCorners = roundCorners;
76 da56b12f Leszek Koltunski
      mPosition = position;
77 903041cd Leszek Koltunski
      mIsAttached = true;
78 da56b12f Leszek Koltunski
79 550db260 Leszek Koltunski
      computeMove(mPosition);
80
      mMove = new Static3D(0,0,0);
81
82 da56b12f Leszek Koltunski
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
83 5d5ed376 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
84 da56b12f Leszek Koltunski
85
      mTexture = new DistortedTexture();
86 c279ea6d Leszek Koltunski
      mTexture.setColorARGB(color);
87 da56b12f Leszek Koltunski
88 77efd5ad Leszek Koltunski
      MatrixEffectScale scaleEffect = new MatrixEffectScale(scale);
89 a76cc4f4 Leszek Koltunski
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
90
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
91 28cb1607 Leszek Koltunski
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
92 da56b12f Leszek Koltunski
93 5d5ed376 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
94
      effects.apply(scaleEffect);
95
      effects.apply(moveEffect);
96
      effects.apply(quat2Effect);
97
      effects.apply(quat1Effect);
98 550db260 Leszek Koltunski
99 5d5ed376 Leszek Koltunski
      mNode = new DistortedNode(mTexture,effects,mesh);
100 550db260 Leszek Koltunski
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 28cb1607 Leszek Koltunski
    public void join(float[] position, float scale)
105 550db260 Leszek Koltunski
      {
106
      int len1 = mPosition.length;
107
      int len2 = position.length;
108
109 28cb1607 Leszek Koltunski
      float[] tmpPosition = new float[len1+len2];
110 da56b12f Leszek Koltunski
111 28cb1607 Leszek Koltunski
      System.arraycopy(mPosition, 0, tmpPosition,    0, len1);
112
      System.arraycopy(position , 0, tmpPosition, len1, len2);
113 550db260 Leszek Koltunski
114 28cb1607 Leszek Koltunski
      computeMove(tmpPosition);
115
      mPosition = tmpPosition;
116 550db260 Leszek Koltunski
117 28cb1607 Leszek Koltunski
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
118 5d5ed376 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
119 28cb1607 Leszek Koltunski
      mNode.setMesh(mesh);
120 550db260 Leszek Koltunski
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
121
      }
122
123 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
    public void reset(float scale)
126
      {
127
      float x0 = mPosition[0];
128
      float x1 = mPosition[1];
129
      float x2 = mPosition[2];
130
131
      mPosition = new float[3];
132
      mPosition[0] = x0;
133
      mPosition[1] = x1;
134
      mPosition[2] = x2;
135
136
      computeMove(mPosition);
137
138
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
139 5d5ed376 Leszek Koltunski
      MeshBase mesh = factory.createMesh(mPosition,false,mRoundCorners);
140 e0b71e6e Leszek Koltunski
      mNode.setMesh(mesh);
141
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
142
      }
143
144 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    public void scaleMove(float scale)
147
      {
148
      mMove.set( scale*mUnscaledX, scale*mUnscaledY, scale*mUnscaledZ);
149 da56b12f Leszek Koltunski
      }
150
151 c279ea6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    public void setTexture(int color)
154
      {
155
      mTexture.setColorARGB(color);
156
      }
157
158 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
    public void detach()
161
      {
162
      mIsAttached = false;
163
      }
164
165 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
    public void attach()
168
      {
169
      mIsAttached = true;
170
      }
171
172 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
    public boolean isAttached()
175
      {
176
      return mIsAttached;
177
      }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
    public float[] getPosition()
182
      {
183
      return mPosition;
184
      }
185
186 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
    public DistortedNode getNode()
189
      {
190
      return mNode;
191
      }
192
}