1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted 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
|
// Distorted 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 Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.examples.rubik;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.content.res.Resources;
|
26
|
import android.opengl.GLSurfaceView;
|
27
|
|
28
|
import org.distorted.library.effect.VertexEffectSink;
|
29
|
import org.distorted.library.main.DistortedLibrary;
|
30
|
import org.distorted.library.main.DistortedScreen;
|
31
|
import org.distorted.library.message.EffectListener;
|
32
|
import org.distorted.library.type.Static3D;
|
33
|
|
34
|
import java.io.InputStream;
|
35
|
|
36
|
import javax.microedition.khronos.egl.EGLConfig;
|
37
|
import javax.microedition.khronos.opengles.GL10;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
|
42
|
{
|
43
|
private static final float CUBE_SCREEN_RATIO = 0.5f;
|
44
|
private static final float CAMERA_DISTANCE = 0.6f; // 0.6 of the length of max(scrHeight,scrWidth)
|
45
|
private static final int MIN_CUBE_SIZE = 1;
|
46
|
private static final int MAX_CUBE_SIZE = 6;
|
47
|
|
48
|
private final RubikSurfaceView mView;
|
49
|
private final DistortedScreen mScreen;
|
50
|
private final Static3D mScale;
|
51
|
private final Resources mResources;
|
52
|
|
53
|
private RubikCube mCube;
|
54
|
private int mNextCubeSize;
|
55
|
private boolean mChangeCubeSizeNow;
|
56
|
private int mNumCube;
|
57
|
private int mScreenWidth, mScreenHeight;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
RubikRenderer(RubikSurfaceView v)
|
62
|
{
|
63
|
mView = v;
|
64
|
mResources = v.getResources();
|
65
|
mScreen = new DistortedScreen();
|
66
|
|
67
|
mScreenWidth = mScreenHeight = 0;
|
68
|
|
69
|
mScale = new Static3D(1,1,1);
|
70
|
|
71
|
mNextCubeSize= MIN_CUBE_SIZE;
|
72
|
mChangeCubeSizeNow = false;
|
73
|
|
74
|
mNumCube = 0;
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
// change the cube size here, 'after the next render' as not to cause artifacts.
|
79
|
|
80
|
public void onDrawFrame(GL10 glUnused)
|
81
|
{
|
82
|
mScreen.render( System.currentTimeMillis() );
|
83
|
|
84
|
if( mChangeCubeSizeNow )
|
85
|
{
|
86
|
mChangeCubeSizeNow = false;
|
87
|
createNextCube();
|
88
|
}
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public void effectFinished(final long effectID)
|
94
|
{
|
95
|
mNextCubeSize++;
|
96
|
if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
|
97
|
mChangeCubeSizeNow = true;
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
103
|
{
|
104
|
float cameraDistance = CAMERA_DISTANCE*(Math.max(width,height));
|
105
|
float fovInDegrees = computeFOV(cameraDistance,height);
|
106
|
|
107
|
mScreen.setProjection( fovInDegrees, 0.1f);
|
108
|
mScreen.resize(width, height);
|
109
|
|
110
|
recomputeScaleFactor(width,height);
|
111
|
|
112
|
mScreenHeight = height;
|
113
|
mScreenWidth = width;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
119
|
{
|
120
|
createNextCube();
|
121
|
VertexEffectSink.enable();
|
122
|
DistortedLibrary.onSurfaceCreated(this);
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private void createNextCube()
|
128
|
{
|
129
|
if( mCube!=null ) mCube.releaseResources();
|
130
|
mCube = new RubikCube(mNextCubeSize, mScale);
|
131
|
mCube.createTexture();
|
132
|
|
133
|
if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
|
134
|
|
135
|
mScreen.detachAll();
|
136
|
mCube.attachToScreen(mScreen);
|
137
|
mCube.addRotation(this);
|
138
|
RubikActivity act = mView.getRubikActivity();
|
139
|
act.setText(++mNumCube);
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
private float computeFOV(float cameraDistance, int screenHeight)
|
145
|
{
|
146
|
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
|
147
|
return (float)(2*halfFOVInRadians*(180/Math.PI));
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private void recomputeScaleFactor(int screenWidth, int screenHeight)
|
153
|
{
|
154
|
float scaleFactor = CUBE_SCREEN_RATIO * Math.min(screenWidth,screenHeight) / mCube.getSize();
|
155
|
mScale.set(scaleFactor,scaleFactor,scaleFactor);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void distortedException(Exception ex)
|
161
|
{
|
162
|
android.util.Log.e("Rubik", ex.getMessage() );
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public int openGlVersion()
|
168
|
{
|
169
|
Context context = mView.getContext();
|
170
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
171
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
172
|
int glESversion = configurationInfo.reqGlEsVersion;
|
173
|
int major = glESversion >> 16;
|
174
|
int minor = glESversion & 0xff;
|
175
|
|
176
|
return 100*major + 10*minor;
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
public InputStream localFile(int fileID)
|
182
|
{
|
183
|
return mResources.openRawResource(fileID);
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public void logMessage(String message)
|
189
|
{
|
190
|
android.util.Log.e("Rubik", message );
|
191
|
}
|
192
|
}
|