X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=assignment1.cpp;h=887ffb0d9a2444a8dacce433606dca42b2aeed75;hp=fd55f4e5bca90c65027b3cf4cbb0107493e5b05e;hb=refs%2Ftags%2Fassignment-1;hpb=5c9edba75c934dae9d11ae0998b92c789db3a920 diff --git a/assignment1.cpp b/assignment1.cpp index fd55f4e..887ffb0 100644 --- a/assignment1.cpp +++ b/assignment1.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef __APPLE__ #include #else @@ -14,14 +15,22 @@ using namespace std; GLuint* vaos; +GLuint* progIds; + +bool hasDrawn = false; void display() { glClear(GL_COLOR_BUFFER_BIT); for (int i = 0; i < 2; i++) { + glUseProgram(progIds[i]); glBindVertexArray(vaos[i]); glDrawArrays(GL_TRIANGLES, 0, 3); } glutSwapBuffers(); + if (!hasDrawn) { + glutPostRedisplay(); + hasDrawn = true; + } } void attachShader(GLuint progId, const char* filePath, GLenum type) { @@ -35,7 +44,8 @@ void attachShader(GLuint progId, const char* filePath, GLenum type) { ifstream file(filePath); stringstream buffer; buffer << file.rdbuf(); - const char* contents = buffer.str().c_str(); + string str = buffer.str(); + const char* contents = str.c_str(); glShaderSource(shader, 1, (const GLchar**)&contents, NULL); glCompileShader(shader); @@ -50,11 +60,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; @@ -66,26 +76,12 @@ GLuint compileShaders() { exit(1); } - glUseProgram(progId); return progId; } #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 - }, - - { - -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, @@ -95,11 +91,11 @@ GLuint* setupBuffers(GLuint progId) { GLuint numVerts = 3; - GLuint vbos[2]; - glGenBuffers(2, vbos); + GLuint vbo; + glGenBuffers(1, &vbo); - GLuint* vaos = new GLuint[2]; - glGenVertexArrays(2, vaos); + GLuint vao; + glGenVertexArrays(1, &vao); GLuint posId = glGetAttribLocation(progId, "vPosition"); GLuint colorId = glGetAttribLocation(progId, "vColor"); @@ -107,30 +103,24 @@ GLuint* setupBuffers(GLuint progId) { GLuint vertsLen = numVerts * 3 * sizeof(GLfloat); GLuint colorsLen = numVerts * 4 * sizeof(GLfloat); - - for(int i = 0; i < 2; i++) { - glBindBuffer(GL_ARRAY_BUFFER, vbos[i]); + glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices[i]); + glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices); glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); - glBindVertexArray(vaos[i]); + glBindVertexArray(vao); glEnableVertexAttribArray(posId); 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; + return vao; } -void init() { - GLuint progId = compileShaders(); - vaos = setupBuffers(progId); - +void validateProgram(GLuint progId) { glValidateProgram(progId); GLint success; @@ -143,17 +133,52 @@ 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); +} + +void keyboard(unsigned char key, int x, int y) { + glutPostRedisplay(); +} + int main(int argc, char** argv) { glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE); + glutInitDisplayMode(GLUT_STENCIL|GLUT_SINGLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE); glutInitWindowSize(800, 600); - glutCreateWindow("Hello Triangle"); + int win = glutCreateWindow("Hello Triangle"); glutDisplayFunc(display); glewInit(); init(); + glutKeyboardFunc(keyboard); + glutMainLoop(); return 0;