Compare commits

...

3 commits

Author SHA1 Message Date
93f4f716c0 triangle 2023-10-22 19:42:14 +02:00
3c36684f41 build and use shader program 2023-10-21 19:26:45 +02:00
fd7f680592 add frag shader again 2023-10-21 19:21:49 +02:00
3 changed files with 87 additions and 14 deletions

View file

@ -1 +1,7 @@
// empty #version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

View file

@ -54,8 +54,8 @@ ShaderProgram::ShaderProgram(Base base, string vertexSource,
string fragSource) { string fragSource) {
int success; int success;
char infoLog[512]; char infoLog[512];
const char* vertexSource_c = vertexSource.c_str(); const char *vertexSource_c = vertexSource.c_str();
const char* fragSource_c = fragSource.c_str(); const char *fragSource_c = fragSource.c_str();
this->base = base; this->base = base;
BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\""; BOOST_LOG_TRIVIAL(info) << "With base program \"" << this->baseName() << "\"";
// NOTE: char arrays and char pointers are not actually the same. // NOTE: char arrays and char pointers are not actually the same.
@ -78,7 +78,6 @@ ShaderProgram::ShaderProgram(Base base, string vertexSource,
exit(EXIT_SHADER); exit(EXIT_SHADER);
} }
/* FIXME: Optionally include a frag shader
BOOST_LOG_TRIVIAL(trace) << "loading fragments shader"; BOOST_LOG_TRIVIAL(trace) << "loading fragments shader";
glShaderSource(this->frag, 1, &fragSource_c, NULL); glShaderSource(this->frag, 1, &fragSource_c, NULL);
BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader"; BOOST_LOG_TRIVIAL(trace) << "compiling fragments shader";
@ -91,7 +90,24 @@ ShaderProgram::ShaderProgram(Base base, string vertexSource,
<< infoLog << std::endl; << infoLog << std::endl;
exit(EXIT_SHADER); exit(EXIT_SHADER);
} }
*/
// combine it all in a actual program
this->program = glCreateProgram();
glAttachShader(this->program, this->vertex);
glAttachShader(this->program, this->frag);
glLinkProgram(this->program);
glGetProgramiv(this->program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->program, 512, NULL, infoLog);
BOOST_LOG_TRIVIAL(fatal) << "could not link shader program:\n\n"
<< infoLog << std::endl;
exit(EXIT_SHADER);
}
// finalizing our setup
glDeleteShader(this->vertex);
glDeleteShader(this->frag);
} }
/** defines the base program behavior. /** defines the base program behavior.
@ -100,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);

View file

@ -17,6 +17,7 @@ protected:
unsigned int vertex; unsigned int vertex;
unsigned int frag; unsigned int frag;
Base base; Base base;
unsigned int program;
}; };
int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window); int mainWindow(ShaderProgram* shaderProgramm, GLFWwindow* window);
GLFWwindow* initGl(); GLFWwindow* initGl();