summaryrefslogtreecommitdiff
path: root/advanced_lighting
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-01-23 19:39:54 +0500
committerpryazha <pryadeiniv@mail.ru>2025-01-23 19:39:54 +0500
commitbf1c59565096ac9774493846cfb15e259d3b0e66 (patch)
tree82e8182af128580b6f38437feccba8c0f48abdc1 /advanced_lighting
parent1ee094199af9c169e1ccaa53c7b0c186c4a3639e (diff)
restructure
Diffstat (limited to 'advanced_lighting')
-rw-r--r--advanced_lighting/blinn_phong/.blinn_phong.c.swpbin0 -> 16384 bytes
-rwxr-xr-xadvanced_lighting/blinn_phong/blinn_phongbin0 -> 1278128 bytes
-rw-r--r--advanced_lighting/blinn_phong/blinn_phong.c210
-rwxr-xr-xadvanced_lighting/blinn_phong/build.sh5
-rw-r--r--advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swpbin0 -> 12288 bytes
-rw-r--r--advanced_lighting/blinn_phong/shaders/blinn_phong.fs62
-rw-r--r--advanced_lighting/blinn_phong/shaders/blinn_phong.vs21
7 files changed, 298 insertions, 0 deletions
diff --git a/advanced_lighting/blinn_phong/.blinn_phong.c.swp b/advanced_lighting/blinn_phong/.blinn_phong.c.swp
new file mode 100644
index 0000000..d1ba9b1
--- /dev/null
+++ b/advanced_lighting/blinn_phong/.blinn_phong.c.swp
Binary files differ
diff --git a/advanced_lighting/blinn_phong/blinn_phong b/advanced_lighting/blinn_phong/blinn_phong
new file mode 100755
index 0000000..1c89f59
--- /dev/null
+++ b/advanced_lighting/blinn_phong/blinn_phong
Binary files differ
diff --git a/advanced_lighting/blinn_phong/blinn_phong.c b/advanced_lighting/blinn_phong/blinn_phong.c
new file mode 100644
index 0000000..88bd294
--- /dev/null
+++ b/advanced_lighting/blinn_phong/blinn_phong.c
@@ -0,0 +1,210 @@
+#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 };
+
+void
+key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
+{
+ switch (action)
+ {
+ case GLFW_PRESS: {
+ switch (key)
+ {
+ case GLFW_KEY_D: {
+ global_input.move_right.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_W: {
+ global_input.move_forward.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_A: {
+ global_input.move_left.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_S: {
+ global_input.move_backward.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_E: {
+ global_input.move_up.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_Q: {
+ global_input.move_down.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_SPACE: {
+ global_input.jump.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_RIGHT: {
+ global_input.action_right.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_LEFT: {
+ global_input.action_left.state = KeyState_PRESS;
+ } break;
+ case GLFW_KEY_ESCAPE: {
+ global_input.exit.state = KeyState_PRESS;
+ } break;
+ }
+ } break;
+
+ case GLFW_RELEASE: {
+ switch (key)
+ {
+ case GLFW_KEY_D: {
+ global_input.move_right.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_W: {
+ global_input.move_forward.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_A: {
+ global_input.move_left.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_S: {
+ global_input.move_backward.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_E: {
+ global_input.move_up.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_Q: {
+ global_input.move_down.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_SPACE: {
+ global_input.jump.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_RIGHT: {
+ global_input.action_right.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_LEFT: {
+ global_input.action_left.state = KeyState_RELEASE;
+ } break;
+ case GLFW_KEY_ESCAPE: {
+ global_input.exit.state = KeyState_RELEASE;
+ } break;
+ }
+ } break;
+ }
+}
+
+void
+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);
+ 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);
+
+ input_update_last_state(&global_input);
+}
+
+void
+render(State *state, U32 shader, U32 texture)
+{
+ MAT4 projection, view, model;
+
+ 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();
+
+ glUseProgram(shader);
+ shader_set_mat4fv(shader, "projection", projection);
+ shader_set_mat4fv(shader, "view", view);
+ shader_set_mat4fv(shader, "model", model);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ mesh_draw(state->mesh);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glUseProgram(0);
+}
+
+int
+main(void)
+{
+ GLFWwindow *window;
+ Arena *arena;
+ State state;
+ 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_width, global_height, "Blinn-Phong shading", 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);
+
+ 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 };
+
+ 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");
+
+ last_time = glfwGetTime();
+
+ while (!glfwWindowShouldClose(window))
+ {
+ glfwPollEvents();
+ if (key_first_press(global_input.exit))
+ glfwSetWindowShouldClose(window, GLFW_TRUE);
+ update(&state);
+ render(&state, blinn_phong_shader, mesh_texture_diffuse);
+ 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/blinn_phong/build.sh b/advanced_lighting/blinn_phong/build.sh
new file mode 100755
index 0000000..21a65b4
--- /dev/null
+++ b/advanced_lighting/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 && ./$TARGET
diff --git a/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp b/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp
new file mode 100644
index 0000000..cfc93b0
--- /dev/null
+++ b/advanced_lighting/blinn_phong/shaders/.blinn_phong.fs.swp
Binary files differ
diff --git a/advanced_lighting/blinn_phong/shaders/blinn_phong.fs b/advanced_lighting/blinn_phong/shaders/blinn_phong.fs
new file mode 100644
index 0000000..23e2968
--- /dev/null
+++ b/advanced_lighting/blinn_phong/shaders/blinn_phong.fs
@@ -0,0 +1,62 @@
+#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
new file mode 100644
index 0000000..d4e5dbc
--- /dev/null
+++ b/advanced_lighting/blinn_phong/shaders/blinn_phong.vs
@@ -0,0 +1,21 @@
+#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);
+}