No matter how many tutorials or YouTube videos I follow, I could never understand VBOs the same. one article or video tells me one thing, another tells me something different. I’m not leaving this post without a way better understanding of VBOs. So, apparently VAOs are a list of VBOs which are lists of points in space. They are saved so the GPU can acess them faster than immediate mode I believe. Here is code from a well known video that I added comments about what I think each block of code did. Instead of asking each after each, I thought you guys could just correct me on anything I don’t understand well. Each block where I added a capitalized comment; I’d appreciate it if you would just explain the code under it(or I could just ask if nobody mentions it :P) .
public class VertexBufferObjectDemo {
public static void main(String[] args) {
//CREATS DISPLAY
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Vertex Buffer Object Demo");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
//ENABLES DISPLAY WITH A ORTHO THAT IS VERY SMALL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(1, -1, 1, -1, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//VERTICIES AND STUFF
final int amountOfVertices = 3;
final int vertexSize = 3;
final int colorSize = 3;
//VARIABLE I USE
float trans = 0.1f;
//CREATES THE POINTS FOR THE TRIANGLE
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});
vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
colorData.put(new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1});
colorData.flip();
//SETS UP VERTEX STUFF
int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//SETS UP COLOR STUFF
int vboColorHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glBufferData(GL_ARRAY_BUFFER, colorData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
// Clear the vertices buffer
vertexData.clear();
// Re-create the vertex data (personal use)
vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});
vertexData.flip();
// Now, upload to the GPU (personal use)
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
//PREPARES TO DRAW CRAP
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glColorPointer(colorSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//DRAWS TRIANGLE USING THE FLOAT POINTS
glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.update();
Display.sync(60);
}
glDeleteBuffers(vboVertexHandle);
glDeleteBuffers(vboColorHandle);
Display.destroy();
System.exit(0);
}
}