Move framebuffer initialization to a helper function

This commit is contained in:
Dimitri Lozeve 2021-02-25 19:57:27 +01:00
parent 163cdd63d4
commit 03d1a2c9f4
3 changed files with 39 additions and 18 deletions

View file

@ -56,28 +56,11 @@ int main(int argc, char *argv[]) {
glUseProgram(buffer_shader);
glUniform1i(glGetUniformLocation(buffer_shader, "u_texture"), 0);
/* Framebuffer */
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
/* color attachment texture */
glGenTextures(1, &texture_color_buffer);
glBindTexture(GL_TEXTURE_2D, texture_color_buffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture_color_buffer, 0);
/* check that the framebuffer is complete */
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
log_error("Framebuffer is not complete");
if (initialize_framebuffer(&framebuffer, &texture_color_buffer, WINDOW_WIDTH, WINDOW_HEIGHT)) {
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_FAILURE;
} else {
log_debug("Framebuffer initialized and complete");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
/* Drawing loop */

View file

@ -100,6 +100,40 @@ unsigned int initialize_vertices() {
return VAO;
}
/**
* @brief Initialize a framebuffer and the associated texture.
*
* @param framebuffer The framebuffer ID to be initialized.
* @param texture_color_buffer The texture ID to be initialized.
* @param texture_width The width of the desired texture image.
* @param texture_height The height of the desired texture image.
* @return 0 on success, 1 on failure.
*/
unsigned int initialize_framebuffer(unsigned int *framebuffer,
unsigned int *texture_color_buffer,
unsigned int texture_width,
unsigned int texture_height) {
glGenFramebuffers(1, framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, *framebuffer);
/* color attachment texture */
glGenTextures(1, texture_color_buffer);
glBindTexture(GL_TEXTURE_2D, *texture_color_buffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_width, texture_height, 0,
GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
*texture_color_buffer, 0);
/* check that the framebuffer is complete */
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
log_error("Framebuffer is not complete");
return 1;
}
log_debug("Framebuffer initialized and complete");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return 0;
}
/**
* @brief Callback to adjust the size of the viewport when the window
* is resized.

View file

@ -5,6 +5,10 @@
GLFWwindow *initialize_window(int width, int height);
unsigned int initialize_vertices();
unsigned int initialize_framebuffer(unsigned int *framebuffer,
unsigned int *texture_color_buffer,
unsigned int texture_width,
unsigned int texture_height);
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void capture_screenshot();
void process_input(GLFWwindow *window, unsigned int *screen_shader,