Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ 50ec342b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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 javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.library.main.DistortedNode;
29
import org.distorted.library.main.DistortedScreen;
30

    
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
37
{
38
   static final int COLOR_DEFAULT = 0xffffff55;
39
   static final int COLOR_MARKED  = 0xffff0000;
40

    
41
   static final float SCREEN_RATIO = 0.5f;
42
   static final float OBJECT_SIZE  = 3.0f;
43

    
44
   private final float[][] POSITIONS = new float[][]
45
        {
46
          {-1,  1,  1},
47
          {-1,  1,  0},
48
          {-1,  1, -1},
49
          {-1,  0,  1},
50
          {-1,  0,  0},
51
          {-1,  0, -1},
52
          {-1, -1,  1},
53
          {-1, -1,  0},
54
          {-1, -1, -1},
55
          { 0, -1,  1},
56
          { 0, -1,  0},
57
          { 0,  1,  1},
58
          { 0,  1,  0},
59
          { 0,  1, -1},
60
          { 0,  0,  1},
61
          { 0,  0, -1},
62
          { 1,  1,  1},
63
          { 1,  1,  0},
64
          { 1,  1, -1},
65
          { 1,  0,  1},
66
          { 1,  0,  0},
67
          { 1, -1,  1},
68
          { 1,  0, -1},
69
          { 1, -1, -1},
70
          { 1, -1,  0},
71
          { 0, -1, -1},
72
        };
73

    
74
   private final BandagedCreatorView mView;
75
   private final DistortedScreen mScreen;
76
   private final Static3D mScale;
77
   private final BandagedCubit[] mCubits;
78
   private final Static4D mQuatT, mQuatA;
79
   private float mScaleValue;
80

    
81
   private float mX, mY, mZ, mW;
82
   private boolean mResetQuats, mSetQuatT;
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   BandagedCreatorRenderer(BandagedCreatorView v)
87
     {
88
     final float BRIGHTNESS = 0.333f;
89

    
90
     mQuatT = new Static4D(0,0,0,1);
91
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
92

    
93
     mView = v;
94

    
95
     mResetQuats = false;
96
     mSetQuatT   = false;
97

    
98
     mScreen = new DistortedScreen();
99
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
100
     mScale = new Static3D(1,1,1);
101
     mCubits= createCubits();
102
     }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
   private BandagedCubit[] createCubits()
107
     {
108
     int len = POSITIONS.length;
109
     BandagedCubit[] cubits = new BandagedCubit[len];
110

    
111
     for(int c=0; c<len; c++)
112
       {
113
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuatT,mQuatA,mScale,COLOR_DEFAULT);
114
       }
115

    
116
     return cubits;
117
     }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
   @Override
122
   public void onDrawFrame(GL10 glUnused)
123
     {
124
     long time = System.currentTimeMillis();
125
     mScreen.render(time);
126

    
127
     if( mSetQuatT )
128
       {
129
       mSetQuatT = false;
130
       mQuatT.set(mX,mY,mZ,mW);
131
       }
132

    
133
     if( mResetQuats )
134
       {
135
       mResetQuats = false;
136

    
137
       float qx = mQuatT.get0();
138
       float qy = mQuatT.get1();
139
       float qz = mQuatT.get2();
140
       float qw = mQuatT.get3();
141

    
142
       float rx = mQuatA.get0();
143
       float ry = mQuatA.get1();
144
       float rz = mQuatA.get2();
145
       float rw = mQuatA.get3();
146

    
147
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
148
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
149
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
150
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
151

    
152
       mQuatT.set(0f, 0f, 0f, 1f);
153
       mQuatA.set(tx, ty, tz, tw);
154
       }
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
   @Override
160
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
161
      {
162
      final float Q = SCREEN_RATIO/OBJECT_SIZE;
163
      mScaleValue = width<height ? Q*width : Q*height;
164

    
165
      mScreen.detachAll();
166
      int len = POSITIONS.length;
167

    
168
      for(int i=0; i<len; i++)
169
        if( mCubits[i].isAttached() )
170
          {
171
          mCubits[i].scaleMove(mScaleValue);
172
          mCubits[i].setTexture(COLOR_DEFAULT);
173
          DistortedNode node = mCubits[i].getNode();
174
          mScreen.attach(node);
175
          }
176

    
177
      mScale.set( mScaleValue,mScaleValue,mScaleValue );
178
      mView.setScreenSize(width,height);
179
      mScreen.resize(width,height);
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
   @Override
185
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
186
      {
187
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
   public void distortedException(Exception ex)
193
     {
194
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   public BandagedCubit[] getCubits()
200
     {
201
     return mCubits;
202
     }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
   public DistortedScreen getScreen()
207
     {
208
     return mScreen;
209
     }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
   private boolean isAdjacent(float[] pos1, float[] pos2)
214
     {
215
     int len1 = pos1.length/3;
216
     int len2 = pos2.length/3;
217

    
218
     for(int i=0; i<len1; i++)
219
       for(int j=0; j<len2; j++)
220
         {
221
         float d0 = pos1[3*i  ] - pos2[3*j  ];
222
         float d1 = pos1[3*i+1] - pos2[3*j+1];
223
         float d2 = pos1[3*i+2] - pos2[3*j+2];
224

    
225
         if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true;
226
         }
227

    
228
     return false;
229
     }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
   public void tryConnectingCubits(int index1, int index2)
234
     {
235
     if( index1!=index2 )
236
       {
237
       float[] pos1 = mCubits[index1].getPosition();
238
       float[] pos2 = mCubits[index2].getPosition();
239

    
240
       if( isAdjacent(pos1,pos2) )
241
         {
242
         mCubits[index2].join(pos1,mScaleValue);
243
         mCubits[index1].detach();
244
         mScreen.detach(mCubits[index1].getNode());
245
         }
246
       }
247
     }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
   public Static4D getQuatAccu()
252
     {
253
     return mQuatA;
254
     }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
   public void setQuatTemp(float x, float y, float z, float w)
259
     {
260
     mX = x;
261
     mY = y;
262
     mZ = z;
263
     mW = w;
264

    
265
     mSetQuatT = true;
266
     }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
   public void resetQuats()
271
     {
272
     mResetQuats = true;
273
     }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
   public void reset()
278
     {
279

    
280
     }
281
}
(2-2/11)