Move shader initialization to a separate function
This commit is contained in:
parent
371ecd7f9b
commit
1b647ede36
4 changed files with 48 additions and 39 deletions
42
src/main.c
42
src/main.c
|
@ -116,8 +116,6 @@ int main(int argc, char *argv[]) {
|
||||||
state.inotify_fd = -1;
|
state.inotify_fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
initialize_shaders(&state, arguments.shader_file, arguments.buffer_file);
|
|
||||||
|
|
||||||
state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT);
|
state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
if (state.window == NULL) {
|
if (state.window == NULL) {
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
@ -126,40 +124,14 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
unsigned int VAO = initialize_vertices();
|
unsigned int VAO = initialize_vertices();
|
||||||
|
|
||||||
state.screen_shader.program = glCreateProgram();
|
int err =
|
||||||
if (!state.screen_shader.program) {
|
initialize_shaders(&state, arguments.shader_file, arguments.buffer_file,
|
||||||
log_error("Could not create screen shader program");
|
WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
if (err) {
|
||||||
glfwDestroyWindow(state.window);
|
glfwDestroyWindow(state.window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
compile_shaders(&state.screen_shader.program, state.screen_shader.filename);
|
|
||||||
glUseProgram(state.screen_shader.program);
|
|
||||||
glUniform1i(glGetUniformLocation(state.screen_shader.program, "u_texture"),
|
|
||||||
0);
|
|
||||||
|
|
||||||
unsigned int framebuffer = 0;
|
|
||||||
unsigned int texture_color_buffer = 0;
|
|
||||||
if (state.buffer_shader.filename) {
|
|
||||||
state.buffer_shader.program = glCreateProgram();
|
|
||||||
if (!state.buffer_shader.program) {
|
|
||||||
log_error("Could not create buffer shader program");
|
|
||||||
glfwDestroyWindow(state.window);
|
|
||||||
glfwTerminate();
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
compile_shaders(&state.buffer_shader.program, state.buffer_shader.filename);
|
|
||||||
glUseProgram(state.buffer_shader.program);
|
|
||||||
glUniform1i(glGetUniformLocation(state.buffer_shader.program, "u_texture"),
|
|
||||||
0);
|
|
||||||
|
|
||||||
if (initialize_framebuffer(&framebuffer, &texture_color_buffer,
|
|
||||||
WINDOW_WIDTH, WINDOW_HEIGHT)) {
|
|
||||||
glfwDestroyWindow(state.window);
|
|
||||||
glfwTerminate();
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Drawing loop */
|
/* Drawing loop */
|
||||||
glfwSetTime(0.0);
|
glfwSetTime(0.0);
|
||||||
|
@ -184,7 +156,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (state.buffer_shader.filename) {
|
if (state.buffer_shader.filename) {
|
||||||
/* bind the framebuffer and draw to it */
|
/* bind the framebuffer and draw to it */
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
glBindFramebuffer(GL_FRAMEBUFFER, state.framebuffer);
|
||||||
|
|
||||||
/* Background */
|
/* Background */
|
||||||
glClearColor(0, 0, 0, 1.0f);
|
glClearColor(0, 0, 0, 1.0f);
|
||||||
|
@ -204,7 +176,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
/* Draw the vertices */
|
/* Draw the vertices */
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture_color_buffer);
|
glBindTexture(GL_TEXTURE_2D, state.texture_color_buffer);
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
@ -229,7 +201,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
/* Draw the vertices */
|
/* Draw the vertices */
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture_color_buffer);
|
glBindTexture(GL_TEXTURE_2D, state.texture_color_buffer);
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,9 @@ struct renderer_state {
|
||||||
GLFWwindow *window; /**< GLFW window where the shaders are rendered. */
|
GLFWwindow *window; /**< GLFW window where the shaders are rendered. */
|
||||||
struct shader_state screen_shader; /**< Shader for the main screen. */
|
struct shader_state screen_shader; /**< Shader for the main screen. */
|
||||||
struct shader_state buffer_shader; /**< Shader for the framebuffer. */
|
struct shader_state buffer_shader; /**< Shader for the framebuffer. */
|
||||||
|
unsigned int framebuffer; /**< Framebuffer. */
|
||||||
|
unsigned int
|
||||||
|
texture_color_buffer; /**< Texture where the framebuffer renders. */
|
||||||
int inotify_fd; /**< inotify file descriptor. */
|
int inotify_fd; /**< inotify file descriptor. */
|
||||||
size_t frame_count; /**< Frame count since the start of the render loop. */
|
size_t frame_count; /**< Frame count since the start of the render loop. */
|
||||||
size_t prev_frame_count; /**< Frame count at the last log. */
|
size_t prev_frame_count; /**< Frame count at the last log. */
|
||||||
|
|
|
@ -8,16 +8,20 @@
|
||||||
#include "shaders.h"
|
#include "shaders.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize shaders and setup inotify if required.
|
* @brief Initialize shaders, compile them, and create the required
|
||||||
|
* texture for the buffer shader.
|
||||||
*
|
*
|
||||||
* @param state The target renderer state.
|
* @param state The target renderer state.
|
||||||
* @param shader_file The file name of the screen shader.
|
* @param shader_file The file name of the screen shader.
|
||||||
* @param buffer_file The file name of the buffer shader, or NULL if no buffer
|
* @param buffer_file The file name of the buffer shader, or NULL if no buffer
|
||||||
* shader.
|
* shader.
|
||||||
|
* @param texture_width The width of the texture for the buffer shader.
|
||||||
|
* @param texture_height The height of the texture for the buffer shader.
|
||||||
* @return 0 on success, 1 on error.
|
* @return 0 on success, 1 on error.
|
||||||
*/
|
*/
|
||||||
int initialize_shaders(struct renderer_state *state, const char *shader_file,
|
int initialize_shaders(struct renderer_state *state, const char *shader_file,
|
||||||
const char *buffer_file) {
|
const char *buffer_file, int texture_width,
|
||||||
|
int texture_height) {
|
||||||
state->screen_shader.filename = shader_file;
|
state->screen_shader.filename = shader_file;
|
||||||
log_info("Screen shader file: %s", state->screen_shader.filename);
|
log_info("Screen shader file: %s", state->screen_shader.filename);
|
||||||
|
|
||||||
|
@ -49,6 +53,35 @@ int initialize_shaders(struct renderer_state *state, const char *shader_file,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state->screen_shader.program = glCreateProgram();
|
||||||
|
if (!state->screen_shader.program) {
|
||||||
|
log_error("Could not create screen shader program");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
compile_shaders(&state->screen_shader.program, state->screen_shader.filename);
|
||||||
|
glUseProgram(state->screen_shader.program);
|
||||||
|
glUniform1i(glGetUniformLocation(state->screen_shader.program, "u_texture"),
|
||||||
|
0);
|
||||||
|
|
||||||
|
if (state->buffer_shader.filename) {
|
||||||
|
state->buffer_shader.program = glCreateProgram();
|
||||||
|
if (!state->buffer_shader.program) {
|
||||||
|
log_error("Could not create buffer shader program");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
compile_shaders(&state->buffer_shader.program,
|
||||||
|
state->buffer_shader.filename);
|
||||||
|
glUseProgram(state->buffer_shader.program);
|
||||||
|
glUniform1i(glGetUniformLocation(state->buffer_shader.program, "u_texture"),
|
||||||
|
0);
|
||||||
|
|
||||||
|
if (initialize_framebuffer(&state->framebuffer,
|
||||||
|
&state->texture_color_buffer, texture_width,
|
||||||
|
texture_height)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
int initialize_shaders(struct renderer_state *state, const char *shader_file,
|
int initialize_shaders(struct renderer_state *state, const char *shader_file,
|
||||||
const char *buffer_file);
|
const char *buffer_file, int window_width,
|
||||||
|
int window_height);
|
||||||
int compile_shaders(unsigned int *shader_program,
|
int compile_shaders(unsigned int *shader_program,
|
||||||
const char *const fragment_shader_file);
|
const char *const fragment_shader_file);
|
||||||
char *read_file(const char *const filename);
|
char *read_file(const char *const filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue