Project

General

Profile

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

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

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
   private final BandagedCreatorView mView;
39
   private final DistortedScreen mScreen;
40
   private final Static3D mScale;
41
   private final Static3D mMoveY;
42
   private final BandagedCubit[] mCubits;
43
   private float mScaleValue;
44

    
45
   static final int COLOR_DEFAULT = 0xffffff55;
46
   static final int COLOR_MARKED  = 0xffff0000;
47

    
48
   static final float SCREEN_RATIO = 0.5f;
49
   static final float OBJECT_SIZE  = 3.0f;
50

    
51
   private final float[][] POSITIONS = new float[][]
52
        {
53
          {-1.0f, +1.0f, +1.0f},
54
          {-1.0f, +1.0f, +0.0f},
55
          {-1.0f, +1.0f, -1.0f},
56
          {-1.0f,  0.0f, +1.0f},
57
          {-1.0f,  0.0f, +0.0f},
58
          {-1.0f,  0.0f, -1.0f},
59
          {-1.0f, -1.0f, +1.0f},
60
          {-1.0f, -1.0f, +0.0f},
61
          {-1.0f, -1.0f, -1.0f},
62
          { 0.0f, -1.0f, +1.0f},
63
          { 0.0f, -1.0f, +0.0f},
64
          { 0.0f, +1.0f, +1.0f},
65
          { 0.0f, +1.0f, +0.0f},
66
          { 0.0f, +1.0f, -1.0f},
67
          { 0.0f,  0.0f, +1.0f},
68
          { 0.0f,  0.0f, -1.0f},
69
          { 1.0f, +1.0f, +1.0f},
70
          { 1.0f, +1.0f, +0.0f},
71
          { 1.0f, +1.0f, -1.0f},
72
          { 1.0f,  0.0f, +1.0f},
73
          { 1.0f,  0.0f, +0.0f},
74
          { 1.0f, -1.0f, +1.0f},
75
          { 1.0f,  0.0f, -1.0f },
76
          { 1.0f, -1.0f, -1.0f },
77
          { 1.0f, -1.0f, +0.0f },
78
          { 0.0f, -1.0f, -1.0f },
79
        };
80

    
81
   Static4D mQuat1, mQuat2;
82
   int mScreenMin;
83

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

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

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

    
93
     mView = v;
94

    
95
     mScreen = new DistortedScreen();
96
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
97
     mScale = new Static3D(1,1,1);
98
     mMoveY = new Static3D(0,0,0);
99
     mCubits= createCubits();
100
     }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
     for(int c=0; c<len; c++)
110
       {
111
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMoveY,mScale,COLOR_DEFAULT);
112
       }
113

    
114
     return cubits;
115
     }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
   @Override
129
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
130
      {
131
      final float Q = SCREEN_RATIO/OBJECT_SIZE;
132
      mScaleValue = width<height ? Q*width : Q*height;
133

    
134
      mScreen.detachAll();
135
      int len = POSITIONS.length;
136

    
137
      for(int i=0; i<len; i++)
138
        {
139
        mCubits[i].scaleMove(mScaleValue);
140
        mCubits[i].setTexture(COLOR_DEFAULT);
141
        DistortedNode node = mCubits[i].getNode();
142
        mScreen.attach(node);
143
        }
144

    
145
      float RB = BandagedCreatorActivity.RATIO_BAR;
146
      float RS = BandagedCreatorActivity.RATIO_SCROLL;
147
      float yOffset = (0.5f*RB/(1-RS))*height;
148
      mMoveY.set1(yOffset);
149

    
150
      mScale.set( mScaleValue,mScaleValue,mScaleValue );
151
      mScreenMin = Math.min(width, height);
152
      mView.setScreenSize(width,height, (int)yOffset);
153
      mScreen.resize(width,height);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
   @Override
159
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
160
      {
161
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   public void distortedException(Exception ex)
167
     {
168
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
169
     }
170

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

    
173
   public BandagedCubit[] getCubits()
174
     {
175
     return mCubits;
176
     }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
   public DistortedScreen getScreen()
181
     {
182
     return mScreen;
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
   private boolean isAdjacent(float[] pos1, float[] pos2)
188
     {
189
     int len1 = pos1.length/3;
190
     int len2 = pos2.length/3;
191

    
192
     for(int i=0; i<len1; i++)
193
       for(int j=0; j<len2; j++)
194
         {
195
         float d0 = pos1[3*i  ] - pos2[3*j  ];
196
         float d1 = pos1[3*i+1] - pos2[3*j+1];
197
         float d2 = pos1[3*i+2] - pos2[3*j+2];
198

    
199
         if( d0*d0 + d1*d1 + d2*d2 == 1 ) return true;
200
         }
201

    
202
     return false;
203
     }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   public void tryConnectingCubits(int index1, int index2)
208
     {
209
     if( index1!=index2 )
210
       {
211
       float[] pos1 = mCubits[index1].getPosition();
212
       float[] pos2 = mCubits[index2].getPosition();
213

    
214
       if( isAdjacent(pos1,pos2) )
215
         {
216
         mCubits[index2].prepareJoin(pos1);
217

    
218
         mCubits[index1].detach();
219
         DistortedNode node1 = mCubits[index1].getNode();
220
         mScreen.detach(node1);
221
         DistortedNode node2 = mCubits[index2].getNode();
222
         mScreen.detach(node2);
223
         mCubits[index2].swapNodes(mScaleValue);
224
         DistortedNode node3 = mCubits[index2].getNode();
225
         mScreen.attach(node3);
226
         }
227
       }
228
     }
229
}
(2-2/11)