90 lines
2.7 KiB
Java
90 lines
2.7 KiB
Java
package engine.graphics;
|
|
|
|
import engine.utils.BufferUtils;
|
|
|
|
import static org.lwjgl.opengl.GL11.*;
|
|
import static org.lwjgl.opengl.GL15.*;
|
|
import static org.lwjgl.opengl.GL30.*;
|
|
|
|
public class VertexArray {
|
|
|
|
private int VAO ,VBO, EBO, CBO, TBO;
|
|
private int count;
|
|
|
|
public VertexArray(float[] vertices, byte[] indices, float[] color, float[] texture) {
|
|
count = indices.length;
|
|
// VERTEX ARRAY OBJECT
|
|
VAO = glGenVertexArrays();
|
|
glBindVertexArray(VAO);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
// VERTEX BUFFER OBJECT
|
|
createVertexBufferObject(vertices);
|
|
// COLOR BUFFER OBJECT
|
|
if (color != null) createColorBufferObject(color);
|
|
// TEXTURE BUFFER OBJECT
|
|
if (texture != null) createTextureBufferObject(texture);
|
|
|
|
EBO = glGenBuffers();
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
private void createVertexBufferObject(float[] vertices){
|
|
VBO = glGenBuffers();
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
|
glEnableVertexAttribArray(0);
|
|
}
|
|
|
|
private void createColorBufferObject(float[] color){
|
|
CBO = glGenBuffers();
|
|
glBindBuffer(GL_ARRAY_BUFFER, CBO);
|
|
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(color), GL_STATIC_DRAW);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
|
|
glEnableVertexAttribArray(1);
|
|
}
|
|
|
|
private void createTextureBufferObject(float[] texture){
|
|
TBO = glGenBuffers();
|
|
glBindBuffer(GL_ARRAY_BUFFER, TBO);
|
|
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW);
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, false, 0, 0);
|
|
glEnableVertexAttribArray(2);
|
|
}
|
|
|
|
public void swapVertexBufferObject(float[] vertices){
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
|
|
}
|
|
|
|
public void swapTextureBufferObject(float [] texture){
|
|
glBindBuffer(GL_ARRAY_BUFFER, TBO);
|
|
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW);
|
|
}
|
|
|
|
public void bind(){
|
|
glBindVertexArray(this.VAO);
|
|
}
|
|
|
|
public void unbind(){
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
public void draw(){
|
|
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0);
|
|
}
|
|
|
|
public void render(){
|
|
bind();
|
|
draw();
|
|
unbind();
|
|
}
|
|
|
|
}
|