From f9ad6fa902c1167d7622ee7af2617d14b62bee21 Mon Sep 17 00:00:00 2001 From: pryazha Date: Wed, 19 Feb 2025 22:26:48 +0500 Subject: quite a lot of changes that I, of course, are not going to describe;) --- advanced_opengl/10.antialiasing/anti_aliasing_msaa | Bin 1277808 -> 1279192 bytes .../10.antialiasing/anti_aliasing_msaa.c | 309 ++++++-------- .../10.antialiasing/anti_aliasing_offscreen | Bin 1278840 -> 1284864 bytes .../10.antialiasing/anti_aliasing_offscreen.c | 453 +++++++++------------ advanced_opengl/10.antialiasing/shaders/color.frag | 8 + advanced_opengl/10.antialiasing/shaders/color.fs | 9 - advanced_opengl/10.antialiasing/shaders/color.vert | 11 + advanced_opengl/10.antialiasing/shaders/color.vs | 12 - .../10.antialiasing/shaders/screen.frag | 110 +++++ advanced_opengl/10.antialiasing/shaders/screen.fs | 119 ------ .../10.antialiasing/shaders/screen.vert | 11 + advanced_opengl/10.antialiasing/shaders/screen.vs | 12 - 12 files changed, 443 insertions(+), 611 deletions(-) create mode 100644 advanced_opengl/10.antialiasing/shaders/color.frag delete mode 100644 advanced_opengl/10.antialiasing/shaders/color.fs create mode 100644 advanced_opengl/10.antialiasing/shaders/color.vert delete mode 100644 advanced_opengl/10.antialiasing/shaders/color.vs create mode 100644 advanced_opengl/10.antialiasing/shaders/screen.frag delete mode 100644 advanced_opengl/10.antialiasing/shaders/screen.fs create mode 100644 advanced_opengl/10.antialiasing/shaders/screen.vert delete mode 100644 advanced_opengl/10.antialiasing/shaders/screen.vs (limited to 'advanced_opengl/10.antialiasing') diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_msaa b/advanced_opengl/10.antialiasing/anti_aliasing_msaa index 36811f7..694aa7d 100755 Binary files a/advanced_opengl/10.antialiasing/anti_aliasing_msaa and b/advanced_opengl/10.antialiasing/anti_aliasing_msaa differ diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c b/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c index 65a5b01..250ab97 100644 --- a/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c +++ b/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c @@ -2,207 +2,126 @@ #include "GLFW/glfw3.h" #include "pwyazh.h" - #include "pwyazh_GL.h" #include "common.h" -#include +int main(void) +{ + GLFWwindow *window; + Arena *arena = 0; + Mesh *cube; + Input input; + F32 target_fps, target_spf, last_time; + MAT4 proj, view, model; + State state; + S32 width, height; + U32 color_shader; -static S32 global_width = 1024, global_height = 768; -static Input global_input; + if (glfwInit() == GLFW_FALSE) { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } -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; - } -} + width = 1024; + height = 768; -void -window_resize_callback(GLFWwindow* window, int width, int height) -{ - global_width = width; - global_height = height; - glViewport(0, 0, global_width, global_height); -} + 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(width, height, "Anti Aliasing (MSAA)", 0, 0); + if (!window) { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } -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); - - V3F camera_dv = get_dv_camera_orbital(&global_input, camera_pos, - v3f_zero(), dt, camera_speed); - camera_pos = v3f_add(camera_pos, camera_dv); - - 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); + glfwMakeContextCurrent(window); + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + 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(Kilobytes(4)); + + cube = mesh_load_obj(arena, "../../data/models/cube.obj"); + color_shader = create_shader_program("shaders/color.vert", + "shaders/color.frag"); + + state.camera = (Camera) { + v3f(0.0f, 0.0f, 3.0f), + 90.0f, 0.1f, 1000.0f, + 0.0f, 0.0f + }; + input = input_init(); + + target_fps = 60.0f; + target_spf = 1.0f/target_fps; + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + /* NOTE(pryazha): Update */ + glfwGetFramebufferSize(window, &width, &height); + process_glfw_keyboard(window, &input); + process_glfw_mouse_pos(window, &input); + + if (key_first_press(input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + V3F dv = get_dv_camera_first_person(&input, &state.camera, + 4.0f, state.dt); + state.camera.pos = v3f_add(state.camera.pos, dv); + + F32 sensitivity = 0.1f; + input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); + state.camera.yaw += input.mouse_offset.x; + state.camera.pitch += input.mouse_offset.y; + if (state.camera.pitch > 89.0f) + state.camera.pitch = 89.0f; + if (state.camera.pitch < -89.0f) + state.camera.pitch = -89.0f; + + input_update_last_state(&input); + + view = get_view_matrix(&state.camera); + proj = perspective(state.camera.fovx, (F32)width/(F32)height, + state.camera.near, state.camera.far); + model = mat4_identity(); + + /* 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, "proj", proj); + shader_set_mat4fv(color_shader, "view", view); + shader_set_mat4fv(color_shader, "model", model); + mesh_draw(cube); + + 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(); + state.dt = current_time-last_time; + last_time = current_time; + } + + arena_release(arena); + glfwTerminate(); + return(0); } diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen index c8a241c..db7f6ec 100755 Binary files a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen and b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen differ diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c index 5c6debb..31f3cae 100644 --- a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c +++ b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c @@ -2,275 +2,200 @@ #include "GLFW/glfw3.h" #include "pwyazh.h" - #include "pwyazh_GL.h" #include "common.h" -#include +int main(void) +{ + GLFWwindow *window; + Arena *arena = 0; + Mesh *cube; + F32 target_fps, target_spf, last_time; + MAT4 proj, view, model; + U32 fbo, intermediate_fbo, color_tex, + screen_texture, rbo, + quad_vao, vbo, color_shader, + screen_shader; + S32 width, height; + State state; + Input input; -#define WIDTH 1024 -#define HEIGHT 768 -static Input global_input; + if (glfwInit() == GLFW_FALSE) { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } -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; - } -} + width = 1024; + height = 768; -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 fbo, intermediate_fbo, color_tex, - screen_texture, rbo, - quad_vao, vbo, - color_shader, screen_shader; - - if (glfwInit() == GLFW_FALSE) - { - fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); - return(1); - } - - glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - window = glfwCreateWindow(WIDTH, HEIGHT, "Anti Aliasing (Off-screen)", 0, 0); - if (!window) - { - fprintf(stderr, "[ERROR] Failed to create window.\n"); - glfwTerminate(); - return(1); - } - - glfwMakeContextCurrent(window); - - glfwSetKeyCallback(window, key_callback); - - if (glewInit() != GLEW_OK) - { - fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); - glfwTerminate(); - return(1); - } - - 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"); - screen_shader = create_shader_program("shaders/screen.vs", - "shaders/screen.fs"); - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glGenTextures(1, &color_tex); - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, color_tex); - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, - WIDTH, HEIGHT, GL_TRUE); - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D_MULTISAMPLE, color_tex, 0); - glGenRenderbuffers(1, &rbo); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, - WIDTH, HEIGHT); - glBindRenderbuffer(GL_RENDERBUFFER, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, - GL_RENDERBUFFER, rbo); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - fprintf(stderr, "[ERROR]: Framebuffer is not complete.\n"); - fprintf(stdout, "[INFO]: Multisample fbo complete.\n"); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - glGenFramebuffers(1, &intermediate_fbo); - glBindFramebuffer(GL_FRAMEBUFFER, intermediate_fbo); - glGenTextures(1, &screen_texture); - glBindTexture(GL_TEXTURE_2D, screen_texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, - 0, GL_RGB, GL_UNSIGNED_BYTE, 0); - 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, screen_texture, 0); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - fprintf(stderr, "[ERROR]: Framebuffer is not complete.\n"); - fprintf(stdout, "[INFO]: Intermediate fbo complete.\n"); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - F32 quad_vertices[] = { - -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, - 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, - -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, - -1.0f, 1.0f, 0.0f, 0.0f, 1.0f - }; - - glGenVertexArrays(1, &quad_vao); - glBindVertexArray(quad_vao); - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32))); - glBindVertexArray(0); - - 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); - - V3F camera_dv = get_dv_camera_orbital(&global_input, camera_pos, - v3f_zero(), dt, camera_speed); - camera_pos = v3f_add(camera_pos, camera_dv); - - projection = perspective(fovx, (F32)WIDTH/(F32)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 */ - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glClearColor(0.15f, 0.15f, 0.15f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); - 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); - - glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediate_fbo); - /* TODO(pryazha): Update width and height */ - glBlitFramebuffer(0, 0, WIDTH, HEIGHT, - 0, 0, WIDTH, HEIGHT, - GL_COLOR_BUFFER_BIT, GL_NEAREST); - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(screen_shader); - glDisable(GL_DEPTH_TEST); - glBindTexture(GL_TEXTURE_2D, screen_texture); - glBindVertexArray(quad_vao); - glDrawArrays(GL_TRIANGLES, 0, 6); - glBindVertexArray(0); - - 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); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + window = glfwCreateWindow(width, height, "Anti Aliasing (Off-screen)", 0, 0); + if (!window) { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } + + glfwMakeContextCurrent(window); + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + if (glewInit() != GLEW_OK) { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + arena = arena_alloc(Kilobytes(4)); + + cube = mesh_load_obj(arena, "../../data/models/cube.obj"); + color_shader = create_shader_program("shaders/color.vert", + "shaders/color.frag"); + screen_shader = create_shader_program("shaders/screen.vert", + "shaders/screen.frag"); + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glGenTextures(1, &color_tex); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, color_tex); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, + width, height, GL_TRUE); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D_MULTISAMPLE, color_tex, 0); + glGenRenderbuffers(1, &rbo); + glBindRenderbuffer(GL_RENDERBUFFER, rbo); + glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, + width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, + GL_RENDERBUFFER, rbo); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + fprintf(stderr, "[ERROR]: Framebuffer is not complete.\n"); + fprintf(stdout, "[INFO]: Multisample fbo complete.\n"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + glGenFramebuffers(1, &intermediate_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, intermediate_fbo); + glGenTextures(1, &screen_texture); + glBindTexture(GL_TEXTURE_2D, screen_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, + 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + 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, screen_texture, 0); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + fprintf(stderr, "[ERROR]: Framebuffer is not complete.\n"); + fprintf(stdout, "[INFO]: Intermediate fbo complete.\n"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + F32 quad_vertices[] = { + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f + }; + + glGenVertexArrays(1, &quad_vao); + glBindVertexArray(quad_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32))); + glBindVertexArray(0); + + state.camera = (Camera) { + v3f(0.0f, 0.0f, 3.0f), + 90.0f, 0.1f, 1000.0f, + 0.0f, 0.0f + }; + input = input_init(); + + target_fps = 60.0f; + target_spf = 1.0f/target_fps; + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* NOTE(pryazha): Update */ + glfwGetFramebufferSize(window, &width, &height); + process_glfw_keyboard(window, &input); + process_glfw_mouse_pos(window, &input); + + if (key_first_press(input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + V3F dv = get_dv_camera_first_person(&input, &state.camera, + 4.0f, state.dt); + state.camera.pos = v3f_add(state.camera.pos, dv); + + F32 sensitivity = 0.1f; + input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); + state.camera.yaw += input.mouse_offset.x; + state.camera.pitch += input.mouse_offset.y; + if (state.camera.pitch > 89.0f) + state.camera.pitch = 89.0f; + if (state.camera.pitch < -89.0f) + state.camera.pitch = -89.0f; + + input_update_last_state(&input); + + view = get_view_matrix(&state.camera); + proj = perspective(state.camera.fovx, (F32)width/(F32)height, + state.camera.near, state.camera.far); + model = mat4_identity(); + + /* NOTE(pryazha): Render */ + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + glUseProgram(color_shader); + shader_set_mat4fv(color_shader, "proj", proj); + shader_set_mat4fv(color_shader, "view", view); + shader_set_mat4fv(color_shader, "model", model); + mesh_draw(cube); + + glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediate_fbo); + glBlitFramebuffer(0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT, GL_NEAREST); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glUseProgram(screen_shader); + glDisable(GL_DEPTH_TEST); + glBindTexture(GL_TEXTURE_2D, screen_texture); + glBindVertexArray(quad_vao); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); + + 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(); + state.dt = current_time-last_time; + last_time = current_time; + } + + arena_release(arena); + glfwTerminate(); + return(0); } diff --git a/advanced_opengl/10.antialiasing/shaders/color.frag b/advanced_opengl/10.antialiasing/shaders/color.frag new file mode 100644 index 0000000..b4a31df --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/color.frag @@ -0,0 +1,8 @@ +#version 330 core + +out vec4 frag_color; + +void main(void) +{ + frag_color = vec4(0.76, 0.47, 0.84, 1.0); +} diff --git a/advanced_opengl/10.antialiasing/shaders/color.fs b/advanced_opengl/10.antialiasing/shaders/color.fs deleted file mode 100644 index 542143e..0000000 --- a/advanced_opengl/10.antialiasing/shaders/color.fs +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core - -out vec4 frag_color; - -void -main(void) -{ - frag_color = vec4(0.76, 0.47, 0.84, 1.0); -} diff --git a/advanced_opengl/10.antialiasing/shaders/color.vert b/advanced_opengl/10.antialiasing/shaders/color.vert new file mode 100644 index 0000000..a8b1247 --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/color.vert @@ -0,0 +1,11 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 proj; +uniform mat4 view; +uniform mat4 model; + +void main(void) +{ + gl_Position = proj*view*model*vec4(apos, 1.0); +} diff --git a/advanced_opengl/10.antialiasing/shaders/color.vs b/advanced_opengl/10.antialiasing/shaders/color.vs deleted file mode 100644 index ade669b..0000000 --- a/advanced_opengl/10.antialiasing/shaders/color.vs +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; - -void -main(void) -{ - gl_Position = projection*view*model*vec4(apos, 1.0); -} diff --git a/advanced_opengl/10.antialiasing/shaders/screen.frag b/advanced_opengl/10.antialiasing/shaders/screen.frag new file mode 100644 index 0000000..5ccc843 --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/screen.frag @@ -0,0 +1,110 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +vec4 inversion() +{ + vec4 result = vec4(1.0-vec3(texture(texture1, tex_coords)), 1.0); + return(result); +} + +vec4 grayscale_average() +{ + vec4 tex_color = texture(texture1, tex_coords); + float average = (tex_color.r+tex_color.g+tex_color.b)/3.0; + vec4 result = vec4(average, average, average, 1.0); + return(result); +} + +vec4 grayscale_weights() +{ + vec4 tex_color = texture(texture1, tex_coords); + float average = 0.2126*tex_color.r+0.7152*tex_color.g+0.0722*tex_color.b; + vec4 result = vec4(average, average, average, 1.0); + return(result); +} + +const float offset = 1.0/300.0; + +vec4 kernel_effect(float[9] kernel) +{ + vec2 offsets[9] = vec2[]( + vec2(-offset, offset), /* top-left */ + vec2(0.0, offset), /* top-center */ + vec2(offset, offset), /* top-right */ + vec2(-offset, 0.0), /* center-left */ + vec2(0.0, 0.0), /* center-center */ + vec2(offset, 0.0), /* center-right */ + vec2(-offset, -offset), /* bottom-left */ + vec2(0.0, -offset), /* bottom-center */ + vec2(offset, -offset) /* bottom-right */ + ); + + vec3 sample_texture[9]; + for (int i = 0; i < 9; ++i) + sample_texture[i] = vec3(texture(texture1, tex_coords+offsets[i])); + + vec3 color = vec3(0.0); + for (int i = 0; i < 9; ++i) + color += sample_texture[i]*kernel[i]; + + return(vec4(color, 1.0)); +} + +void +main(void) +{ + float strange_kernel[9] = float[]( + -1, -1, -1, + -1, 9, -1, + -1, -1, -1 + ); + + float gaussian_blur_kernel[9] = float[]( + 1.0/16.0, 2.0/16.0, 1.0/16.0, + 2.0/16.0, 4.0/16.0, 2.0/16.0, + 1.0/16.0, 2.0/16.0, 1.0/16.0 + ); + + float box_blur_kernel[9] = float[]( + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0 + ); + + float edge_detection_kernel[9] = float[]( + 1.0, 1.0, 1.0, + 1.0, -8.0, 1.0, + 1.0, 1.0, 1.0 + ); + + float sharpening_kernel[9] = float[]( + 0.0, -1.0, 0.0, + -1.0, 5.0, -1.0, + 0.0, -1.0, 0.0 + ); + + float sobel_kernel_vertical[9] = float[]( + -1.0, 0.0, 1.0, + -2.0, 0.0, 2.0, + -1.0, 0.0, 0.0 + ); + float sobel_kernel_horizontal[9] = float[]( + -1.0, -2.0, -1.0, + 0.0, 0.0, 0.0, + 1.0, 2.0, 1.0 + ); + + float funny_kernel[9] = float[]( + 1.0, 1.0, 1.0, + 1.0, -4.0, 1.0, + 1.0, 1.0, 1.0 + ); + + frag_color = kernel_effect(funny_kernel); + frag_color *= vec4(0.76, 0.47, 0.84, 1.0); +} diff --git a/advanced_opengl/10.antialiasing/shaders/screen.fs b/advanced_opengl/10.antialiasing/shaders/screen.fs deleted file mode 100644 index 183d490..0000000 --- a/advanced_opengl/10.antialiasing/shaders/screen.fs +++ /dev/null @@ -1,119 +0,0 @@ -#version 330 core - -out vec4 frag_color; - -in vec2 tex_coords; - -uniform sampler2D texture1; - -vec4 -inversion() -{ - vec4 result = vec4(1.0-vec3(texture(texture1, tex_coords)), 1.0); - return(result); -} - -vec4 -grayscale_average() -{ - vec4 tex_color = texture(texture1, tex_coords); - float average = (tex_color.r+tex_color.g+tex_color.b)/3.0; - vec4 result = vec4(average, average, average, 1.0); - return(result); -} - -vec4 -grayscale_weights() -{ - vec4 tex_color = texture(texture1, tex_coords); - float average = 0.2126*tex_color.r+0.7152*tex_color.g+0.0722*tex_color.b; - vec4 result = vec4(average, average, average, 1.0); - return(result); -} - -const float offset = 1.0/300.0; - -vec4 -kernel_effect(float[9] kernel) -{ - vec2 offsets[9] = vec2[]( - vec2(-offset, offset), /* top-left */ - vec2(0.0, offset), /* top-center */ - vec2(offset, offset), /* top-right */ - vec2(-offset, 0.0), /* center-left */ - vec2(0.0, 0.0), /* center-center */ - vec2(offset, 0.0), /* center-right */ - vec2(-offset, -offset), /* bottom-left */ - vec2(0.0, -offset), /* bottom-center */ - vec2(offset, -offset) /* bottom-right */ - ); - - vec3 sample_texture[9]; - for (int offset_index = 0; - offset_index < 9; - ++offset_index) - { - sample_texture[offset_index] = - vec3(texture(texture1, tex_coords+offsets[offset_index])); - } - - vec3 color = vec3(0.0); - for (int i = 0; i < 9; ++i) - color += sample_texture[i]*kernel[i]; - - return(vec4(color, 1.0)); -} - -void -main(void) -{ - float strange_kernel[9] = float[]( - -1, -1, -1, - -1, 9, -1, - -1, -1, -1 - ); - - float gaussian_blur_kernel[9] = float[]( - 1.0/16.0, 2.0/16.0, 1.0/16.0, - 2.0/16.0, 4.0/16.0, 2.0/16.0, - 1.0/16.0, 2.0/16.0, 1.0/16.0 - ); - - float box_blur_kernel[9] = float[]( - 1.0/9.0, 1.0/9.0, 1.0/9.0, - 1.0/9.0, 1.0/9.0, 1.0/9.0, - 1.0/9.0, 1.0/9.0, 1.0/9.0 - ); - - float edge_detection_kernel[9] = float[]( - 1.0, 1.0, 1.0, - 1.0, -8.0, 1.0, - 1.0, 1.0, 1.0 - ); - - float sharpening_kernel[9] = float[]( - 0.0, -1.0, 0.0, - -1.0, 5.0, -1.0, - 0.0, -1.0, 0.0 - ); - - float sobel_kernel_vertical[9] = float[]( - -1.0, 0.0, 1.0, - -2.0, 0.0, 2.0, - -1.0, 0.0, 0.0 - ); - float sobel_kernel_horizontal[9] = float[]( - -1.0, -2.0, -1.0, - 0.0, 0.0, 0.0, - 1.0, 2.0, 1.0 - ); - - float funny_kernel[9] = float[]( - 1.0, 1.0, 1.0, - 1.0, -4.0, 1.0, - 1.0, 1.0, 1.0 - ); - - frag_color = kernel_effect(funny_kernel); - frag_color *= vec4(0.76, 0.47, 0.84, 1.0); -} diff --git a/advanced_opengl/10.antialiasing/shaders/screen.vert b/advanced_opengl/10.antialiasing/shaders/screen.vert new file mode 100644 index 0000000..e473ddd --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/screen.vert @@ -0,0 +1,11 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +out vec2 tex_coords; + +void main(void) +{ + gl_Position = vec4(apos, 1.0); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/10.antialiasing/shaders/screen.vs b/advanced_opengl/10.antialiasing/shaders/screen.vs deleted file mode 100644 index 1f233ee..0000000 --- a/advanced_opengl/10.antialiasing/shaders/screen.vs +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; -layout(location = 1) in vec2 atex_coords; - -out vec2 tex_coords; - -void -main(void) -{ - gl_Position = vec4(apos, 1.0); - tex_coords = atex_coords; -} -- cgit v1.2.3