Draw 2 triangles with 2 VAOS+VBOS
authorLuke Lau <luke_lau@icloud.com>
Sun, 16 Sep 2018 12:26:10 +0000 (13:26 +0100)
committerLuke Lau <luke_lau@icloud.com>
Sun, 16 Sep 2018 12:26:10 +0000 (13:26 +0100)
assignment1.cpp

index 43cd626..fd55f4e 100644 (file)
 
 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);