From 5c9edba75c934dae9d11ae0998b92c789db3a920 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Sun, 16 Sep 2018 13:26:10 +0100 Subject: [PATCH 1/1] Draw 2 triangles with 2 VAOS+VBOS --- assignment1.cpp | 64 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/assignment1.cpp b/assignment1.cpp index 43cd626..fd55f4e 100644 --- a/assignment1.cpp +++ b/assignment1.cpp @@ -13,9 +13,14 @@ using namespace std; +GLuint* vaos; + void display() { glClear(GL_COLOR_BUFFER_BIT); - glDrawArrays(GL_TRIANGLES, 0, 6); + for (int i = 0; i < 2; i++) { + glBindVertexArray(vaos[i]); + glDrawArrays(GL_TRIANGLES, 0, 3); + } glutSwapBuffers(); } @@ -65,63 +70,66 @@ GLuint compileShaders() { return progId; } -void generateObjectBuffers() { - GLfloat vertices[] = { +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) + +GLuint* setupBuffers(GLuint progId) { + GLfloat vertices[2][9] = { + { 0.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, - 0.5f, 1.0f, 0.0f, + 0.5f, 1.0f, 0.0f + }, + { -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -0.5f, 1.0f, 0.0f + } }; GLfloat colors[] = { - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1 }; - GLuint numVerts = 6; + GLuint numVerts = 3; + + GLuint vbos[2]; + glGenBuffers(2, vbos); - GLuint VBO; - glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, numVerts * 7 * sizeof(GLfloat), NULL, GL_STATIC_DRAW); + GLuint* vaos = new GLuint[2]; + glGenVertexArrays(2, vaos); + + GLuint posId = glGetAttribLocation(progId, "vPosition"); + GLuint colorId = glGetAttribLocation(progId, "vColor"); GLuint vertsLen = numVerts * 3 * sizeof(GLfloat); GLuint colorsLen = numVerts * 4 * sizeof(GLfloat); - glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices); - glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); -} -#define BUFFER_OFFSET(i) ((char *)NULL + (i)) -void linkBuffers(GLuint progId) { - GLuint numVerts = 6; - GLuint posId = glGetAttribLocation(progId, "vPosition"); - GLuint colorId = glGetAttribLocation(progId, "vColor"); + for(int i = 0; i < 2; i++) { + glBindBuffer(GL_ARRAY_BUFFER, vbos[i]); + glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW); + + glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices[i]); + glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); - GLuint vao = 0; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + glBindVertexArray(vaos[i]); glEnableVertexAttribArray(posId); - glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(colorId); + + glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat))); + } + return vaos; } void init() { GLuint progId = compileShaders(); - generateObjectBuffers(); - linkBuffers(progId); + vaos = setupBuffers(progId); glValidateProgram(progId); -- 2.30.2