-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vertices.java
156 lines (136 loc) · 7.49 KB
/
Vertices.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
package d3kod.graphics.text;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import android.opengl.GLES20;
import d3kod.graphics.shader.programs.AttribVariable;
public class Vertices {
//--Constants--//
final static int POSITION_CNT_2D = 2; // Number of Components in Vertex Position for 2D
final static int POSITION_CNT_3D = 3; // Number of Components in Vertex Position for 3D
final static int COLOR_CNT = 4; // Number of Components in Vertex Color
final static int TEXCOORD_CNT = 2; // Number of Components in Vertex Texture Coords
final static int NORMAL_CNT = 3; // Number of Components in Vertex Normal
private static final int MVP_MATRIX_INDEX_CNT = 1; // Number of Components in MVP matrix index
final static int INDEX_SIZE = Short.SIZE / 8; // Index Byte Size (Short.SIZE = bits)
private static final String TAG = "Vertices";
//--Members--//
// NOTE: all members are constant, and initialized in constructor!
public final int positionCnt; // Number of Position Components (2=2D, 3=3D)
public final int vertexStride; // Vertex Stride (Element Size of a Single Vertex)
public final int vertexSize; // Bytesize of a Single Vertex
final IntBuffer vertices; // Vertex Buffer
final ShortBuffer indices; // Index Buffer
public int numVertices; // Number of Vertices in Buffer
public int numIndices; // Number of Indices in Buffer
final int[] tmpBuffer; // Temp Buffer for Vertex Conversion
private int mTextureCoordinateHandle;
private int mPositionHandle;
private int mMVPIndexHandle;
//--Constructor--//
// D: create the vertices/indices as specified (for 2d/3d)
// A: maxVertices - maximum vertices allowed in buffer
// maxIndices - maximum indices allowed in buffer
public Vertices(int maxVertices, int maxIndices) {
// this.gl = gl; // Save GL Instance
this.positionCnt = POSITION_CNT_2D; // Set Position Component Count
this.vertexStride = this.positionCnt + TEXCOORD_CNT + MVP_MATRIX_INDEX_CNT; // Calculate Vertex Stride
this.vertexSize = this.vertexStride * 4; // Calculate Vertex Byte Size
ByteBuffer buffer = ByteBuffer.allocateDirect( maxVertices * vertexSize ); // Allocate Buffer for Vertices (Max)
buffer.order( ByteOrder.nativeOrder() ); // Set Native Byte Order
this.vertices = buffer.asIntBuffer(); // Save Vertex Buffer
if ( maxIndices > 0 ) { // IF Indices Required
buffer = ByteBuffer.allocateDirect( maxIndices * INDEX_SIZE ); // Allocate Buffer for Indices (MAX)
buffer.order( ByteOrder.nativeOrder() ); // Set Native Byte Order
this.indices = buffer.asShortBuffer(); // Save Index Buffer
}
else // ELSE Indices Not Required
indices = null; // No Index Buffer
numVertices = 0; // Zero Vertices in Buffer
numIndices = 0; // Zero Indices in Buffer
this.tmpBuffer = new int[maxVertices * vertexSize / 4]; // Create Temp Buffer
// initialize the shader attribute handles
mTextureCoordinateHandle = AttribVariable.A_TexCoordinate.getHandle();
mMVPIndexHandle = AttribVariable.A_MVPMatrixIndex.getHandle();
mPositionHandle = AttribVariable.A_Position.getHandle();
}
//--Set Vertices--//
// D: set the specified vertices in the vertex buffer
// NOTE: optimized to use integer buffer!
// A: vertices - array of vertices (floats) to set
// offset - offset to first vertex in array
// length - number of floats in the vertex array (total)
// for easy setting use: vtx_cnt * (this.vertexSize / 4)
// R: [none]
public void setVertices(float[] vertices, int offset, int length) {
this.vertices.clear(); // Remove Existing Vertices
int last = offset + length; // Calculate Last Element
for ( int i = offset, j = 0; i < last; i++, j++ ) // FOR Each Specified Vertex
tmpBuffer[j] = Float.floatToRawIntBits( vertices[i] ); // Set Vertex as Raw Integer Bits in Buffer
this.vertices.put( tmpBuffer, 0, length ); // Set New Vertices
this.vertices.flip(); // Flip Vertex Buffer
this.numVertices = length / this.vertexStride; // Save Number of Vertices
}
//--Set Indices--//
// D: set the specified indices in the index buffer
// A: indices - array of indices (shorts) to set
// offset - offset to first index in array
// length - number of indices in array (from offset)
// R: [none]
public void setIndices(short[] indices, int offset, int length) {
this.indices.clear(); // Clear Existing Indices
this.indices.put( indices, offset, length ); // Set New Indices
this.indices.flip(); // Flip Index Buffer
this.numIndices = length; // Save Number of Indices
}
//--Bind--//
// D: perform all required binding/state changes before rendering batches.
// USAGE: call once before calling draw() multiple times for this buffer.
// A: [none]
// R: [none]
public void bind() {
// bind vertex position pointer
vertices.position( 0 ); // Set Vertex Buffer to Position
GLES20.glVertexAttribPointer(mPositionHandle, positionCnt,
GLES20.GL_FLOAT, false, vertexSize, vertices);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// bind texture position pointer
vertices.position(positionCnt); // Set Vertex Buffer to Texture Coords (NOTE: position based on whether color is also specified)
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXCOORD_CNT,
GLES20.GL_FLOAT, false, vertexSize, vertices);
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
// bind MVP Matrix index position handle
vertices.position(positionCnt + TEXCOORD_CNT);
GLES20.glVertexAttribPointer(mMVPIndexHandle, MVP_MATRIX_INDEX_CNT,
GLES20.GL_FLOAT, false, vertexSize, vertices);
GLES20.glEnableVertexAttribArray(mMVPIndexHandle);
}
//--Draw--//
// D: draw the currently bound vertices in the vertex/index buffers
// USAGE: can only be called after calling bind() for this buffer.
// A: primitiveType - the type of primitive to draw
// offset - the offset in the vertex/index buffer to start at
// numVertices - the number of vertices (indices) to draw
// R: [none]
public void draw(int primitiveType, int offset, int numVertices) {
if (indices != null) { // IF Indices Exist
indices.position(offset); // Set Index Buffer to Specified Offset
//draw indexed
GLES20.glDrawElements(primitiveType, numVertices,
GLES20.GL_UNSIGNED_SHORT, indices);
}
else { // ELSE No Indices Exist
//draw direct
GLES20.glDrawArrays(primitiveType, offset, numVertices);
}
}
//--Unbind--//
// D: clear binding states when done rendering batches.
// USAGE: call once before calling draw() multiple times for this buffer.
// A: [none]
// R: [none]
public void unbind() {
GLES20.glDisableVertexAttribArray(mTextureCoordinateHandle);
}
}