/
HelloOpenGLES10Renderer.java
108 lines (79 loc) · 2.99 KB
/
HelloOpenGLES10Renderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.gl.test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class HelloOpenGLES10Renderer implements Renderer {
private int id;
private FPSLogger fps = new FPSLogger();
public void onSurfaceCreated(GL10 gl10, EGLConfig config) {
GL11 gl = (GL11) gl10;
// Set the background frame color
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glEnable(GL11.GL_BLEND);
gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
initShapes(gl);
}
public void onDrawFrame(GL10 gl10) {
GL11 gl = (GL11) gl10;
// Redraw background color
gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0.23671875f, 0.76953125f, 0.22265625f, 1.0f);
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, id);
gl.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
for(int i=0;i<300;i++) {
gl.glTranslatef(0.04f, 0.0f, 0f);
gl.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
}
fps.log();
//gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
//gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
public void onSurfaceChanged(GL10 gl10, int width, int height) {
GL11 gl = (GL11) gl10;
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL11.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
//GLU.gluOrtho2D(gl, width , 0, height, 0);
gl.glMatrixMode(GL11.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
}
private void initShapes(GL11 gl){
FloatBuffer triangleVB;
float triangleCoords[] = {
// X, Y, Z
-0.5f, -0.25f, 0,
0.5f, -0.25f, 0,
0.0f, 0.559016994f, 0
};
// initialize vertex Buffer for triangle
ByteBuffer vbb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
triangleVB = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer
triangleVB.put(triangleCoords); // add the coordinates to the FloatBuffer
triangleVB.position(0); // set the buffer to read the first coordinate
int [] ids = new int[1];
gl.glGenBuffers(1, ids,0);
id = ids[0];
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, id);
gl.glBufferData(GL11.GL_ARRAY_BUFFER, triangleCoords.length*4 , triangleVB, GL11.GL_DYNAMIC_DRAW);
gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
}
}