Revision dc10a48d
Added by Leszek Koltunski about 1 year ago
src/main/java/org/distorted/examples/rubik/RubikRenderer.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.examples.rubik; |
21 | 21 |
|
22 |
import android.app.ActivityManager; |
|
23 |
import android.content.Context; |
|
24 |
import android.content.pm.ConfigurationInfo; |
|
25 |
import android.content.res.Resources; |
|
22 | 26 |
import android.opengl.GLSurfaceView; |
23 | 27 |
|
24 | 28 |
import org.distorted.library.effect.VertexEffectSink; |
... | ... | |
27 | 31 |
import org.distorted.library.message.EffectListener; |
28 | 32 |
import org.distorted.library.type.Static3D; |
29 | 33 |
|
34 |
import java.io.InputStream; |
|
35 |
|
|
30 | 36 |
import javax.microedition.khronos.egl.EGLConfig; |
31 | 37 |
import javax.microedition.khronos.opengles.GL10; |
32 | 38 |
|
33 | 39 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
34 | 40 |
|
35 |
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
|
|
41 |
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
|
|
36 | 42 |
{ |
37 | 43 |
private static final float CUBE_SCREEN_RATIO = 0.5f; |
38 | 44 |
private static final float CAMERA_DISTANCE = 0.6f; // 0.6 of the length of max(scrHeight,scrWidth) |
39 | 45 |
private static final int MIN_CUBE_SIZE = 1; |
40 | 46 |
private static final int MAX_CUBE_SIZE = 6; |
41 | 47 |
|
42 |
private RubikSurfaceView mView; |
|
43 |
private DistortedScreen mScreen; |
|
44 |
private Static3D mScale; |
|
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; |
|
45 | 54 |
private int mNextCubeSize; |
46 | 55 |
private boolean mChangeCubeSizeNow; |
47 | 56 |
private int mNumCube; |
48 |
private RubikCube mCube; |
|
49 |
|
|
50 | 57 |
private int mScreenWidth, mScreenHeight; |
51 | 58 |
|
52 | 59 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
54 | 61 |
RubikRenderer(RubikSurfaceView v) |
55 | 62 |
{ |
56 | 63 |
mView = v; |
57 |
|
|
64 |
mResources = v.getResources(); |
|
58 | 65 |
mScreen = new DistortedScreen(); |
59 | 66 |
|
60 | 67 |
mScreenWidth = mScreenHeight = 0; |
... | ... | |
94 | 101 |
|
95 | 102 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
96 | 103 |
{ |
97 |
float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
|
|
104 |
float cameraDistance = CAMERA_DISTANCE*(Math.max(width,height));
|
|
98 | 105 |
float fovInDegrees = computeFOV(cameraDistance,height); |
99 | 106 |
|
100 | 107 |
mScreen.setProjection( fovInDegrees, 0.1f); |
... | ... | |
112 | 119 |
{ |
113 | 120 |
createNextCube(); |
114 | 121 |
VertexEffectSink.enable(); |
115 |
DistortedLibrary.onSurfaceCreated(mView.getContext(), this); |
|
116 |
} |
|
117 |
|
|
118 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
119 |
|
|
120 |
public void distortedException(Exception ex) |
|
121 |
{ |
|
122 |
android.util.Log.e("Rubik", ex.getMessage() ); |
|
122 |
DistortedLibrary.onSurfaceCreated(this); |
|
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
154 | 154 |
float scaleFactor = CUBE_SCREEN_RATIO * Math.min(screenWidth,screenHeight) / mCube.getSize(); |
155 | 155 |
mScale.set(scaleFactor,scaleFactor,scaleFactor); |
156 | 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 |
} |
|
157 | 192 |
} |
Also available in: Unified diff
Decouple (to a large degree) the OpenGL Library from Android.