diff options
Diffstat (limited to 'advanced_opengl')
83 files changed, 4829 insertions, 0 deletions
diff --git a/advanced_opengl/1.depth/build.sh b/advanced_opengl/1.depth/build.sh new file mode 100755 index 0000000..837b21c --- /dev/null +++ b/advanced_opengl/1.depth/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='depth' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/1.depth/depth b/advanced_opengl/1.depth/depth Binary files differnew file mode 100755 index 0000000..db67630 --- /dev/null +++ b/advanced_opengl/1.depth/depth diff --git a/advanced_opengl/1.depth/depth.c b/advanced_opengl/1.depth/depth.c new file mode 100644 index 0000000..eb79c1c --- /dev/null +++ b/advanced_opengl/1.depth/depth.c @@ -0,0 +1,274 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1280 +#define HEIGHT 720 + +static V3F camera_pos; +static F32 dt; +static Input 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "depth testing", 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); + } + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + U32 shader = create_shader_program("shaders/depth.vs", "shaders/depth.fs"); + U32 marble_texture = load_texture("../../data/textures/marble.jpg"); + U32 metal_texture = load_texture("../../data/textures/metal.png"); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4*a)) + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 VAO, VBO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 0.0f, 3.0f); + + /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */ + + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + V3F left, up, forward; + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + + forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); + left = v3f_norm(v3f_cross(world_up, forward)); + up = v3f_cross(forward, left); + + V3F dp = v3f_zero(); + + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + + input_update_last_state(&input); + + camera_pos = v3f_add(camera_pos, dp); + + /* INFO(pryazha): render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(shader); + MAT4 model, view, projection; + + view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); + shader_set_mat4fv(shader, "view", view); + + /* projection = ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f); */ + F32 fovx = 90.0f; + F32 near = 0.1f; + F32 far = 100.0f; + projection = perspective(fovx, (F32)WIDTH/(F32)HEIGHT, near, far); + + shader_set_mat4fv(shader, "projection", projection); + + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(VAO); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + model = transform_apply(cube_positions[transform_index]); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + glBindTexture(GL_TEXTURE_2D, metal_texture); + model = transform_apply(platform_position); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/1.depth/shaders/depth.fs b/advanced_opengl/1.depth/shaders/depth.fs new file mode 100644 index 0000000..2f3d9ca --- /dev/null +++ b/advanced_opengl/1.depth/shaders/depth.fs @@ -0,0 +1,27 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D our_texture; + +float near = 0.1f; +float far = 100.0f; + +float +linearize_depth(float depth) +{ + float ndc_z = 2.0f*depth-1.0f; + return((2.0f*near*far)/(far+near-ndc_z*(far-near))); +} + +void +main(void) +{ + /* + frag_color = texture(our_texture, tex_coords); + */ + float depth = linearize_depth(gl_FragCoord.z)/far; + frag_color = vec4(vec3(depth), 1.0f); +} diff --git a/advanced_opengl/1.depth/shaders/depth.vs b/advanced_opengl/1.depth/shaders/depth.vs new file mode 100644 index 0000000..d3d92d1 --- /dev/null +++ b/advanced_opengl/1.depth/shaders/depth.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_msaa b/advanced_opengl/10.antialiasing/anti_aliasing_msaa Binary files differnew file mode 100755 index 0000000..d0afd7e --- /dev/null +++ b/advanced_opengl/10.antialiasing/anti_aliasing_msaa 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); +} diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen Binary files differnew file mode 100755 index 0000000..72be7e2 --- /dev/null +++ b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c new file mode 100644 index 0000000..91a26f9 --- /dev/null +++ b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c @@ -0,0 +1,276 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define 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; + } +} + +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); + + camera_pos = update_camera_orbital(global_input, + camera_pos, v3f_zero(), + dt, camera_speed); + + 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); +} diff --git a/advanced_opengl/10.antialiasing/build.sh b/advanced_opengl/10.antialiasing/build.sh new file mode 100755 index 0000000..9d1011a --- /dev/null +++ b/advanced_opengl/10.antialiasing/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +set -x +gcc -o anti_aliasing_msaa $CFLAGS $INCLUDE $LFLAGS anti_aliasing_msaa.c $LIBS +gcc -o anti_aliasing_offscreen $CFLAGS $INCLUDE $LFLAGS anti_aliasing_offscreen.c $LIBS diff --git a/advanced_opengl/10.antialiasing/shaders/color.fs b/advanced_opengl/10.antialiasing/shaders/color.fs new file mode 100644 index 0000000..b935555 --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/color.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(0.76f, 0.47f, 0.84f, 1.0f); +} diff --git a/advanced_opengl/10.antialiasing/shaders/color.vs b/advanced_opengl/10.antialiasing/shaders/color.vs new file mode 100644 index 0000000..ade669b --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/color.vs @@ -0,0 +1,12 @@ +#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.fs b/advanced_opengl/10.antialiasing/shaders/screen.fs new file mode 100644 index 0000000..8fefa24 --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/screen.fs @@ -0,0 +1,122 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +vec4 +inversion() +{ + vec4 result = vec4(1.0f-vec3(texture(texture1, tex_coords)), 1.0f); + return(result); +} + +vec4 +grayscale_average() +{ + vec4 tex_color = texture(texture1, tex_coords); + float average = (tex_color.r+tex_color.g+tex_color.b)/3.0f; + vec4 result = vec4(average, average, average, 1.0f); + 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.0722f*tex_color.b; + vec4 result = vec4(average, average, average, 1.0f); + return(result); +} + +const float offset = 1.0f/300.0f; + +vec4 +kernel_effect(float[9] kernel) +{ + vec2 offsets[9] = vec2[]( + vec2(-offset, offset), /* top-left */ + vec2(0.0f, offset), /* top-center */ + vec2(offset, offset), /* top-right */ + vec2(-offset, 0.0f), /* center-left */ + vec2(0.0f, 0.0f), /* center-center */ + vec2(offset, 0.0f), /* center-right */ + vec2(-offset, -offset), /* bottom-left */ + vec2(0.0f, -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.0f); + for (int i = 0; i < 9; ++i) + color += sample_texture[i]*kernel[i]; + + return(vec4(color, 1.0f)); +} + +void +main(void) +{ + float strange_kernel[9] = float[]( + -1, -1, -1, + -1, 9, -1, + -1, -1, -1 + ); + + float gaussian_blur_kernel[9] = float[]( + 1.0f/16.0f, 2.0f/16.0f, 1.0f/16.0f, + 2.0f/16.0f, 4.0f/16.0f, 2.0f/16.0f, + 1.0f/16.0f, 2.0f/16.0f, 1.0f/16.0f + ); + + float box_blur_kernel[9] = float[]( + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f + ); + + float edge_detection_kernel[9] = float[]( + 1.0f, 1.0f, 1.0f, + 1.0f, -8.0f, 1.0f, + 1.0f, 1.0f, 1.0f + ); + + float sharpening_kernel[9] = float[]( + 0.0f, -1.0f, 0.0f, + -1.0f, 5.0f, -1.0f, + 0.0f, -1.0f, 0.0f + ); + + float sobel_kernel_vertical[9] = float[]( + -1.0f, 0.0f, 1.0f, + -2.0f, 0.0f, 2.0f, + -1.0f, 0.0f, 0.0f + ); + float sobel_kernel_horizontal[9] = float[]( + -1.0f, -2.0f, -1.0f, + 0.0f, 0.0f, 0.0f, + 1.0f, 2.0f, 1.0f + ); + + float funny_kernel[9] = float[]( + 1.0f, 1.0f, 1.0f, + 1.0f, -4.0f, 1.0f, + 1.0f, 1.0f, 1.0f + ); + + /* + frag_color = kernel_effect(funny_kernel); + frag_color *= vec4(0.76f, 0.47f, 0.84f, 1.0f); + */ + frag_color = grayscale_weights(); +} diff --git a/advanced_opengl/10.antialiasing/shaders/screen.vs b/advanced_opengl/10.antialiasing/shaders/screen.vs new file mode 100644 index 0000000..0a5213b --- /dev/null +++ b/advanced_opengl/10.antialiasing/shaders/screen.vs @@ -0,0 +1,12 @@ +#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.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/2.stencil/build.sh b/advanced_opengl/2.stencil/build.sh new file mode 100755 index 0000000..335dd5d --- /dev/null +++ b/advanced_opengl/2.stencil/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='stencil' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/2.stencil/shaders/outline.fs b/advanced_opengl/2.stencil/shaders/outline.fs new file mode 100644 index 0000000..b935555 --- /dev/null +++ b/advanced_opengl/2.stencil/shaders/outline.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(0.76f, 0.47f, 0.84f, 1.0f); +} diff --git a/advanced_opengl/2.stencil/shaders/stencil.fs b/advanced_opengl/2.stencil/shaders/stencil.fs new file mode 100644 index 0000000..ff7dd9b --- /dev/null +++ b/advanced_opengl/2.stencil/shaders/stencil.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D our_texture; + +void +main(void) +{ + frag_color = texture(our_texture, tex_coords); +} diff --git a/advanced_opengl/2.stencil/shaders/stencil.vs b/advanced_opengl/2.stencil/shaders/stencil.vs new file mode 100644 index 0000000..d3d92d1 --- /dev/null +++ b/advanced_opengl/2.stencil/shaders/stencil.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/2.stencil/stencil b/advanced_opengl/2.stencil/stencil Binary files differnew file mode 100755 index 0000000..ad0f48c --- /dev/null +++ b/advanced_opengl/2.stencil/stencil diff --git a/advanced_opengl/2.stencil/stencil.c b/advanced_opengl/2.stencil/stencil.c new file mode 100644 index 0000000..9f45048 --- /dev/null +++ b/advanced_opengl/2.stencil/stencil.c @@ -0,0 +1,299 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1280 +#define HEIGHT 720 + +static V3F camera_pos; +static F32 dt; +static Input 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Stencil testing", 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); + } + + glEnable(GL_DEPTH_TEST); + + glEnable(GL_STENCIL_TEST); + glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + + U32 shader = create_shader_program("shaders/stencil.vs", "shaders/stencil.fs"); + U32 outline_shader = create_shader_program("shaders/stencil.vs", "shaders/outline.fs"); + U32 marble_texture = load_texture("../../data/textures/marble.jpg"); + U32 metal_texture = load_texture("../../data/textures/metal.png"); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4*a)) + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 VAO, VBO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 0.0f, 3.0f); + + /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */ + + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + V3F left, up, forward; + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + + forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); + left = v3f_norm(v3f_cross(world_up, forward)); + up = v3f_cross(forward, left); + + V3F dp = v3f_zero(); + + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + input_update_last_state(&input); + + camera_pos = v3f_add(camera_pos, dp); + + /* INFO(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + /* NOTE(pryazha): Update stencil buffer with ones while drawing the cubes */ + glStencilFunc(GL_ALWAYS, 1, 0xFF); + glStencilMask(0xFF); + + MAT4 model, view, projection; + view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + + glUseProgram(shader); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "projection", projection); + + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(VAO); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + model = transform_apply(cube_positions[transform_index]); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + /* NOTE(pryazha): Don't update stencil buffer while drawing the floor */ + glStencilMask(0x00); + + glBindTexture(GL_TEXTURE_2D, metal_texture); + model = transform_apply(platform_position); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + + glStencilFunc(GL_NOTEQUAL, 1, 0xFF); + glStencilMask(0x00); + glDisable(GL_DEPTH_TEST); + + glUseProgram(outline_shader); + shader_set_mat4fv(outline_shader, "view", view); + shader_set_mat4fv(outline_shader, "projection", projection); + + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + Transform transform = cube_positions[transform_index]; + transform = transform_scale(transform, v3f(1.1f, 1.1f, 1.1f)); + shader_set_mat4fv(outline_shader, "model", transform_apply(transform)); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + glStencilMask(0xFF); + glStencilFunc(GL_ALWAYS, 1, 0xFF); + glEnable(GL_DEPTH_TEST); + + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/3.blending/blending b/advanced_opengl/3.blending/blending Binary files differnew file mode 100755 index 0000000..063ce51 --- /dev/null +++ b/advanced_opengl/3.blending/blending diff --git a/advanced_opengl/3.blending/blending.c b/advanced_opengl/3.blending/blending.c new file mode 100644 index 0000000..9ac92ea --- /dev/null +++ b/advanced_opengl/3.blending/blending.c @@ -0,0 +1,373 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1280 +#define HEIGHT 720 + +static V3F camera_pos; +static F32 dt; +static Input 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Blending", 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); + } + + glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + U32 shader = create_shader_program("shaders/blending.vs", "shaders/blending.fs"); + U32 grass_shader = create_shader_program("shaders/blending.vs", "shaders/grass.fs"); + U32 marble_texture = load_texture("../../data/textures/marble.jpg"); + U32 metal_texture = load_texture("../../data/textures/metal.png"); + U32 grass_texture = load_texture("../../data/textures/grass.png"); + U32 window_texture = load_texture("../../data/textures/window.png"); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4.0f*a)) + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 cube_vao, quad_vao, vbo; + + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + F32 quad_side = 1.0f; + F32 quad_vertices[] = { + /* NOTE(pryazha): left top triangle */ + -quad_side/2.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -quad_side/2.0f, quad_side, 0.0f, 0.0f, 1.0f, + quad_side/2.0f, quad_side, 0.0f, 1.0f, 1.0f, + + /* NOTE(pryazha): right bottom triangle */ + -quad_side/2.0f, 0.0f, 0.0f, 0.0f, 0.0f, + quad_side/2.0f, quad_side, 0.0f, 1.0f, 1.0f, + quad_side/2.0f, 0.0f, 0.0f, 1.0f, 0.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); + + Transform grass_positions[] = { + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(1.0f, -0.5f, 0.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(-1.5f, -0.5f, 2.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(0.0f, -0.5f, -1.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(3.0f, -0.5f, 1.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(-2.0f, -0.5f, -3.0f)) + }; + + Transform window_transforms[] = { + transform_make_translate(v3f(0.0f, -0.5f, a+0.1f)), + transform_make_translate(v3f(0.0f, -0.5f, 2.0f)), + transform_make_translate(v3f(1.0f, -0.5f, -1.0f)), + transform_make_translate(v3f(-2.0f, -0.5f, 0.5f)), + transform_make_translate(v3f(-3.0f, -0.5f, -3.0f)), + transform_make_translate(v3f(3.0f, -0.5f, 0.2)), + }; + + F32 angle = 0.0f; + for (S32 window_index = 0; + window_index < (S32)ArrayCount(window_transforms); + ++window_index) + { + Transform *transform = window_transforms+window_index; + *transform = transform_rotate(*transform, v3f(0.0f, angle, 0.0f)); + angle += 30.0f; + } + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 0.0f, 3.0f); + + /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */ + + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + V3F left, up, forward; + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + + forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); + left = v3f_norm(v3f_cross(world_up, forward)); + up = v3f_cross(forward, left); + + V3F dp = v3f_zero(); + + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + + input_update_last_state(&input); + + camera_pos = v3f_add(camera_pos, dp); + + Transform sorted_window_transforms[ArrayCount(window_transforms)]; + MemoryCopyArray(sorted_window_transforms, window_transforms); + for (S32 i = 0; i < (S32)ArrayCount(sorted_window_transforms); ++i) { + for (S32 pos_index = 1; + pos_index < (S32)ArrayCount(sorted_window_transforms); + ++pos_index) + { + F32 distance_left = v3f_length(v3f_sub(sorted_window_transforms[pos_index-1].translate, camera_pos)); + F32 distance_right = v3f_length(v3f_sub(sorted_window_transforms[pos_index].translate, camera_pos)); + if (distance_left < distance_right) { + Transform temp = sorted_window_transforms[pos_index]; + sorted_window_transforms[pos_index] = sorted_window_transforms[pos_index-1]; + sorted_window_transforms[pos_index-1] = temp; + } + } + } + + /* INFO(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + MAT4 model, view, projection; + view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + + glUseProgram(shader); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "projection", projection); + + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(cube_vao); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + model = transform_apply(cube_positions[transform_index]); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + glBindTexture(GL_TEXTURE_2D, metal_texture); + model = transform_apply(platform_position); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + + glUseProgram(grass_shader); + glBindTexture(GL_TEXTURE_2D, grass_texture); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "projection", projection); + glBindVertexArray(quad_vao); + + F32 angle = 0.0f; + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(grass_positions); + ++transform_index) + { + Transform *transform = grass_positions+transform_index; + Transform new_transform = transform_rotate(*transform, v3f(0.0f, angle, 0.0f)); + shader_set_mat4fv(shader, "model", transform_apply(new_transform)); + glDrawArrays(GL_TRIANGLES, 0, 6); + angle += 30.0f; + } + + glUseProgram(shader); + glBindTexture(GL_TEXTURE_2D, window_texture); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "projection", projection); + for (S32 window_index = 0; + window_index < (S32)ArrayCount(sorted_window_transforms); + ++window_index) + { + MAT4 model = transform_apply(sorted_window_transforms[window_index]); + shader_set_mat4fv(shader, "model", model); + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/3.blending/build.sh b/advanced_opengl/3.blending/build.sh new file mode 100755 index 0000000..1fb5c9c --- /dev/null +++ b/advanced_opengl/3.blending/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='blending' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/3.blending/shaders/blending.fs b/advanced_opengl/3.blending/shaders/blending.fs new file mode 100644 index 0000000..f58c5d3 --- /dev/null +++ b/advanced_opengl/3.blending/shaders/blending.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/3.blending/shaders/blending.vs b/advanced_opengl/3.blending/shaders/blending.vs new file mode 100644 index 0000000..d3d92d1 --- /dev/null +++ b/advanced_opengl/3.blending/shaders/blending.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/3.blending/shaders/grass.fs b/advanced_opengl/3.blending/shaders/grass.fs new file mode 100644 index 0000000..cecb0ce --- /dev/null +++ b/advanced_opengl/3.blending/shaders/grass.fs @@ -0,0 +1,17 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + vec4 tex_color = texture(texture1, tex_coords); + if (tex_color.a <= 0.2) { + discard; + } + frag_color = tex_color; +} diff --git a/advanced_opengl/4.face_culling/build.sh b/advanced_opengl/4.face_culling/build.sh new file mode 100755 index 0000000..e60b2dc --- /dev/null +++ b/advanced_opengl/4.face_culling/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='face_culling' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/4.face_culling/face_culling b/advanced_opengl/4.face_culling/face_culling Binary files differnew file mode 100755 index 0000000..62d75b4 --- /dev/null +++ b/advanced_opengl/4.face_culling/face_culling diff --git a/advanced_opengl/4.face_culling/face_culling.c b/advanced_opengl/4.face_culling/face_culling.c new file mode 100644 index 0000000..9769b53 --- /dev/null +++ b/advanced_opengl/4.face_culling/face_culling.c @@ -0,0 +1,306 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1280 +#define HEIGHT 720 + +static V3F camera_pos; +static F32 dt; +static Input 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Face culling", 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); + } + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glFrontFace(GL_CW); + glCullFace(GL_BACK); + + U32 shader = create_shader_program("shaders/face_culling.vs", "shaders/face_culling.fs"); + + U32 grid_texture = load_texture("../../data/textures/grid.png"); + + F32 a = 0.5f; + /* NOTE(pryazha): Counter-clockwise order */ + F32 cube_vertices_ccw[] = { + /* NOTE(pryazha): Back face */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + a, a, -a, 0.0f, 1.0f, /* top-left */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Front face */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 1.0f, 1.0f, /* top-right */ + -a, a, a, 0.0f, 1.0f, /* top-left */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, -a, a, 1.0f, 0.0f, /* bottom-right */ + a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Left face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, -a, a, 1.0f, 0.0f, /* bottom-right */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Right face */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + a, a, a, 0.0f, 1.0f, /* top-left */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Top face */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 1.0f, 0.0f, /* bottom-right */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Bottom face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + -a, -a, a, 0.0f, 1.0f, /* top-left */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + }; + + F32 cube_vertices_cw[] = { + /* NOTE(pryazha): Back face */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + -a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Front face */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 1.0f, 1.0f, /* top-right */ + a, -a, a, 1.0f, 0.0f, /* bottom-right */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + -a, a, a, 0.0f, 1.0f, /* top-left */ + a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Left face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + -a, -a, a, 1.0f, 0.0f, /* bottom-right */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Right face */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 0.0f, 1.0f, /* top-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Top face */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + a, a, a, 1.0f, 0.0f, /* bottom-right */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Bottom face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, -a, a, 0.0f, 1.0f, /* top-left */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + }; + + U32 cube_vao, vbo; + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices_cw), cube_vertices_cw, 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); + + Transform cube_transform = transform_default(); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 1.0f, 3.0f); + + F32 last_time = glfwGetTime(); + + F32 d_angle = 45.0f; + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + V3F left, up, forward; + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + + forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); + left = v3f_norm(v3f_cross(world_up, forward)); + up = v3f_cross(forward, left); + + V3F dp = v3f_zero(); + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + + input_update_last_state(&input); + + camera_pos = v3f_add(camera_pos, dp); + + /* INFO(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + MAT4 model, view, projection; + cube_transform = transform_rotate(cube_transform, v3f(0.0f, d_angle*dt, 0.0f)); + model = transform_apply(cube_transform); + view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + + glUseProgram(shader); + glBindTexture(GL_TEXTURE_2D, grid_texture); + shader_set_mat4fv(shader, "model", model); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "projection", projection); + glBindVertexArray(cube_vao); + glDrawArrays(GL_TRIANGLES, 0, 36); + + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/4.face_culling/shaders/face_culling.fs b/advanced_opengl/4.face_culling/shaders/face_culling.fs new file mode 100644 index 0000000..f58c5d3 --- /dev/null +++ b/advanced_opengl/4.face_culling/shaders/face_culling.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/4.face_culling/shaders/face_culling.vs b/advanced_opengl/4.face_culling/shaders/face_culling.vs new file mode 100644 index 0000000..d3d92d1 --- /dev/null +++ b/advanced_opengl/4.face_culling/shaders/face_culling.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/5.framebuffers/build.sh b/advanced_opengl/5.framebuffers/build.sh new file mode 100755 index 0000000..58a1162 --- /dev/null +++ b/advanced_opengl/5.framebuffers/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='framebuffers' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/5.framebuffers/framebuffers b/advanced_opengl/5.framebuffers/framebuffers Binary files differnew file mode 100755 index 0000000..4b2c4c5 --- /dev/null +++ b/advanced_opengl/5.framebuffers/framebuffers diff --git a/advanced_opengl/5.framebuffers/framebuffers.c b/advanced_opengl/5.framebuffers/framebuffers.c new file mode 100644 index 0000000..a0523cc --- /dev/null +++ b/advanced_opengl/5.framebuffers/framebuffers.c @@ -0,0 +1,424 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define HEIGHT 768 + +static V3F camera_pos; +static F32 dt; +static Input 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Framebuffers", 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); + } + + U32 screen_shader = create_shader_program("shaders/screen.vs", "shaders/screen.fs"); + U32 cube_shader = create_shader_program("shaders/cube.vs", "shaders/cube.fs"); + U32 marble_texture = load_texture("../../data/textures/marble.jpg"); + U32 metal_texture = load_texture("../../data/textures/metal.png"); + U32 window_texture = load_texture("../../data/textures/window.png"); + + U32 fbo; + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + U32 texture_colorbuffer; + glGenTextures(1, &texture_colorbuffer); + glBindTexture(GL_TEXTURE_2D, texture_colorbuffer); + 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); + glBindTexture(GL_TEXTURE_2D, 0); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + texture_colorbuffer, 0); + + U32 rbo; + glGenRenderbuffers(1, &rbo); + glBindRenderbuffer(GL_RENDERBUFFER, rbo); + glRenderbufferStorage(GL_RENDERBUFFER, 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"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4.0f*a)) + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 cube_vao, quad_vao, vbo; + + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + 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); + + Transform window_transforms[] = { + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(0.0f, 0.0f, a+0.1f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(0.0f, 0.0f, 2.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(1.0f, 0.0f, -1.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(-2.0f, 0.0f, 0.5f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(-3.0f, 0.0f, -3.0f)), + transform_make_scale_translate(v3f(0.5f, 0.5f, 0.5f), v3f(3.0f, 0.0f, 0.2)), + }; + + F32 angle = 0.0f; + for (S32 window_index = 0; + window_index < (S32)ArrayCount(window_transforms); + ++window_index) + { + Transform *transform = window_transforms+window_index; + *transform = transform_rotate(*transform, v3f(0.0f, angle, 0.0f)); + angle += 30.0f; + } + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 1.0f, 3.0f); + + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + V3F left, up, forward; + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + + forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); + left = v3f_norm(v3f_cross(world_up, forward)); + up = v3f_cross(forward, left); + + V3F dp = v3f_zero(); + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + + input_update_last_state(&input); + + camera_pos = v3f_add(camera_pos, dp); + + Transform sorted_window_transforms[ArrayCount(window_transforms)]; + MemoryCopyArray(sorted_window_transforms, window_transforms); + for (S32 i = 0; i < (S32)ArrayCount(sorted_window_transforms); ++i) { + for (S32 pos_index = 1; + pos_index < (S32)ArrayCount(sorted_window_transforms); + ++pos_index) + { + F32 distance_left = v3f_length(v3f_sub(sorted_window_transforms[pos_index-1].translate, camera_pos)); + F32 distance_right = v3f_length(v3f_sub(sorted_window_transforms[pos_index].translate, camera_pos)); + if (distance_left < distance_right) { + Transform temp = sorted_window_transforms[pos_index]; + sorted_window_transforms[pos_index] = sorted_window_transforms[pos_index-1]; + sorted_window_transforms[pos_index-1] = temp; + } + } + } + + /* INFO(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); + MAT4 view, projection; + view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "projection", projection); + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(cube_vao); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + shader_set_mat4fv(cube_shader, "model", transform_apply(cube_positions[transform_index])); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + glBindTexture(GL_TEXTURE_2D, metal_texture); + shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position)); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glUseProgram(cube_shader); + glBindTexture(GL_TEXTURE_2D, window_texture); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "projection", projection); + glBindVertexArray(quad_vao); + for (S32 window_index = 0; + window_index < (S32)ArrayCount(sorted_window_transforms); + ++window_index) + { + shader_set_mat4fv(cube_shader, "model", transform_apply(sorted_window_transforms[window_index])); + glDrawArrays(GL_TRIANGLES, 0, 6); + } + glBindVertexArray(0); + glDisable(GL_BLEND); + + 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, texture_colorbuffer); + glBindVertexArray(quad_vao); + shader_set_mat4fv(screen_shader, "model", mat4_identity()); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); + + 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); + V3F target = v3f_add(camera_pos, v3f_negate(forward)); + view = look_at(camera_pos, target, v3f(0.0f, 1.0f, 0.0f)); + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "projection", projection); + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(cube_vao); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + shader_set_mat4fv(cube_shader, "model", transform_apply(cube_positions[transform_index])); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + glBindTexture(GL_TEXTURE_2D, metal_texture); + shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position)); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glUseProgram(cube_shader); + glBindTexture(GL_TEXTURE_2D, window_texture); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "projection", projection); + glBindVertexArray(quad_vao); + for (S32 window_index = 0; + window_index < (S32)ArrayCount(sorted_window_transforms); + ++window_index) + { + shader_set_mat4fv(cube_shader, "model", transform_apply(sorted_window_transforms[window_index])); + glDrawArrays(GL_TRIANGLES, 0, 6); + } + glBindVertexArray(0); + glDisable(GL_BLEND); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glUseProgram(screen_shader); + glDisable(GL_DEPTH_TEST); + glBindTexture(GL_TEXTURE_2D, texture_colorbuffer); + glBindVertexArray(quad_vao); + MAT4 model = transform_apply(transform_make_scale_translate(v3f(0.3f, 0.3f, 0.3f), v3f(-0.7f, 0.7f, 0.0f))); + shader_set_mat4fv(screen_shader, "model", model); + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/5.framebuffers/shaders/cube.fs b/advanced_opengl/5.framebuffers/shaders/cube.fs new file mode 100644 index 0000000..f58c5d3 --- /dev/null +++ b/advanced_opengl/5.framebuffers/shaders/cube.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/5.framebuffers/shaders/cube.vs b/advanced_opengl/5.framebuffers/shaders/cube.vs new file mode 100644 index 0000000..3c11932 --- /dev/null +++ b/advanced_opengl/5.framebuffers/shaders/cube.vs @@ -0,0 +1,17 @@ +#version 330 core + +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/5.framebuffers/shaders/screen.fs b/advanced_opengl/5.framebuffers/shaders/screen.fs new file mode 100644 index 0000000..8f0b2da --- /dev/null +++ b/advanced_opengl/5.framebuffers/shaders/screen.fs @@ -0,0 +1,119 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +vec4 +inversion() +{ + vec4 result = vec4(1.0f-vec3(texture(texture1, tex_coords)), 1.0f); + return(result); +} + +vec4 +grayscale_average() +{ + vec4 tex_color = texture(texture1, tex_coords); + float average = (tex_color.r+tex_color.g+tex_color.b)/3.0f; + vec4 result = vec4(average, average, average, 1.0f); + 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.0722f*tex_color.b; + vec4 result = vec4(average, average, average, 1.0f); + return(result); +} + +const float offset = 1.0f/300.0f; + +vec4 +kernel_effect(float[9] kernel) +{ + vec2 offsets[9] = vec2[]( + vec2(-offset, offset), /* top-left */ + vec2(0.0f, offset), /* top-center */ + vec2(offset, offset), /* top-right */ + vec2(-offset, 0.0f), /* center-left */ + vec2(0.0f, 0.0f), /* center-center */ + vec2(offset, 0.0f), /* center-right */ + vec2(-offset, -offset), /* bottom-left */ + vec2(0.0f, -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.0f); + for (int i = 0; i < 9; ++i) + color += sample_texture[i]*kernel[i]; + + return(vec4(color, 1.0f)); +} + +void +main(void) +{ + float strange_kernel[9] = float[]( + -1, -1, -1, + -1, 9, -1, + -1, -1, -1 + ); + + float gaussian_blur_kernel[9] = float[]( + 1.0f/16.0f, 2.0f/16.0f, 1.0f/16.0f, + 2.0f/16.0f, 4.0f/16.0f, 2.0f/16.0f, + 1.0f/16.0f, 2.0f/16.0f, 1.0f/16.0f + ); + + float box_blur_kernel[9] = float[]( + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, + 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f + ); + + float edge_detection_kernel[9] = float[]( + 1.0f, 1.0f, 1.0f, + 1.0f, -8.0f, 1.0f, + 1.0f, 1.0f, 1.0f + ); + + float sharpening_kernel[9] = float[]( + 0.0f, -1.0f, 0.0f, + -1.0f, 5.0f, -1.0f, + 0.0f, -1.0f, 0.0f + ); + + float sobel_kernel_vertical[9] = float[]( + -1.0f, 0.0f, 1.0f, + -2.0f, 0.0f, 2.0f, + -1.0f, 0.0f, 0.0f + ); + float sobel_kernel_horizontal[9] = float[]( + -1.0f, -2.0f, -1.0f, + 0.0f, 0.0f, 0.0f, + 1.0f, 2.0f, 1.0f + ); + + float funny_kernel[9] = float[]( + 1.0f, 1.0f, 1.0f, + 1.0f, -4.0f, 1.0f, + 1.0f, 1.0f, 1.0f + ); + + frag_color = kernel_effect(funny_kernel); + frag_color *= vec4(0.76f, 0.47f, 0.84f, 1.0f); +} diff --git a/advanced_opengl/5.framebuffers/shaders/screen.vs b/advanced_opengl/5.framebuffers/shaders/screen.vs new file mode 100644 index 0000000..157dde2 --- /dev/null +++ b/advanced_opengl/5.framebuffers/shaders/screen.vs @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +out vec2 tex_coords; + +uniform mat4 model; + +void +main(void) +{ + gl_Position = model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/6.cubemaps/build.sh b/advanced_opengl/6.cubemaps/build.sh new file mode 100755 index 0000000..bf1d66c --- /dev/null +++ b/advanced_opengl/6.cubemaps/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='cubemaps' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/6.cubemaps/cubemaps b/advanced_opengl/6.cubemaps/cubemaps Binary files differnew file mode 100755 index 0000000..661279d --- /dev/null +++ b/advanced_opengl/6.cubemaps/cubemaps diff --git a/advanced_opengl/6.cubemaps/cubemaps.c b/advanced_opengl/6.cubemaps/cubemaps.c new file mode 100644 index 0000000..5ea5300 --- /dev/null +++ b/advanced_opengl/6.cubemaps/cubemaps.c @@ -0,0 +1,451 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define HEIGHT 768 + +static V3F camera_pos; +static F32 dt; +static Input input; +static B32 first_mouse = 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) +{ + if (first_mouse) + { + input.last_mouse_pos = v2f(xpos, ypos); + first_mouse = 0; + } + input.mouse_offset = v2f(input.mouse_offset.x+((F32)xpos-input.last_mouse_pos.x), + input.mouse_offset.y+((F32)ypos-input.last_mouse_pos.y)); + input.last_mouse_pos = v2f((F32)xpos, (F32)ypos); +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Cubemaps", 0, 0); + if (!window) + { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } + + glfwMakeContextCurrent(window); + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glfwSetKeyCallback(window, key_callback); + glfwSetCursorPosCallback(window, cursor_pos_callback); + + if (glewInit() != GLEW_OK) + { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + U32 skybox_shader = create_shader_program("shaders/skybox.vs", "shaders/skybox.fs"); + U32 reflection_shader = create_shader_program("shaders/reflection.vs", "shaders/reflection.fs"); + U32 refraction_shader = create_shader_program("shaders/refraction.vs", "shaders/refraction.fs"); + U32 cube_shader = create_shader_program("shaders/cube.vs", "shaders/cube.fs"); + U32 grid_texture = load_texture("../../data/textures/grid.png"); + + const char *cube_texture_filenames[6] = { + "../../data/textures/skybox/right.jpg", + "../../data/textures/skybox/left.jpg", + "../../data/textures/skybox/top.jpg", + "../../data/textures/skybox/bottom.jpg", + "../../data/textures/skybox/front.jpg", + "../../data/textures/skybox/back.jpg" + }; + U32 skybox_texture = load_cubemap(cube_texture_filenames); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4.0f*a)) + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 cube_vao, vbo; + + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + a = 1.0f; + F32 skybox_cube_vertices[] = { + -a, -a, -a, + a, -a, -a, + a, a, -a, + a, a, -a, + -a, a, -a, + -a, -a, -a, + + -a, -a, a, + a, -a, a, + a, a, a, + a, a, a, + -a, a, a, + -a, -a, a, + + -a, a, a, + -a, a, -a, + -a, -a, -a, + -a, -a, -a, + -a, -a, a, + -a, a, a, + + a, a, a, + a, a, -a, + a, -a, -a, + a, -a, -a, + a, -a, a, + a, a, a, + + -a, -a, -a, + a, -a, -a, + a, -a, a, + a, -a, a, + -a, -a, a, + -a, -a, -a, + + -a, a, -a, + a, a, -a, + a, a, a, + a, a, a, + -a, a, a, + -a, a, -a, + }; + + U32 skybox_cube_vao; + glGenVertexArrays(1, &skybox_cube_vao); + glBindVertexArray(skybox_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(skybox_cube_vertices), skybox_cube_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(F32), (void *)0); + glBindVertexArray(0); + + F32 normals_cube_vertices[] = { + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f + }; + U32 normals_cube_vao; + glGenVertexArrays(1, &normals_cube_vao); + glBindVertexArray(normals_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(normals_cube_vertices), normals_cube_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)(3*sizeof(F32))); + glBindVertexArray(0); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 1.0f, 3.0f); + F32 yaw = 0.0f, pitch = 0.0f; + + F32 last_time = glfwGetTime(); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + + MAT4 view, projection; + view = mat4_make_translate(v3f_negate(camera_pos)); + view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + + V3F left, up, forward; + + left = v3f(-view.m0.x, -view.m1.x, -view.m2.x); + up = v3f(view.m0.y, view.m1.y, view.m2.y); + forward = v3f(-view.m0.z, -view.m1.z, -view.m2.z); + + V3F dp = v3f_zero(); + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + + camera_pos = v3f_add(camera_pos, dp); + + /* NOTE(pryazha): Mouse handling */ + F32 sensitivity = 0.1f; + input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); + yaw += input.mouse_offset.x; + pitch += input.mouse_offset.y; + if (pitch > 89.0f) + pitch = 89.0f; + if (pitch < -89.0f) + pitch = -89.0f; + + view = mat4_make_translate(v3f_negate(camera_pos)); + view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); + + input_update_last_state(&input); + + /* NOTE(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "projection", projection); + glBindTexture(GL_TEXTURE_2D, grid_texture); + + glBindVertexArray(cube_vao); + for (S32 transform_index = 0; + transform_index < (S32)ArrayCount(cube_positions); + ++transform_index) + { + shader_set_mat4fv(cube_shader, "model", transform_apply(cube_positions[transform_index])); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position)); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glUseProgram(reflection_shader); + shader_set_mat4fv(reflection_shader, "model", mat4_make_translate(v3f(0.0f, 2.0f, 0.0f))); + shader_set_mat4fv(reflection_shader, "view", view); + shader_set_mat4fv(reflection_shader, "projection", projection); + shader_set_3fv(reflection_shader, "camera_pos", camera_pos); + glBindVertexArray(normals_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + glBindVertexArray(0); + + glUseProgram(refraction_shader); + shader_set_mat4fv(refraction_shader, "model", mat4_make_translate(v3f(2.0f, 2.0f, 0.0f))); + shader_set_mat4fv(refraction_shader, "view", view); + shader_set_mat4fv(refraction_shader, "projection", projection); + shader_set_3fv(refraction_shader, "camera_pos", camera_pos); + glBindVertexArray(normals_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + glBindVertexArray(0); + + glUseProgram(skybox_shader); + MAT4 rotate_view = mat4_identity(); + rotate_view.m0 = v4f(view.m0.x, view.m0.y, view.m0.z, 0.0f); + rotate_view.m1 = v4f(view.m1.x, view.m1.y, view.m1.z, 0.0f); + rotate_view.m2 = v4f(view.m2.x, view.m2.y, view.m2.z, 0.0f); + shader_set_mat4fv(skybox_shader, "view", rotate_view); + shader_set_mat4fv(skybox_shader, "projection", projection); + glBindVertexArray(skybox_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/6.cubemaps/shaders/cube.fs b/advanced_opengl/6.cubemaps/shaders/cube.fs new file mode 100644 index 0000000..f58c5d3 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/cube.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/6.cubemaps/shaders/cube.vs b/advanced_opengl/6.cubemaps/shaders/cube.vs new file mode 100644 index 0000000..3c11932 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/cube.vs @@ -0,0 +1,17 @@ +#version 330 core + +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/6.cubemaps/shaders/reflection.fs b/advanced_opengl/6.cubemaps/shaders/reflection.fs new file mode 100644 index 0000000..3723ea9 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/reflection.fs @@ -0,0 +1,17 @@ +#version 330 core + +in vec3 pos; +in vec3 normal; + +out vec4 frag_color; + +uniform vec3 camera_pos; +uniform samplerCube skybox; + +void +main(void) +{ + vec3 i = pos-camera_pos; + vec3 r = reflect(i, normalize(normal)); + frag_color = texture(skybox, r); +} diff --git a/advanced_opengl/6.cubemaps/shaders/reflection.vs b/advanced_opengl/6.cubemaps/shaders/reflection.vs new file mode 100644 index 0000000..34ad142 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/reflection.vs @@ -0,0 +1,19 @@ +#version 330 core + +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; + +out vec3 pos; +out vec3 normal; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void +main(void) +{ + pos = vec3(model*vec4(apos, 1.0f)); + normal = mat3(transpose(inverse(model)))*anormal; + gl_Position = projection*view*model*vec4(apos, 1.0f); +} diff --git a/advanced_opengl/6.cubemaps/shaders/refraction.fs b/advanced_opengl/6.cubemaps/shaders/refraction.fs new file mode 100644 index 0000000..8a8a8c5 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/refraction.fs @@ -0,0 +1,18 @@ +#version 330 core + +in vec3 pos; +in vec3 normal; + +out vec4 frag_color; + +uniform vec3 camera_pos; +uniform samplerCube skybox; + +void +main(void) +{ + float ratio = 1.0f/1.52f; + vec3 i = pos-camera_pos; + vec3 r = refract(i, normalize(normal), ratio); + frag_color = texture(skybox, r); +} diff --git a/advanced_opengl/6.cubemaps/shaders/refraction.vs b/advanced_opengl/6.cubemaps/shaders/refraction.vs new file mode 100644 index 0000000..34ad142 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/refraction.vs @@ -0,0 +1,19 @@ +#version 330 core + +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; + +out vec3 pos; +out vec3 normal; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void +main(void) +{ + pos = vec3(model*vec4(apos, 1.0f)); + normal = mat3(transpose(inverse(model)))*anormal; + gl_Position = projection*view*model*vec4(apos, 1.0f); +} diff --git a/advanced_opengl/6.cubemaps/shaders/skybox.fs b/advanced_opengl/6.cubemaps/shaders/skybox.fs new file mode 100644 index 0000000..bf69c8b --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/skybox.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec3 tex_coords; + +out vec4 frag_color; + +uniform samplerCube skybox; + +void +main(void) +{ + frag_color = texture(skybox, tex_coords); +} diff --git a/advanced_opengl/6.cubemaps/shaders/skybox.vs b/advanced_opengl/6.cubemaps/shaders/skybox.vs new file mode 100644 index 0000000..4259dc1 --- /dev/null +++ b/advanced_opengl/6.cubemaps/shaders/skybox.vs @@ -0,0 +1,16 @@ +#version 330 core + +layout(location = 0) in vec3 apos; + +out vec3 tex_coords; + +uniform mat4 view; +uniform mat4 projection; + +void +main(void) +{ + tex_coords = apos; + vec4 pos = projection*view*vec4(apos, 1.0f); + gl_Position = pos.xyww; +} diff --git a/advanced_opengl/7.uniform_buffer/build.sh b/advanced_opengl/7.uniform_buffer/build.sh new file mode 100755 index 0000000..d5620a3 --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='uniform_buffer' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs b/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs new file mode 100644 index 0000000..8aa5add --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0f, 0.0f, 0.0f, 1.0f); +} diff --git a/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs b/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs new file mode 100644 index 0000000..630242d --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +layout(std140) uniform Matrices +{ + mat4 projection; + mat4 view; +}; + +uniform mat4 model; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); +} diff --git a/advanced_opengl/7.uniform_buffer/shaders/cube.fs b/advanced_opengl/7.uniform_buffer/shaders/cube.fs new file mode 100644 index 0000000..f58c5d3 --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/cube.fs @@ -0,0 +1,13 @@ +#version 330 core + +out vec4 frag_color; + +in vec2 tex_coords; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/7.uniform_buffer/shaders/cube.vs b/advanced_opengl/7.uniform_buffer/shaders/cube.vs new file mode 100644 index 0000000..cb0b7bf --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/cube.vs @@ -0,0 +1,21 @@ +#version 330 core + +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +layout(std140) uniform Matrices +{ + uniform mat4 projection; + uniform mat4 view; +}; + +uniform mat4 model; + +out vec2 tex_coords; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/7.uniform_buffer/shaders/skybox.fs b/advanced_opengl/7.uniform_buffer/shaders/skybox.fs new file mode 100644 index 0000000..bf69c8b --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/skybox.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec3 tex_coords; + +out vec4 frag_color; + +uniform samplerCube skybox; + +void +main(void) +{ + frag_color = texture(skybox, tex_coords); +} diff --git a/advanced_opengl/7.uniform_buffer/shaders/skybox.vs b/advanced_opengl/7.uniform_buffer/shaders/skybox.vs new file mode 100644 index 0000000..36a07ed --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/shaders/skybox.vs @@ -0,0 +1,19 @@ +#version 330 core + +layout(location = 0) in vec3 apos; + +out vec3 tex_coords; + +layout(std140) uniform Matrices +{ + uniform mat4 projection; + uniform mat4 view; +}; + +void +main(void) +{ + tex_coords = apos; + vec4 pos = projection*view*vec4(apos, 1.0f); + gl_Position = pos.xyww; +} diff --git a/advanced_opengl/7.uniform_buffer/uniform_buffer b/advanced_opengl/7.uniform_buffer/uniform_buffer Binary files differnew file mode 100755 index 0000000..e2b3e42 --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/uniform_buffer diff --git a/advanced_opengl/7.uniform_buffer/uniform_buffer.c b/advanced_opengl/7.uniform_buffer/uniform_buffer.c new file mode 100644 index 0000000..e67b5c9 --- /dev/null +++ b/advanced_opengl/7.uniform_buffer/uniform_buffer.c @@ -0,0 +1,392 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define HEIGHT 768 + +static V3F camera_pos; +static F32 dt; +static Input input; +static B32 first_mouse = 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: { + input.move_right.state = KeyState_PRESS; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_PRESS; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_PRESS; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_D: { + input.move_right.state = KeyState_RELEASE; + } break; + case GLFW_KEY_W: { + input.move_forward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_A: { + input.move_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_S: { + input.move_backward.state = KeyState_RELEASE; + } break; + case GLFW_KEY_E: { + input.move_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_Q: { + input.move_down.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) +{ + if (first_mouse) + { + input.last_mouse_pos = v2f(xpos, ypos); + first_mouse = 0; + } + input.mouse_offset = v2f(input.mouse_offset.x+((F32)xpos-input.last_mouse_pos.x), + input.mouse_offset.y+((F32)ypos-input.last_mouse_pos.y)); + input.last_mouse_pos = v2f((F32)xpos, (F32)ypos); +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "Advanced data (uniform buffer)", 0, 0); + if (!window) + { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } + + glfwMakeContextCurrent(window); + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glfwSetKeyCallback(window, key_callback); + glfwSetCursorPosCallback(window, cursor_pos_callback); + + if (glewInit() != GLEW_OK) + { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + U32 skybox_shader = create_shader_program("shaders/skybox.vs", "shaders/skybox.fs"); + U32 cube_shader = create_shader_program("shaders/cube.vs", "shaders/cube.fs"); + U32 color_cube_shader = create_shader_program("shaders/color_cube.vs", + "shaders/color_cube.fs"); + + U32 skybox_matrices = glGetUniformBlockIndex(skybox_shader, "Matrices"); + U32 cube_matrices = glGetUniformBlockIndex(cube_shader, "Matrices"); + U32 color_cube_matrices = glGetUniformBlockIndex(color_cube_shader, "Matrices"); + + glUniformBlockBinding(skybox_shader, skybox_matrices, 0); + glUniformBlockBinding(cube_shader, cube_matrices, 0); + glUniformBlockBinding(color_cube_shader, color_cube_matrices, 0); + + U32 matrices_ubo; + glGenBuffers(1, &matrices_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo); + glBufferData(GL_UNIFORM_BUFFER, 2*sizeof(MAT4), 0, GL_STATIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + glBindBufferRange(GL_UNIFORM_BUFFER, 0, matrices_ubo, 0, 2*sizeof(MAT4)); + + U32 grid_texture = load_texture("../../data/textures/grid.png"); + + const char *cube_texture_filenames[6] = { + "../../data/textures/skybox/right.jpg", + "../../data/textures/skybox/left.jpg", + "../../data/textures/skybox/top.jpg", + "../../data/textures/skybox/bottom.jpg", + "../../data/textures/skybox/front.jpg", + "../../data/textures/skybox/back.jpg" + }; + U32 skybox_texture = load_cubemap(cube_texture_filenames); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform platform_position = + transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), + v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); + + U32 cube_vao, vbo; + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), 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); + + a = 1.0f; + F32 skybox_cube_vertices[] = { + -a, -a, -a, + a, -a, -a, + a, a, -a, + a, a, -a, + -a, a, -a, + -a, -a, -a, + + -a, -a, a, + a, -a, a, + a, a, a, + a, a, a, + -a, a, a, + -a, -a, a, + + -a, a, a, + -a, a, -a, + -a, -a, -a, + -a, -a, -a, + -a, -a, a, + -a, a, a, + + a, a, a, + a, a, -a, + a, -a, -a, + a, -a, -a, + a, -a, a, + a, a, a, + + -a, -a, -a, + a, -a, -a, + a, -a, a, + a, -a, a, + -a, -a, a, + -a, -a, -a, + + -a, a, -a, + a, a, -a, + a, a, a, + a, a, a, + -a, a, a, + -a, a, -a, + }; + + U32 skybox_cube_vao; + glGenVertexArrays(1, &skybox_cube_vao); + glBindVertexArray(skybox_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(skybox_cube_vertices), skybox_cube_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(F32), (void *)0); + glBindVertexArray(0); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + camera_pos = v3f(0.0f, 1.0f, 3.0f); + F32 yaw = 0.0f, pitch = 0.0f; + + F32 last_time = glfwGetTime(); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + + MAT4 view, projection; + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(MAT4), (const void *)&projection); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + view = mat4_make_translate(v3f_negate(camera_pos)); + view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* INFO(pryazha): update */ + + V3F left, up, forward; + + left = v3f(-view.m0.x, -view.m1.x, -view.m2.x); + up = v3f(view.m0.y, view.m1.y, view.m2.y); + forward = v3f(-view.m0.z, -view.m1.z, -view.m2.z); + + V3F dp = v3f_zero(); + F32 speed = 2.0f; + if (key_is_pressed(input.move_right)) + dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); + if (key_is_pressed(input.move_forward)) + dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); + if (key_is_pressed(input.move_left)) + dp = v3f_add(dp, v3f_scalef(left, speed*dt)); + if (key_is_pressed(input.move_backward)) + dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); + if (key_is_pressed(input.move_up)) + dp = v3f_add(dp, v3f_scalef(up, speed*dt)); + if (key_is_pressed(input.move_down)) + dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); + input_update_last_state(&input); + camera_pos = v3f_add(camera_pos, dp); + + /* NOTE(pryazha): Mouse handling */ + F32 sensitivity = 0.1f; + input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); + yaw += input.mouse_offset.x; + pitch += input.mouse_offset.y; + if (pitch > 89.0f) + pitch = 89.0f; + if (pitch < -89.0f) + pitch = -89.0f; + input.mouse_offset = v2f_zero(); + + view = mat4_make_translate(v3f_negate(camera_pos)); + view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); + glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, sizeof(MAT4), sizeof(MAT4), (const void *)&view); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + /* NOTE(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "model", mat4_make_translate(v3f(-2.0f, 0.0f, 0.0f))); + glBindTexture(GL_TEXTURE_2D, grid_texture); + glBindVertexArray(cube_vao); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); + + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position)); + glBindTexture(GL_TEXTURE_2D, grid_texture); + glBindVertexArray(cube_vao); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); + + glUseProgram(color_cube_shader); + shader_set_mat4fv(cube_shader, "model", mat4_make_translate(v3f(2.0f, 0.0f, 0.0f))); + glBindVertexArray(cube_vao); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glUseProgram(skybox_shader); + MAT4 rotate_view = mat4_identity(); + rotate_view.m0 = v4f(view.m0.x, view.m0.y, view.m0.z, 0.0f); + rotate_view.m1 = v4f(view.m1.x, view.m1.y, view.m1.z, 0.0f); + rotate_view.m2 = v4f(view.m2.x, view.m2.y, view.m2.z, 0.0f); + glBindBuffer(GL_UNIFORM_BUFFER, matrices_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, sizeof(MAT4), sizeof(MAT4), (const void *)&rotate_view); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + glBindVertexArray(skybox_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + 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; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/8.geometry_shader/build.sh b/advanced_opengl/8.geometry_shader/build.sh new file mode 100755 index 0000000..fc81331 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='geometry_shader' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/8.geometry_shader/geometry_shader b/advanced_opengl/8.geometry_shader/geometry_shader Binary files differnew file mode 100755 index 0000000..f245068 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/geometry_shader diff --git a/advanced_opengl/8.geometry_shader/geometry_shader.c b/advanced_opengl/8.geometry_shader/geometry_shader.c new file mode 100644 index 0000000..678708c --- /dev/null +++ b/advanced_opengl/8.geometry_shader/geometry_shader.c @@ -0,0 +1,339 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define HEIGHT 768 + +static F32 dt; +static Input input; + +void +key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + switch (action) + { + case GLFW_PRESS: { + switch (key) + { + case GLFW_KEY_SPACE: { + input.jump.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_SPACE: { + input.jump.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +draw_cube(U32 cube_vao, U32 texture) +{ + glBindVertexArray(cube_vao); + glBindTexture(GL_TEXTURE_2D, texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); +} + +int +main(void) +{ + GLFWwindow *window; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + glfwWindowHint(GLFW_RESIZABLE, 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, "depth testing", 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); + } + + glEnable(GL_DEPTH_TEST); + + U32 shader = create_shader_program_geom("shaders/base.vs", + "shaders/base.fs", + "shaders/base.gs"); + U32 explosion_shader = create_shader_program_geom("shaders/explosion.vs", + "shaders/explosion.fs", + "shaders/explosion.gs"); + U32 normals_shader = create_shader_program_geom("shaders/normals.vs", + "shaders/normals.fs", + "shaders/normals.gs"); + U32 basic_shader = create_shader_program("shaders/basic.vs", "shaders/basic.fs"); + + U32 grid_texture = load_texture("../../data/textures/grid.png"); + + F32 points[] = { + -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 1.0f, 1.0f, 0.0f + }; + + GLuint points_vao, vbo; + glGenVertexArrays(1, &points_vao); + glBindVertexArray(points_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(2*sizeof(F32))); + glBindVertexArray(0); + + F32 a = 0.5f; + /* NOTE(pryazha): Counter-clockwise order */ + F32 cube_vertices[] = { + /* NOTE(pryazha): Back face */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + a, a, -a, 0.0f, 1.0f, /* top-left */ + a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + -a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Front face */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 1.0f, 1.0f, /* top-right */ + -a, a, a, 0.0f, 1.0f, /* top-left */ + -a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, -a, a, 1.0f, 0.0f, /* bottom-right */ + a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Left face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + -a, -a, a, 1.0f, 0.0f, /* bottom-right */ + -a, a, a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Right face */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + a, a, a, 0.0f, 1.0f, /* top-left */ + a, -a, a, 0.0f, 0.0f, /* bottom-left */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Top face */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + -a, a, -a, 0.0f, 1.0f, /* top-left */ + -a, a, a, 0.0f, 0.0f, /* bottom-left */ + a, a, a, 1.0f, 0.0f, /* bottom-right */ + a, a, -a, 1.0f, 1.0f, /* top-right */ + + /* NOTE(pryazha): Bottom face */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + -a, -a, a, 0.0f, 1.0f, /* top-left */ + -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ + a, -a, -a, 1.0f, 0.0f, /* bottom-right */ + a, -a, a, 1.0f, 1.0f, /* top-right */ + }; + + U32 cube_vao; + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_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); + + F32 vertices[] = { + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f + }; + + U32 normals_cube_vao; + glGenVertexArrays(1, &normals_cube_vao); + glBindVertexArray(normals_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)(3*sizeof(F32))); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)(6*sizeof(F32))); + glBindVertexArray(0); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + + F32 last_time = glfwGetTime(); + F32 time = last_time; + + S32 scene = 0; + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* NOTE(pryazha): Update */ + if (key_first_press(input.jump)) + { + scene++; + scene %= 3; + } + + input_update_last_state(&input); + + /* NOTE(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + if (scene == 0) + { + glUseProgram(shader); + glBindVertexArray(points_vao); + glDrawArrays(GL_POINTS, 0, 4); + glBindVertexArray(0); + } + else if (scene == 1) + { + F32 radius = 2.0f; + F32 x = f32_sin(0.5*time)*radius; + F32 z = f32_cos(0.5*time)*radius; + MAT4 projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + MAT4 view = look_at(v3f(x, 1.0f, z), v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + MAT4 model = mat4_identity(); + glUseProgram(explosion_shader); + shader_set_mat4fv(explosion_shader, "projection", projection); + shader_set_mat4fv(explosion_shader, "view", view); + shader_set_mat4fv(explosion_shader, "model", model); + shader_set_1f(explosion_shader, "time", time); + glBindVertexArray(cube_vao); + glBindTexture(GL_TEXTURE_2D, grid_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + } + else + { + MAT4 projection, view, model; + F32 radius, x, z; + + radius = 2.0f; + x = f32_sin(0.5*time)*radius; + z = f32_cos(0.5*time)*radius; + + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + view = look_at(v3f(x, 1.0f, z), v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + model = mat4_identity(); + + glUseProgram(basic_shader); + shader_set_mat4fv(basic_shader, "projection", projection); + shader_set_mat4fv(basic_shader, "view", view); + shader_set_mat4fv(basic_shader, "model", model); + draw_cube(normals_cube_vao, grid_texture); + + glUseProgram(normals_shader); + shader_set_mat4fv(normals_shader, "projection", projection); + shader_set_mat4fv(normals_shader, "view", view); + shader_set_mat4fv(normals_shader, "model", model); + draw_cube(normals_cube_vao, grid_texture); + } + + 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; + time += dt; + last_time = current_time; + } + + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &points_vao); + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/base.fs b/advanced_opengl/8.geometry_shader/shaders/base.fs new file mode 100644 index 0000000..283d885 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/base.fs @@ -0,0 +1,11 @@ +#version 330 core + +in vec3 fcolor; + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(fcolor, 1.0f); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/base.gs b/advanced_opengl/8.geometry_shader/shaders/base.gs new file mode 100644 index 0000000..6b140c5 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/base.gs @@ -0,0 +1,27 @@ +#version 330 core +layout(points) in; +layout(triangle_strip, max_vertices = 5) out; + +in VS_OUT { + vec3 color; +} gs_in[]; + +out vec3 fcolor; + +void +main(void) +{ + fcolor = gs_in[0].color; + gl_Position = gl_in[0].gl_Position+vec4(-0.2, -0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(0.2, -0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(-0.2, 0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(0.2, 0.2, 0.0, 0.0); + EmitVertex(); + fcolor = vec3(1.0, 1.0, 1.0); + gl_Position = gl_in[0].gl_Position+vec4(0.0, 0.4, 0.0, 0.0); + EmitVertex(); + EndPrimitive(); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/base.vs b/advanced_opengl/8.geometry_shader/shaders/base.vs new file mode 100644 index 0000000..2924e6c --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/base.vs @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec2 apos; +layout(location = 1) in vec3 acolor; + +out VS_OUT { + vec3 color; +} vs_out; + +void +main(void) +{ + vs_out.color = acolor; + gl_Position = vec4(apos, 0.0f, 1.0f); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/basic.fs b/advanced_opengl/8.geometry_shader/shaders/basic.fs new file mode 100644 index 0000000..bdd4b9a --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/basic.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/basic.vs b/advanced_opengl/8.geometry_shader/shaders/basic.vs new file mode 100644 index 0000000..9a630b6 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/basic.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 2) in vec2 atex_coords; + +out vec2 tex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/advanced_opengl/8.geometry_shader/shaders/explosion.fs b/advanced_opengl/8.geometry_shader/shaders/explosion.fs new file mode 100644 index 0000000..70e7151 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/explosion.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec2 ftex_coords; + +out vec4 frag_color; + +uniform sampler2D texture0; + +void +main(void) +{ + frag_color = texture(texture0, ftex_coords); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/explosion.gs b/advanced_opengl/8.geometry_shader/shaders/explosion.gs new file mode 100644 index 0000000..1883ad2 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/explosion.gs @@ -0,0 +1,44 @@ +#version 330 core +layout(triangles) in; +layout(triangle_strip, max_vertices = 3) out; + +in VS_OUT { + vec2 gtex_coords; +} gs_in[]; + +out vec2 ftex_coords; + +uniform float time; + +vec3 +get_normal(void) +{ + vec3 a = vec3(gl_in[0].gl_Position)-vec3(gl_in[1].gl_Position); + vec3 b = vec3(gl_in[2].gl_Position)-vec3(gl_in[1].gl_Position); + return(normalize(cross(a, b))); +} + +vec4 +explode(vec4 position, vec3 normal) +{ + float magnitude = 0.2f; + vec3 direction = normal*magnitude*((sin(time)+1.0f)/2.0f); + vec4 result = position+vec4(direction, 0.0f); + return(result); +} + +void +main(void) +{ + vec3 normal = get_normal(); + gl_Position = explode(gl_in[0].gl_Position, normal); + ftex_coords = gs_in[0].gtex_coords; + EmitVertex(); + gl_Position = explode(gl_in[1].gl_Position, normal); + ftex_coords = gs_in[1].gtex_coords; + EmitVertex(); + gl_Position = explode(gl_in[2].gl_Position, normal); + ftex_coords = gs_in[2].gtex_coords; + EmitVertex(); + EndPrimitive(); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/explosion.vs b/advanced_opengl/8.geometry_shader/shaders/explosion.vs new file mode 100644 index 0000000..5925b8f --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/explosion.vs @@ -0,0 +1,18 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +out VS_OUT { + vec2 gtex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + vs_out.gtex_coords = atex_coords; + gl_Position = projection*view*model*vec4(apos, 1.0f); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/normals.fs b/advanced_opengl/8.geometry_shader/shaders/normals.fs new file mode 100644 index 0000000..fbbafbf --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/normals.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/normals.gs b/advanced_opengl/8.geometry_shader/shaders/normals.gs new file mode 100644 index 0000000..3720a5f --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/normals.gs @@ -0,0 +1,44 @@ +#version 330 core +layout(triangles) in; +layout(line_strip, max_vertices = 6) out; + +in VS_OUT { + vec3 normal; +} gs_in[]; + +uniform mat4 projection; + +const float MAGNITUDE = 0.4; + +void +generate_line(int index) +{ + gl_Position = projection*gl_in[index].gl_Position; + EmitVertex(); + gl_Position = projection*(gl_in[index].gl_Position+ + vec4(gs_in[index].normal, 0.0)*MAGNITUDE); + EmitVertex(); + EndPrimitive(); +} + +void +main(void) +{ + /* + vec4 center = (gl_in[0].gl_Position+ + gl_in[1].gl_Position+ + gl_in[2].gl_Position)/3.0; + vec4 end = center+vec4(((gs_in[0].normal+ + gs_in[1].normal+ + gs_in[2].normal)/3.0), + 0.0)*MAGNITUDE; + gl_Position = projection*center; + EmitVertex(); + gl_Position = projection*end; + EmitVertex(); + EndPrimitive(); + */ + generate_line(0); + generate_line(1); + generate_line(2); +} diff --git a/advanced_opengl/8.geometry_shader/shaders/normals.vs b/advanced_opengl/8.geometry_shader/shaders/normals.vs new file mode 100644 index 0000000..3b728e5 --- /dev/null +++ b/advanced_opengl/8.geometry_shader/shaders/normals.vs @@ -0,0 +1,18 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; + +out VS_OUT { + vec3 normal; +} vs_out; + +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + mat3 normal_matrix = mat3(transpose(inverse(view*model))); + vs_out.normal = normalize(normal_matrix*anormal); + gl_Position = view*model*vec4(apos, 1.0); +} diff --git a/advanced_opengl/9.instancing/build.sh b/advanced_opengl/9.instancing/build.sh new file mode 100755 index 0000000..40b314b --- /dev/null +++ b/advanced_opengl/9.instancing/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='instancing' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_opengl/9.instancing/instancing b/advanced_opengl/9.instancing/instancing Binary files differnew file mode 100755 index 0000000..08b099d --- /dev/null +++ b/advanced_opengl/9.instancing/instancing diff --git a/advanced_opengl/9.instancing/instancing.c b/advanced_opengl/9.instancing/instancing.c new file mode 100644 index 0000000..90647fb --- /dev/null +++ b/advanced_opengl/9.instancing/instancing.c @@ -0,0 +1,402 @@ +#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; + + if (glfwInit() == GLFW_FALSE) + { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } + + /* glfwWindowHint(GLFW_RESIZABLE, 0); */ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + window = glfwCreateWindow(global_width, global_height, "Instancing", 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); + + U32 instancing_uniform_array_shader = + create_shader_program("shaders/instancing_uniform_array.vs", + "shaders/instancing_uniform_array.fs"); + U32 instanced_arrays_shader = + create_shader_program("shaders/instanced_arrays.vs", + "shaders/instanced_arrays.fs"); + + F32 quad_vertices[] = { + -0.05f, 0.05f, 1.0f, 0.0f, 0.0f, + 0.05f, -0.05f, 0.0f, 1.0f, 0.0f, + -0.05f, -0.05f, 0.0f, 0.0f, 1.0f, + + -0.05f, 0.05f, 1.0f, 0.0f, 0.0f, + 0.05f, -0.05f, 0.0f, 1.0f, 0.0f, + 0.05f, 0.05f, 0.0f, 1.0f, 1.0f, + }; + + GLuint quad_vao, vbo; + 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, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(2*sizeof(F32))); + glBindVertexArray(0); + + V2F translations[100]; + S32 index = 0; + F32 offset = 0.1f; + for (S32 y = -10; + y < 10; + y += 2) + { + for (S32 x = -10; + x < 10; + x += 2) + { + V2F translation = v2f((F32)x, (F32)y); + translation = v2f_scalef(translation, 1.0f/10.0f); + translation = v2f_add(translation, v2f(offset, offset)); + translations[index++] = translation; + } + } + + U32 instance_vbo; + glBindVertexArray(quad_vao); + glGenBuffers(1, &instance_vbo); + glBindBuffer(GL_ARRAY_BUFFER, instance_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(translations), translations, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(V2F), (void *)0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexAttribDivisor(2, 1); + glBindVertexArray(0); + + arena = arena_alloc(Megabytes(64)); + + U32 default_shader = create_shader_program("shaders/default.vs", "shaders/default.fs"); + U32 instanced_shader = create_shader_program("shaders/instanced_mat4.vs", + "shaders/instanced_mat4.fs"); + Mesh *planet_mesh = mesh_load_obj(arena, "../../data/models/planet/planet.obj"); + U32 planet_texture = load_texture("../../data/models/planet/mars.png"); + Mesh *rock_mesh = mesh_load_obj(arena, "../../data/models/rock/rock.obj"); + U32 rock_texture = load_texture("../../data/models/rock/rock.png"); + + S32 amount = 80000; + F32 radius = 150.0f; + offset = 25.0f; + MAT4 trns[amount]; + for (S32 i = 0; i < amount; ++i) + { + F32 angle, displacement, x, y, z, scale; + MAT4 model = mat4_identity(); + + angle = rand()%360; + model = mat4_rotate_angles(model, v3f(angle, angle, angle)); + + scale = (rand()%20)/100.0f+0.05f; + model = mat4_scale(model, v3f(scale, scale, scale)); + + angle = ((F32)i/(F32)amount)*360.0f; + displacement = (rand()%(S32)(2.0f*offset))-offset; + x = f32_sin(DEG2RAD*angle)*radius+displacement; + displacement = (rand()%(S32)(2.0f*offset))-offset; + y = 0.4f*displacement; + displacement = (rand()%(S32)(2.0f*offset))-offset; + z = f32_cos(DEG2RAD*angle)*radius; + model = mat4_translate(model, v3f(x, y, z)); + + trns[i] = model; + } + + U32 buffer; + glGenBuffers(1, &buffer); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + glBufferData(GL_ARRAY_BUFFER, amount*sizeof(MAT4), &trns[0], GL_STATIC_DRAW); + glBindVertexArray(rock_mesh->vao); + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4*sizeof(V4F), (void *)0); + glEnableVertexAttribArray(4); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4*sizeof(V4F), (void *)(1*sizeof(V4F))); + glEnableVertexAttribArray(5); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 4*sizeof(V4F), (void *)(2*sizeof(V4F))); + glEnableVertexAttribArray(6); + glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 4*sizeof(V4F), (void *)(3*sizeof(V4F))); + + glVertexAttribDivisor(3, 1); + glVertexAttribDivisor(4, 1); + glVertexAttribDivisor(5, 1); + glVertexAttribDivisor(6, 1); + glBindVertexArray(0); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + + F32 last_time = glfwGetTime(); + F32 time = last_time; + F32 dt; + + S32 scene = 0; + S32 number_scene = 4; + + MAT4 projection, view, model; + + V3F camera_pos = v3f(0.0f, 50.0f, 220.0f); + F32 camera_speed = 10.0f; + F32 fovx = 90.0f; + F32 near = 0.1f; + F32 far = 1000.0f; + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* NOTE(pryazha): Update */ + if (key_is_pressed(global_input.exit)) + { + glfwSetWindowShouldClose(window, GLFW_TRUE); + } + if (key_first_press(global_input.jump) || + key_first_press(global_input.action_right)) + { + scene++; + } + else if (key_first_press(global_input.action_left)) + { + scene--; + if (scene < 0) + scene = number_scene-1; + } + scene %= number_scene; + + /* NOTE(pryazha): Render */ + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + switch (scene) + { + case 0: { + glUseProgram(instancing_uniform_array_shader); + for (S32 translation_index = 0; + translation_index < 100; + ++translation_index) + { + char temp[256]; + snprintf(temp, 256, "offsets[%d]", translation_index); + shader_set_2fv(instancing_uniform_array_shader, + temp, translations[translation_index]); + } + glBindVertexArray(quad_vao); + glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100); + glBindVertexArray(0); + } break; + + case 1: { + glUseProgram(instanced_arrays_shader); + glBindVertexArray(quad_vao); + glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100); + glBindVertexArray(0); + } break; + + case 2: { + 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); + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + view = look_at(camera_pos, v3f_zero(), world_up); + + glUseProgram(default_shader); + shader_set_mat4fv(default_shader, "projection", projection); + shader_set_mat4fv(default_shader, "view", view); + + F32 scale = 5.0f; + model = mat4_make_scale(v3f(scale, scale, scale)); + shader_set_mat4fv(default_shader, "model", model); + glBindTexture(GL_TEXTURE_2D, planet_texture); + mesh_draw(planet_mesh); + + glBindTexture(GL_TEXTURE_2D, rock_texture); + for (S32 i = 0; i < amount; ++i) + { + model = trns[i]; + shader_set_mat4fv(default_shader, "model", model); + mesh_draw(rock_mesh); + } + } break; + + case 3: { + 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); + V3F world_up = v3f(0.0f, 1.0f, 0.0f); + view = look_at(camera_pos, v3f_zero(), world_up); + + glUseProgram(default_shader); + shader_set_mat4fv(default_shader, "projection", projection); + shader_set_mat4fv(default_shader, "view", view); + F32 scale = 5.0f; + model = mat4_make_scale(v3f(scale, scale, scale)); + shader_set_mat4fv(default_shader, "model", model); + glBindTexture(GL_TEXTURE_2D, planet_texture); + mesh_draw(planet_mesh); + glBindTexture(GL_TEXTURE_2D, 0); + + glUseProgram(instanced_shader); + shader_set_mat4fv(default_shader, "projection", projection); + shader_set_mat4fv(default_shader, "view", view); + glBindTexture(GL_TEXTURE_2D, rock_texture); + glBindVertexArray(rock_mesh->vao); + glDrawElementsInstanced(GL_TRIANGLES, rock_mesh->index_count, + GL_UNSIGNED_INT, 0, amount); + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); + } break; + } + + input_update_last_state(&global_input); + + 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; + time += dt; + last_time = current_time; + } + + glfwTerminate(); + return(0); +} diff --git a/advanced_opengl/9.instancing/shaders/default.fs b/advanced_opengl/9.instancing/shaders/default.fs new file mode 100644 index 0000000..c655ac8 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/default.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D texture0; + +void +main(void) +{ + frag_color = texture(texture0, tex_coords); +}
\ No newline at end of file diff --git a/advanced_opengl/9.instancing/shaders/default.vs b/advanced_opengl/9.instancing/shaders/default.vs new file mode 100644 index 0000000..85d3c19 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/default.vs @@ -0,0 +1,18 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out vec3 normal; +out vec2 tex_coords; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + tex_coords = atex_coords; + gl_Position = projection*view*model*vec4(apos, 1.0); +} diff --git a/advanced_opengl/9.instancing/shaders/instanced_arrays.fs b/advanced_opengl/9.instancing/shaders/instanced_arrays.fs new file mode 100644 index 0000000..a32e52e --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instanced_arrays.fs @@ -0,0 +1,11 @@ +#version 330 core + +in vec3 color; + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(color, 1.0); +} diff --git a/advanced_opengl/9.instancing/shaders/instanced_arrays.vs b/advanced_opengl/9.instancing/shaders/instanced_arrays.vs new file mode 100644 index 0000000..e7bbeb7 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instanced_arrays.vs @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec2 apos; +layout(location = 1) in vec3 acolor; +layout(location = 2) in vec2 aoffset; + +out vec3 color; + +void +main(void) +{ + color = acolor; + vec2 pos = apos*(gl_InstanceID/100.0f); + gl_Position = vec4(pos+aoffset, 0.0f, 1.0f); +} diff --git a/advanced_opengl/9.instancing/shaders/instanced_mat4.fs b/advanced_opengl/9.instancing/shaders/instanced_mat4.fs new file mode 100644 index 0000000..ae625b2 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instanced_mat4.fs @@ -0,0 +1,14 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} + diff --git a/advanced_opengl/9.instancing/shaders/instanced_mat4.vs b/advanced_opengl/9.instancing/shaders/instanced_mat4.vs new file mode 100644 index 0000000..5ab3a81 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instanced_mat4.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 2) in vec2 atex_coords; +layout(location = 3) in mat4 instance_matrix; + +out vec2 tex_coords; + +uniform mat4 projection; +uniform mat4 view; + +void +main(void) +{ + tex_coords = atex_coords; + gl_Position = projection*view*instance_matrix*vec4(apos, 1.0f); +} diff --git a/advanced_opengl/9.instancing/shaders/instancing_uniform_array.fs b/advanced_opengl/9.instancing/shaders/instancing_uniform_array.fs new file mode 100644 index 0000000..a32e52e --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instancing_uniform_array.fs @@ -0,0 +1,11 @@ +#version 330 core + +in vec3 color; + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(color, 1.0); +} diff --git a/advanced_opengl/9.instancing/shaders/instancing_uniform_array.vs b/advanced_opengl/9.instancing/shaders/instancing_uniform_array.vs new file mode 100644 index 0000000..a77be31 --- /dev/null +++ b/advanced_opengl/9.instancing/shaders/instancing_uniform_array.vs @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec2 apos; +layout(location = 1) in vec3 acolor; + +out vec3 color; + +uniform vec2 offsets[100]; + +void +main(void) +{ + color = acolor; + gl_Position = vec4(apos+offsets[gl_InstanceID], 0.0, 1.0); +} |