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[] mMove;
|
42
|
|
43
|
private final float[][] POSITIONS = new float[][]
|
44
|
{
|
45
|
{-1.0f, +1.0f, +1.0f},
|
46
|
{-1.0f, +1.0f, +0.0f},
|
47
|
{-1.0f, +1.0f, -1.0f},
|
48
|
{-1.0f, 0.0f, +1.0f},
|
49
|
{-1.0f, 0.0f, +0.0f},
|
50
|
{-1.0f, 0.0f, -1.0f},
|
51
|
{-1.0f, -1.0f, +1.0f},
|
52
|
{-1.0f, -1.0f, +0.0f},
|
53
|
{-1.0f, -1.0f, -1.0f},
|
54
|
{ 0.0f, -1.0f, +1.0f},
|
55
|
{ 0.0f, -1.0f, +0.0f},
|
56
|
{ 0.0f, +1.0f, +1.0f},
|
57
|
{ 0.0f, +1.0f, +0.0f},
|
58
|
{ 0.0f, +1.0f, -1.0f},
|
59
|
{ 0.0f, 0.0f, +1.0f},
|
60
|
{ 0.0f, 0.0f, -1.0f},
|
61
|
{ 1.0f, +1.0f, +1.0f},
|
62
|
{ 1.0f, +1.0f, +0.0f},
|
63
|
{ 1.0f, +1.0f, -1.0f},
|
64
|
{ 1.0f, 0.0f, +1.0f},
|
65
|
{ 1.0f, 0.0f, +0.0f},
|
66
|
{ 1.0f, -1.0f, +1.0f},
|
67
|
{ 1.0f, 0.0f, -1.0f },
|
68
|
{ 1.0f, -1.0f, -1.0f },
|
69
|
{ 1.0f, -1.0f, +0.0f },
|
70
|
{ 0.0f, -1.0f, -1.0f },
|
71
|
};
|
72
|
|
73
|
Static4D mQuat1, mQuat2;
|
74
|
int mScreenMin;
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
BandagedCreatorRenderer(BandagedCreatorView v)
|
79
|
{
|
80
|
final float BRIGHTNESS = 0.333f;
|
81
|
|
82
|
mQuat1 = new Static4D(0,0,0,1);
|
83
|
mQuat2 = new Static4D(0,0,0,1);
|
84
|
|
85
|
mView = v;
|
86
|
|
87
|
mScreen = new DistortedScreen();
|
88
|
mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
|
89
|
mScale= new Static3D(1,1,1);
|
90
|
|
91
|
int len = POSITIONS.length;
|
92
|
mMove = new Static3D[len];
|
93
|
|
94
|
for(int i=0; i<len; i++) mMove[i] = new Static3D(0,0,0);
|
95
|
|
96
|
BandagedCubit[] cubits = createCubits();
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
private BandagedCubit[] createCubits()
|
102
|
{
|
103
|
int len = POSITIONS.length;
|
104
|
BandagedCubit[] cubits = new BandagedCubit[len];
|
105
|
|
106
|
for(int c=0; c<len; c++)
|
107
|
{
|
108
|
computeMove(mMove[c],POSITIONS[c]);
|
109
|
cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,mMove[c],mScale);
|
110
|
DistortedNode node = cubits[c].getNode();
|
111
|
mScreen.attach(node);
|
112
|
}
|
113
|
|
114
|
return cubits;
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
private void computeMove(Static3D move, float[] position)
|
120
|
{
|
121
|
int numCenters = position.length/3;
|
122
|
float totalX=0.0f, totalY=0.0f, totalZ=0.0f;
|
123
|
|
124
|
for(int center=0; center<numCenters; center++)
|
125
|
{
|
126
|
totalX += position[3*center ];
|
127
|
totalY += position[3*center+1];
|
128
|
totalZ += position[3*center+2];
|
129
|
}
|
130
|
|
131
|
totalX /= numCenters;
|
132
|
totalY /= numCenters;
|
133
|
totalZ /= numCenters;
|
134
|
|
135
|
move.set(totalX,totalY,totalZ);
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
@Override
|
141
|
public void onDrawFrame(GL10 glUnused)
|
142
|
{
|
143
|
long time = System.currentTimeMillis();
|
144
|
mScreen.render(time);
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
@Override
|
150
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
151
|
{
|
152
|
final float Q = 0.15f;
|
153
|
float scale = width<height ? Q*width : Q*height;
|
154
|
|
155
|
int len = POSITIONS.length;
|
156
|
|
157
|
for(int i=0; i<len; i++)
|
158
|
{
|
159
|
float x = mMove[i].get0();
|
160
|
float y = mMove[i].get1();
|
161
|
float z = mMove[i].get2();
|
162
|
|
163
|
mMove[i].set(x*scale,y*scale,z*scale);
|
164
|
}
|
165
|
|
166
|
mScale.set( scale,scale,scale );
|
167
|
mScreenMin = Math.min(width, height);
|
168
|
mScreen.resize(width,height);
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
@Override
|
174
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
175
|
{
|
176
|
DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
public void distortedException(Exception ex)
|
182
|
{
|
183
|
android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
|
184
|
}
|
185
|
}
|