diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-08-02 13:21:42 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-08-02 13:21:42 +0500 |
commit | 2e64d3c5d6eb1eb04168d39d5eb5f2d89af1a8b0 (patch) | |
tree | 85c8a586a1343469cdcdab5dccad023053571ecd /pbr/1.1.lighting | |
parent | 33d5f67044d104d69cb2d11e78e6a79bc20d4c4e (diff) |
Diffstat (limited to 'pbr/1.1.lighting')
-rwxr-xr-x | pbr/1.1.lighting/build.sh | 5 | ||||
-rw-r--r-- | pbr/1.1.lighting/default.frag | 15 | ||||
-rw-r--r-- | pbr/1.1.lighting/default.vert | 17 | ||||
-rw-r--r-- | pbr/1.1.lighting/hdr.frag | 18 | ||||
-rw-r--r-- | pbr/1.1.lighting/hdr.vert | 17 | ||||
-rwxr-xr-x | pbr/1.1.lighting/lighting | bin | 401936 -> 0 bytes | |||
-rw-r--r-- | pbr/1.1.lighting/lighting.c | 157 | ||||
-rw-r--r-- | pbr/1.1.lighting/pbr.frag | 105 | ||||
-rw-r--r-- | pbr/1.1.lighting/pbr.vert | 21 |
9 files changed, 0 insertions, 355 deletions
diff --git a/pbr/1.1.lighting/build.sh b/pbr/1.1.lighting/build.sh deleted file mode 100755 index 2cd71b7..0000000 --- a/pbr/1.1.lighting/build.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -. ../../config -TARGET='lighting' -set -x -gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS diff --git a/pbr/1.1.lighting/default.frag b/pbr/1.1.lighting/default.frag deleted file mode 100644 index 9351a1b..0000000 --- a/pbr/1.1.lighting/default.frag +++ /dev/null @@ -1,15 +0,0 @@ -#version 330 core - -in vert_t { - vec2 tex_coords; -} vert; - -out vec4 frag_color; - -uniform sampler2D colorbuffer; - -void main(void) -{ - vec3 color = vec3(texture(colorbuffer, vert.tex_coords)); - frag_color = vec4(color, 1.0); -} diff --git a/pbr/1.1.lighting/default.vert b/pbr/1.1.lighting/default.vert deleted file mode 100644 index 1b953d7..0000000 --- a/pbr/1.1.lighting/default.vert +++ /dev/null @@ -1,17 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 position; -layout(location = 2) in vec2 tex_coords; - -out vert_t { - vec2 tex_coords; -} vert; - -uniform mat4 model; - -void main(void) -{ - gl_Position = model * vec4(position, 1.0); - - vert.tex_coords = tex_coords; -} diff --git a/pbr/1.1.lighting/hdr.frag b/pbr/1.1.lighting/hdr.frag deleted file mode 100644 index 01650ae..0000000 --- a/pbr/1.1.lighting/hdr.frag +++ /dev/null @@ -1,18 +0,0 @@ -#version 330 core - -in vert_t { - vec2 tex_coords; -} vert; - -out vec4 frag_color; - -uniform sampler2D colorbuffer; - -void main(void) -{ - const float gamma = 2.2; - vec3 color = vec3(texture(colorbuffer, vert.tex_coords)); - color /= color + vec3(1.0); - color = pow(color, vec3(1.0 / gamma)); - frag_color = vec4(color, 1.0); -} diff --git a/pbr/1.1.lighting/hdr.vert b/pbr/1.1.lighting/hdr.vert deleted file mode 100644 index 1b953d7..0000000 --- a/pbr/1.1.lighting/hdr.vert +++ /dev/null @@ -1,17 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 position; -layout(location = 2) in vec2 tex_coords; - -out vert_t { - vec2 tex_coords; -} vert; - -uniform mat4 model; - -void main(void) -{ - gl_Position = model * vec4(position, 1.0); - - vert.tex_coords = tex_coords; -} diff --git a/pbr/1.1.lighting/lighting b/pbr/1.1.lighting/lighting Binary files differdeleted file mode 100755 index a68bd2b..0000000 --- a/pbr/1.1.lighting/lighting +++ /dev/null diff --git a/pbr/1.1.lighting/lighting.c b/pbr/1.1.lighting/lighting.c deleted file mode 100644 index d2a941d..0000000 --- a/pbr/1.1.lighting/lighting.c +++ /dev/null @@ -1,157 +0,0 @@ -#include "GL/glew.h" -#include "GLFW/glfw3.h" -#include "common.h" - -int main(void) -{ - state_t state = init_state(1600, 800); - - init_glfw(&state); - init_gl(); - - /* meshes */ - Mesh *sphere = mesh_load_obj(state.arena, "../../data/models/sphere.obj"); - Mesh *quad = mesh_gen_quad(state.arena); - - /* shaders */ - add_shader("default.vert", "default.frag", 0); - add_shader("pbr.vert", "pbr.frag", 0); - add_shader("hdr.vert", "hdr.frag", 0); - - light_t lights[4] = { - {{-6.0f, 0.0f, 7.0f}, {300.0f, 300.0f, 300.0f}}, - {{-4.0f, 0.0f, 6.0f}, {300.0f, 300.0f, 300.0f}}, - {{ 1.0f, 0.0f, 5.0f}, {300.0f, 300.0f, 300.0f}}, - {{ 3.0f, 0.0f, 8.0f}, {300.0f, 300.0f, 300.0f}}, - }; - - /* framebuffers */ - U32 hdr_fbo, hdr_colorbuffer, hdr_rbo; - glGenFramebuffers(1, &hdr_fbo); - glGenTextures(1, &hdr_colorbuffer); - glBindTexture(GL_TEXTURE_2D, hdr_colorbuffer); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, state.width, state.height, 0, GL_RGBA, GL_FLOAT, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glGenRenderbuffers(1, &hdr_rbo); - glBindRenderbuffer(GL_RENDERBUFFER, hdr_rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, state.width, state.height); - glBindFramebuffer(GL_FRAMEBUFFER, hdr_fbo); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, hdr_colorbuffer, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, hdr_rbo); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - die("failed to create framebuffer"); - glBindRenderbuffer(GL_RENDERBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - F32 time = 0; - - S32 show_gamma = 0; - - while (!glfwWindowShouldClose(state.window)) { - handle_glfw_events(state.window, &state.input); - F32 dt = lock_framerate(60); - fps_info(dt, 10); - - /* update */ - update_camera_first_person(&state.camera, &state.input, dt, 2.0f); - F32 limit = 4.0f; - for (U32 i = 0; i < (sizeof(lights) / sizeof(lights[0])); i++) - lights[i].position.y = sinf(time + i) * limit; - time += dt; - - if (key_first_press(state.input.jump)) - show_gamma = 1 - show_gamma; - - /* render */ - F32 ar = 0; - if (show_gamma) { - ar = (F32)state.width / 2.0f / (F32)state.height; - } else { - ar = (F32)state.width / (F32)state.height; - } - MAT4 projection = camera_persp(state.camera, ar); - MAT4 view = get_view_matrix(&state.camera); - - glClearColor(0.16f, 0.16f, 0.16f, 1.0f); - - glBindFramebuffer(GL_FRAMEBUFFER, hdr_fbo); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - U32 shader = find_shader("pbr"); - if (!shader) die("wha"); - glUseProgram(shader); - shader_set_mat4fv(shader, "projection", projection); - shader_set_mat4fv(shader, "view", view); - shader_set_3fv(shader, "camera", state.camera.pos); - shader_set_3fv(shader, "color", v3f(0.04, 0.04, 0.04)); - shader_set_1f(shader, "ao", 1.0f); - for (U32 i = 0; i < (sizeof(lights) / sizeof(lights[0])); i++) { - char str[512]; - snprintf(str, 512, "lights[%d].position", i); - shader_set_3fv(shader, str, lights[i].position); - snprintf(str, 512, "lights[%d].color", i); - shader_set_3fv(shader, str, lights[i].color); - } - S32 rows = 7; - S32 cols = 7; - F32 offset = 3.0f; - for (S32 row = 0; row < rows; row++) { - shader_set_1f(shader, "metallic", (F32)row / (F32)rows); - for (S32 col = 0; col < cols; col++) { - shader_set_1f(shader, "roughness", clamp(0.05f, (F32)col / (F32)cols, 1.0f)); - V3F pos = {(col - cols / 2.0f) * offset, (row - rows / 2.0f) * offset, 0.0f}; - MAT4 model = mat4_make_translate(pos); - shader_set_mat4fv(shader, "model", model); - mesh_draw(sphere); - } - } - - for (U32 i = 0; i < (sizeof(lights) / sizeof(lights[0])); i++) { - MAT4 model = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f)); - model = mat4_translate(model, lights[i].position); - shader_set_mat4fv(shader, "model", model); - mesh_draw(sphere); - } - - glUseProgram(0); - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - if (show_gamma) { - shader = find_shader("default"); - if (!shader) die("!"); - glUseProgram(shader); - glBindTexture(GL_TEXTURE_2D, hdr_colorbuffer); - MAT4 model = mat4_make_scale(v3f(0.5f, 1.0f, 1.0f)); - model = mat4_translate(model, v3f(-0.5f, 0.0f, 0.0f)); - shader_set_mat4fv(shader, "model", model); - mesh_draw(quad); - glUseProgram(0); - - shader = find_shader("hdr"); - if (!shader) die("!"); - glUseProgram(shader); - glBindTexture(GL_TEXTURE_2D, hdr_colorbuffer); - model = mat4_make_scale(v3f(0.5f, 1.0f, 1.0f)); - model = mat4_translate(model, v3f(0.5f, 0.0f, 0.0f)); - shader_set_mat4fv(shader, "model", model); - mesh_draw(quad); - glUseProgram(0); - } else { - shader = find_shader("hdr"); - if (!shader) die("!"); - glUseProgram(shader); - glBindTexture(GL_TEXTURE_2D, hdr_colorbuffer); - shader_set_mat4fv(shader, "model", mat4_identity()); - mesh_draw(quad); - glUseProgram(0); - } - - glfwSwapBuffers(state.window); - } - - return 0; -} diff --git a/pbr/1.1.lighting/pbr.frag b/pbr/1.1.lighting/pbr.frag deleted file mode 100644 index db9edea..0000000 --- a/pbr/1.1.lighting/pbr.frag +++ /dev/null @@ -1,105 +0,0 @@ -#version 330 core - -in vert_t { - vec3 position; - vec3 normal; -} vert; - -out vec4 frag_color; - -uniform vec3 camera; - -uniform vec3 color; -uniform float metallic; -uniform float roughness; -uniform float ao; - -struct light_t { - vec3 position; - vec3 color; -}; - -const int light_count = 4; -uniform light_t lights[light_count]; - -const float PI = 3.14159265358; - -vec3 fresnel_shlick(float costheta, vec3 f0) -{ - return f0 + (1.0 - f0) * pow(1.0 - costheta, 5.0); -} - -float distribution_ggx(vec3 n, vec3 h, float roughness) -{ - float a = roughness * roughness; - float a2 = a * a; - float ndoth = max(dot(n, h), 0.0); - float ndoth2 = ndoth * ndoth; - - float num = a2; - float denom = (ndoth2 * (a2 - 1.0) + 1.0); - denom = PI * denom * denom; - - return num / denom; -} - -float geometry_shlick_ggx(float ndotv, float roughness) -{ - float r = roughness + 1.0; - float k = r * r / 8.0; - - float num = ndotv; - float denom = ndotv * (1.0 - k) + k; - - return num / denom; -} - -float geometry_smith(vec3 n, vec3 v, vec3 l, float roughness) -{ - float ndotv = max(dot(n, v), 0.0); - float ndotl = max(dot(n, l), 0.0); - float ggx2 = geometry_shlick_ggx(ndotv, roughness); - float ggx1 = geometry_shlick_ggx(ndotl, roughness); - - return ggx1 * ggx2; -} - -void main(void) -{ - vec3 n = vert.normal; - vec3 v = normalize(camera - vert.position); - - vec3 lo = vec3(0.0); - for (int i = 0; i < light_count; i++) { - vec3 l = normalize(lights[i].position - vert.position); - vec3 h = normalize(v + l); - float ndotv = max(dot(n, v), 0.0); - float ndotl = max(dot(n, l), 0.0); - - float distance = length(lights[i].position - vert.position); - float attenuation = 1.0 / (distance * distance); - - vec3 radiance = lights[i].color * attenuation; - - vec3 f0 = vec3(0.04); - f0 = mix(f0, color, metallic); - vec3 f = fresnel_shlick(max(dot(h, v), 0.0), f0); - - float ndf = distribution_ggx(n, h, roughness); - float g = geometry_smith(n, v, l, roughness); - - vec3 numerator = ndf * g * f; - float denominator = 4.0 * ndotv * ndotl; - vec3 specular = numerator / max(denominator, 0.001); - - vec3 ks = f; - vec3 kd = vec3(1.0) - ks; - - kd *= 1.0 - metallic; - - lo += (kd * color / PI + specular) * radiance * ndotl; - } - - vec3 ambient = vec3(0.03) * color * ao; - frag_color = vec4(ambient + lo, 1.0); -} diff --git a/pbr/1.1.lighting/pbr.vert b/pbr/1.1.lighting/pbr.vert deleted file mode 100644 index e6e3800..0000000 --- a/pbr/1.1.lighting/pbr.vert +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 position; -layout(location = 1) in vec3 normal; - -out vert_t { - vec3 position; - vec3 normal; -} vert; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; - -void main(void) -{ - gl_Position = projection * view * model * vec4(position, 1.0); - - vert.position = vec3(model * vec4(position, 1.0)); - vert.normal = normalize(mat3(transpose(inverse(model))) * normal); -} |