This commit is contained in:
Christoph J. Scherr 2023-10-22 19:42:14 +02:00
parent 3c36684f41
commit 93f4f716c0
1 changed files with 59 additions and 10 deletions

View File

@ -106,7 +106,6 @@ ShaderProgram::ShaderProgram(Base base, string vertexSource,
} }
// finalizing our setup // finalizing our setup
glUseProgram(this->program);
glDeleteShader(this->vertex); glDeleteShader(this->vertex);
glDeleteShader(this->frag); glDeleteShader(this->frag);
} }
@ -117,29 +116,79 @@ ShaderProgram::ShaderProgram(Base base, string vertexSource,
*/ */
int ShaderProgram::run(GLFWwindow *window) { int ShaderProgram::run(GLFWwindow *window) {
if (this->base == Triangle) { if (this->base == Triangle) {
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vTriangle[] = { float vTriangle[] = {
-0.5f, -0.5f, 0.0f, // opengl -0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // treats 3d arrays 0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // as large 1d arrays 0.0f, 0.5f, 0.0f // top
}; };
unsigned int oTriangle;
glGenBuffers(1, &oTriangle); unsigned int VBOTriangle, VAOTriangle;
glBindBuffer(GL_ARRAY_BUFFER, oTriangle); 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(vTriangle), vTriangle, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vTriangle), vTriangle, 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);
// 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)) { while (!glfwWindowShouldClose(window)) {
// input
// -----
processInput(window); processInput(window);
glClearColor(0.9f, 0.3f, 0.3f, 1.0f); // render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); 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);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse
// moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); 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();
return 0; return 0;
} else // Empty } else // Empty
{ {
// use the compiled shader
return 0; glUseProgram(this->program);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
processInput(window); processInput(window);