diff options
Diffstat (limited to 'advanced_opengl/10.antialiasing/anti_aliasing_msaa.c')
-rw-r--r-- | advanced_opengl/10.antialiasing/anti_aliasing_msaa.c | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c b/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c new file mode 100644 index 0000000..9fcb645 --- /dev/null +++ b/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c @@ -0,0 +1,208 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +static S32 global_width = 1024, global_height = 768; +static Input global_input; + +void +key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + switch (action) + { + case GLFW_PRESS: { + switch (key) + { + case GLFW_KEY_D: { + global_input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + global_input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + global_input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + global_input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + global_input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + global_input.move_down.state = KeyState_PRESS; + } break; + case GLFW_KEY_SPACE: { + global_input.jump.state = KeyState_PRESS; + } break; + case GLFW_KEY_RIGHT: { + global_input.action_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_ESCAPE: { + global_input.exit.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + global_input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + global_input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + global_input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + global_input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + global_input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + global_input.move_down.state = KeyState_RELEASE; + } break; + case GLFW_KEY_SPACE: { + global_input.jump.state = KeyState_RELEASE; + } break; + case GLFW_KEY_RIGHT: { + global_input.action_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_ESCAPE: { + global_input.exit.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +window_resize_callback(GLFWwindow* window, int width, int height) +{ + global_width = width; + global_height = height; + glViewport(0, 0, global_width, global_height); +} + +int +main(void) +{ + GLFWwindow *window; + Arena *arena = 0; + F32 target_fps, target_spf, + last_time, dt; + MAT4 projection, view, model; + V3F camera_pos; + F32 camera_speed, fovx, near, far; + U32 color_shader; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_SAMPLES, 4); + window = glfwCreateWindow(global_width, global_height, "Anti Aliasing (MSAA)", 0, 0); + if (!window) + { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } + + glfwMakeContextCurrent(window); + + glfwSetKeyCallback(window, key_callback); + glfwSetWindowSizeCallback(window, window_resize_callback); + + if (glewInit() != GLEW_OK) + { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); + + arena = arena_alloc(Megabytes(64)); + + Mesh *cube_mesh = mesh_load_obj(arena, "../../data/models/cube.obj"); + color_shader = create_shader_program("shaders/color.vs", + "shaders/color.fs"); + + target_fps = 60.0f; + target_spf = 1.0f/target_fps; + + camera_pos = v3f(0.0f, 0.0f, 3.0f); + camera_speed = 2.0f; + fovx = 90.0f; + near = 0.1f; + far = 1000.0f; + + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* NOTE(pryazha): Update */ + if (key_is_pressed(global_input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + camera_pos = update_camera_orbital(global_input, + camera_pos, v3f_zero(), + dt, camera_speed); + + projection = perspective(fovx, (F32)global_width/(F32)global_height, near, far); + view = look_at(camera_pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + model = mat4_identity(); + + input_update_last_state(&global_input); + + /* NOTE(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glUseProgram(color_shader); + shader_set_mat4fv(color_shader, "projection", projection); + shader_set_mat4fv(color_shader, "view", view); + shader_set_mat4fv(color_shader, "model", model); + mesh_draw(cube_mesh); + + glfwSwapBuffers(window); + + F32 elapsed = glfwGetTime()-last_time; + if (elapsed < target_spf) + { + U32 sleep_time = (U32)(target_spf-elapsed); + if (sleep_time > 0) + sleep(sleep_time); + } + F32 current_time = glfwGetTime(); + dt = current_time-last_time; + last_time = current_time; + } + + arena_release(arena); + glfwTerminate(); + return(0); +} |