Draw 2 triangles with 2 VAOS+VBOS
[opengl.git] / assignment1.cpp
index 43cd62629d323b3e3bc4cc3cebf6b2087d520d16..fd55f4e5bca90c65027b3cf4cbb0107493e5b05e 100644 (file)
 
 using namespace std;
 
 
 using namespace std;
 
+GLuint* vaos;
+
 void display() {
        glClear(GL_COLOR_BUFFER_BIT);
 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();
 }
 
        glutSwapBuffers();
 }
 
@@ -65,63 +70,66 @@ GLuint compileShaders() {
        return progId;
 }
 
        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.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
                        -1.0f, -1.0f, 0.0f,
                        0.0f, -1.0f, 0.0f,
                        -0.5f, 1.0f, 0.0f
+               }
        };
 
        GLfloat colors[] = {
        };
 
        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
        };
 
                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);
 
        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);
 
                glEnableVertexAttribArray(posId);
-       glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0);
-
                glEnableVertexAttribArray(colorId);
                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)));
                glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat)));
+       }
 
 
+       return vaos;
 }
 
 void init() {
        GLuint progId = compileShaders();
 }
 
 void init() {
        GLuint progId = compileShaders();
-       generateObjectBuffers();
-       linkBuffers(progId);
+       vaos = setupBuffers(progId);
        
        glValidateProgram(progId);
        
        
        glValidateProgram(progId);