Cleanup, extra comments and error logging
This commit is contained in:
parent
aa75721e94
commit
0b7da3fad7
43
src/main.cc
43
src/main.cc
@ -38,6 +38,7 @@ struct App {
|
|||||||
GLuint vbo;
|
GLuint vbo;
|
||||||
|
|
||||||
GLuint shader_program;
|
GLuint shader_program;
|
||||||
|
bool running;
|
||||||
};
|
};
|
||||||
|
|
||||||
int init (App &app);
|
int init (App &app);
|
||||||
@ -55,8 +56,10 @@ int main() {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup the geometry
|
||||||
create_vertex_spec(app);
|
create_vertex_spec(app);
|
||||||
|
|
||||||
|
// Setup graphics pipeline. At the very minimum creating vertex and fragment shaders
|
||||||
create_graphics_pipeline(app);
|
create_graphics_pipeline(app);
|
||||||
|
|
||||||
run_main_loop(app);
|
run_main_loop(app);
|
||||||
@ -92,7 +95,7 @@ int init(App &app) {
|
|||||||
return EXIT_CODE_OPENGL_CONTEXT_FAILED;
|
return EXIT_CODE_OPENGL_CONTEXT_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init glad
|
// Init GLAD
|
||||||
if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) {
|
if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) {
|
||||||
fprintf(stderr, "Failed to initialise glad");
|
fprintf(stderr, "Failed to initialise glad");
|
||||||
return EXIT_CODE_GLAD_LOADER_FAILED;
|
return EXIT_CODE_GLAD_LOADER_FAILED;
|
||||||
@ -110,11 +113,12 @@ int init(App &app) {
|
|||||||
|
|
||||||
void create_vertex_spec(App &app) {
|
void create_vertex_spec(App &app) {
|
||||||
const std::vector<GLfloat> vertices = {
|
const std::vector<GLfloat> vertices = {
|
||||||
-0.8f, -0.8f, 0.0f,
|
-0.8f, -0.8f, 0.0f, // position
|
||||||
0.8f, -0.8f, 0.0f,
|
0.8f, -0.8f, 0.0f, // position
|
||||||
0.0f, 0.8f, 0.0f,
|
0.0f, 0.8f, 0.0f, // position
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Create and activate the VAO
|
||||||
glGenVertexArrays(1, &app.vao);
|
glGenVertexArrays(1, &app.vao);
|
||||||
glBindVertexArray(app.vao);
|
glBindVertexArray(app.vao);
|
||||||
|
|
||||||
@ -134,7 +138,8 @@ void create_vertex_spec(App &app) {
|
|||||||
// for each vertex and the offset of the first instance of the component in the buffer
|
// for each vertex and the offset of the first instance of the component in the buffer
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
|
||||||
|
|
||||||
// Cleanup. The order matters. Unbind VAO first then disable the attributes. If you do it the
|
// Cleanup.
|
||||||
|
// The order matters. Unbind VAO first then disable the attributes. If you do it the
|
||||||
// other way around, the VAO will store that the attributes are disabled and nothing will be
|
// other way around, the VAO will store that the attributes are disabled and nothing will be
|
||||||
// drawn during the rendering stage
|
// drawn during the rendering stage
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@ -160,13 +165,13 @@ void create_graphics_pipeline(App &app) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void run_main_loop(App &app) {
|
void run_main_loop(App &app) {
|
||||||
bool running = true;
|
app.running = true;
|
||||||
|
|
||||||
while (running) {
|
while (app.running) {
|
||||||
while (SDL_PollEvent(&app.event)) {
|
while (SDL_PollEvent(&app.event)) {
|
||||||
switch (app.event.type) {
|
switch (app.event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
running = false;
|
app.running = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,6 +193,9 @@ void run_main_loop(App &app) {
|
|||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
// End draw call
|
// End draw call
|
||||||
|
|
||||||
|
// Not necessary if we only have one shader program
|
||||||
|
glUseProgram(0);
|
||||||
|
|
||||||
SDL_GL_SwapWindow(app.window);
|
SDL_GL_SwapWindow(app.window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,8 +215,25 @@ GLuint create_shader_program(const std::string &vertex_shader_source, const std:
|
|||||||
glAttachShader(shader_program, fragment_shader);
|
glAttachShader(shader_program, fragment_shader);
|
||||||
glLinkProgram (shader_program);
|
glLinkProgram (shader_program);
|
||||||
|
|
||||||
|
GLint link_status;
|
||||||
|
glGetProgramiv(shader_program, GL_LINK_STATUS, &link_status);
|
||||||
|
if (link_status != GL_TRUE) {
|
||||||
|
GLsizei log_length = 0;
|
||||||
|
GLchar message[1024];
|
||||||
|
glGetProgramInfoLog(shader_program, 1024, &log_length, message);
|
||||||
|
fprintf(stderr, "Failed to link program: %s\n", message);
|
||||||
|
}
|
||||||
|
|
||||||
// Validate the shader_program
|
// Validate the shader_program
|
||||||
glValidateProgram(shader_program);
|
glValidateProgram(shader_program);
|
||||||
|
GLint validation_status;
|
||||||
|
glGetProgramiv(shader_program, GL_VALIDATE_STATUS, &validation_status);
|
||||||
|
if (validation_status != GL_TRUE) {
|
||||||
|
GLsizei log_length = 0;
|
||||||
|
GLchar message[1024];
|
||||||
|
glGetProgramInfoLog(shader_program, 1024, &log_length, message);
|
||||||
|
fprintf(stderr, "Failed to validate program: %s\n", message);
|
||||||
|
}
|
||||||
|
|
||||||
return shader_program;
|
return shader_program;
|
||||||
}
|
}
|
||||||
@ -226,7 +251,7 @@ GLuint compile_shader(GLuint type, const std::string &source) {
|
|||||||
GLsizei log_length = 0;
|
GLsizei log_length = 0;
|
||||||
GLchar message[1024];
|
GLchar message[1024];
|
||||||
glGetShaderInfoLog(shader, 1024, &log_length, message);
|
glGetShaderInfoLog(shader, 1024, &log_length, message);
|
||||||
fprintf(stderr, "%s\n", message);
|
fprintf(stderr, "Failed to compile shader: %s\n", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return shader;
|
return shader;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user