Project

General

Profile

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

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

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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 77efd5ad Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25 9530f6b0 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.library.main.DistortedLibrary;
28 da56b12f Leszek Koltunski
import org.distorted.library.main.DistortedNode;
29 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
30
31 77efd5ad Leszek Koltunski
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33 9530f6b0 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
public class BandagedCreatorRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
37
{
38 77efd5ad Leszek Koltunski
   private final GLSurfaceView mView;
39 9530f6b0 Leszek Koltunski
   private final DistortedScreen mScreen;
40 77efd5ad Leszek Koltunski
   private final Static3D mScale;
41 c279ea6d Leszek Koltunski
   private final Static3D mMoveWhole;
42 a76cc4f4 Leszek Koltunski
   private final Static3D[] mMove;
43 c279ea6d Leszek Koltunski
   private final Static3D[] mMoveUnscaled;
44
   private final BandagedCubit[] mCubits;
45
46
   private final int COLOR_DEFAULT = 0xffffff55;
47 9530f6b0 Leszek Koltunski
48 77efd5ad Leszek Koltunski
   private final float[][] POSITIONS = new float[][]
49 da56b12f Leszek Koltunski
        {
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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
   BandagedCreatorRenderer(BandagedCreatorView v)
84
     {
85
     final float BRIGHTNESS = 0.333f;
86
87 da56b12f Leszek Koltunski
     mQuat1 = new Static4D(0,0,0,1);
88 c279ea6d Leszek Koltunski
     mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
89 da56b12f Leszek Koltunski
90 9530f6b0 Leszek Koltunski
     mView = v;
91 77efd5ad Leszek Koltunski
92 9530f6b0 Leszek Koltunski
     mScreen = new DistortedScreen();
93
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
94 77efd5ad Leszek Koltunski
     mScale= new Static3D(1,1,1);
95 c279ea6d Leszek Koltunski
     mMoveWhole = new Static3D(0,0,0);
96 da56b12f Leszek Koltunski
97 a76cc4f4 Leszek Koltunski
     int len = POSITIONS.length;
98 c279ea6d Leszek Koltunski
     mMove         = new Static3D[len];
99
     mMoveUnscaled = new Static3D[len];
100 a76cc4f4 Leszek Koltunski
101 c279ea6d Leszek Koltunski
     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 a76cc4f4 Leszek Koltunski
107 c279ea6d Leszek Koltunski
     mCubits = createCubits();
108 da56b12f Leszek Koltunski
     }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111 a76cc4f4 Leszek Koltunski
112 da56b12f Leszek Koltunski
   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 c279ea6d Leszek Koltunski
       computeMove(mMoveUnscaled[c],POSITIONS[c]);
120
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMove[c],mMoveWhole,mScale, COLOR_DEFAULT);
121 da56b12f Leszek Koltunski
       }
122
123
     return cubits;
124 9530f6b0 Leszek Koltunski
     }
125 a76cc4f4 Leszek Koltunski
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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a76cc4f4 Leszek Koltunski
      final float Q = 0.15f;
162
      float scale = width<height ? Q*width : Q*height;
163
164 c279ea6d Leszek Koltunski
      mScreen.detachAll();
165 a76cc4f4 Leszek Koltunski
      int len = POSITIONS.length;
166
167
      for(int i=0; i<len; i++)
168
        {
169 c279ea6d Leszek Koltunski
        float x = mMoveUnscaled[i].get0();
170
        float y = mMoveUnscaled[i].get1();
171
        float z = mMoveUnscaled[i].get2();
172 a76cc4f4 Leszek Koltunski
173
        mMove[i].set(x*scale,y*scale,z*scale);
174 c279ea6d Leszek Koltunski
175
        mCubits[i].setTexture(COLOR_DEFAULT);
176
        DistortedNode node = mCubits[i].getNode();
177
        mScreen.attach(node);
178 a76cc4f4 Leszek Koltunski
        }
179 77efd5ad Leszek Koltunski
180 c279ea6d Leszek Koltunski
      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 642d9162 Leszek Koltunski
      mScale.set( scale,scale,scale );
186 da56b12f Leszek Koltunski
      mScreenMin = Math.min(width, height);
187 9530f6b0 Leszek Koltunski
      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
}