X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=assignment1.cpp;h=b3049f0583f956317d1b38e07ec0914aae1acb1f;hp=43cd62629d323b3e3bc4cc3cebf6b2087d520d16;hb=6cbf05fce9821efdbbb141c6a0dbff6e6745fbc0;hpb=1dcbad027a4537b550766e9053a4418e8256c81c diff --git a/assignment1.cpp b/assignment1.cpp index 43cd626..b3049f0 100644 --- a/assignment1.cpp +++ b/assignment1.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef __APPLE__ #include #else @@ -13,9 +14,16 @@ using namespace std; +GLuint* vaos; +GLuint* progIds; + void display() { glClear(GL_COLOR_BUFFER_BIT); - glDrawArrays(GL_TRIANGLES, 0, 6); + for (int i = 0; i < 2; i++) { + glUseProgram(progIds[i]); + glBindVertexArray(vaos[i]); + glDrawArrays(GL_TRIANGLES, 0, 3); + } glutSwapBuffers(); } @@ -45,11 +53,11 @@ void attachShader(GLuint progId, const char* filePath, GLenum type) { glAttachShader(progId, shader); } -GLuint compileShaders() { +GLuint compileShaders(char* vertexShader, char* fragmentShader) { GLuint progId = glCreateProgram(); - attachShader(progId, "vertex.glsl", GL_VERTEX_SHADER); - attachShader(progId, "fragment.glsl", GL_FRAGMENT_SHADER); + attachShader(progId, vertexShader, GL_VERTEX_SHADER); + attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER); glLinkProgram(progId); GLint success = 0; @@ -61,68 +69,51 @@ GLuint compileShaders() { exit(1); } - glUseProgram(progId); return progId; } -void generateObjectBuffers() { - GLfloat vertices[] = { - 0.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - 0.5f, 1.0f, 0.0f, +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) - -1.0f, -1.0f, 0.0f, - 0.0f, -1.0f, 0.0f, - -0.5f, 1.0f, 0.0f - }; +GLuint setupBuffers(GLfloat* vertices, GLuint progId) { 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 VBO; - glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, numVerts * 7 * sizeof(GLfloat), NULL, GL_STATIC_DRAW); + GLuint vbo; + glGenBuffers(1, &vbo); + + GLuint vao; + glGenVertexArrays(1, &vao); + + 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)) + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW); -void linkBuffers(GLuint progId) { - GLuint numVerts = 6; - GLuint posId = glGetAttribLocation(progId, "vPosition"); - GLuint colorId = glGetAttribLocation(progId, "vColor"); + glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices); + glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); - GLuint vao = 0; - glGenVertexArrays(1, &vao); glBindVertexArray(vao); 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 vao; } -void init() { - GLuint progId = compileShaders(); - generateObjectBuffers(); - linkBuffers(progId); - +void validateProgram(GLuint progId) { glValidateProgram(progId); GLint success; @@ -135,6 +126,35 @@ void init() { } } +void init() { + GLfloat vertices[2][9] = { + { + 0.0f, -1.0f, 0.0f, + 1.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 + } + }; + + vaos = new GLuint[2]; + progIds = new GLuint[2]; + + GLuint progId1 = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl"); + vaos[0] = setupBuffers(vertices[0], progId1); + progIds[0] = progId1; + validateProgram(progId1); + + GLuint progId2 = compileShaders((char*)"vertex.glsl", (char*)"yellow.glsl"); + vaos[1] = setupBuffers(vertices[1], progId2); + progIds[1] = progId2; + validateProgram(progId2); +} + int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);