22
22
import android .opengl .GLES30 ;
23
23
24
24
/**
25
- * A two-dimensional triangle for use as a drawn object in OpenGL ES 2.0.
25
+ * A two-dimensional triangle for use as a drawn object in OpenGL ES 3.1
26
+ *
27
+ * actually fails with the 300. I do not have a clue what it doesn't work.
26
28
*/
27
29
public class Triangle {
28
30
29
31
private final String vertexShaderCode =
30
- // This matrix member variable provides a hook to manipulate
31
- // the coordinates of the objects that use this vertex shader
32
- "uniform mat4 uMVPMatrix;" +
33
- "attribute vec4 vPosition;" +
34
- "void main() {" +
32
+ // This matrix member variable provides a hook to manipulate
33
+ // the coordinates of the objects that use this vertex shader
34
+ "#version 310 es\n " +
35
+ "uniform mat4 uMVPMatrix;\n " +
36
+ "in vec4 vPosition;\n " +
37
+ "void main() {\n " +
35
38
// the matrix must be included as a modifier of gl_Position
36
39
// Note that the uMVPMatrix factor *must be first* in order
37
40
// for the matrix multiplication product to be correct.
38
- " gl_Position = uMVPMatrix * vPosition;" +
39
- "}" ;
41
+ " gl_Position = uMVPMatrix * vPosition;\n " +
42
+ "}\n " ;
40
43
41
44
private final String fragmentShaderCode =
42
- "precision mediump float;" +
43
- "uniform vec4 vColor;" +
44
- "void main() {" +
45
- " gl_FragColor = vColor;" +
46
- "}" ;
45
+ "#version 310 es \n " +
46
+ "precision mediump float; \n " +
47
+ "in uniform vec4 vColor;\n " +
48
+ "out vec4 gl_FragColor;\n " +
49
+ "void main() {\n " +
50
+ " gl_FragColor = vColor;\n " +
51
+ "}\n " ;
47
52
48
53
private final FloatBuffer vertexBuffer ;
49
54
private final int mProgram ;
@@ -54,24 +59,24 @@ public class Triangle {
54
59
// number of coordinates per vertex in this array
55
60
static final int COORDS_PER_VERTEX = 3 ;
56
61
static float triangleCoords [] = {
57
- // in counterclockwise order:
58
- 0.0f , 0.622008459f , 0.0f , // top
59
- -0.5f , -0.311004243f , 0.0f , // bottom left
60
- 0.5f , -0.311004243f , 0.0f // bottom right
62
+ // in counterclockwise order:
63
+ 0.0f , 0.622008459f , 0.0f , // top
64
+ -0.5f , -0.311004243f , 0.0f , // bottom left
65
+ 0.5f , -0.311004243f , 0.0f // bottom right
61
66
};
62
67
private final int vertexCount = triangleCoords .length / COORDS_PER_VERTEX ;
63
68
private final int vertexStride = COORDS_PER_VERTEX * 4 ; // 4 bytes per vertex
64
69
65
- float color [] = { 0.63671875f , 0.76953125f , 0.22265625f , 0.0f };
70
+ float color [] = {0.63671875f , 0.76953125f , 0.22265625f , 0.0f };
66
71
67
72
/**
68
73
* Sets up the drawing object data for use in an OpenGL ES context.
69
74
*/
70
75
public Triangle () {
71
76
// initialize vertex byte buffer for shape coordinates
72
77
ByteBuffer bb = ByteBuffer .allocateDirect (
73
- // (number of coordinate values * 4 bytes per float)
74
- triangleCoords .length * 4 );
78
+ // (number of coordinate values * 4 bytes per float)
79
+ triangleCoords .length * 4 );
75
80
// use the device hardware's native byte order
76
81
bb .order (ByteOrder .nativeOrder ());
77
82
@@ -84,9 +89,9 @@ public Triangle() {
84
89
85
90
// prepare shaders and OpenGL program
86
91
int vertexShader = MyGLRenderer .loadShader (
87
- GLES30 .GL_VERTEX_SHADER , vertexShaderCode );
92
+ GLES30 .GL_VERTEX_SHADER , vertexShaderCode );
88
93
int fragmentShader = MyGLRenderer .loadShader (
89
- GLES30 .GL_FRAGMENT_SHADER , fragmentShaderCode );
94
+ GLES30 .GL_FRAGMENT_SHADER , fragmentShaderCode );
90
95
91
96
mProgram = GLES30 .glCreateProgram (); // create empty OpenGL Program
92
97
GLES30 .glAttachShader (mProgram , vertexShader ); // add the vertex shader to program
@@ -99,7 +104,7 @@ public Triangle() {
99
104
* Encapsulates the OpenGL ES instructions for drawing this shape.
100
105
*
101
106
* @param mvpMatrix - The Model View Project matrix in which to draw
102
- * this shape.
107
+ * this shape.
103
108
*/
104
109
public void draw (float [] mvpMatrix ) {
105
110
// Add program to OpenGL environment
@@ -113,9 +118,9 @@ public void draw(float[] mvpMatrix) {
113
118
114
119
// Prepare the triangle coordinate data
115
120
GLES30 .glVertexAttribPointer (
116
- mPositionHandle , COORDS_PER_VERTEX ,
117
- GLES30 .GL_FLOAT , false ,
118
- vertexStride , vertexBuffer );
121
+ mPositionHandle , COORDS_PER_VERTEX ,
122
+ GLES30 .GL_FLOAT , false ,
123
+ vertexStride , vertexBuffer );
119
124
120
125
// get handle to fragment shader's vColor member
121
126
mColorHandle = GLES30 .glGetUniformLocation (mProgram , "vColor" );
0 commit comments