-
Notifications
You must be signed in to change notification settings - Fork 740
/
VAORenderer.java
215 lines (172 loc) · 6.99 KB
/
VAORenderer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// The MIT License (MIT)
//
// Copyright (c) 2013 Dan Ginsburg, Budirijanto Purnomo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Book: OpenGL(R) ES 3.0 Programming Guide, 2nd Edition
// Authors: Dan Ginsburg, Budirijanto Purnomo, Dave Shreiner, Aaftab Munshi
// ISBN-10: 0-321-93388-5
// ISBN-13: 978-0-321-93388-1
// Publisher: Addison-Wesley Professional
// URLs: http://www.opengles-book.com
// http://my.safaribooksonline.com/book/animation-and-3d/9780133440133
//
// VertexArrayObjects
//
// This is a simple example drawing a primitive with
// Vertex Array Objects (VAOs)
//
package com.openglesbook.VertexArrayObjects;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import com.openglesbook.common.ESShader;
import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
public class VAORenderer implements GLSurfaceView.Renderer
{
///
// Constructor
//
public VAORenderer ( Context context )
{
mVertices = ByteBuffer.allocateDirect ( mVerticesData.length * 4 )
.order ( ByteOrder.nativeOrder() ).asFloatBuffer();
mVertices.put ( mVerticesData ).position ( 0 );
mIndices = ByteBuffer.allocateDirect ( mIndicesData.length * 2 )
.order ( ByteOrder.nativeOrder() ).asShortBuffer();
mIndices.put ( mIndicesData ).position ( 0 );
}
///
// Initialize the shader and program object
//
public void onSurfaceCreated ( GL10 glUnused, EGLConfig config )
{
String vShaderStr =
"#version 300 es \n" +
"layout(location = 0) in vec4 a_position; \n" +
"layout(location = 1) in vec4 a_color; \n" +
"out vec4 v_color; \n" +
"void main() \n" +
"{ \n" +
" v_color = a_color; \n" +
" gl_Position = a_position; \n" +
"}";
String fShaderStr =
"#version 300 es \n" +
"precision mediump float; \n" +
"in vec4 v_color; \n" +
"out vec4 o_fragColor; \n" +
"void main() \n" +
"{ \n" +
" o_fragColor = v_color; \n" +
"}" ;
// Load the shaders and get a linked program object
mProgramObject = ESShader.loadProgram ( vShaderStr, fShaderStr );
// Generate VBO Ids and load the VBOs with data
GLES30.glGenBuffers ( 2, mVBOIds, 0 );
GLES30.glBindBuffer ( GLES30.GL_ARRAY_BUFFER, mVBOIds[0] );
mVertices.position ( 0 );
GLES30.glBufferData ( GLES30.GL_ARRAY_BUFFER, mVerticesData.length * 4,
mVertices, GLES30.GL_STATIC_DRAW );
GLES30.glBindBuffer ( GLES30.GL_ELEMENT_ARRAY_BUFFER, mVBOIds[1] );
mIndices.position ( 0 );
GLES30.glBufferData ( GLES30.GL_ELEMENT_ARRAY_BUFFER, 2 * mIndicesData.length,
mIndices, GLES30.GL_STATIC_DRAW );
// Generate VAO Id
GLES30.glGenVertexArrays ( 1, mVAOId, 0 );
// Bind the VAO and then setup the vertex
// attributes
GLES30.glBindVertexArray ( mVAOId[0] );
GLES30.glBindBuffer ( GLES30.GL_ARRAY_BUFFER, mVBOIds[0] );
GLES30.glBindBuffer ( GLES30.GL_ELEMENT_ARRAY_BUFFER, mVBOIds[1] );
GLES30.glEnableVertexAttribArray ( VERTEX_POS_INDX );
GLES30.glEnableVertexAttribArray ( VERTEX_COLOR_INDX );
GLES30.glVertexAttribPointer ( VERTEX_POS_INDX, VERTEX_POS_SIZE,
GLES30.GL_FLOAT, false, VERTEX_STRIDE,
0 );
GLES30.glVertexAttribPointer ( VERTEX_COLOR_INDX, VERTEX_COLOR_SIZE,
GLES30.GL_FLOAT, false, VERTEX_STRIDE,
( VERTEX_POS_SIZE * 4 ) );
// Reset to the default VAO
GLES30.glBindVertexArray ( 0 );
GLES30.glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );
}
// /
// Draw a triangle using the shader pair created in onSurfaceCreated()
//
public void onDrawFrame ( GL10 glUnused )
{
// Set the viewport
GLES30.glViewport ( 0, 0, mWidth, mHeight );
// Clear the color buffer
GLES30.glClear ( GLES30.GL_COLOR_BUFFER_BIT );
// Use the program object
GLES30.glUseProgram ( mProgramObject );
// Bind the VAO
GLES30.glBindVertexArray ( mVAOId[0] );
// Draw with the VAO settings
GLES30.glDrawElements ( GLES30.GL_TRIANGLES, mIndicesData.length, GLES30.GL_UNSIGNED_SHORT, 0 );
// Return to the default VAO
GLES30.glBindVertexArray ( 0 );
}
///
// Handle surface changes
//
public void onSurfaceChanged ( GL10 glUnused, int width, int height )
{
mWidth = width;
mHeight = height;
}
// Handle to a program object
private int mProgramObject;
// Additional member variables
private int mWidth;
private int mHeight;
private FloatBuffer mVertices;
private ShortBuffer mIndices;
// VertexBufferObject Ids
private int [] mVBOIds = new int[2];
// VertexArrayObject Id
private int [] mVAOId = new int[1];
// 3 vertices, with (x,y,z) ,(r, g, b, a) per-vertex
private final float[] mVerticesData =
{
0.0f, 0.5f, 0.0f, // v0
1.0f, 0.0f, 0.0f, 1.0f, // c0
-0.5f, -0.5f, 0.0f, // v1
0.0f, 1.0f, 0.0f, 1.0f, // c1
0.5f, -0.5f, 0.0f, // v2
0.0f, 0.0f, 1.0f, 1.0f, // c2
};
private final short[] mIndicesData =
{
0, 1, 2
};
final int VERTEX_POS_SIZE = 3; // x, y and z
final int VERTEX_COLOR_SIZE = 4; // r, g, b, and a
final int VERTEX_POS_INDX = 0;
final int VERTEX_COLOR_INDX = 1;
final int VERTEX_STRIDE = ( 4 * ( VERTEX_POS_SIZE + VERTEX_COLOR_SIZE ) );
}