Skip to content
Permalink
3720964c8f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
75 lines (59 sloc) 1.91 KB
package game.graphics;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import game.utils.BufferUtils;
public class VertexArray {
private int vao, vbo, ibo, tbo;
private int numVertices;
public VertexArray(int numVertices) {
this.numVertices = numVertices;
vao = glGenVertexArrays();
}
public VertexArray(float[] vertices, byte[] indices, float[] textureCoords) {
numVertices = indices.length;
vao = glGenVertexArrays();
glBindVertexArray(vao);
// Note: pointerOffset parameters must be zero
vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(Shader.VERTEX_ATTRIB);
tbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoords), GL_STATIC_DRAW);
glVertexAttribPointer(Shader.TCOORD_ATTRIB, 2, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(Shader.TCOORD_ATTRIB);
ibo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0); // Unbind when done
}
public void bind() {
glBindVertexArray(vao);
if (ibo > 0) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}
}
public void unbind() {
if (ibo > 0) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
}
public void draw() {
if (ibo > 0) {
glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_BYTE, 0);
} else {
glDrawArrays(GL_TRIANGLES, 0, numVertices);
}
}
public void render() {
bind();
draw();
}
}