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.opengl.GLSurfaceView;
|
23
|
|
24
|
import org.distorted.library.effect.VertexEffectSink;
|
25
|
import org.distorted.library.main.DistortedLibrary;
|
26
|
import org.distorted.library.main.DistortedScreen;
|
27
|
import org.distorted.library.message.EffectListener;
|
28
|
import org.distorted.library.type.Static3D;
|
29
|
|
30
|
import javax.microedition.khronos.egl.EGLConfig;
|
31
|
import javax.microedition.khronos.opengles.GL10;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
|
36
|
{
|
37
|
private static final float CUBE_SCREEN_RATIO = 0.5f;
|
38
|
private static final float CAMERA_DISTANCE = 0.6f; // 0.6 of the length of max(scrHeight,scrWidth)
|
39
|
private static final int MIN_CUBE_SIZE = 1;
|
40
|
private static final int MAX_CUBE_SIZE = 6;
|
41
|
|
42
|
private RubikSurfaceView mView;
|
43
|
private DistortedScreen mScreen;
|
44
|
private Static3D mScale;
|
45
|
private int mNextCubeSize;
|
46
|
private boolean mChangeCubeSizeNow;
|
47
|
private int mNumCube;
|
48
|
private RubikCube mCube;
|
49
|
|
50
|
private int mScreenWidth, mScreenHeight;
|
51
|
|
52
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53
|
|
54
|
RubikRenderer(RubikSurfaceView v)
|
55
|
{
|
56
|
mView = v;
|
57
|
|
58
|
mScreen = new DistortedScreen();
|
59
|
|
60
|
mScreenWidth = mScreenHeight = 0;
|
61
|
|
62
|
mScale = new Static3D(1,1,1);
|
63
|
|
64
|
mNextCubeSize= MIN_CUBE_SIZE;
|
65
|
mChangeCubeSizeNow = false;
|
66
|
|
67
|
mNumCube = 0;
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
// change the cube size here, 'after the next render' as not to cause artifacts.
|
72
|
|
73
|
public void onDrawFrame(GL10 glUnused)
|
74
|
{
|
75
|
mScreen.render( System.currentTimeMillis() );
|
76
|
|
77
|
if( mChangeCubeSizeNow )
|
78
|
{
|
79
|
mChangeCubeSizeNow = false;
|
80
|
createNextCube();
|
81
|
}
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
public void effectFinished(final long effectID)
|
87
|
{
|
88
|
mNextCubeSize++;
|
89
|
if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
|
90
|
mChangeCubeSizeNow = true;
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
96
|
{
|
97
|
float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
|
98
|
float fovInDegrees = computeFOV(cameraDistance,height);
|
99
|
|
100
|
mScreen.setProjection( fovInDegrees, 0.1f);
|
101
|
mScreen.resize(width, height);
|
102
|
|
103
|
recomputeScaleFactor(width,height);
|
104
|
|
105
|
mScreenHeight = height;
|
106
|
mScreenWidth = width;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
112
|
{
|
113
|
createNextCube();
|
114
|
|
115
|
VertexEffectSink.enable();
|
116
|
|
117
|
try
|
118
|
{
|
119
|
DistortedLibrary.onCreate(mView.getContext());
|
120
|
}
|
121
|
catch(Exception ex)
|
122
|
{
|
123
|
android.util.Log.e("Rubik", ex.getMessage() );
|
124
|
}
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
private void createNextCube()
|
130
|
{
|
131
|
if( mCube!=null ) mCube.releaseResources();
|
132
|
mCube = new RubikCube(mNextCubeSize, mScale);
|
133
|
mCube.createTexture();
|
134
|
|
135
|
if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
|
136
|
|
137
|
mScreen.detachAll();
|
138
|
mCube.attachToScreen(mScreen);
|
139
|
mCube.addRotation(this);
|
140
|
RubikActivity act = mView.getRubikActivity();
|
141
|
act.setText(++mNumCube);
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
private float computeFOV(float cameraDistance, int screenHeight)
|
147
|
{
|
148
|
double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
|
149
|
return (float)(2*halfFOVInRadians*(180/Math.PI));
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
private void recomputeScaleFactor(int screenWidth, int screenHeight)
|
155
|
{
|
156
|
float scaleFactor = CUBE_SCREEN_RATIO * Math.min(screenWidth,screenHeight) / mCube.getSize();
|
157
|
mScale.set(scaleFactor,scaleFactor,scaleFactor);
|
158
|
}
|
159
|
}
|