diff --git a/src/graphics.cpp b/src/graphics.cpp index ed1ec4e..bcc1b88 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -22,6 +22,9 @@ ShaderProgram::Base ShaderProgram::baseFromName(string name) { } if (lname == "triangle") { return Triangle; + } + if (lname == "polygons") { + return Polygons; } else { BOOST_LOG_TRIVIAL(fatal) << "unknown base program \"" << name << "\""; exit(EXIT_USAGE); @@ -40,6 +43,8 @@ string ShaderProgram::baseName() { return "Empty"; case ShaderProgram::Base::Triangle: return "Triangle"; + case ShaderProgram::Base::Polygons: + return "Polygons"; default: // this should never occur, as the switch case should cover all variants. throw std::runtime_error("Undefined base Program"); @@ -181,6 +186,89 @@ int ShaderProgram::run(GLFWwindow *window) { glDeleteBuffers(1, &VBOTriangle); glDeleteProgram(this->program); + // glfw: terminate, clearing all previously allocated GLFW resources. + // ------------------------------------------------------------------ + glfwTerminate(); + return 0; + } else if (this->base == Polygons) { + // set up vertex data (and buffer(s)) and configure vertex attributes + // ------------------------------------------------------------------ + float vertices[] = { + 0.5f, 0.8f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f // top left + }; + unsigned int indices[] = { + // note that we start from 0! + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle + }; + + unsigned int VBOTriangle, VAOTriangle, EBOindicies; + glGenBuffers(1, &EBOindicies); + glGenVertexArrays(1, &VAOTriangle); + glGenBuffers(1, &VBOTriangle); + // bind the Vertex Array Object first, then bind and set vertex buffer(s), + // and then configure vertex attributes(s). + glBindVertexArray(VAOTriangle); + + glBindBuffer(GL_ARRAY_BUFFER, VBOTriangle); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOindicies); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, + GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), + (void *)0); + glEnableVertexAttribArray(0); + + // note that this is allowed, the call to glVertexAttribPointer registered + // VBO as the vertex attribute's bound vertex buffer object so afterwards we + // can safely unbind + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOindicies); + + // You can unbind the VAO afterwards so other VAO calls won't accidentally + // modify this VAO, but this rarely happens. Modifying other VAOs requires a + // call to glBindVertexArray anyways so we generally don't unbind VAOs (nor + // VBOs) when it's not directly necessary. + glBindVertexArray(0); + + // uncomment this call to draw in wireframe polygons. + // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + while (!glfwWindowShouldClose(window)) { + // input + // ----- + processInput(window); + + // render + // ------ + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + // draw our first triangle + glUseProgram(this->program); + glBindVertexArray( + VAOTriangle); // seeing as we only have a single VAO there's no + // need to bind it every time, but we'll do so to + // keep things a bit more organized + /* glDrawArrays(GL_TRIANGLES, 0, 3); */ + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + // glBindVertexArray(0); // no need to unbind it every time + + // glfw: swap buffers and poll IO events (keys pressed/released, mouse + // moved etc.) + // ------------------------------------------------------------------------------- + glfwSwapBuffers(window); + glfwPollEvents(); + } + // optional: de-allocate all resources once they've outlived their purpose: + // ------------------------------------------------------------------------ + glDeleteVertexArrays(1, &VAOTriangle); + glDeleteBuffers(1, &VBOTriangle); + glDeleteProgram(this->program); + // glfw: terminate, clearing all previously allocated GLFW resources. // ------------------------------------------------------------------ glfwTerminate(); diff --git a/src/graphics.hpp b/src/graphics.hpp index 9b93039..2020329 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -7,7 +7,7 @@ class ShaderProgram { public: - enum Base { Empty, Triangle }; + enum Base { Empty, Triangle, Polygons }; std::string baseName(); static Base baseFromName(std::string name); ShaderProgram(Base base, std::string vertexSource, std::string fragSource);