summaryrefslogtreecommitdiff
path: root/advanced_lighting/9.ssao
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-06-15 16:11:31 +0500
committerpryazha <pryadeiniv@mail.ru>2025-06-15 16:11:31 +0500
commit9d944f26d359e4bc1ffd8e44350b0df9f0935b18 (patch)
tree9759a83a76ab741a587b99f00c2463eafd05d813 /advanced_lighting/9.ssao
parentd65ddd07a43d5ffdcf2ddf90d6f86626cf9b92d8 (diff)
something
Diffstat (limited to 'advanced_lighting/9.ssao')
-rwxr-xr-xadvanced_lighting/9.ssao/build5
-rw-r--r--advanced_lighting/9.ssao/deferred.frag44
-rw-r--r--advanced_lighting/9.ssao/deferred.vert16
-rw-r--r--advanced_lighting/9.ssao/gbuffer.frag17
-rw-r--r--advanced_lighting/9.ssao/gbuffer.vert21
-rw-r--r--advanced_lighting/9.ssao/occlusion.frag14
-rw-r--r--advanced_lighting/9.ssao/screen.frag14
-rw-r--r--advanced_lighting/9.ssao/screen.vert16
-rwxr-xr-xadvanced_lighting/9.ssao/ssaobin0 -> 1291232 bytes
-rw-r--r--advanced_lighting/9.ssao/ssao.c331
-rw-r--r--advanced_lighting/9.ssao/ssao.frag45
-rw-r--r--advanced_lighting/9.ssao/ssao.vert16
-rw-r--r--advanced_lighting/9.ssao/xoshiro128plus.c28
13 files changed, 567 insertions, 0 deletions
diff --git a/advanced_lighting/9.ssao/build b/advanced_lighting/9.ssao/build
new file mode 100755
index 0000000..dc6929a
--- /dev/null
+++ b/advanced_lighting/9.ssao/build
@@ -0,0 +1,5 @@
+#!/bin/sh
+. ../../config
+TARGET='ssao'
+set -x
+gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS
diff --git a/advanced_lighting/9.ssao/deferred.frag b/advanced_lighting/9.ssao/deferred.frag
new file mode 100644
index 0000000..5a3cc33
--- /dev/null
+++ b/advanced_lighting/9.ssao/deferred.frag
@@ -0,0 +1,44 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out vec4 frag_color;
+
+uniform sampler2D positions;
+uniform sampler2D normals;
+uniform sampler2D albedo;
+uniform sampler2D ssao;
+
+uniform vec3 view_position;
+
+struct light_t {
+ vec3 position;
+ vec3 color;
+};
+
+uniform light_t light;
+
+const float linear = 0.7f;
+const float quadratic = 1.8f;
+
+void main()
+{
+ vec3 position = texture(positions, vert.tex_coords).xyz;
+ vec3 normal = texture(normals, vert.tex_coords).xyz;
+ vec3 color = texture(albedo, vert.tex_coords).rgb;
+ float occlusion = texture(ssao, vert.tex_coords).r;
+
+ vec3 ambient = 0.1 * color * occlusion;
+ vec3 result = ambient;
+ vec3 view_dir = normalize(-position);
+ vec3 light_dir = normalize(light.position - position);
+ vec3 diffuse = max(dot(normal, light_dir), 0.0) * color * light.color;
+ vec3 halfway_dir = normalize(view_dir + light_dir);
+ vec3 specular = pow(max(dot(halfway_dir, normal), 0.0), 16.0) * vec3(0.5);
+ float distance = length(light.position - position);
+ float attenuation = 1.0 / (1.0 + linear * distance + quadratic * distance * distance);
+ result += (diffuse + specular) * attenuation;
+ frag_color = vec4(result, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/deferred.vert b/advanced_lighting/9.ssao/deferred.vert
new file mode 100644
index 0000000..335635b
--- /dev/null
+++ b/advanced_lighting/9.ssao/deferred.vert
@@ -0,0 +1,16 @@
+#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()
+{
+ vert.tex_coords = tex_coords;
+ gl_Position = model * vec4(position, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/gbuffer.frag b/advanced_lighting/9.ssao/gbuffer.frag
new file mode 100644
index 0000000..7755107
--- /dev/null
+++ b/advanced_lighting/9.ssao/gbuffer.frag
@@ -0,0 +1,17 @@
+#version 330 core
+
+in vert_t {
+ vec3 position;
+ vec3 normal;
+} vert;
+
+layout(location = 0) out vec3 position;
+layout(location = 1) out vec3 normal;
+layout(location = 2) out vec3 albedo;
+
+void main()
+{
+ position = vert.position;
+ normal = normalize(vert.normal);
+ albedo.rgb = vec3(0.95);
+}
diff --git a/advanced_lighting/9.ssao/gbuffer.vert b/advanced_lighting/9.ssao/gbuffer.vert
new file mode 100644
index 0000000..81ab951
--- /dev/null
+++ b/advanced_lighting/9.ssao/gbuffer.vert
@@ -0,0 +1,21 @@
+#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()
+{
+ vert.position = vec3(view * model * vec4(position, 1.0));
+ mat3 matrix_normal = transpose(inverse(mat3(view * model)));
+ vert.normal = matrix_normal * normal;
+ gl_Position = projection * view * model * vec4(position, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/occlusion.frag b/advanced_lighting/9.ssao/occlusion.frag
new file mode 100644
index 0000000..41a436b
--- /dev/null
+++ b/advanced_lighting/9.ssao/occlusion.frag
@@ -0,0 +1,14 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out vec4 frag_color;
+
+uniform sampler2D colorbuffer;
+
+void main()
+{
+ frag_color = vec4(vec3(texture(colorbuffer, vert.tex_coords).r), 1.0);
+}
diff --git a/advanced_lighting/9.ssao/screen.frag b/advanced_lighting/9.ssao/screen.frag
new file mode 100644
index 0000000..3ae592d
--- /dev/null
+++ b/advanced_lighting/9.ssao/screen.frag
@@ -0,0 +1,14 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out vec4 frag_color;
+
+uniform sampler2D colorbuffer;
+
+void main()
+{
+ frag_color = vec4(texture(colorbuffer, vert.tex_coords).rgb, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/screen.vert b/advanced_lighting/9.ssao/screen.vert
new file mode 100644
index 0000000..335635b
--- /dev/null
+++ b/advanced_lighting/9.ssao/screen.vert
@@ -0,0 +1,16 @@
+#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()
+{
+ vert.tex_coords = tex_coords;
+ gl_Position = model * vec4(position, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/ssao b/advanced_lighting/9.ssao/ssao
new file mode 100755
index 0000000..72a9677
--- /dev/null
+++ b/advanced_lighting/9.ssao/ssao
Binary files differ
diff --git a/advanced_lighting/9.ssao/ssao.c b/advanced_lighting/9.ssao/ssao.c
new file mode 100644
index 0000000..3cfca03
--- /dev/null
+++ b/advanced_lighting/9.ssao/ssao.c
@@ -0,0 +1,331 @@
+#include "GL/glew.h"
+#include "GLFW/glfw3.h"
+
+#include "pwyazh.h"
+#include "pwyazh_GL.h"
+
+#include "xoshiro128plus.c"
+
+#include "common.h"
+
+#include "assert.h"
+
+int main(void)
+{
+ // glfw init
+ glfwSetErrorCallback(error_callback);
+ if (glfwInit() == GLFW_FALSE)
+ return 1;
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
+ S32 width = 1600;
+ S32 height = 900;
+ GLFWwindow *window = glfwCreateWindow(width, height, "ssao", 0, 0);
+ if (!window)
+ return 1;
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+ glfwMakeContextCurrent(window);
+ if (glewInit() != GLEW_OK)
+ return 1;
+ glEnable(GL_DEPTH_TEST);
+
+ // program init
+ Arena *arena = arena_alloc(Kilobytes(256));
+ Input input = input_init();
+ Camera camera = {
+ .pos = v3f(0.0f, 2.0f, 5.0f),
+ .fovx = 90.0f,
+ .near = 0.1f,
+ .far = 100.0f,
+ .yaw = 0.0f,
+ .pitch = 0.0f
+ };
+ V3F camera_dp = {0};
+ light_t light = {{2.0f, 2.0f, 2.0f}, {5.0f, 5.0f, 10.0f}};
+
+ // meshes
+ Mesh *quad = mesh_gen_quad(arena);
+ Mesh *cube = mesh_load_obj(arena, "../../data/models/cube.obj");
+
+ // framebuffers
+ U32 gbuffer;
+ glGenFramebuffers(1, &gbuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, gbuffer);
+
+ U32 gpositions;
+ glGenTextures(1, &gpositions);
+ glBindTexture(GL_TEXTURE_2D, gpositions);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, 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_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gpositions, 0);
+
+ U32 gnormals;
+ glGenTextures(1, &gnormals);
+ glBindTexture(GL_TEXTURE_2D, gnormals);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, 0);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gnormals, 0);
+
+ U32 galbedo;
+ glGenTextures(1, &galbedo);
+ glBindTexture(GL_TEXTURE_2D, galbedo);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, galbedo, 0);
+ U32 attachments[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
+ glDrawBuffers(3, attachments);
+
+ U32 rbo;
+ glGenRenderbuffers(1, &rbo);
+ glBindRenderbuffer(GL_RENDERBUFFER, rbo);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);
+
+ if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+ fprintf(stderr, "error: gbuffer not complete\n");
+ fprintf(stdout, "info: gbuffer complete\n");
+
+ // shaders
+ U32 shader_gbuffer = load_shader("gbuffer.vert", "gbuffer.frag");
+ U32 shader_ssao = load_shader("ssao.vert", "ssao.frag");
+ U32 shader_deferred = load_shader("deferred.vert", "deferred.frag");
+ U32 shader_screen = load_shader("screen.vert", "screen.frag");
+ U32 shader_occlusion = load_shader("screen.vert", "occlusion.frag");
+
+ glUseProgram(shader_ssao);
+ shader_set_1i(shader_ssao, "positions", 0);
+ shader_set_1i(shader_ssao, "normals", 1);
+ shader_set_1i(shader_ssao, "noise", 2);
+ glUseProgram(shader_deferred);
+ shader_set_1i(shader_deferred, "positions", 0);
+ shader_set_1i(shader_deferred, "normals", 1);
+ shader_set_1i(shader_deferred, "albedo", 2);
+ shader_set_1i(shader_deferred, "ssao", 3);
+ glUseProgram(0);
+
+ S32 show_gbuffer = 0;
+
+ S32 nsamples = 64;
+ V3F *samples = malloc(nsamples * sizeof(V3F));
+ S32 file = open("/dev/urandom", O_RDONLY);
+ if (file < 0) {
+ perror("error:");
+ return 1;
+ }
+ U32 seed[4];
+ if (read(file, seed, sizeof(seed)) < 0) {
+ perror("error");
+ return 1;
+ }
+ for (S32 i = 0; i < nsamples; i++) {
+ V3F sample = {
+ nextf(seed) * 2.0f - 1.0f,
+ nextf(seed) * 2.0f - 1.0f,
+ nextf(seed)
+ };
+ sample = v3f_norm(sample);
+ float scale = (F32)i / 64.0f;
+ scale = lerp(0.1f, 1.0f, scale * scale);
+ sample = v3f_scalef(sample, scale);
+ samples[i] = sample;
+ assert(-1.0f < sample.x && sample.x < 1.0f);
+ assert(-1.0f < sample.y && sample.y < 1.0f);
+ assert(0.0f < sample.z && sample.z < 1.0f);
+ // printf("color:\t%f\t%f\t%f\n", sample.x, sample.y, sample.z);
+ }
+ V3F noise[16];
+ for (S32 i = 0; i < 16; i++) {
+ noise[i] = (V3F){
+ nextf(seed) * 2.0f - 1.0f,
+ nextf(seed) * 2.0f - 1.0f,
+ 0.0f,
+ };
+ }
+ close(file);
+
+ U32 noise_texture;
+ glGenTextures(1, &noise_texture);
+ glBindTexture(GL_TEXTURE_2D, noise_texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 4, 4, 0, GL_RGB, GL_FLOAT, noise);
+ 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_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ U32 ssao_fbo;
+ glGenFramebuffers(1, &ssao_fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, ssao_fbo);
+ U32 ssao_colorbuffer;
+ glGenTextures(1, &ssao_colorbuffer);
+ glBindTexture(GL_TEXTURE_2D, ssao_colorbuffer);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_FLOAT, 0);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ssao_colorbuffer, 0);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ F64 last_time = glfwGetTime();
+ while (!glfwWindowShouldClose(window)) {
+ F64 time = glfwGetTime();
+ F32 dt = time - last_time;
+ last_time = time;
+
+ input_update_last_state(&input);
+
+ glfwPollEvents();
+ glfwGetFramebufferSize(window, &width, &height);
+ process_glfw_keyboard(window, &input);
+ process_glfw_mouse_pos(window, &input);
+
+ if (key_first_press(input.exit))
+ glfwSetWindowShouldClose(window, GLFW_TRUE);
+
+ F32 speed = 2.0f;
+ V3F dv = get_dv_camera_first_person(&input, &camera, speed, dt);
+ camera_dp = v3f_add(camera_dp, dv);
+ camera_dp = v3f_scalef(camera_dp, 0.8f);
+ camera.pos = v3f_add(camera.pos, camera_dp);
+
+ F32 sensitivity = 0.1f;
+ input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity);
+ camera.yaw += input.mouse_offset.x;
+ camera.pitch += input.mouse_offset.y;
+ if (camera.pitch > 89.0f)
+ camera.pitch = 89.0f;
+ if (camera.pitch < -89.0f)
+ camera.pitch = -89.0f;
+
+ // update
+ if (key_first_press(input.jump))
+ show_gbuffer = !show_gbuffer;
+
+ // render
+ F32 ar = (F32)width / (F32)height;
+ MAT4 projection = camera_persp(camera, ar);
+ MAT4 view = get_view_matrix(&camera);
+
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ // render stuff into gbuffer
+ glBindFramebuffer(GL_FRAMEBUFFER, gbuffer);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glUseProgram(shader_gbuffer);
+
+ shader_set_mat4fv(shader_gbuffer, "projection", projection);
+ shader_set_mat4fv(shader_gbuffer, "view", view);
+
+ shader_set_mat4fv(shader_gbuffer, "model", mat4_identity());
+ mesh_draw(cube);
+
+ MAT4 model = mat4_make_scale(v3f(10.0f, 0.2f, 10.0f));
+ model = mat4_translate(model, v3f(0.0f, -1.25f, 0.0f));
+ shader_set_mat4fv(shader_gbuffer, "model", model);
+ mesh_draw(cube);
+
+ glUseProgram(0);
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ // render ssao texture
+ glBindFramebuffer(GL_FRAMEBUFFER, ssao_fbo);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ glUseProgram(shader_ssao);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, gpositions);
+
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, gnormals);
+
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, noise_texture);
+
+ for (S32 i = 0; i < nsamples; i++) {
+
+ char str[512] = {0};
+ snprintf(str, 512, "samples[%d]", i);
+ shader_set_3fv(shader_ssao, str, samples[i]);
+ }
+ shader_set_mat4fv(shader_ssao, "projection", projection);
+
+ mesh_draw(quad);
+
+ glUseProgram(0);
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ if (show_gbuffer) {
+ MAT4 scale = mat4_make_scale(v3f(0.5f, 0.5f, 0.5f));
+ MAT4 leftup = mat4_translate(scale, v3f(-0.5f, 0.5f, 0.0f));
+ MAT4 rightup = mat4_translate(scale, v3f(0.5f, 0.5f, 0.0f));
+ MAT4 leftdown = mat4_translate(scale, v3f(-0.5f, -0.5f, 0.0f));
+ MAT4 rightdown = mat4_translate(scale, v3f(0.5f, -0.5f, 0.0f));
+
+ glDisable(GL_DEPTH_TEST);
+
+ glUseProgram(shader_screen);
+ shader_set_mat4fv(shader_screen, "model", leftup);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, gpositions);
+ mesh_draw(quad);
+
+ shader_set_mat4fv(shader_screen, "model", rightup);
+ glBindTexture(GL_TEXTURE_2D, gnormals);
+ mesh_draw(quad);
+
+ shader_set_mat4fv(shader_screen, "model", leftdown);
+ glBindTexture(GL_TEXTURE_2D, galbedo);
+ mesh_draw(quad);
+ glUseProgram(0);
+
+ glUseProgram(shader_occlusion);
+ shader_set_mat4fv(shader_occlusion, "model", rightdown);
+ glBindTexture(GL_TEXTURE_2D, ssao_colorbuffer);
+ mesh_draw(quad);
+ glUseProgram(0);
+
+ glEnable(GL_DEPTH_TEST);
+ } else {
+ // render scene lighting
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glUseProgram(shader_deferred);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, gpositions);
+
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, gnormals);
+
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, galbedo);
+
+ glActiveTexture(GL_TEXTURE3);
+ glBindTexture(GL_TEXTURE_2D, ssao_colorbuffer);
+
+ V4F light_pos_v4 = v4f(light.position.x, light.position.y, light.position.z, 1.0f);
+ V4F light_view_pos = mat4_v4f_mul(view, light_pos_v4);
+ shader_set_3fv(shader_deferred, "light.position", v3f_from_v4f(light_view_pos));
+ shader_set_3fv(shader_deferred, "light.color", light.color);
+ shader_set_mat4fv(shader_deferred, "model", mat4_identity());
+
+ mesh_draw(quad);
+
+ glUseProgram(0);
+ }
+
+ glfwSwapBuffers(window);
+ }
+
+ return 0;
+}
diff --git a/advanced_lighting/9.ssao/ssao.frag b/advanced_lighting/9.ssao/ssao.frag
new file mode 100644
index 0000000..e4d04b1
--- /dev/null
+++ b/advanced_lighting/9.ssao/ssao.frag
@@ -0,0 +1,45 @@
+#version 330 core
+
+in vert_t {
+ vec2 tex_coords;
+} vert;
+
+out float frag_color;
+
+uniform sampler2D positions;
+uniform sampler2D normals;
+uniform sampler2D noise;
+
+uniform vec3 samples[64];
+uniform mat4 projection;
+
+const vec2 noise_scale = vec2(1600.0 / 4.0, 900.0 / 4.0)
+
+void main()
+{
+ vec3 position = texture(positions, vert.tex_coords).rgb;
+ vec3 normal = texture(normals, vert.tex_coords).rgb;
+ vec3 random = texture(noise, vert.tex_coords * noise_scale).rgb;
+
+ vec3 tangent = normalize(random - normal * dot(random, normal));
+ vec3 bitangent = cross(normal, tangent);
+ mat3 TBN = mat3(tangent, bitangent, normal);
+
+ int nsamples = 64;
+ float radius = 0.5;
+ float bias = 0.025;
+ float occlusion = 0.0;
+ for (int i = 0; i < nsamples; i++) {
+ vec3 sample = TBN * samples[i];
+ sample = position + sample * radius;
+ vec4 offset = vec4(sample, 1.0);
+ offset = projection * offset;
+ offset.xyz /= offset.w;
+ offset.xyz = offset.xyz * 0.5 + 0.5;
+ float depth = texture(positions, offset.xy).z;
+ // float range_check = smoothstep(0.0, 1.0, radius / abs(position.z - depth));
+ occlusion += (depth >= sample.z + bias ? 1.0 : 0.0); // * range_check;
+ }
+ occlusion = 1.0 - (occlusion / nsamples);
+ frag_color = occlusion;
+}
diff --git a/advanced_lighting/9.ssao/ssao.vert b/advanced_lighting/9.ssao/ssao.vert
new file mode 100644
index 0000000..3ce6fb2
--- /dev/null
+++ b/advanced_lighting/9.ssao/ssao.vert
@@ -0,0 +1,16 @@
+#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()
+{
+ vert.tex_coords = tex_coords;
+ gl_Position = model * vec4(position, 1.0);
+}
diff --git a/advanced_lighting/9.ssao/xoshiro128plus.c b/advanced_lighting/9.ssao/xoshiro128plus.c
new file mode 100644
index 0000000..b55c63b
--- /dev/null
+++ b/advanced_lighting/9.ssao/xoshiro128plus.c
@@ -0,0 +1,28 @@
+#include <stdint.h>
+
+/* --- xoshiro128+ --- */
+static inline uint32_t rotl(const uint32_t x, int k) {
+ return (x << k) | (x >> (32 - k));
+}
+
+uint32_t next(uint32_t *s) {
+ const uint32_t result = s[0] + s[3];
+ const uint32_t t = s[1] << 9;
+ s[2] ^= s[0];
+ s[3] ^= s[1];
+ s[1] ^= s[2];
+ s[0] ^= s[3];
+ s[2] ^= t;
+ s[3] = rotl(s[3], 11);
+ return result;
+}
+/* --- /xoshiro128+ --- */
+
+float int_to_float(uint32_t random) {
+ union { uint32_t u32; float f; } u = { .u32 = random >> 9 | 0x3f800000 };
+ return u.f - 1.0;
+}
+
+float nextf(uint32_t* s) {
+ return int_to_float(next(s));
+}