4
4
import android .opengl .GLSurfaceView ;
5
5
import android .opengl .Matrix ;
6
6
7
+ import com .asha .md360player4android .Sphere ;
8
+
7
9
import java .nio .FloatBuffer ;
8
10
import java .nio .IntBuffer ;
9
11
10
12
import javax .microedition .khronos .egl .EGLConfig ;
11
13
import javax .microedition .khronos .opengles .GL10 ;
12
14
15
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMathDegreesToRadians ;
16
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMatrix4Identity ;
17
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMatrix4MakePerspective ;
18
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMatrix4Multiply ;
19
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMatrix4Rotate ;
20
+ import static com .asha .md360player4android .common .GLKMatrixUtil .GLKMatrix4Scale ;
21
+
13
22
/**
14
23
* This class implements our custom renderer. Note that the GL10 parameter passed in is unused for OpenGL ES 2.0
15
24
* renderers -- the static class GLES20 is used instead.
@@ -44,57 +53,51 @@ public class LessonOneRenderer implements GLSurfaceView.Renderer
44
53
/** This will be used to pass in model color information. */
45
54
private int mColorHandle ;
46
55
47
- /** How many bytes per float. */
48
- private final int mBytesPerFloat = 4 ;
49
-
50
- /** How many elements per vertex. */
51
- private final int mStrideBytes = 7 * mBytesPerFloat ;
52
-
53
- /** Offset of the position data. */
54
- private final int mPositionOffset = 0 ;
55
-
56
56
/** Size of the position data in elements. */
57
57
private final int mPositionDataSize = 3 ;
58
58
59
- /** Offset of the color data. */
60
- private final int mColorOffset = 3 ;
61
59
62
60
/** Size of the color data in elements. */
63
61
private final int mColorDataSize = 4 ;
64
62
63
+ private Sphere sphere = new Sphere (50 ,1.0f );
64
+
65
65
/**
66
66
* Initialize the model data.
67
67
*/
68
68
public LessonOneRenderer ()
69
69
{
70
70
// Define points for equilateral triangles.
71
71
}
72
-
72
+
73
+
74
+ private static final float DEFAULT_OVERTURE = 85.0f ;
75
+
76
+
77
+ private float [] projectionMatrix = new float [16 ];
78
+ private float [] modelViewMatrix = new float [16 ];
79
+ //private float[] mMVPMatrix = new float[16];
80
+
81
+ public void update (){
82
+ float aspect = 1.5f ;
83
+ GLKMatrix4MakePerspective (GLKMathDegreesToRadians (DEFAULT_OVERTURE ),aspect , 0.1f , 400.0f ,projectionMatrix );
84
+ GLKMatrix4Rotate (projectionMatrix , (float ) Math .PI , 1.0f , 0.0f , 0.0f );
85
+
86
+ GLKMatrix4Identity (modelViewMatrix );
87
+ modelViewMatrix = GLKMatrix4Scale (modelViewMatrix , 300.0f , 300.0f , 300.0f );
88
+
89
+ GLKMatrix4Multiply (projectionMatrix ,modelViewMatrix ,mMVPMatrix );
90
+ }
91
+
92
+
93
+
73
94
@ Override
74
95
public void onSurfaceCreated (GL10 glUnused , EGLConfig config )
75
96
{
76
97
// Set the background clear color to gray.
77
98
GLES20 .glClearColor (0.5f , 0.5f , 0.5f , 0.5f );
78
-
79
- // Position the eye behind the origin.
80
- final float eyeX = 0.0f ;
81
- final float eyeY = 0.0f ;
82
- final float eyeZ = 1.5f ;
83
-
84
- // We are looking toward the distance
85
- final float lookX = 0.0f ;
86
- final float lookY = 0.0f ;
87
- final float lookZ = -5.0f ;
88
-
89
- // Set our up vector. This is where our head would be pointing were we holding the camera.
90
- final float upX = 0.0f ;
91
- final float upY = 1.0f ;
92
- final float upZ = 0.0f ;
93
-
94
- // Set the view matrix. This matrix can be said to represent the camera position.
95
- // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
96
- // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
97
- Matrix .setLookAtM (mViewMatrix , 0 , eyeX , eyeY , eyeZ , lookX , lookY , lookZ , upX , upY , upZ );
99
+
100
+ update ();
98
101
99
102
final String vertexShader =
100
103
"uniform mat4 u_MVPMatrix; \n " // A constant representing the combined model/view/projection matrix.
@@ -119,7 +122,7 @@ public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
119
122
// triangle per fragment.
120
123
+ "void main() \n " // The entry point for our fragment shader.
121
124
+ "{ \n "
122
- + " gl_FragColor = v_Color; \n " // Pass the color directly through the pipeline.
125
+ + " gl_FragColor = v_Color; \n " // Pass the color directly through the pipeline.
123
126
+ "} \n " ;
124
127
125
128
// Load in the vertex shader.
@@ -222,33 +225,19 @@ public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
222
225
GLES20 .glUseProgram (programHandle );
223
226
224
227
225
- initTriangle ();
228
+ initSphere ();
226
229
}
227
230
228
- private void initTriangle () {
229
- // This triangle is red, green, and blue.
230
- final float [] triangle1VerticesData = {
231
- // X, Y, Z,
232
- -0.5f , -0.25f , 0.0f ,
231
+ private void initSphere () {
233
232
234
- 0.5f , -0.25f , 0.0f ,
235
-
236
- 0.0f , 0.559016994f , 0.0f ,
237
- };
238
-
239
- final float [] colorVerticesData = {
240
- // R, G, B, A
241
- 1.0f , 0.0f , 0.0f , 1.0f ,
242
-
243
- 0.0f , 0.0f , 1.0f , 1.0f ,
244
-
245
- 0.0f , 1.0f , 0.0f , 1.0f
246
- };
247
233
IntBuffer vertexBufferId = IntBuffer .allocate (1 );
248
234
IntBuffer colorBufferId = IntBuffer .allocate (1 );
249
- FloatBuffer vertexBuffer = FloatBuffer .wrap (triangle1VerticesData );
250
- FloatBuffer colorBuffer = FloatBuffer .wrap (colorVerticesData );
251
- int numVertices = 3 ;
235
+ IntBuffer vertexIndicesBufferId = IntBuffer .allocate (1 );
236
+ FloatBuffer vertexBuffer = FloatBuffer .wrap (sphere .vertices );
237
+ FloatBuffer colorBuffer = FloatBuffer .wrap (sphere .colors );
238
+ IntBuffer indicesBuffer = IntBuffer .wrap (sphere .indices );
239
+ int numVertices = sphere .numVertices ;
240
+ int numIndices = sphere .numIndices ;
252
241
253
242
GLES20 .glGenBuffers (1 ,vertexBufferId );
254
243
GLES20 .glBindBuffer (GLES20 .GL_ARRAY_BUFFER , vertexBufferId .get (0 ));
@@ -261,6 +250,10 @@ private void initTriangle() {
261
250
GLES20 .glBufferData (GLES20 .GL_ARRAY_BUFFER , numVertices *4 *4 , colorBuffer , GLES20 .GL_DYNAMIC_DRAW );
262
251
GLES20 .glVertexAttribPointer (mColorHandle ,mColorDataSize ,GLES20 .GL_FLOAT , false , 0 , 0 );
263
252
GLES20 .glEnableVertexAttribArray (mColorHandle );
253
+
254
+ GLES20 .glGenBuffers (1 ,vertexIndicesBufferId );
255
+ GLES20 .glBindBuffer (GLES20 .GL_ELEMENT_ARRAY_BUFFER ,vertexIndicesBufferId .get (0 ));
256
+ GLES20 .glBufferData (GLES20 .GL_ELEMENT_ARRAY_BUFFER ,numIndices *4 ,indicesBuffer ,GLES20 .GL_STATIC_DRAW );
264
257
}
265
258
266
259
@ Override
@@ -300,11 +293,11 @@ public void onDrawFrame(GL10 glUnused)
300
293
*/
301
294
private void draw ()
302
295
{
303
- Matrix .multiplyMM (mMVPMatrix , 0 , mViewMatrix , 0 , mModelMatrix , 0 );
304
- Matrix .multiplyMM (mMVPMatrix , 0 , mProjectionMatrix , 0 , mMVPMatrix , 0 );
296
+ // Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
297
+ // Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
305
298
GLES20 .glUniformMatrix4fv (mMVPMatrixHandle , 1 , false , mMVPMatrix , 0 );
306
299
307
-
308
- GLES20 .glDrawArrays (GLES20 .GL_TRIANGLES , 0 , 3 );
300
+ //GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,sphere.numVertices);
301
+ GLES20 .glDrawElements (GLES20 .GL_TRIANGLES , sphere . numIndices , GLES20 . GL_UNSIGNED_SHORT , 0 );
309
302
}
310
303
}
0 commit comments