diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-02-06 12:38:09 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-02-06 12:38:09 +0500 |
commit | 926cbd0d49890772f911e6a6bedb7835605ced89 (patch) | |
tree | ea27e2c84e32ab6d8d29af9e61cd432e30a46bc6 | |
parent | bf1c59565096ac9774493846cfb15e259d3b0e66 (diff) |
change
101 files changed, 2517 insertions, 272 deletions
@@ -1,10 +1,17 @@ -blending/blending -depth/depth -face_culling/face_culling -framebuffers/framebuffers -geometry_shader/geometry_shader -instancing/instancing -mesh_loading_test/mesh_loading_test -skybox/skybox -stencil/stencil -uniform_buffer/uniform_buffer +advanced_opengl/1.depth/depth +advanced_opengl/2.stencil/stencil +advanced_opengl/3.blending/blending +advanced_opengl/4.face_culling/face_culling +advanced_opengl/5.framebuffers/framebuffers +advanced_opengl/6.cubemaps/cubemaps +advanced_opengl/7.uniform_buffer/uniform_buffer +advanced_opengl/8.geometry_shader/geometry_shader +advanced_opengl/9.instancing/instancing +advanced_opengl/10.antialiasing/anti_aliasing_msaa +advanced_opengl/10.antialiasing/anti_aliasing_offscreen +advanced_lighting/1.blinn_phong/blinn_phong +advanced_lighting/2.gamma_correction/gamma_correction +libs/glew/lib/libGLEW.so.2.1.0 +advanced_lighting/3.1.shadow_mapping/shadow_mapping +advanced_lighting/3.2.point_shadows/point_shadows +advanced_lighting/4.normal_mapping/normal_mapping diff --git a/advanced_lighting/blinn_phong/blinn_phong.c b/advanced_lighting/1.blinn_phong/blinn_phong.c index 88bd294..6bbd70a 100644 --- a/advanced_lighting/blinn_phong/blinn_phong.c +++ b/advanced_lighting/1.blinn_phong/blinn_phong.c @@ -10,6 +10,8 @@ static S32 global_width = 1024, global_height = 768; static Input global_input; static V3F camera_dp = (V3F){ 0.0f, 0.0f, 0.0f }; +static V3F light_pos = (V3F){ 0.0f, 0.0f, 0.0f }; +static S32 blinn = 0; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) @@ -43,6 +45,9 @@ key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) case GLFW_KEY_RIGHT: { global_input.action_right.state = KeyState_PRESS; } break; + case GLFW_KEY_UP: { + global_input.action_up.state = KeyState_PRESS; + } break; case GLFW_KEY_LEFT: { global_input.action_left.state = KeyState_PRESS; } break; @@ -79,6 +84,9 @@ key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) case GLFW_KEY_RIGHT: { global_input.action_right.state = KeyState_RELEASE; } break; + case GLFW_KEY_UP: { + global_input.action_up.state = KeyState_RELEASE; + } break; case GLFW_KEY_LEFT: { global_input.action_left.state = KeyState_RELEASE; } break; @@ -109,34 +117,59 @@ update(State *state) { V3F camera_dv = get_dv_camera_orbital(&global_input, state->camera.pos, v3f_zero(), state->dt, 2.0f); + if (key_is_pressed(global_input.action_up)) { + camera_dv = v3f_scalef(camera_dv, 3.0f); + } camera_dp = v3f_add(camera_dp, camera_dv); - camera_dp = v3f_scalef(camera_dp, 0.8f); + camera_dp = v3f_scalef(camera_dp, 0.7f); state->camera.pos = v3f_add(state->camera.pos, camera_dp); - + + if (key_first_press(global_input.jump)) + blinn = blinn ? 0 : 1; + input_update_last_state(&global_input); } void -render(State *state, U32 shader, U32 texture) +render(State *state, U32 *shaders, U32 texture, Mesh **meshes) { MAT4 projection, view, model; - + U32 shader; + Mesh *mesh; + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + projection = perspective(state->camera.fovx, (F32)global_width/(F32)global_height, state->camera.near, state->camera.far); view = look_at(state->camera.pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); model = mat4_identity(); - + + U32 mesh_index = 0; + shader = shaders[mesh_index]; + mesh = meshes[mesh_index]; glUseProgram(shader); shader_set_mat4fv(shader, "projection", projection); shader_set_mat4fv(shader, "view", view); shader_set_mat4fv(shader, "model", model); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); - mesh_draw(state->mesh); + shader_set_3fv(shader, "light_pos", light_pos); + shader_set_3fv(shader, "view_pos", state->camera.pos); + shader_set_1i(shader, "blinn", blinn); + mesh_draw(mesh); + mesh_index++; + + shader = shaders[mesh_index]; + mesh = meshes[mesh_index]; + glUseProgram(shader); + shader_set_mat4fv(shader, "projection", projection); + shader_set_mat4fv(shader, "view", view); + model = mat4_make_scale(v3f(0.1f, 0.1f, 0.1f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(mesh); + glBindTexture(GL_TEXTURE_2D, 0); - glUseProgram(0); } int @@ -145,7 +178,10 @@ main(void) GLFWwindow *window; Arena *arena; State state; + Mesh *meshes[2]; + U32 shaders[2]; F64 time, last_time; + U32 texture; glfwSetErrorCallback(error_callback); @@ -166,22 +202,34 @@ main(void) if (glewInit() != GLEW_OK) goto error; - + glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); - - glEnable(GL_CULL_FACE); /* NOTE(pryazha): init */ arena = arena_alloc(Megabytes(64)); - state.camera = (Camera) {v3f(0.0f, 0.0f, 3.0f), 90.0f, 0.1f, 1000.0f }; + state.camera = (Camera){ v3f(0.0f, 0.0f, 3.0f), 90.0f, 0.1f, 1000.0f }; - U32 blinn_phong_shader = create_shader_program("shaders/blinn_phong.vs", - "shaders/blinn_phong.fs"); - U32 mesh_texture_diffuse = load_texture("../../data/textures/oak_veneer_01_diff_4k.jpg"); - state.mesh = mesh_load_obj(arena, "../../data/models/cube.obj"); + Vertex vertices[] = { + // positions // normals // texcoords + vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)), + vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)), + vertex(v3f( 10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 10.0f)) + }; + U32 indices[] = { 0, 1, 2, 3, 4, 5 }; + meshes[0] = mesh_init(arena, vertices, 6, indices, 6); + meshes[1] = mesh_load_obj(arena, "../../data/models/cube.obj"); + + shaders[0] = create_shader_program("shaders/blinn_phong.vs", "shaders/blinn_phong.fs"); + shaders[1] = create_shader_program("shaders/light.vs", "shaders/light.fs"); + + texture = load_texture("../../data/textures/wood.png"); + last_time = glfwGetTime(); while (!glfwWindowShouldClose(window)) @@ -190,9 +238,9 @@ main(void) if (key_first_press(global_input.exit)) glfwSetWindowShouldClose(window, GLFW_TRUE); update(&state); - render(&state, blinn_phong_shader, mesh_texture_diffuse); + render(&state, shaders, texture, meshes); glfwSwapBuffers(window); - + time = glfwGetTime(); state.dt = time-last_time; last_time = time; diff --git a/advanced_lighting/1.blinn_phong/build.sh b/advanced_lighting/1.blinn_phong/build.sh new file mode 100755 index 0000000..41fb831 --- /dev/null +++ b/advanced_lighting/1.blinn_phong/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='blinn_phong' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_lighting/1.blinn_phong/shaders/blinn_phong.fs b/advanced_lighting/1.blinn_phong/shaders/blinn_phong.fs new file mode 100644 index 0000000..5c66b3e --- /dev/null +++ b/advanced_lighting/1.blinn_phong/shaders/blinn_phong.fs @@ -0,0 +1,44 @@ +#version 330 core + +in VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform sampler2D texture0; +uniform vec3 light_pos; +uniform vec3 view_pos; +uniform bool blinn; + +void +main(void) +{ + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 normal = normalize(fs_in.normal); + + vec3 texture_color = texture(texture0, fs_in.tex_coords).rgb; + + float diff = max(dot(normal, light_dir), 0.0); + + float spec = 0.0; + if (blinn) + { + vec3 halfway_dir = normalize(light_dir+view_dir); + spec = pow(max(dot(normal, halfway_dir), 0.0), 16.0); + } + else + { + vec3 reflect_dir = reflect(-light_dir, normal); + spec = pow(max(dot(view_dir, reflect_dir), 0.0), 8.0); + } + + vec3 ambient = 0.05*texture_color; + vec3 diffuse = diff*texture_color; + vec3 specular = vec3(0.3)*spec; + + frag_color = vec4(ambient+diffuse+specular, 1.0); +} diff --git a/advanced_lighting/1.blinn_phong/shaders/blinn_phong.vs b/advanced_lighting/1.blinn_phong/shaders/blinn_phong.vs new file mode 100644 index 0000000..4d1b792 --- /dev/null +++ b/advanced_lighting/1.blinn_phong/shaders/blinn_phong.vs @@ -0,0 +1,23 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + vs_out.tex_coords = atex_coords; + gl_Position = projection*view*model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/1.blinn_phong/shaders/light.fs b/advanced_lighting/1.blinn_phong/shaders/light.fs new file mode 100644 index 0000000..bd00d82 --- /dev/null +++ b/advanced_lighting/1.blinn_phong/shaders/light.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0); +} diff --git a/advanced_lighting/1.blinn_phong/shaders/light.vs b/advanced_lighting/1.blinn_phong/shaders/light.vs new file mode 100644 index 0000000..ade669b --- /dev/null +++ b/advanced_lighting/1.blinn_phong/shaders/light.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_lighting/2.gamma_correction/build.sh b/advanced_lighting/2.gamma_correction/build.sh new file mode 100755 index 0000000..2b65776 --- /dev/null +++ b/advanced_lighting/2.gamma_correction/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='gamma_correction' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_lighting/2.gamma_correction/gamma_correction.c b/advanced_lighting/2.gamma_correction/gamma_correction.c new file mode 100644 index 0000000..68edae7 --- /dev/null +++ b/advanced_lighting/2.gamma_correction/gamma_correction.c @@ -0,0 +1,297 @@ +#include "pwyazh.h" + +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +static S32 global_width = 1024, global_height = 768; +static Input global_input; +static V3F camera_dp = (V3F){ 0.0f, 0.0f, 0.0f }; +static V3F light_pos = (V3F){ 0.0f, 0.0f, 0.0f }; +static U32 gamma_correction = 0; + +static V3F light_positions[] = { + (V3F){-3.0f, 0.0f, 0.0f}, + (V3F){-1.0f, 0.0f, 0.0f}, + (V3F){1.0f, 0.0f, 0.0f}, + (V3F){3.0f, 0.0f, 0.0} +}; +static V3F light_colors[] = { + (V3F){0.25f, 0.25f, 0.25f}, + (V3F){0.50f, 0.50f, 0.50f}, + (V3F){0.75f, 0.75f, 0.75f}, + (V3F){1.0f, 1.0f, 1.0} +}; + +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_UP: { + global_input.action_up.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_UP: { + global_input.action_up.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 +resize_callback(GLFWwindow* window, int width, int height) +{ + global_width = width; + global_height = height; + glViewport(0, 0, global_width, global_height); +} + +void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "[ERROR] GLFW: %s\n", desc); +} + +void +update(State *state) +{ + V3F camera_dv = get_dv_camera_orbital(&global_input, state->camera.pos, + v3f_zero(), state->dt, 2.0f); + if (key_is_pressed(global_input.action_up)) + camera_dv = v3f_scalef(camera_dv, 3.0f); + camera_dp = v3f_add(camera_dp, camera_dv); + camera_dp = v3f_scalef(camera_dp, 0.7f); + state->camera.pos = v3f_add(state->camera.pos, camera_dp); + + if (key_first_press(global_input.action_right)) + gamma_correction = !gamma_correction; + + F32 time = glfwGetTime(); + for (S32 position_index = 0; + position_index < (S32)ArrayCount(light_positions); + position_index++) + { + F32 radius = position_index*2.0+2.0; + F32 angle = time*position_index*pi_F32/6.0; + F32 x = f32_sin(angle)*radius; + F32 z = f32_cos(angle)*radius; + light_positions[position_index] = v3f(x, light_positions[position_index].y, z); + } + + input_update_last_state(&global_input); +} + +void +render(State *state, U32 *shaders, U32 texture, U32 texture_gamma_corrected, Mesh **meshes) +{ + MAT4 projection, view, model; + U32 shader; + Mesh *mesh; + + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + projection = perspective(state->camera.fovx, (F32)global_width/(F32)global_height, + state->camera.near, state->camera.far); + view = look_at(state->camera.pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + model = mat4_identity(); + + U32 mesh_index = 0; + shader = shaders[mesh_index]; + mesh = meshes[mesh_index]; + glUseProgram(shader); + shader_set_mat4fv(shader, "projection", projection); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "model", model); + if (gamma_correction) + glBindTexture(GL_TEXTURE_2D, texture_gamma_corrected); + else + glBindTexture(GL_TEXTURE_2D, texture); + shader_set_3fv(shader, "light_pos", light_pos); + shader_set_3fv(shader, "view_pos", state->camera.pos); + shader_set_1i(shader, "gamma_correction", gamma_correction); + U32 light_positions_location = glGetUniformLocation(shader, "light_positions"); + glUniform3fv(light_positions_location, 4, (F32 *)&light_positions[0]); + U32 light_colors_location = glGetUniformLocation(shader, "light_colors"); + glUniform3fv(light_colors_location, 4, (F32 *)&light_colors[0]); + mesh_draw(mesh); + mesh_index++; + + shader = shaders[mesh_index]; + mesh = meshes[mesh_index]; + glUseProgram(shader); + shader_set_mat4fv(shader, "projection", projection); + shader_set_mat4fv(shader, "view", view); + for (S32 light_index = 0; + light_index < (S32)ArrayCount(light_positions); + light_index++) + { + model = mat4_make_scale(v3f(0.1f, 0.1f, 0.1f)); + model = mat4_translate(model, light_positions[light_index]); + shader_set_mat4fv(shader, "model", model); + mesh_draw(mesh); + } + glBindTexture(GL_TEXTURE_2D, 0); +} + +int +main(void) +{ + GLFWwindow *window; + Arena *arena; + State state; + Mesh *meshes[2]; + U32 shaders[2]; + F64 time, last_time; + U32 texture, texture_gamma_corrected; + + glfwSetErrorCallback(error_callback); + + if (glfwInit() == GLFW_FALSE) + return(1); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_SAMPLES, 4); + window = glfwCreateWindow(global_width, global_height, "Gamma correction", 0, 0); + if (!window) + goto error; + + glfwSetKeyCallback(window, key_callback); + glfwSetWindowSizeCallback(window, resize_callback); + + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) + goto error; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + /* NOTE(pryazha): A less controlled method of applying gamma correction */ + /* glEnable(GL_FRAMEBUFFER_SRGB); */ + + /* NOTE(pryazha): init */ + arena = arena_alloc(Megabytes(64)); + + state.camera = (Camera){ v3f(0.0f, 0.0f, 3.0f), 90.0f, 0.1f, 1000.0f }; + + Vertex vertices[] = { + // positions // normals // texcoords + vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)), + + vertex(v3f( 10.0f, -0.5f, 10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 0.0f)), + vertex(v3f(-10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f( 0.0f, 10.0f)), + vertex(v3f( 10.0f, -0.5f, -10.0f), v3f(0.0f, 1.0f, 0.0f), v2f(10.0f, 10.0f)) + }; + U32 indices[] = { 0, 1, 2, 3, 4, 5 }; + meshes[0] = mesh_init(arena, vertices, 6, indices, 6); + meshes[1] = mesh_load_obj(arena, "../../data/models/cube.obj"); + + shaders[0] = create_shader_program("shaders/blinn_phong.vs", "shaders/blinn_phong.fs"); + shaders[1] = create_shader_program("shaders/light.vs", "shaders/light.fs"); + + texture = load_texture_gamma("../../data/textures/wood.png", 0); + texture_gamma_corrected = load_texture_gamma("../../data/textures/wood.png", 1); + + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + if (key_first_press(global_input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + update(&state); + render(&state, shaders, texture, texture_gamma_corrected, meshes); + glfwSwapBuffers(window); + + time = glfwGetTime(); + state.dt = time-last_time; + last_time = time; +#if 0 + fprintf(stdout, "[INFO]: dt: %f\n", state.dt); +#endif + } + + glfwTerminate(); + return(0); + + error: + glfwTerminate(); + return(1); +} diff --git a/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs new file mode 100644 index 0000000..35e701c --- /dev/null +++ b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.fs @@ -0,0 +1,58 @@ +#version 330 core + +in VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform sampler2D texture0; +uniform vec3 light_positions[4]; +uniform vec3 light_colors[4]; +uniform vec3 view_pos; +uniform bool gamma_correction; + +vec3 +blinn_phong(vec3 light_dir, vec3 view_dir, vec3 normal, + vec3 light_pos, vec3 light_color) +{ + float diff = max(dot(normal, light_dir), 0.0); + vec3 diffuse = diff*light_color; + + vec3 halfway_dir = normalize(light_dir+view_dir); + float spec = pow(max(dot(normal, halfway_dir), 0.0), 16.0); + vec3 specular = spec*light_color; + + float distance = length(light_pos-fs_in.frag_pos); + float attenuation = 1.0/(gamma_correction ? distance*distance : distance); + + return((diffuse+specular)*attenuation); +} + +void +main(void) +{ + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 normal = normalize(fs_in.normal); + vec3 texture_color = texture(texture0, fs_in.tex_coords).rgb; + + vec3 lighting = vec3(0.0); + for (int i = 0; i < 4; i++) + { + vec3 light_pos = light_positions[i]; + vec3 light_color = light_colors[i]; + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + lighting += blinn_phong(light_dir, view_dir, normal, + light_pos, light_color); + } + + texture_color *= lighting; + + float gamma = 2.2; + if (gamma_correction) + texture_color = pow(texture_color, vec3(1.0/gamma)); + + frag_color = vec4(texture_color, 1.0); +} diff --git a/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs new file mode 100644 index 0000000..4d1b792 --- /dev/null +++ b/advanced_lighting/2.gamma_correction/shaders/blinn_phong.vs @@ -0,0 +1,23 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + vs_out.tex_coords = atex_coords; + gl_Position = projection*view*model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/2.gamma_correction/shaders/light.fs b/advanced_lighting/2.gamma_correction/shaders/light.fs new file mode 100644 index 0000000..bd00d82 --- /dev/null +++ b/advanced_lighting/2.gamma_correction/shaders/light.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0); +} diff --git a/advanced_lighting/2.gamma_correction/shaders/light.vs b/advanced_lighting/2.gamma_correction/shaders/light.vs new file mode 100644 index 0000000..ade669b --- /dev/null +++ b/advanced_lighting/2.gamma_correction/shaders/light.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_lighting/3.1.shadow_mapping/build.sh b/advanced_lighting/3.1.shadow_mapping/build.sh new file mode 100755 index 0000000..bba445d --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='shadow_mapping' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.fs b/advanced_lighting/3.1.shadow_mapping/shaders/color.fs new file mode 100644 index 0000000..bd00d82 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/color.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.vs b/advanced_lighting/3.1.shadow_mapping/shaders/color.vs new file mode 100644 index 0000000..ade669b --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/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_lighting/3.1.shadow_mapping/shaders/debug_quad.fs b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs new file mode 100644 index 0000000..d250894 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs @@ -0,0 +1,27 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D depth_map; +uniform float near; +uniform float far; + +float +linearize_depth(float depth) +{ + float z = (depth+1.0)*0.5; + return((2.0*near*far)/(far+near-z*(far-near))); +} + +void +main(void) +{ + float depth = texture(depth_map, tex_coords).r; + + /* frag_color = vec4(vec3(depth), 1.0); */ + + /* NOTE(pryazha): For visual debugging of perspective projection light */ + frag_color = vec4(vec3(linearize_depth(depth)/far), 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs new file mode 100644 index 0000000..5717608 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.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) +{ + tex_coords = atex_coords; + gl_Position = model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs new file mode 100644 index 0000000..1c7018a --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs @@ -0,0 +1,72 @@ +#version 330 core + +in VS_OUT { + vec4 frag_pos_light_space; + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform vec3 light_pos; +uniform vec3 view_pos; + +uniform sampler2D diffuse_texture; +uniform sampler2D shadow_map; + +float +calculate_shadow() +{ + vec3 proj_coords = fs_in.frag_pos_light_space.xyz/fs_in.frag_pos_light_space.w; + proj_coords = proj_coords*0.5+0.5; + float closest_depth = texture(shadow_map, proj_coords.xy).r; + float current_depth = proj_coords.z; + vec3 normal = normalize(fs_in.normal); + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float bias = max(0.04*(1.0-dot(normal, light_dir)), 0.004); + float shadow = 0; + vec2 texel_size = 1.0/textureSize(shadow_map, 0); + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + float pcf_depth = texture(shadow_map, proj_coords.xy+vec2(x, y)*texel_size).r; + shadow += (current_depth-bias > pcf_depth) ? 1.0 : 0.0; + } + } + shadow /= 9.0; + + if (proj_coords.z > 1.0) + shadow = 0.0; + + return(shadow); +} + +void +main(void) +{ + vec3 color = texture(diffuse_texture, fs_in.tex_coords).rgb; + vec3 normal = normalize(fs_in.normal); + vec3 light_color = vec3(0.3); + + float ambient_strength = 0.01; + vec3 ambient = color*ambient_strength; + + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float diff = max(dot(light_dir, normal), 0.0); + vec3 diffuse = diff*color; + + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 halfway_dir = normalize(light_dir+view_dir); + float spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); + vec3 specular = spec*color; + + float shadow = calculate_shadow(); + vec3 lighting = ambient+((diffuse+specular)*(1.0-shadow)*light_color); + + float gamma = 2.2; + lighting = pow(lighting, vec3(1.0/gamma)); + + frag_color = vec4(lighting, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs new file mode 100644 index 0000000..3ecd5b1 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs @@ -0,0 +1,26 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec4 frag_pos_light_space; + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; +uniform mat4 light_space_matrix; + +void +main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + vs_out.frag_pos_light_space = light_space_matrix*vec4(vs_out.frag_pos, 1.0); + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + vs_out.tex_coords = atex_coords; + gl_Position = projection*view*vec4(vs_out.frag_pos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs new file mode 100644 index 0000000..16b9908 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs @@ -0,0 +1,6 @@ +#version 330 core + +void +main(void) +{ +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs new file mode 100644 index 0000000..b9752ed --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs @@ -0,0 +1,11 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 light_space_matrix; +uniform mat4 model; + +void +main(void) +{ + gl_Position = light_space_matrix*model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shadow_mapping.c b/advanced_lighting/3.1.shadow_mapping/shadow_mapping.c new file mode 100644 index 0000000..7f03415 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shadow_mapping.c @@ -0,0 +1,417 @@ +#include "pwyazh.h" + +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +static S32 global_screen_width = 800, global_screen_height = 600; +static U32 global_shadow_width = 1024, global_shadow_height = 1024; + +static Input global_input; + +static V3F camera_dp = (V3F){ 0.0f, 0.0f, 0.0f }; + +static U32 global_depth_map_fbo, global_depth_map; + +static U32 global_debug_quad_shader, global_simple_depth_shader, + global_shadow_shader, global_color_shader; + +static U32 global_wood_texture; + +static U32 global_quad_vao; + +static U32 shadows_ortho = 1; + +static V3F light_pos; + +void +glfw_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_UP: { + global_input.action_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_DOWN: { + global_input.action_down.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_UP: { + global_input.action_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_DOWN: { + global_input.action_down.state = KeyState_RELEASE; + } break; + case GLFW_KEY_ESCAPE: { + global_input.exit.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +resize_callback(GLFWwindow* window, int width, int height) +{ + global_screen_width = width; + global_screen_height = height; + glViewport(0, 0, global_screen_width, global_screen_height); +} + +void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "[ERROR] GLFW: %s\n", desc); +} + +void +update(State *state, F32 time) +{ + F32 radius; + + V3F camera_dv = get_dv_camera_orbital(&global_input, state->camera.pos, + v3f_zero(), state->dt, 2.0f); + if (key_is_pressed(global_input.action_up)) + camera_dv = v3f_scalef(camera_dv, 3.0f); + camera_dp = v3f_add(camera_dp, camera_dv); + camera_dp = v3f_scalef(camera_dp, 0.8f); + state->camera.pos = v3f_add(state->camera.pos, camera_dp); + + if (key_first_press(global_input.action_down)) + shadows_ortho = !shadows_ortho; + + input_update_last_state(&global_input); + + F32 x, z, angular_speed; + + angular_speed = 1.0f; + radius = 4.0f; + x = f32_sin(time*angular_speed)*radius; + z = f32_cos(time*angular_speed)*radius; + + light_pos = v3f(x, 4.0f, z); +} + +void +render_scene(U32 shader, Mesh *cube, Mesh *plane) +{ + MAT4 model; + + model = mat4_identity(); + shader_set_mat4fv(shader, "model", model); + mesh_draw(plane); + + /* + model = mat4_identity(); + model = mat4_make_scale(v3f(10.0f, 0.1f, 10.0f)); + model = mat4_translate(model, v3f(0.0f, -0.6f, 0.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + */ + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(0.0f, 1.5f, 0.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(2.0f, 0.0f, 1.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.25f, 0.25f, 0.25f)); + model = mat4_rotate_angles(model, v3f(60.0f, 0.0f, 0.0f)); + model = mat4_translate(model, v3f(-1.0f, 0.0f, 2.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); +} + +void +render(State *state, Mesh *cube, Mesh *plane) +{ + /* MAT4 projection, view, model; */ + MAT4 light_projection, light_view, model; + F32 near, far, fovx; + V3F target, up; + + if (shadows_ortho) + { + near = 1.0f; + far = 10.0f; + F32 half_side = 10.0f; + light_projection = ortho(-half_side, half_side, -half_side, half_side, near, far); + } + else + { + fovx = 90.0f; + near = 1.0f; + far = 50.0f; + light_projection = perspective(fovx, (F32)global_shadow_width/(F32)global_shadow_height, + near, far); + } + + target = v3f_zero(); + up = v3f(0.0f, 1.0f, 0.0f); + light_view = look_at(light_pos, target, up); + MAT4 light_space_matrix = mat4_mul(light_projection, light_view); + + glUseProgram(global_simple_depth_shader); + shader_set_mat4fv(global_simple_depth_shader, "light_space_matrix", light_space_matrix); + glViewport(0, 0, global_shadow_width, global_shadow_height); + glBindFramebuffer(GL_FRAMEBUFFER, global_depth_map_fbo); + glClear(GL_DEPTH_BUFFER_BIT); + // glCullFace(GL_FRONT); + render_scene(global_simple_depth_shader, cube, plane); + // glCullFace(GL_BACK); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + glViewport(0, 0, global_screen_width, global_screen_height); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + MAT4 projection = perspective(state->camera.fovx, (F32)global_screen_width/(F32)global_screen_height, + state->camera.near, state->camera.far); + MAT4 view = look_at(state->camera.pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + glUseProgram(global_shadow_shader); + shader_set_mat4fv(global_shadow_shader, "projection", projection); + shader_set_mat4fv(global_shadow_shader, "view", view); + + shader_set_3fv(global_shadow_shader, "light_pos", light_pos); + shader_set_3fv(global_shadow_shader, "view_pos", state->camera.pos); + shader_set_mat4fv(global_shadow_shader, "light_space_matrix", light_space_matrix); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, global_wood_texture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, global_depth_map); + render_scene(global_shadow_shader, cube, plane); + + glUseProgram(global_color_shader); + shader_set_mat4fv(global_shadow_shader, "projection", projection); + shader_set_mat4fv(global_shadow_shader, "view", view); + model = mat4_make_scale(v3f(0.1f, 0.1f, 0.1f)); + model = mat4_translate(model, light_pos); + shader_set_mat4fv(global_shadow_shader, "model", model); + mesh_draw(cube); + + glDisable(GL_DEPTH_TEST); + glUseProgram(global_debug_quad_shader); + F32 scale = 0.3f; + Transform transform = transform_make_scale_translate(v3f(scale, scale, scale), + v3f(1.0f-scale, 1.0f-scale, 0.0f)); + model = transform_apply(transform); + shader_set_mat4fv(global_debug_quad_shader, "model", model); + shader_set_1f(global_debug_quad_shader, "near", near); + shader_set_1f(global_debug_quad_shader, "far", far); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, global_depth_map); + glBindVertexArray(global_quad_vao); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); + glEnable(GL_DEPTH_TEST); +} + +int +main(void) +{ + GLFWwindow *window; + Arena *arena; + State state; + Mesh *cube_mesh, *plane_mesh; + F64 time, last_time; + + glfwSetErrorCallback(error_callback); + + if (glfwInit() == GLFW_FALSE) + return(1); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_SAMPLES, 4); + window = glfwCreateWindow(global_screen_width, global_screen_height, "Shadow mapping", 0, 0); + if (!window) + goto error; + + glfwSetKeyCallback(window, glfw_key_callback); + glfwSetWindowSizeCallback(window, resize_callback); + + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) + goto error; + + glEnable(GL_DEPTH_TEST); + // glEnable(GL_CULL_FACE); + + /* NOTE(pryazha): Init */ + arena = arena_alloc(Megabytes(64)); + + state.camera = (Camera){ v3f(0.0f, 1.0f, 3.0f), 90.0f, 0.1f, 100.0f }; + light_pos = v3f(-2.0f, 4.0f, -2.0f); + + /* NOTE(pryazha): Meshes */ + Vertex plane_vertices[] = { + // positions // normals // texcoords + vertex(v3f(-25.0f, -0.5f, 25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(0.0f, 0.0f)), + vertex(v3f(25.0f, -0.5f, 25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(25.0f, 0.0f)), + vertex(v3f(-25.0f, -0.5f, -25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(0.0f, 25.0f)), + + vertex(v3f(25.0f, -0.5f, 25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(25.0f, 0.0f)), + vertex(v3f(25.0f, -0.5f, -25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(25.0f, 25.0f)), + vertex(v3f(-25.0f, -0.5f, -25.0f), v3f(0.0f, 1.0f, 0.0f), v2f(0.0f, 25.0f)), + }; + U32 indices[] = { 0, 1, 2, 3, 4, 5 }; + plane_mesh = mesh_init(arena, plane_vertices, ArrayCount(plane_vertices), indices, ArrayCount(indices)); + cube_mesh = mesh_load_obj(arena, "../../data/models/cube.obj"); + + 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 + }; + + U32 vbo; + glGenVertexArrays(1, &global_quad_vao); + glBindVertexArray(global_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); + + /* NOTE(pryazha): Shaders */ + global_debug_quad_shader = create_shader_program("shaders/debug_quad.vs", + "shaders/debug_quad.fs"); + global_simple_depth_shader = create_shader_program("shaders/simple_depth.vs", + "shaders/simple_depth.fs"); + global_shadow_shader = create_shader_program("shaders/shadow.vs", "shaders/shadow.fs"); + global_color_shader = create_shader_program("shaders/color.vs", "shaders/color.fs"); + + glUseProgram(global_shadow_shader); + shader_set_1i(global_shadow_shader, "diffuse_texture", 0); + shader_set_1i(global_shadow_shader, "shadow_map", 1); + glUseProgram(0); + + global_wood_texture = load_texture_gamma("../../data/textures/wood.png", 1); + + glGenFramebuffers(1, &global_depth_map_fbo); + glGenTextures(1, &global_depth_map); + glBindTexture(GL_TEXTURE_2D, global_depth_map); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, global_shadow_width, + global_shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glBindFramebuffer(GL_FRAMEBUFFER, global_depth_map_fbo); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, + global_depth_map, 0); + V4F border_color = v4f(1.0f, 1.0f, 1.0f, 1.0f); + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, (const F32 *)&border_color); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + fprintf(stderr, "[ERROR]: Failed to complete depth map framebuffer.\n"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + if (key_first_press(global_input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + update(&state, time); + render(&state, cube_mesh, plane_mesh); + glfwSwapBuffers(window); + + time = glfwGetTime(); + state.dt = time-last_time; + last_time = time; +#if 0 + fprintf(stdout, "[INFO]: dt: %f\n", state.dt); +#endif + } + + glfwTerminate(); + return(0); + + error: + glfwTerminate(); + return(1); +} diff --git a/advanced_lighting/3.2.point_shadows/build.sh b/advanced_lighting/3.2.point_shadows/build.sh new file mode 100755 index 0000000..bf934b6 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../../config +TARGET='point_shadows' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_lighting/3.2.point_shadows/point_shadows b/advanced_lighting/3.2.point_shadows/point_shadows Binary files differnew file mode 100755 index 0000000..c4668ab --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/point_shadows diff --git a/advanced_lighting/3.2.point_shadows/point_shadows.c b/advanced_lighting/3.2.point_shadows/point_shadows.c new file mode 100644 index 0000000..0f60565 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/point_shadows.c @@ -0,0 +1,383 @@ +#include "pwyazh.h" + +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +static S32 global_screen_width = 800, global_screen_height = 600; +static Input global_input; +static B32 first_mouse = 1; + +void +glfw_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_UP: { + global_input.action_up.state = KeyState_PRESS; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_PRESS; + } break; + case GLFW_KEY_DOWN: { + global_input.action_down.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_UP: { + global_input.action_up.state = KeyState_RELEASE; + } break; + case GLFW_KEY_LEFT: { + global_input.action_left.state = KeyState_RELEASE; + } break; + case GLFW_KEY_DOWN: { + global_input.action_down.state = KeyState_RELEASE; + } break; + case GLFW_KEY_ESCAPE: { + global_input.exit.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +void +resize_callback(GLFWwindow* window, int width, int height) +{ + global_screen_width = width; + global_screen_height = height; + /* glViewport(0, 0, global_screen_width, global_screen_height); */ +} + +void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "[ERROR] GLFW: %s\n", desc); +} + +void +cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) +{ + if (first_mouse) + { + global_input.last_mouse_pos = v2f(xpos, ypos); + first_mouse = 0; + } + global_input.mouse_offset = v2f(global_input.mouse_offset.x+((F32)xpos-global_input.last_mouse_pos.x), + global_input.mouse_offset.y+((F32)ypos-global_input.last_mouse_pos.y)); + global_input.last_mouse_pos = v2f((F32)xpos, (F32)ypos); +} + + +void +render_scene(U32 shader, Mesh *cube) +{ + MAT4 model; + + model = mat4_identity(); + model = mat4_make_scale(v3f(5.0f, 5.0f, 5.0f)); + shader_set_mat4fv(shader, "model", model); + glDisable(GL_CULL_FACE); + shader_set_1i(shader, "reverse_normals", 1); + mesh_draw(cube); + shader_set_1i(shader, "reverse_normals", 0); + glEnable(GL_CULL_FACE); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(0.0f, 1.5f, 0.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(2.0f, 0.0f, 1.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.25f, 0.25f, 0.25f)); + model = mat4_rotate_angles(model, v3f(60.0f, 0.0f, 0.0f)); + model = mat4_translate(model, v3f(-1.0f, 0.0f, 2.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); +} + +int +main(void) +{ + GLFWwindow *window; + Arena *arena; + State state; + Mesh *cube; + F64 time, last_time; + V3F light_pos; + MAT4 shadow_transforms[6]; + MAT4 proj, view; + + glfwSetErrorCallback(error_callback); + + if (glfwInit() == GLFW_FALSE) + return(1); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_SAMPLES, 4); + window = glfwCreateWindow(global_screen_width, global_screen_height, "Point shadows", 0, 0); + if (!window) + goto error; + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glfwSetKeyCallback(window, glfw_key_callback); + glfwSetWindowSizeCallback(window, resize_callback); + glfwSetCursorPosCallback(window, cursor_pos_callback); + + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) + goto error; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + /* NOTE(pryazha): Init */ + arena = arena_alloc(Megabytes(64)); + + state.camera = (Camera){ + .pos = v3f(0.0f, 1.0f, 3.0f), + .fovx = 100.0f, + .near = 0.1f, + .far = 100.0f, + .yaw = 0.0f, + .pitch = 0.0f + }; + V3F camera_dp = v3f_zero(); + /* light_pos = v3f(-2.0f, 4.0f, -2.0f); */ + light_pos = v3f_zero(); + + /* NOTE(pryazha): Meshes */ + cube = mesh_load_obj(arena, "../../data/models/cube.obj"); + + 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 + }; + U32 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, 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); + + /* NOTE(pryazha): Shaders */ + U32 depth_shader = create_shader_program_geom("shaders/depth.vs", + "shaders/depth.fs", + "shaders/depth.gs"); + U32 shadow_shader = create_shader_program("shaders/shadow.vs", "shaders/shadow.fs"); + + glUseProgram(shadow_shader); + shader_set_1i(shadow_shader, "diffuse_texture", 0); + shader_set_1i(shadow_shader, "depth_cubemap", 1); + glUseProgram(0); + + U32 wood_texture = load_texture_gamma("../../data/textures/wood.png", 0); + + /* NOTE(pryazha): Depth framebuffer init */ + U32 depth_cubemap_fbo, depth_cubemap; + U32 shadow_width = 1024, shadow_height = 1024; + glGenFramebuffers(1, &depth_cubemap_fbo); + glGenTextures(1, &depth_cubemap); + glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubemap); + for (U32 cubemap_side = 0; cubemap_side < 6; ++cubemap_side) + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+cubemap_side, 0, GL_DEPTH_COMPONENT, + shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER); + glBindFramebuffer(GL_FRAMEBUFFER, depth_cubemap_fbo); + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_cubemap, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + fprintf(stderr, "[ERROR]: Failed to complete depth map framebuffer.\n"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + if (key_first_press(global_input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + /* NOTE(pryazha): For now it's easier to write all of the logic in the main loop */ + /* NOTE(pryazha): Update */ + F32 x, y, z, angular_speed, radius; + V3F camera_dv; + + camera_dv = get_dv_camera_first_person(&global_input, &state.camera, + 1.0f, state.dt); + if (key_is_pressed(global_input.action_up)) + camera_dv = v3f_scalef(camera_dv, 3.0f); + camera_dp = v3f_add(camera_dp, camera_dv); + camera_dp = v3f_scalef(camera_dp, 0.8f); + state.camera.pos = v3f_add(state.camera.pos, camera_dp); + + F32 sensitivity = 0.1f; + global_input.mouse_offset = v2f_scalef(global_input.mouse_offset, sensitivity); + state.camera.yaw += global_input.mouse_offset.x; + state.camera.pitch += global_input.mouse_offset.y; + if (state.camera.pitch > 89.0f) + state.camera.pitch = 89.0f; + if (state.camera.pitch < -89.0f) + state.camera.pitch = -89.0f; + + input_update_last_state(&global_input); + + angular_speed = 1.0f; + radius = 4.0f; + x = f32_sin(time*angular_speed)*radius; + y = f32_sin(time*angular_speed)*f32_sin(time*angular_speed)*radius; + z = f32_cos(time*angular_speed)*radius; + + light_pos = v3f(x, y, z); + + /* NOTE(pryazha): Render */ + + /* NOTE(pryazha): Render the depth cubemap */ + F32 fovx, aspect, near, far; + fovx = 90.0f; + aspect = (F32)shadow_width/(F32)shadow_height; + near = 1.0f; + far = 25.0f; + proj = perspective(fovx, aspect, near, far); + shadow_transforms[0] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 1.0f, 0.0f, 0.0f)), v3f(0.0f, -1.0f, 0.0f))); /* right */ + shadow_transforms[1] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f(-1.0f, 0.0f, 0.0f)), v3f(0.0f, -1.0f, 0.0f))); /* left */ + shadow_transforms[2] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 1.0f, 0.0f)), v3f(0.0f, 0.0f, 1.0f))); /* top */ + shadow_transforms[3] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, -1.0f, 0.0f)), v3f(0.0f, 0.0f, -1.0f))); /* bottom */ + shadow_transforms[4] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 0.0f, 1.0f)), v3f(0.0f, -1.0f, 0.0f))); /* near */ + shadow_transforms[5] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 0.0f, -1.0f)), v3f(0.0f, -1.0f, 0.0f))); /* far */ + glViewport(0, 0, shadow_width, shadow_height); + glBindFramebuffer(GL_FRAMEBUFFER, depth_cubemap_fbo); + glClear(GL_DEPTH_BUFFER_BIT); + glUseProgram(depth_shader); + shader_set_1f(depth_shader, "far", far); + shader_set_3fv(depth_shader, "light_pos", light_pos); + for (U32 i = 0; i < 6; ++i) + { + char uniform_name[256]; + snprintf(uniform_name, 256, "shadow_transforms[%d]", i); + shader_set_mat4fv(depth_shader, uniform_name, shadow_transforms[i]); + } + render_scene(depth_shader, cube); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + /* NOTE(pryazha): Render the scene as normal */ + glViewport(0, 0, global_screen_width, global_screen_height); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(shadow_shader); + + aspect = (F32)global_screen_width/(F32)global_screen_height; + proj = perspective(state.camera.fovx, aspect, state.camera.near, state.camera.far); + view = get_view_matrix(&state.camera); + + shader_set_mat4fv(shadow_shader, "projection", proj); + shader_set_mat4fv(shadow_shader, "view", view); + shader_set_3fv(shadow_shader, "light_pos", light_pos); + shader_set_3fv(shadow_shader, "view_pos", state.camera.pos); + shader_set_1f(shadow_shader, "far", far); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, wood_texture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubemap); + render_scene(shadow_shader, cube); + + glfwSwapBuffers(window); + + time = glfwGetTime(); + state.dt = time-last_time; + last_time = time; + } + + glfwTerminate(); + return(0); + + error: + glfwTerminate(); + return(1); +} diff --git a/advanced_lighting/3.2.point_shadows/shaders/depth.fs b/advanced_lighting/3.2.point_shadows/shaders/depth.fs new file mode 100644 index 0000000..88fb9e6 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/shaders/depth.fs @@ -0,0 +1,14 @@ +#version 330 core + +in vec4 frag_pos; + +uniform vec3 light_pos; +uniform float far; + +void +main(void) +{ + float light_distance = length(frag_pos.xyz-light_pos); + light_distance = light_distance/far; + gl_FragDepth = light_distance; +} diff --git a/advanced_lighting/3.2.point_shadows/shaders/depth.gs b/advanced_lighting/3.2.point_shadows/shaders/depth.gs new file mode 100644 index 0000000..638e2b9 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/shaders/depth.gs @@ -0,0 +1,27 @@ +#version 330 core +layout(triangles) in; +layout(triangle_strip, max_vertices=18) out; + +uniform mat4 shadow_transforms[6]; + +out vec4 frag_pos; + +void +main(void) +{ + for (int face = 0; + face < 6; + ++face) + { + gl_Layer = face; + for (int vertex = 0; + vertex < 3; + ++vertex) + { + frag_pos = gl_in[vertex].gl_Position; + gl_Position = shadow_transforms[face]*frag_pos; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/advanced_lighting/3.2.point_shadows/shaders/depth.vs b/advanced_lighting/3.2.point_shadows/shaders/depth.vs new file mode 100644 index 0000000..37a3484 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/shaders/depth.vs @@ -0,0 +1,10 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 model; + +void +main(void) +{ + gl_Position = model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.2.point_shadows/shaders/shadow.fs b/advanced_lighting/3.2.point_shadows/shaders/shadow.fs new file mode 100644 index 0000000..6a3a8b0 --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/shaders/shadow.fs @@ -0,0 +1,105 @@ +#version 330 core + +in VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform vec3 light_pos; +uniform vec3 view_pos; +uniform float far; + +uniform sampler2D diffuse_texture; +uniform samplerCube depth_cubemap; + +vec3 grid_sampling_disk[20] = vec3[] +( + vec3(1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1), + vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1), + vec3(1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), + vec3(1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1), + vec3(0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1) +); + +float +calculate_shadow(vec3 frag_pos) +{ + vec3 frag_to_light = frag_pos-light_pos; + /* float closest_depth = texture(depth_cubemap, frag_to_light).r; */ + /* closest_depth *= far; */ + float current_depth = length(frag_to_light); + /* float bias = 0.05; */ + /* float shadow = current_depth-bias > closest_depth ? 1.0 : 0.0; */ + /* NOTE(pryazha): Display depth value for debugging */ + /* frag_color = vec4(vec3(closest_depth/far), 1.0); */ + /* + float shadow = 0.0; + float offset = 0.1; + float samples = 4.0; + for (float x = -offset; + x < offset; + x += offset/(samples*0.5)) + { + for (float y = -offset; + y < offset; + y += offset/(samples*0.5)) + { + for (float z = -offset; + z < offset; + z += offset/(samples*0.5)) + { + float closest_depth = texture(depth_cubemap, frag_to_light+vec3(x, y, z)).r; + closest_depth *= far; + if (current_depth-bias > closest_depth) + shadow += 1.0; + } + } + } + shadow /= (samples*samples*samples); + */ + + float shadow = 0.0; + float bias = 0.15; + int samples = 20; + float view_distance = length(view_pos-frag_pos); + float disk_radius = (1.0+(view_distance/far))/25.0; + for (int sample = 0; + sample < samples; + ++sample) + { + float closest_depth = texture(depth_cubemap, frag_to_light+ + grid_sampling_disk[sample]*disk_radius).r; + closest_depth *= far; + if (current_depth-bias > closest_depth) + shadow += 1.0; + } + shadow /= float(samples); + return(shadow); +} + +void +main(void) +{ + vec3 color = texture(diffuse_texture, fs_in.tex_coords).rgb; + vec3 normal = normalize(fs_in.normal); + vec3 light_color = vec3(0.5); + + vec3 ambient = 0.5*light_color; + + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float diff = max(dot(light_dir, normal), 0.0); + vec3 diffuse = diff*light_color; + + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 halfway_dir = normalize(light_dir+view_dir); + float spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); + vec3 specular = spec*light_color; + + float shadow = calculate_shadow(fs_in.frag_pos); + vec3 lighting = (ambient+(1.0-shadow)*(diffuse+specular))*color; + + frag_color = vec4(lighting, 1.0); +} diff --git a/advanced_lighting/3.2.point_shadows/shaders/shadow.vs b/advanced_lighting/3.2.point_shadows/shaders/shadow.vs new file mode 100644 index 0000000..ac8ee4f --- /dev/null +++ b/advanced_lighting/3.2.point_shadows/shaders/shadow.vs @@ -0,0 +1,29 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +uniform bool reverse_normals; + +void +main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + if (reverse_normals) + vs_out.normal = mat3(transpose(inverse(model)))*(-1.0*anormal); + else + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + + vs_out.tex_coords = atex_coords; + gl_Position = projection*view*vec4(vs_out.frag_pos, 1.0); +} diff --git a/advanced_lighting/blinn_phong/build.sh b/advanced_lighting/4.normal_mapping/build.sh index 21a65b4..262203d 100755 --- a/advanced_lighting/blinn_phong/build.sh +++ b/advanced_lighting/4.normal_mapping/build.sh @@ -1,5 +1,5 @@ #!/bin/sh . ../../config -TARGET='blinn_phong' +TARGET='normal_mapping' set -x gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/advanced_lighting/4.normal_mapping/normal_mapping b/advanced_lighting/4.normal_mapping/normal_mapping Binary files differnew file mode 100755 index 0000000..6d33c26 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/normal_mapping diff --git a/advanced_lighting/4.normal_mapping/normal_mapping.c b/advanced_lighting/4.normal_mapping/normal_mapping.c new file mode 100644 index 0000000..1d65b49 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/normal_mapping.c @@ -0,0 +1,288 @@ +#include "pwyazh.h" + +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh_GL.h" + +#include "common.h" + +static S32 global_screen_width = 800, global_screen_height = 600; +static Input global_input; +static B32 first_mouse = 1; + +void +resize_callback(GLFWwindow* window, int width, int height) +{ + global_screen_width = width; + global_screen_height = height; + glViewport(0, 0, global_screen_width, global_screen_height); +} + +void +cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) +{ + if (first_mouse) + { + global_input.last_mouse_pos = v2f(xpos, ypos); + first_mouse = 0; + } + global_input.mouse_offset = v2f(global_input.mouse_offset.x+((F32)xpos-global_input.last_mouse_pos.x), + global_input.mouse_offset.y+((F32)ypos-global_input.last_mouse_pos.y)); + global_input.last_mouse_pos = v2f((F32)xpos, (F32)ypos); +} + +void +render_scene(U32 shader, Mesh *cube) +{ + MAT4 model; + + model = mat4_identity(); + model = mat4_make_scale(v3f(5.0f, 5.0f, 5.0f)); + shader_set_mat4fv(shader, "model", model); + glDisable(GL_CULL_FACE); + shader_set_1i(shader, "reverse_normals", 1); + mesh_draw(cube); + shader_set_1i(shader, "reverse_normals", 0); + glEnable(GL_CULL_FACE); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(0.0f, 1.5f, 0.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); + model = mat4_translate(model, v3f(2.0f, 0.0f, 1.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); + + model = mat4_identity(); + model = mat4_make_scale(v3f(0.25f, 0.25f, 0.25f)); + model = mat4_rotate_angles(model, v3f(60.0f, 0.0f, 0.0f)); + model = mat4_translate(model, v3f(-1.0f, 0.0f, 2.0f)); + shader_set_mat4fv(shader, "model", model); + mesh_draw(cube); +} + +int +main(void) +{ + GLFWwindow *window; + Arena *arena; + State state; + Mesh *cube; + F64 time, last_time; + V3F light_pos; + MAT4 shadow_transforms[6]; + MAT4 proj, view; + + glfwSetErrorCallback(error_callback); + + if (glfwInit() == GLFW_FALSE) + return(1); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_SAMPLES, 4); + window = glfwCreateWindow(global_screen_width, global_screen_height, "Point shadows", 0, 0); + if (!window) + goto error; + + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + glfwSetWindowSizeCallback(window, resize_callback); + glfwSetCursorPosCallback(window, cursor_pos_callback); + + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) + goto error; + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + /* NOTE(pryazha): Init */ + arena = arena_alloc(Megabytes(64)); + + state.camera = (Camera){ + .pos = v3f(0.0f, 1.0f, 3.0f), + .fovx = 100.0f, + .near = 0.1f, + .far = 100.0f, + .yaw = 0.0f, + .pitch = 0.0f + }; + V3F camera_dp = v3f_zero(); + /* light_pos = v3f(-2.0f, 4.0f, -2.0f); */ + light_pos = v3f_zero(); + + /* NOTE(pryazha): Meshes */ + cube = mesh_load_obj(arena, "../../data/models/cube.obj"); + + 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 + }; + U32 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, 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); + + /* NOTE(pryazha): Shaders */ + U32 depth_shader = create_shader_program_geom("shaders/depth.vs", + "shaders/depth.fs", + "shaders/depth.gs"); + U32 shadow_shader = create_shader_program("shaders/shadow.vs", "shaders/shadow.fs"); + + glUseProgram(shadow_shader); + shader_set_1i(shadow_shader, "diffuse_texture", 0); + shader_set_1i(shadow_shader, "depth_cubemap", 1); + glUseProgram(0); + + U32 wood_texture = load_texture_gamma("../../data/textures/wood.png", 0); + + /* NOTE(pryazha): Depth framebuffer init */ + U32 depth_cubemap_fbo, depth_cubemap; + U32 shadow_width = 1024, shadow_height = 1024; + glGenFramebuffers(1, &depth_cubemap_fbo); + glGenTextures(1, &depth_cubemap); + glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubemap); + for (U32 cubemap_side = 0; cubemap_side < 6; ++cubemap_side) + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+cubemap_side, 0, GL_DEPTH_COMPONENT, + shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER); + glBindFramebuffer(GL_FRAMEBUFFER, depth_cubemap_fbo); + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_cubemap, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + fprintf(stderr, "[ERROR]: Failed to complete depth map framebuffer.\n"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + + /* NOTE(pryazha): For now it's easier to write all of the logic in the main loop */ + + /* NOTE(pryazha): Update */ + process_glfw_keyboard(window, &global_input); + + if (key_first_press(global_input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + F32 x, y, z, angular_speed, radius; + V3F camera_dv; + + camera_dv = get_dv_camera_first_person(&global_input, &state.camera, + 1.0f, state.dt); + if (key_is_pressed(global_input.action_up)) + camera_dv = v3f_scalef(camera_dv, 3.0f); + camera_dp = v3f_add(camera_dp, camera_dv); + camera_dp = v3f_scalef(camera_dp, 0.8f); + state.camera.pos = v3f_add(state.camera.pos, camera_dp); + + F32 sensitivity = 0.1f; + global_input.mouse_offset = v2f_scalef(global_input.mouse_offset, sensitivity); + state.camera.yaw += global_input.mouse_offset.x; + state.camera.pitch += global_input.mouse_offset.y; + if (state.camera.pitch > 89.0f) + state.camera.pitch = 89.0f; + if (state.camera.pitch < -89.0f) + state.camera.pitch = -89.0f; + + input_update_last_state(&global_input); + + angular_speed = 1.0f; + radius = 4.0f; + x = f32_sin(time*angular_speed)*radius; + y = f32_sin(time*angular_speed)*radius; + z = f32_cos(time*angular_speed)*radius; + + light_pos = v3f(x, y, z); + + /* NOTE(pryazha): Render */ + + /* NOTE(pryazha): Render the depth cubemap */ + F32 fovx, aspect, near, far; + fovx = 90.0f; + aspect = (F32)shadow_width/(F32)shadow_height; + near = 1.0f; + far = 25.0f; + proj = perspective(fovx, aspect, near, far); + shadow_transforms[0] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 1.0f, 0.0f, 0.0f)), v3f(0.0f, -1.0f, 0.0f))); /* right */ + shadow_transforms[1] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f(-1.0f, 0.0f, 0.0f)), v3f(0.0f, -1.0f, 0.0f))); /* left */ + shadow_transforms[2] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 1.0f, 0.0f)), v3f(0.0f, 0.0f, 1.0f))); /* top */ + shadow_transforms[3] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, -1.0f, 0.0f)), v3f(0.0f, 0.0f, -1.0f))); /* bottom */ + shadow_transforms[4] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 0.0f, 1.0f)), v3f(0.0f, -1.0f, 0.0f))); /* near */ + shadow_transforms[5] = mat4_mul(proj, look_at(light_pos, v3f_add(light_pos, v3f( 0.0f, 0.0f, -1.0f)), v3f(0.0f, -1.0f, 0.0f))); /* far */ + glViewport(0, 0, shadow_width, shadow_height); + glBindFramebuffer(GL_FRAMEBUFFER, depth_cubemap_fbo); + glClear(GL_DEPTH_BUFFER_BIT); + glUseProgram(depth_shader); + shader_set_1f(depth_shader, "far", far); + shader_set_3fv(depth_shader, "light_pos", light_pos); + for (U32 i = 0; i < 6; ++i) + { + char uniform_name[256]; + snprintf(uniform_name, 256, "shadow_transforms[%d]", i); + shader_set_mat4fv(depth_shader, uniform_name, shadow_transforms[i]); + } + render_scene(depth_shader, cube); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + /* NOTE(pryazha): Render the scene as normal */ + glViewport(0, 0, global_screen_width, global_screen_height); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(shadow_shader); + + aspect = (F32)global_screen_width/(F32)global_screen_height; + proj = perspective(state.camera.fovx, aspect, state.camera.near, state.camera.far); + view = get_view_matrix(&state.camera); + + shader_set_mat4fv(shadow_shader, "projection", proj); + shader_set_mat4fv(shadow_shader, "view", view); + shader_set_3fv(shadow_shader, "light_pos", light_pos); + shader_set_3fv(shadow_shader, "view_pos", state.camera.pos); + shader_set_1f(shadow_shader, "far", far); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, wood_texture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubemap); + render_scene(shadow_shader, cube); + + glfwSwapBuffers(window); + + time = glfwGetTime(); + state.dt = time-last_time; + last_time = time; + } + + glfwTerminate(); + return(0); + + error: + glfwTerminate(); + return(1); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/depth.fs b/advanced_lighting/4.normal_mapping/shaders/depth.fs new file mode 100644 index 0000000..88fb9e6 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/depth.fs @@ -0,0 +1,14 @@ +#version 330 core + +in vec4 frag_pos; + +uniform vec3 light_pos; +uniform float far; + +void +main(void) +{ + float light_distance = length(frag_pos.xyz-light_pos); + light_distance = light_distance/far; + gl_FragDepth = light_distance; +} diff --git a/advanced_lighting/4.normal_mapping/shaders/depth.gs b/advanced_lighting/4.normal_mapping/shaders/depth.gs new file mode 100644 index 0000000..638e2b9 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/depth.gs @@ -0,0 +1,27 @@ +#version 330 core +layout(triangles) in; +layout(triangle_strip, max_vertices=18) out; + +uniform mat4 shadow_transforms[6]; + +out vec4 frag_pos; + +void +main(void) +{ + for (int face = 0; + face < 6; + ++face) + { + gl_Layer = face; + for (int vertex = 0; + vertex < 3; + ++vertex) + { + frag_pos = gl_in[vertex].gl_Position; + gl_Position = shadow_transforms[face]*frag_pos; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/advanced_lighting/4.normal_mapping/shaders/depth.vs b/advanced_lighting/4.normal_mapping/shaders/depth.vs new file mode 100644 index 0000000..37a3484 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/depth.vs @@ -0,0 +1,10 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 model; + +void +main(void) +{ + gl_Position = model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/shadow.fs b/advanced_lighting/4.normal_mapping/shaders/shadow.fs new file mode 100644 index 0000000..6a3a8b0 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/shadow.fs @@ -0,0 +1,105 @@ +#version 330 core + +in VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform vec3 light_pos; +uniform vec3 view_pos; +uniform float far; + +uniform sampler2D diffuse_texture; +uniform samplerCube depth_cubemap; + +vec3 grid_sampling_disk[20] = vec3[] +( + vec3(1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1), + vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1), + vec3(1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), + vec3(1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1), + vec3(0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1) +); + +float +calculate_shadow(vec3 frag_pos) +{ + vec3 frag_to_light = frag_pos-light_pos; + /* float closest_depth = texture(depth_cubemap, frag_to_light).r; */ + /* closest_depth *= far; */ + float current_depth = length(frag_to_light); + /* float bias = 0.05; */ + /* float shadow = current_depth-bias > closest_depth ? 1.0 : 0.0; */ + /* NOTE(pryazha): Display depth value for debugging */ + /* frag_color = vec4(vec3(closest_depth/far), 1.0); */ + /* + float shadow = 0.0; + float offset = 0.1; + float samples = 4.0; + for (float x = -offset; + x < offset; + x += offset/(samples*0.5)) + { + for (float y = -offset; + y < offset; + y += offset/(samples*0.5)) + { + for (float z = -offset; + z < offset; + z += offset/(samples*0.5)) + { + float closest_depth = texture(depth_cubemap, frag_to_light+vec3(x, y, z)).r; + closest_depth *= far; + if (current_depth-bias > closest_depth) + shadow += 1.0; + } + } + } + shadow /= (samples*samples*samples); + */ + + float shadow = 0.0; + float bias = 0.15; + int samples = 20; + float view_distance = length(view_pos-frag_pos); + float disk_radius = (1.0+(view_distance/far))/25.0; + for (int sample = 0; + sample < samples; + ++sample) + { + float closest_depth = texture(depth_cubemap, frag_to_light+ + grid_sampling_disk[sample]*disk_radius).r; + closest_depth *= far; + if (current_depth-bias > closest_depth) + shadow += 1.0; + } + shadow /= float(samples); + return(shadow); +} + +void +main(void) +{ + vec3 color = texture(diffuse_texture, fs_in.tex_coords).rgb; + vec3 normal = normalize(fs_in.normal); + vec3 light_color = vec3(0.5); + + vec3 ambient = 0.5*light_color; + + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float diff = max(dot(light_dir, normal), 0.0); + vec3 diffuse = diff*light_color; + + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 halfway_dir = normalize(light_dir+view_dir); + float spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); + vec3 specular = spec*light_color; + + float shadow = calculate_shadow(fs_in.frag_pos); + vec3 lighting = (ambient+(1.0-shadow)*(diffuse+specular))*color; + + frag_color = vec4(lighting, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/shadow.vs b/advanced_lighting/4.normal_mapping/shaders/shadow.vs new file mode 100644 index 0000000..ac8ee4f --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/shadow.vs @@ -0,0 +1,29 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +uniform bool reverse_normals; + +void +main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + if (reverse_normals) + vs_out.normal = mat3(transpose(inverse(model)))*(-1.0*anormal); + else + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + + vs_out.tex_coords = atex_coords; + gl_Position = projection*view*vec4(vs_out.frag_pos, 1.0); +} diff --git a/advanced_lighting/blinn_phong/.blinn_phong.c.swp b/advanced_lighting/blinn_phong/.blinn_phong.c.swp Binary files differdeleted file mode 100644 index d1ba9b1..0000000 --- a/advanced_lighting/blinn_phong/.blinn_phong.c.swp +++ /dev/null diff --git a/advanced_lighting/blinn_phong/blinn_phong b/advanced_lighting/blinn_phong/blinn_phong Binary files differdeleted file mode 100755 index 1c89f59..0000000 --- a/advanced_lighting/blinn_phong/blinn_phong +++ /dev/null diff --git a/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp b/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp Binary files differdeleted file mode 100644 index cfc93b0..0000000 --- a/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp +++ /dev/null diff --git a/advanced_lighting/blinn_phong/shaders/blinn_phong.fs b/advanced_lighting/blinn_phong/shaders/blinn_phong.fs deleted file mode 100644 index 23e2968..0000000 --- a/advanced_lighting/blinn_phong/shaders/blinn_phong.fs +++ /dev/null @@ -1,62 +0,0 @@ -#version 330 core - -in vec2 tex_coords; -in vec3 normal; -in vec3 frag_pos; - -out vec4 frag_color; - -uniform sampler2D texture_diffuse; -uniform sampler2D texture_specular; - -uniform vec3 light_pos; -uniform vec3 view_pos; - -uniform float ambient_strength; -uniform float specular_strength; -uniform float shininess; - -vec4 -spotlight(vec3 light_dir, vec3 view_dir) -{ - vec3 tex_color_diff = texture(texture_diffuse, tex_coords); - float ambient_strength = 0.1f; - vec4 ambient = tex_color_diff*ambient_strength; - vec4 diffuse = tex_color_diff*dot(-light_dir, ) - return(color); -} - -vec4 -blinn_phong() -{ - // Sample the diffuse color from the texture - vec3 diffuse_color = texture(texture_diffuse, tex_coords).rgb; - - // Sample the specular color from the texture (if available) - vec3 specular_color = texture(texture_specular, tex_coords).rgb; - - // Ambient lighting - vec3 ambient = ambient_strength*diffuse_color; - - // Diffuse lighting - vec3 norm = normalize(normal); - vec3 light_dir = normalize(light_pos-frag_pos); - float diff = max(dot(norm, light_dir), 0.0); - vec3 diffuse = diff * diffuse_color; - - // Specular lighting - vec3 view_dir = normalize(view_pos-frag_pos); - vec3 halfway_dir = normalize(light_dir+view_dir); // Halfway vector - float spec = pow(max(dot(norm, halfway_dir), 0.0), shininess); - vec3 specular = specular_strength*spec*specular_color; - - // Combine all components - vec3 result = (ambient+diffuse+specular); - return((result, 1.0)); -} - -void -main(void) -{ - frag_color = blinn_phong(); -} diff --git a/advanced_lighting/blinn_phong/shaders/blinn_phong.vs b/advanced_lighting/blinn_phong/shaders/blinn_phong.vs deleted file mode 100644 index d4e5dbc..0000000 --- a/advanced_lighting/blinn_phong/shaders/blinn_phong.vs +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; -layout(location = 1) in vec3 anormal; -layout(location = 2) in vec2 atex_coords; - -out vec3 frag_pos; -out vec3 normal; -out vec2 tex_coords; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; - -void -main(void) -{ - frag_pos = vec3(model*vec4(apos, 1.0f); - normal = mat3(transpose(inverse(view*model)))*anormal; - tex_coords = atex_coords; - gl_Position = projection*view*model*vec4(apos, 1.0f); -} diff --git a/advanced_lighting/build.sh b/advanced_lighting/build.sh new file mode 100755 index 0000000..4829c7d --- /dev/null +++ b/advanced_lighting/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh +targets=$(ls) +for target in $targets ; do + if [ $target != 'build.sh' ] ; then + cd $target + ./build.sh + cd .. + fi +done diff --git a/advanced_opengl/1.depth/build.sh b/advanced_opengl/1.depth/build.sh index 837b21c..04fe803 100755 --- a/advanced_opengl/1.depth/build.sh +++ b/advanced_opengl/1.depth/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='depth' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/1.depth/depth b/advanced_opengl/1.depth/depth Binary files differindex db67630..68f04d6 100755 --- a/advanced_opengl/1.depth/depth +++ b/advanced_opengl/1.depth/depth diff --git a/advanced_opengl/1.depth/shaders/depth.fs b/advanced_opengl/1.depth/shaders/depth.fs index 2f3d9ca..a515c15 100644 --- a/advanced_opengl/1.depth/shaders/depth.fs +++ b/advanced_opengl/1.depth/shaders/depth.fs @@ -6,14 +6,14 @@ in vec2 tex_coords; uniform sampler2D our_texture; -float near = 0.1f; -float far = 100.0f; +float near = 0.1; +float far = 100.0; float linearize_depth(float depth) { - float ndc_z = 2.0f*depth-1.0f; - return((2.0f*near*far)/(far+near-ndc_z*(far-near))); + float ndc_z = 2.0*depth-1.0; + return((2.0*near*far)/(far+near-ndc_z*(far-near))); } void @@ -23,5 +23,5 @@ main(void) frag_color = texture(our_texture, tex_coords); */ float depth = linearize_depth(gl_FragCoord.z)/far; - frag_color = vec4(vec3(depth), 1.0f); + frag_color = vec4(vec3(depth), 1.0); } diff --git a/advanced_opengl/1.depth/shaders/depth.vs b/advanced_opengl/1.depth/shaders/depth.vs index d3d92d1..62e71a2 100644 --- a/advanced_opengl/1.depth/shaders/depth.vs +++ b/advanced_opengl/1.depth/shaders/depth.vs @@ -11,6 +11,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_msaa b/advanced_opengl/10.antialiasing/anti_aliasing_msaa Binary files differindex d0afd7e..36811f7 100755 --- a/advanced_opengl/10.antialiasing/anti_aliasing_msaa +++ 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 index 9fcb645..65a5b01 100644 --- a/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c +++ b/advanced_opengl/10.antialiasing/anti_aliasing_msaa.c @@ -169,9 +169,9 @@ main(void) 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); + V3F camera_dv = get_dv_camera_orbital(&global_input, camera_pos, + v3f_zero(), dt, camera_speed); + camera_pos = v3f_add(camera_pos, camera_dv); projection = perspective(fovx, (F32)global_width/(F32)global_height, near, far); view = look_at(camera_pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); diff --git a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen Binary files differindex 72be7e2..c8a241c 100755 --- a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen +++ 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 index 91a26f9..5c6debb 100644 --- a/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c +++ b/advanced_opengl/10.antialiasing/anti_aliasing_offscreen.c @@ -218,9 +218,9 @@ main(void) 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); + V3F camera_dv = get_dv_camera_orbital(&global_input, camera_pos, + v3f_zero(), dt, camera_speed); + camera_pos = v3f_add(camera_pos, camera_dv); projection = perspective(fovx, (F32)WIDTH/(F32)HEIGHT, near, far); view = look_at(camera_pos, v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); diff --git a/advanced_opengl/10.antialiasing/shaders/color.fs b/advanced_opengl/10.antialiasing/shaders/color.fs index b935555..542143e 100644 --- a/advanced_opengl/10.antialiasing/shaders/color.fs +++ b/advanced_opengl/10.antialiasing/shaders/color.fs @@ -5,5 +5,5 @@ out vec4 frag_color; void main(void) { - frag_color = vec4(0.76f, 0.47f, 0.84f, 1.0f); + frag_color = vec4(0.76, 0.47, 0.84, 1.0); } diff --git a/advanced_opengl/10.antialiasing/shaders/screen.fs b/advanced_opengl/10.antialiasing/shaders/screen.fs index 8fefa24..183d490 100644 --- a/advanced_opengl/10.antialiasing/shaders/screen.fs +++ b/advanced_opengl/10.antialiasing/shaders/screen.fs @@ -9,7 +9,7 @@ uniform sampler2D texture1; vec4 inversion() { - vec4 result = vec4(1.0f-vec3(texture(texture1, tex_coords)), 1.0f); + vec4 result = vec4(1.0-vec3(texture(texture1, tex_coords)), 1.0); return(result); } @@ -17,8 +17,8 @@ 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); + float average = (tex_color.r+tex_color.g+tex_color.b)/3.0; + vec4 result = vec4(average, average, average, 1.0); return(result); } @@ -26,25 +26,25 @@ 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); + float average = 0.2126*tex_color.r+0.7152*tex_color.g+0.0722*tex_color.b; + vec4 result = vec4(average, average, average, 1.0); return(result); } -const float offset = 1.0f/300.0f; +const float offset = 1.0/300.0; vec4 kernel_effect(float[9] kernel) { vec2 offsets[9] = vec2[]( vec2(-offset, offset), /* top-left */ - vec2(0.0f, offset), /* top-center */ + vec2(0.0, 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, 0.0), /* center-left */ + vec2(0.0, 0.0), /* center-center */ + vec2(offset, 0.0), /* center-right */ vec2(-offset, -offset), /* bottom-left */ - vec2(0.0f, -offset), /* bottom-center */ + vec2(0.0, -offset), /* bottom-center */ vec2(offset, -offset) /* bottom-right */ ); @@ -57,11 +57,11 @@ kernel_effect(float[9] kernel) vec3(texture(texture1, tex_coords+offsets[offset_index])); } - vec3 color = vec3(0.0f); + vec3 color = vec3(0.0); for (int i = 0; i < 9; ++i) color += sample_texture[i]*kernel[i]; - return(vec4(color, 1.0f)); + return(vec4(color, 1.0)); } void @@ -74,49 +74,46 @@ main(void) ); 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 + 1.0/16.0, 2.0/16.0, 1.0/16.0, + 2.0/16.0, 4.0/16.0, 2.0/16.0, + 1.0/16.0, 2.0/16.0, 1.0/16.0 ); float box_blur_kernel[9] = float[]( - 1.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 + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0 ); float edge_detection_kernel[9] = float[]( - 1.0f, 1.0f, 1.0f, - 1.0f, -8.0f, 1.0f, - 1.0f, 1.0f, 1.0f + 1.0, 1.0, 1.0, + 1.0, -8.0, 1.0, + 1.0, 1.0, 1.0 ); float sharpening_kernel[9] = float[]( - 0.0f, -1.0f, 0.0f, - -1.0f, 5.0f, -1.0f, - 0.0f, -1.0f, 0.0f + 0.0, -1.0, 0.0, + -1.0, 5.0, -1.0, + 0.0, -1.0, 0.0 ); float sobel_kernel_vertical[9] = float[]( - -1.0f, 0.0f, 1.0f, - -2.0f, 0.0f, 2.0f, - -1.0f, 0.0f, 0.0f + -1.0, 0.0, 1.0, + -2.0, 0.0, 2.0, + -1.0, 0.0, 0.0 ); float sobel_kernel_horizontal[9] = float[]( - -1.0f, -2.0f, -1.0f, - 0.0f, 0.0f, 0.0f, - 1.0f, 2.0f, 1.0f + -1.0, -2.0, -1.0, + 0.0, 0.0, 0.0, + 1.0, 2.0, 1.0 ); float funny_kernel[9] = float[]( - 1.0f, 1.0f, 1.0f, - 1.0f, -4.0f, 1.0f, - 1.0f, 1.0f, 1.0f + 1.0, 1.0, 1.0, + 1.0, -4.0, 1.0, + 1.0, 1.0, 1.0 ); - /* frag_color = kernel_effect(funny_kernel); - frag_color *= vec4(0.76f, 0.47f, 0.84f, 1.0f); - */ - frag_color = grayscale_weights(); + frag_color *= vec4(0.76, 0.47, 0.84, 1.0); } diff --git a/advanced_opengl/10.antialiasing/shaders/screen.vs b/advanced_opengl/10.antialiasing/shaders/screen.vs index 0a5213b..1f233ee 100644 --- a/advanced_opengl/10.antialiasing/shaders/screen.vs +++ b/advanced_opengl/10.antialiasing/shaders/screen.vs @@ -7,6 +7,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = vec4(apos, 1.0f); + gl_Position = vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/2.stencil/build.sh b/advanced_opengl/2.stencil/build.sh index 335dd5d..c4aa562 100755 --- a/advanced_opengl/2.stencil/build.sh +++ b/advanced_opengl/2.stencil/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='stencil' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/2.stencil/shaders/outline.fs b/advanced_opengl/2.stencil/shaders/outline.fs index b935555..542143e 100644 --- a/advanced_opengl/2.stencil/shaders/outline.fs +++ b/advanced_opengl/2.stencil/shaders/outline.fs @@ -5,5 +5,5 @@ out vec4 frag_color; void main(void) { - frag_color = vec4(0.76f, 0.47f, 0.84f, 1.0f); + frag_color = vec4(0.76, 0.47, 0.84, 1.0); } diff --git a/advanced_opengl/2.stencil/shaders/stencil.vs b/advanced_opengl/2.stencil/shaders/stencil.vs index d3d92d1..62e71a2 100644 --- a/advanced_opengl/2.stencil/shaders/stencil.vs +++ b/advanced_opengl/2.stencil/shaders/stencil.vs @@ -11,6 +11,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/2.stencil/stencil b/advanced_opengl/2.stencil/stencil Binary files differindex ad0f48c..eb4eac5 100755 --- a/advanced_opengl/2.stencil/stencil +++ b/advanced_opengl/2.stencil/stencil diff --git a/advanced_opengl/3.blending/blending b/advanced_opengl/3.blending/blending Binary files differindex 063ce51..40fe12a 100755 --- a/advanced_opengl/3.blending/blending +++ b/advanced_opengl/3.blending/blending diff --git a/advanced_opengl/3.blending/build.sh b/advanced_opengl/3.blending/build.sh index 1fb5c9c..51f3629 100755 --- a/advanced_opengl/3.blending/build.sh +++ b/advanced_opengl/3.blending/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='blending' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/3.blending/shaders/blending.vs b/advanced_opengl/3.blending/shaders/blending.vs index d3d92d1..62e71a2 100644 --- a/advanced_opengl/3.blending/shaders/blending.vs +++ b/advanced_opengl/3.blending/shaders/blending.vs @@ -11,6 +11,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/4.face_culling/build.sh b/advanced_opengl/4.face_culling/build.sh index e60b2dc..d67eefa 100755 --- a/advanced_opengl/4.face_culling/build.sh +++ b/advanced_opengl/4.face_culling/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='face_culling' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/4.face_culling/face_culling b/advanced_opengl/4.face_culling/face_culling Binary files differindex 62d75b4..eb6e3e9 100755 --- a/advanced_opengl/4.face_culling/face_culling +++ b/advanced_opengl/4.face_culling/face_culling diff --git a/advanced_opengl/4.face_culling/shaders/face_culling.vs b/advanced_opengl/4.face_culling/shaders/face_culling.vs index d3d92d1..62e71a2 100644 --- a/advanced_opengl/4.face_culling/shaders/face_culling.vs +++ b/advanced_opengl/4.face_culling/shaders/face_culling.vs @@ -11,6 +11,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/5.framebuffers/build.sh b/advanced_opengl/5.framebuffers/build.sh index 58a1162..cb55ebf 100755 --- a/advanced_opengl/5.framebuffers/build.sh +++ b/advanced_opengl/5.framebuffers/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='framebuffers' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/5.framebuffers/framebuffers b/advanced_opengl/5.framebuffers/framebuffers Binary files differindex 4b2c4c5..a1f7170 100755 --- a/advanced_opengl/5.framebuffers/framebuffers +++ b/advanced_opengl/5.framebuffers/framebuffers diff --git a/advanced_opengl/5.framebuffers/shaders/cube.vs b/advanced_opengl/5.framebuffers/shaders/cube.vs index 3c11932..aea5c2c 100644 --- a/advanced_opengl/5.framebuffers/shaders/cube.vs +++ b/advanced_opengl/5.framebuffers/shaders/cube.vs @@ -12,6 +12,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/5.framebuffers/shaders/screen.fs b/advanced_opengl/5.framebuffers/shaders/screen.fs index 8f0b2da..183d490 100644 --- a/advanced_opengl/5.framebuffers/shaders/screen.fs +++ b/advanced_opengl/5.framebuffers/shaders/screen.fs @@ -9,7 +9,7 @@ uniform sampler2D texture1; vec4 inversion() { - vec4 result = vec4(1.0f-vec3(texture(texture1, tex_coords)), 1.0f); + vec4 result = vec4(1.0-vec3(texture(texture1, tex_coords)), 1.0); return(result); } @@ -17,8 +17,8 @@ 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); + float average = (tex_color.r+tex_color.g+tex_color.b)/3.0; + vec4 result = vec4(average, average, average, 1.0); return(result); } @@ -26,25 +26,25 @@ 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); + float average = 0.2126*tex_color.r+0.7152*tex_color.g+0.0722*tex_color.b; + vec4 result = vec4(average, average, average, 1.0); return(result); } -const float offset = 1.0f/300.0f; +const float offset = 1.0/300.0; vec4 kernel_effect(float[9] kernel) { vec2 offsets[9] = vec2[]( vec2(-offset, offset), /* top-left */ - vec2(0.0f, offset), /* top-center */ + vec2(0.0, 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, 0.0), /* center-left */ + vec2(0.0, 0.0), /* center-center */ + vec2(offset, 0.0), /* center-right */ vec2(-offset, -offset), /* bottom-left */ - vec2(0.0f, -offset), /* bottom-center */ + vec2(0.0, -offset), /* bottom-center */ vec2(offset, -offset) /* bottom-right */ ); @@ -57,11 +57,11 @@ kernel_effect(float[9] kernel) vec3(texture(texture1, tex_coords+offsets[offset_index])); } - vec3 color = vec3(0.0f); + vec3 color = vec3(0.0); for (int i = 0; i < 9; ++i) color += sample_texture[i]*kernel[i]; - return(vec4(color, 1.0f)); + return(vec4(color, 1.0)); } void @@ -74,46 +74,46 @@ main(void) ); 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 + 1.0/16.0, 2.0/16.0, 1.0/16.0, + 2.0/16.0, 4.0/16.0, 2.0/16.0, + 1.0/16.0, 2.0/16.0, 1.0/16.0 ); float box_blur_kernel[9] = float[]( - 1.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 + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0, + 1.0/9.0, 1.0/9.0, 1.0/9.0 ); float edge_detection_kernel[9] = float[]( - 1.0f, 1.0f, 1.0f, - 1.0f, -8.0f, 1.0f, - 1.0f, 1.0f, 1.0f + 1.0, 1.0, 1.0, + 1.0, -8.0, 1.0, + 1.0, 1.0, 1.0 ); float sharpening_kernel[9] = float[]( - 0.0f, -1.0f, 0.0f, - -1.0f, 5.0f, -1.0f, - 0.0f, -1.0f, 0.0f + 0.0, -1.0, 0.0, + -1.0, 5.0, -1.0, + 0.0, -1.0, 0.0 ); float sobel_kernel_vertical[9] = float[]( - -1.0f, 0.0f, 1.0f, - -2.0f, 0.0f, 2.0f, - -1.0f, 0.0f, 0.0f + -1.0, 0.0, 1.0, + -2.0, 0.0, 2.0, + -1.0, 0.0, 0.0 ); float sobel_kernel_horizontal[9] = float[]( - -1.0f, -2.0f, -1.0f, - 0.0f, 0.0f, 0.0f, - 1.0f, 2.0f, 1.0f + -1.0, -2.0, -1.0, + 0.0, 0.0, 0.0, + 1.0, 2.0, 1.0 ); float funny_kernel[9] = float[]( - 1.0f, 1.0f, 1.0f, - 1.0f, -4.0f, 1.0f, - 1.0f, 1.0f, 1.0f + 1.0, 1.0, 1.0, + 1.0, -4.0, 1.0, + 1.0, 1.0, 1.0 ); frag_color = kernel_effect(funny_kernel); - frag_color *= vec4(0.76f, 0.47f, 0.84f, 1.0f); + frag_color *= vec4(0.76, 0.47, 0.84, 1.0); } diff --git a/advanced_opengl/5.framebuffers/shaders/screen.vs b/advanced_opengl/5.framebuffers/shaders/screen.vs index 157dde2..8fdea55 100644 --- a/advanced_opengl/5.framebuffers/shaders/screen.vs +++ b/advanced_opengl/5.framebuffers/shaders/screen.vs @@ -9,6 +9,6 @@ uniform mat4 model; void main(void) { - gl_Position = model*vec4(apos, 1.0f); + gl_Position = model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/6.cubemaps/build.sh b/advanced_opengl/6.cubemaps/build.sh index bf1d66c..9bda0bc 100755 --- a/advanced_opengl/6.cubemaps/build.sh +++ b/advanced_opengl/6.cubemaps/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='cubemaps' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/6.cubemaps/cubemaps b/advanced_opengl/6.cubemaps/cubemaps Binary files differindex 661279d..bdf54f5 100755 --- a/advanced_opengl/6.cubemaps/cubemaps +++ b/advanced_opengl/6.cubemaps/cubemaps diff --git a/advanced_opengl/6.cubemaps/shaders/cube.vs b/advanced_opengl/6.cubemaps/shaders/cube.vs index 3c11932..aea5c2c 100644 --- a/advanced_opengl/6.cubemaps/shaders/cube.vs +++ b/advanced_opengl/6.cubemaps/shaders/cube.vs @@ -12,6 +12,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/6.cubemaps/shaders/reflection.vs b/advanced_opengl/6.cubemaps/shaders/reflection.vs index 34ad142..c9a0698 100644 --- a/advanced_opengl/6.cubemaps/shaders/reflection.vs +++ b/advanced_opengl/6.cubemaps/shaders/reflection.vs @@ -13,7 +13,7 @@ uniform mat4 projection; void main(void) { - pos = vec3(model*vec4(apos, 1.0f)); + pos = vec3(model*vec4(apos, 1.0)); normal = mat3(transpose(inverse(model)))*anormal; - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/6.cubemaps/shaders/refraction.fs b/advanced_opengl/6.cubemaps/shaders/refraction.fs index 8a8a8c5..ef8c415 100644 --- a/advanced_opengl/6.cubemaps/shaders/refraction.fs +++ b/advanced_opengl/6.cubemaps/shaders/refraction.fs @@ -11,7 +11,7 @@ uniform samplerCube skybox; void main(void) { - float ratio = 1.0f/1.52f; + float ratio = 1.0/1.52; 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 index 34ad142..c9a0698 100644 --- a/advanced_opengl/6.cubemaps/shaders/refraction.vs +++ b/advanced_opengl/6.cubemaps/shaders/refraction.vs @@ -13,7 +13,7 @@ uniform mat4 projection; void main(void) { - pos = vec3(model*vec4(apos, 1.0f)); + pos = vec3(model*vec4(apos, 1.0)); normal = mat3(transpose(inverse(model)))*anormal; - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/6.cubemaps/shaders/skybox.vs b/advanced_opengl/6.cubemaps/shaders/skybox.vs index 4259dc1..c790148 100644 --- a/advanced_opengl/6.cubemaps/shaders/skybox.vs +++ b/advanced_opengl/6.cubemaps/shaders/skybox.vs @@ -11,6 +11,6 @@ void main(void) { tex_coords = apos; - vec4 pos = projection*view*vec4(apos, 1.0f); + vec4 pos = projection*view*vec4(apos, 1.0); gl_Position = pos.xyww; } diff --git a/advanced_opengl/7.uniform_buffer/build.sh b/advanced_opengl/7.uniform_buffer/build.sh index d5620a3..54642df 100755 --- a/advanced_opengl/7.uniform_buffer/build.sh +++ b/advanced_opengl/7.uniform_buffer/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='uniform_buffer' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs b/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs index 8aa5add..fbbafbf 100644 --- a/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs +++ b/advanced_opengl/7.uniform_buffer/shaders/color_cube.fs @@ -5,5 +5,5 @@ out vec4 frag_color; void main(void) { - frag_color = vec4(1.0f, 0.0f, 0.0f, 1.0f); + frag_color = vec4(1.0, 0.0, 0.0, 1.0); } diff --git a/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs b/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs index 630242d..17a3448 100644 --- a/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs +++ b/advanced_opengl/7.uniform_buffer/shaders/color_cube.vs @@ -12,5 +12,5 @@ uniform mat4 model; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/7.uniform_buffer/shaders/cube.vs b/advanced_opengl/7.uniform_buffer/shaders/cube.vs index cb0b7bf..054164d 100644 --- a/advanced_opengl/7.uniform_buffer/shaders/cube.vs +++ b/advanced_opengl/7.uniform_buffer/shaders/cube.vs @@ -16,6 +16,6 @@ out vec2 tex_coords; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/7.uniform_buffer/shaders/skybox.vs b/advanced_opengl/7.uniform_buffer/shaders/skybox.vs index 36a07ed..03b2171 100644 --- a/advanced_opengl/7.uniform_buffer/shaders/skybox.vs +++ b/advanced_opengl/7.uniform_buffer/shaders/skybox.vs @@ -14,6 +14,6 @@ void main(void) { tex_coords = apos; - vec4 pos = projection*view*vec4(apos, 1.0f); + vec4 pos = projection*view*vec4(apos, 1.0); gl_Position = pos.xyww; } diff --git a/advanced_opengl/7.uniform_buffer/uniform_buffer b/advanced_opengl/7.uniform_buffer/uniform_buffer Binary files differindex e2b3e42..57aa64d 100755 --- a/advanced_opengl/7.uniform_buffer/uniform_buffer +++ b/advanced_opengl/7.uniform_buffer/uniform_buffer diff --git a/advanced_opengl/8.geometry_shader/build.sh b/advanced_opengl/8.geometry_shader/build.sh index fc81331..f3339cb 100755 --- a/advanced_opengl/8.geometry_shader/build.sh +++ b/advanced_opengl/8.geometry_shader/build.sh @@ -2,4 +2,4 @@ . ../../config TARGET='geometry_shader' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/8.geometry_shader/geometry_shader b/advanced_opengl/8.geometry_shader/geometry_shader Binary files differindex f245068..77fcab4 100755 --- a/advanced_opengl/8.geometry_shader/geometry_shader +++ b/advanced_opengl/8.geometry_shader/geometry_shader diff --git a/advanced_opengl/8.geometry_shader/shaders/base.fs b/advanced_opengl/8.geometry_shader/shaders/base.fs index 283d885..73897db 100644 --- a/advanced_opengl/8.geometry_shader/shaders/base.fs +++ b/advanced_opengl/8.geometry_shader/shaders/base.fs @@ -7,5 +7,5 @@ out vec4 frag_color; void main(void) { - frag_color = vec4(fcolor, 1.0f); + frag_color = vec4(fcolor, 1.0); } diff --git a/advanced_opengl/8.geometry_shader/shaders/base.vs b/advanced_opengl/8.geometry_shader/shaders/base.vs index 2924e6c..9ad9e7a 100644 --- a/advanced_opengl/8.geometry_shader/shaders/base.vs +++ b/advanced_opengl/8.geometry_shader/shaders/base.vs @@ -10,5 +10,5 @@ void main(void) { vs_out.color = acolor; - gl_Position = vec4(apos, 0.0f, 1.0f); + gl_Position = vec4(apos, 0.0, 1.0); } diff --git a/advanced_opengl/8.geometry_shader/shaders/basic.vs b/advanced_opengl/8.geometry_shader/shaders/basic.vs index 9a630b6..77e8b70 100644 --- a/advanced_opengl/8.geometry_shader/shaders/basic.vs +++ b/advanced_opengl/8.geometry_shader/shaders/basic.vs @@ -11,6 +11,6 @@ uniform mat4 projection; void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/8.geometry_shader/shaders/explosion.gs b/advanced_opengl/8.geometry_shader/shaders/explosion.gs index 1883ad2..3498878 100644 --- a/advanced_opengl/8.geometry_shader/shaders/explosion.gs +++ b/advanced_opengl/8.geometry_shader/shaders/explosion.gs @@ -21,9 +21,9 @@ get_normal(void) 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); + float magnitude = 0.2; + vec3 direction = normal*magnitude*((sin(time)+1.0)/2.0); + vec4 result = position+vec4(direction, 0.0); return(result); } diff --git a/advanced_opengl/8.geometry_shader/shaders/explosion.vs b/advanced_opengl/8.geometry_shader/shaders/explosion.vs index 5925b8f..46abd27 100644 --- a/advanced_opengl/8.geometry_shader/shaders/explosion.vs +++ b/advanced_opengl/8.geometry_shader/shaders/explosion.vs @@ -14,5 +14,5 @@ void main(void) { vs_out.gtex_coords = atex_coords; - gl_Position = projection*view*model*vec4(apos, 1.0f); + gl_Position = projection*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/9.instancing/build.sh b/advanced_opengl/9.instancing/build.sh index 40b314b..ebb6a01 100755 --- a/advanced_opengl/9.instancing/build.sh +++ b/advanced_opengl/9.instancing/build.sh @@ -1,5 +1,6 @@ #!/bin/sh . ../../config +CFLAGS='-O2' TARGET='instancing' set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/advanced_opengl/9.instancing/instancing b/advanced_opengl/9.instancing/instancing Binary files differindex 08b099d..c722860 100755 --- a/advanced_opengl/9.instancing/instancing +++ b/advanced_opengl/9.instancing/instancing diff --git a/advanced_opengl/9.instancing/instancing.c b/advanced_opengl/9.instancing/instancing.c index 90647fb..f24a258 100644 --- a/advanced_opengl/9.instancing/instancing.c +++ b/advanced_opengl/9.instancing/instancing.c @@ -322,9 +322,10 @@ main(void) } break; case 2: { - camera_pos = update_camera_orbital(global_input, - camera_pos, v3f_zero(), - dt, camera_speed); + V3F camera_dv = get_dv_camera_orbital(&global_input, + camera_pos, v3f_zero(), + dt, camera_speed); + camera_pos = v3f_add(camera_pos, camera_dv); projection = perspective(fovx, (F32)global_width/(F32)global_height, near, far); V3F world_up = v3f(0.0f, 1.0f, 0.0f); @@ -350,9 +351,10 @@ main(void) } break; case 3: { - camera_pos = update_camera_orbital(global_input, - camera_pos, v3f_zero(), - dt, camera_speed); + V3F camera_dv = get_dv_camera_orbital(&global_input, + camera_pos, v3f_zero(), + dt, camera_speed); + camera_pos = v3f_add(camera_pos, camera_dv); projection = perspective(fovx, (F32)global_width/(F32)global_height, near, far); V3F world_up = v3f(0.0f, 1.0f, 0.0f); diff --git a/advanced_opengl/9.instancing/shaders/instanced_arrays.vs b/advanced_opengl/9.instancing/shaders/instanced_arrays.vs index e7bbeb7..4ea0fe4 100644 --- a/advanced_opengl/9.instancing/shaders/instanced_arrays.vs +++ b/advanced_opengl/9.instancing/shaders/instanced_arrays.vs @@ -9,6 +9,6 @@ void main(void) { color = acolor; - vec2 pos = apos*(gl_InstanceID/100.0f); - gl_Position = vec4(pos+aoffset, 0.0f, 1.0f); + vec2 pos = apos*(gl_InstanceID/100.0); + gl_Position = vec4(pos+aoffset, 0.0, 1.0); } diff --git a/advanced_opengl/9.instancing/shaders/instanced_mat4.vs b/advanced_opengl/9.instancing/shaders/instanced_mat4.vs index 5ab3a81..c65be90 100644 --- a/advanced_opengl/9.instancing/shaders/instanced_mat4.vs +++ b/advanced_opengl/9.instancing/shaders/instanced_mat4.vs @@ -12,5 +12,5 @@ void main(void) { tex_coords = atex_coords; - gl_Position = projection*view*instance_matrix*vec4(apos, 1.0f); + gl_Position = projection*view*instance_matrix*vec4(apos, 1.0); } diff --git a/advanced_opengl/build.sh b/advanced_opengl/build.sh new file mode 100755 index 0000000..4829c7d --- /dev/null +++ b/advanced_opengl/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh +targets=$(ls) +for target in $targets ; do + if [ $target != 'build.sh' ] ; then + cd $target + ./build.sh + cd .. + fi +done diff --git a/data/models/cube.obj b/data/models/cube.obj index f40e456..6d52fd0 100644 --- a/data/models/cube.obj +++ b/data/models/cube.obj @@ -16,25 +16,26 @@ vn -1.0000 -0.0000 -0.0000 vn -0.0000 -1.0000 -0.0000 vn 1.0000 -0.0000 -0.0000 vn -0.0000 -0.0000 -1.0000 -vt 0.625000 0.500000 -vt 0.875000 0.500000 -vt 0.875000 0.750000 -vt 0.625000 0.750000 -vt 0.375000 0.750000 -vt 0.625000 1.000000 -vt 0.375000 1.000000 -vt 0.375000 0.000000 -vt 0.625000 0.000000 -vt 0.625000 0.250000 -vt 0.375000 0.250000 -vt 0.125000 0.500000 -vt 0.375000 0.500000 -vt 0.125000 0.750000 +vt 0.999900 0.999900 +vt 0.000100 0.999900 +vt 0.000100 0.000100 +vt 0.999900 0.000100 +vt 0.000100 0.000100 +vt 0.999900 0.000100 +vt 0.999900 0.999900 +vt 0.000100 0.999900 +vt 0.999900 0.999900 +vt 0.000100 0.999900 +vt 0.000100 0.000100 +vt 0.999900 0.000100 +vt 0.999900 0.000100 +vt 0.000100 0.999900 +vt 0.000100 0.000100 s 0 usemtl Material f 1/1/1 5/2/1 7/3/1 3/4/1 -f 4/5/2 3/4/2 7/6/2 8/7/2 -f 8/8/3 7/9/3 5/10/3 6/11/3 -f 6/12/4 2/13/4 4/5/4 8/14/4 -f 2/13/5 1/1/5 3/4/5 4/5/5 -f 6/11/6 5/10/6 1/1/6 2/13/6 +f 4/5/2 3/6/2 7/7/2 8/8/2 +f 8/9/3 7/10/3 5/11/3 6/12/3 +f 6/13/4 2/7/4 4/14/4 8/15/4 +f 2/5/5 1/6/5 3/7/5 4/8/5 +f 6/9/6 5/10/6 1/11/6 2/12/6 diff --git a/data/textures/oak_veneer_01_ao_4k.jpg b/data/textures/oak_veneer_01_ao_4k.jpg Binary files differnew file mode 100644 index 0000000..98522ef --- /dev/null +++ b/data/textures/oak_veneer_01_ao_4k.jpg diff --git a/data/textures/wood.png b/data/textures/wood.png Binary files differnew file mode 100644 index 0000000..c785f41 --- /dev/null +++ b/data/textures/wood.png diff --git a/libs/common.h b/libs/common.h index 481e356..c7513a2 100644 --- a/libs/common.h +++ b/libs/common.h @@ -329,8 +329,7 @@ load_texture(char *texture_filename) format = GL_RGBA; glBindTexture(GL_TEXTURE_2D, texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, - GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -350,6 +349,52 @@ load_texture(char *texture_filename) } U32 +load_texture_gamma(char *filename, B32 gamma_correction) +{ + S32 width, height, number_channels; + U32 texture_id; + + glGenTextures(1, &texture_id); + + stbi_set_flip_vertically_on_load(1); + U8 *data = stbi_load(filename, &width, &height, &number_channels, 0); + if (data) { + GLenum internal_format, data_format; + if (number_channels == 1) + { + internal_format = data_format = GL_RED; + } + else if (number_channels == 3) + { + internal_format = gamma_correction ? GL_SRGB : GL_RGB; + data_format = GL_RGB; + } + else if (number_channels == 4) + { + internal_format = gamma_correction ? GL_SRGB_ALPHA : GL_RGBA; + data_format = GL_RGBA; + } + + glBindTexture(GL_TEXTURE_2D, texture_id); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, + 0, data_format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + fprintf(stdout, "[INFO]: Texture (\"%s\") is loaded successfully\n", filename); + } else { + fprintf(stderr, "[ERROR]: Failed to load texture: \"%s\"\n", filename); + } + stbi_image_free(data); + + return(texture_id); +} + +U32 load_cubemap(const char *texture_filenames[6]) { U32 texture_id; @@ -433,6 +478,38 @@ input_update_last_state(Input *input) input->exit.last = input->exit.state; } +void +process_glfw_key(GLFWwindow *window, S32 glfw_keycode, Key *key) +{ + if (glfwGetKey(window, glfw_keycode) == GLFW_PRESS) + key->state = KeyState_PRESS; + else + key->state = KeyState_RELEASE; +} + +void +process_glfw_keyboard(GLFWwindow *window, Input *input) +{ + process_glfw_key(window, GLFW_KEY_D, &input->move_right); + process_glfw_key(window, GLFW_KEY_W, &input->move_forward); + process_glfw_key(window, GLFW_KEY_A, &input->move_left); + process_glfw_key(window, GLFW_KEY_S, &input->move_backward); + process_glfw_key(window, GLFW_KEY_E, &input->move_up); + process_glfw_key(window, GLFW_KEY_Q, &input->move_down); + process_glfw_key(window, GLFW_KEY_SPACE, &input->jump); + process_glfw_key(window, GLFW_KEY_RIGHT, &input->action_right); + process_glfw_key(window, GLFW_KEY_UP, &input->action_up); + process_glfw_key(window, GLFW_KEY_LEFT, &input->action_left); + process_glfw_key(window, GLFW_KEY_DOWN, &input->action_down); + process_glfw_key(window, GLFW_KEY_ESCAPE, &input->exit); +} + +void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "[ERROR] GLFW: %s\n", desc); +} + B32 key_is_pressed(Key key) { @@ -637,11 +714,24 @@ typedef struct { F32 fovx; F32 near; F32 far; + + /* NOTE(pryazha): In degrees */ + F32 yaw; + F32 pitch; } Camera; +MAT4 +get_view_matrix(Camera *camera) +{ + MAT4 view; + view = mat4_make_translate(v3f_negate(camera->pos)); + view = mat4_rotate_angles(view, v3f(camera->pitch, camera->yaw, 0.0f)); + return(view); +} + V3F get_dv_camera_orbital(Input *input, V3F pos, V3F target, - F32 dt, F32 speed) + F32 dt, F32 acceleration) { V3F up, f, l, u, dv; @@ -652,7 +742,6 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_zero(); - /* if (key_is_pressed(input->move_right)) dv = v3f_sub(dv, l); if (key_is_pressed(input->move_forward)) @@ -665,13 +754,35 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_add(dv, u); if (key_is_pressed(input->move_down)) dv = v3f_sub(dv, u); - */ + + dv = v3f_norm(dv); + + dv = v3f_scalef(dv, acceleration*dt); + + return(dv); +} + +void +get_camera_vectors(Camera *camera, V3F *l, V3F *u, V3F *f) +{ + MAT4 view; + view = get_view_matrix(camera); + *l = v3f(-view.m0.x, -view.m1.x, -view.m2.x); + *u = v3f(view.m0.y, view.m1.y, view.m2.y); + *f = v3f(-view.m0.z, -view.m1.z, -view.m2.z); +} + +V3F +get_dv_camera_first_person(Input *input, Camera *camera, + F32 acceleration, F32 dt) +{ + V3F f, l, u, dv; + get_camera_vectors(camera, &l, &u, &f); + + dv = v3f_zero(); + if (key_is_pressed(input->move_right)) - { dv = v3f_sub(dv, l); - dv = v3f_add(dv, f); - dv = v3f_norm(dv); - } if (key_is_pressed(input->move_forward)) dv = v3f_add(dv, f); if (key_is_pressed(input->move_left)) @@ -682,15 +793,16 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_add(dv, u); if (key_is_pressed(input->move_down)) dv = v3f_sub(dv, u); - - dv = v3f_scalef(dv, speed*dt); + + dv = v3f_norm(dv); + + dv = v3f_scalef(dv, acceleration*dt); return(dv); } typedef struct { Camera camera; - Mesh *mesh; F32 dt; } State; |