Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorRenderer.java @ c279ea6d

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 GLSurfaceView mView;
39
   private final DistortedScreen mScreen;
40
   private final Static3D mScale;
41
   private final Static3D mMoveWhole;
42
   private final Static3D[] mMove;
43
   private final Static3D[] mMoveUnscaled;
44
   private final BandagedCubit[] mCubits;
45

    
46
   private final int COLOR_DEFAULT = 0xffffff55;
47

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

    
78
   Static4D mQuat1, mQuat2;
79
   int mScreenMin;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
   BandagedCreatorRenderer(BandagedCreatorView v)
84
     {
85
     final float BRIGHTNESS = 0.333f;
86

    
87
     mQuat1 = new Static4D(0,0,0,1);
88
     mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
89

    
90
     mView = v;
91

    
92
     mScreen = new DistortedScreen();
93
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
94
     mScale= new Static3D(1,1,1);
95
     mMoveWhole = new Static3D(0,0,0);
96

    
97
     int len = POSITIONS.length;
98
     mMove         = new Static3D[len];
99
     mMoveUnscaled = new Static3D[len];
100

    
101
     for(int i=0; i<len; i++)
102
       {
103
       mMove[i] = new Static3D(0,0,0);
104
       mMoveUnscaled[i] = new Static3D(0,0,0);
105
       }
106

    
107
     mCubits = createCubits();
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   private BandagedCubit[] createCubits()
113
     {
114
     int len = POSITIONS.length;
115
     BandagedCubit[] cubits = new BandagedCubit[len];
116

    
117
     for(int c=0; c<len; c++)
118
       {
119
       computeMove(mMoveUnscaled[c],POSITIONS[c]);
120
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMove[c],mMoveWhole,mScale, COLOR_DEFAULT);
121
       }
122

    
123
     return cubits;
124
     }
125

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

    
128
    private void computeMove(Static3D move, float[] position)
129
      {
130
      int numCenters = position.length/3;
131
      float totalX=0.0f, totalY=0.0f, totalZ=0.0f;
132

    
133
      for(int center=0; center<numCenters; center++)
134
        {
135
        totalX += position[3*center  ];
136
        totalY += position[3*center+1];
137
        totalZ += position[3*center+2];
138
        }
139

    
140
      totalX /= numCenters;
141
      totalY /= numCenters;
142
      totalZ /= numCenters;
143

    
144
      move.set(totalX,totalY,totalZ);
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
   @Override
150
   public void onDrawFrame(GL10 glUnused)
151
     {
152
     long time = System.currentTimeMillis();
153
     mScreen.render(time);
154
     }
155

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

    
158
   @Override
159
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
160
      {
161
      final float Q = 0.15f;
162
      float scale = width<height ? Q*width : Q*height;
163

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

    
167
      for(int i=0; i<len; i++)
168
        {
169
        float x = mMoveUnscaled[i].get0();
170
        float y = mMoveUnscaled[i].get1();
171
        float z = mMoveUnscaled[i].get2();
172

    
173
        mMove[i].set(x*scale,y*scale,z*scale);
174

    
175
        mCubits[i].setTexture(COLOR_DEFAULT);
176
        DistortedNode node = mCubits[i].getNode();
177
        mScreen.attach(node);
178
        }
179

    
180
      float RB = BandagedCreatorActivity.RATIO_BAR;
181
      float RS = BandagedCreatorActivity.RATIO_SCROLL;
182
      float yOffset = (0.5f*RB/(1-RS))*height;
183
      mMoveWhole.set1(yOffset);
184

    
185
      mScale.set( scale,scale,scale );
186
      mScreenMin = Math.min(width, height);
187
      mScreen.resize(width,height);
188
      }
189

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

    
192
   @Override
193
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
194
      {
195
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
   public void distortedException(Exception ex)
201
     {
202
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
203
     }
204
}
(2-2/10)