X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=assignment1.cpp;h=fd55f4e5bca90c65027b3cf4cbb0107493e5b05e;hp=43cd62629d323b3e3bc4cc3cebf6b2087d520d16;hb=5c9edba75c934dae9d11ae0998b92c789db3a920;hpb=1dcbad027a4537b550766e9053a4418e8256c81c;ds=sidebyside 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);