Compare commits

..

33 Commits

Author SHA1 Message Date
71f6d03635 Implement Multi-Sample Anti Aliasing (MSAA) 2024-12-30 21:01:16 +00:00
e9a9b8a2cf Use instanced arrays to draw the lights 2024-12-30 19:07:53 +00:00
ce561fc44d Only compile normals and light shaders when needed 2024-12-30 17:35:49 +00:00
acefc1d254 Add ability to draw normals 2024-12-30 17:31:54 +00:00
f65410b7ad Reformat 2024-12-28 22:41:22 +00:00
0e43f07d80 Refactor setting shader uniform block binding point to method 2024-12-28 22:39:23 +00:00
e8f4f4ae12 Move common shader data to uniform buffer 2024-12-28 22:28:20 +00:00
3aed2a9614 Update shaders to use interface blocks 2024-12-28 20:00:05 +00:00
21b8cc56f3 Add reflective and refractive models 2024-12-28 17:15:07 +00:00
e6d5aac3d4 Compile in debug mode 2024-12-28 16:17:11 +00:00
e29b519977 Rebase 2024-12-28 16:15:22 +00:00
13bedc164c Implement skybox 2024-12-28 00:14:25 +00:00
68cc990650 Implement double frame buffering 2024-12-27 22:22:32 +00:00
9de54d016d Enable back face culling 2024-12-27 20:36:18 +00:00
8447717875 Remove backpack model and replace it with Suzanne 2024-12-27 20:01:57 +00:00
ebcfe3eca9 Add assimp as submodule 2024-12-27 20:01:39 +00:00
19e722459d Fix bug with loading material textures 2024-12-27 16:37:19 +00:00
0a4fb8e76f Implement model loading with assimp 2024-12-26 00:59:00 +00:00
806c260893 Fix linking issues 2024-12-26 00:16:45 +00:00
bfbdbf75f3 Create mesh abstraction 2024-12-25 21:22:48 +00:00
c4a607f660 Add assimp 2024-12-25 21:22:34 +00:00
c65a3ba314 Create a scene with multiple lights 2024-12-23 23:41:23 +00:00
b7e5eea448 Play with lights 2024-12-22 01:36:08 +00:00
e720355d04 Ignore python scripts 2024-12-08 00:05:36 +00:00
fbda4821ee Add specular map and abstract loading textures 2024-12-08 00:04:49 +00:00
cc1f6afb26 Add diffuse map 2024-12-07 23:24:40 +00:00
621204abe5 Darken background 2024-12-03 23:18:19 +00:00
1193e128e8 Reformat 2024-12-03 21:39:34 +00:00
d0070045b1 Reformat 2024-12-03 21:30:49 +00:00
6bcd4802aa Define material and light properties 2024-12-03 21:29:50 +00:00
9cbfa55c89 Move light around the scene 2024-12-03 20:08:40 +00:00
d8ab418180 Reformat 2024-12-02 22:07:17 +00:00
babe58f0a5 Start the lighting chapters 2024-12-01 22:30:42 +00:00
30 changed files with 3725 additions and 175 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.cache
compile_commands.json
main
*.py

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "src/assimp"]
path = src/assimp
url = git@github.com:assimp/assimp

View File

@ -3,15 +3,15 @@
CC=clang
CFLAGS="-g -c -Wall -Isrc/glad/include"
CXX=clang++
CXXFLAGS="-g -Wall -std=c++20 $(pkg-config --cflags sdl2) -Isrc/glad/include -Isrc/glm"
LIBS="$(pkg-config --libs sdl2) -ldl"
CXXFLAGS="-g -Wall -std=c++20 $(pkg-config --cflags sdl2) -Isrc/glad/include -Isrc/glm -Isrc/assimp/include -Isrc/assimp/build/include"
LIBS="$(pkg-config --libs sdl2) -ldl -lz -lminizip -Lsrc/assimp/build/lib/ -lassimp"
GLAD_SRC="src/glad/src/glad.c"
GLAD_OBJ="glad.o"
SRC="src/*.cc $GLAD_OBJ src/glm/glm/glm.cppm"
OUT=main
(set -x ; $CC $CFLAGS $GLAD_SRC -o $GLAD_OBJ)
(set -x ; $CXX $CXXFLAGS $LIBS $SRC -o $OUT)
(set -x ; $CXX $CXXFLAGS $SRC $LIBS -o $OUT)
if [[ -f $GLAD_OBJ ]]; then
rm $GLAD_OBJ

BIN
images/container2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
images/skybox/back.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

BIN
images/skybox/bottom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
images/skybox/front.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
images/skybox/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

BIN
images/skybox/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

BIN
images/skybox/top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

BIN
models/suzanne/diffuse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

BIN
models/suzanne/specular.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 526 KiB

View File

@ -0,0 +1,12 @@
# Blender 4.3.2 MTL File: 'None'
# www.blender.org
newmtl Material
Ka 1.000000 1.000000 1.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd diffuse.png
map_Ks specular.png

2541
models/suzanne/suzanne.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,118 @@
#version 330 core
in vec2 tex_coords;
#define POINT_LIGHT_COUNT 4
struct Material {
sampler2D diffuse1;
sampler2D specular1;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
struct SpotLight {
vec3 position;
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float cutoff;
float outer_cutoff;
};
in VS_OUT {
vec3 vert_normal;
vec3 frag_position;
vec2 uv_coords;
} fs_in;
uniform Material material;
uniform DirLight directional_light;
uniform PointLight point_lights[POINT_LIGHT_COUNT];
uniform SpotLight spot_light;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
vec3 calc_dir_light(DirLight light, vec3 normal, vec3 view_direction);
vec3 calc_point_light(PointLight light, vec3 normal, vec3 frag_position, vec3 view_direction);
vec3 calc_spot_light(SpotLight light, vec3 normal, vec3 frag_position, vec3 view_direction);
out vec4 color;
uniform float mix_factor;
uniform sampler2D texture0;
uniform sampler2D texture1;
void main() {
color = mix(texture(texture0, tex_coords), texture(texture1, tex_coords), mix_factor);
vec3 normal = normalize(fs_in.vert_normal);
vec3 view_direction = normalize(fs_in.frag_position - camera_position);
vec3 result = calc_dir_light(directional_light, normal, view_direction);
for (int i = 0; i < POINT_LIGHT_COUNT; ++i) {
result += calc_point_light(point_lights[i], normal, fs_in.frag_position, view_direction);
}
result += calc_spot_light(spot_light, normal, fs_in.frag_position, view_direction);
color = vec4(result, 1.0);
};
vec3 calc_dir_light(DirLight light, vec3 normal, vec3 view_direction) {
vec3 light_direction = normalize(-light.direction);
vec3 reflect_direction = reflect(-light_direction, normal);
float diff = max(dot(normal, light_direction), 0.0);
vec3 diff_tex = vec3(texture(material.diffuse1, fs_in.uv_coords));
float spec = pow(max(dot(reflect_direction, view_direction), 0.0), material.shininess);
vec3 ambient = light.ambient * diff_tex;
vec3 diffuse = light.diffuse * (diff * diff_tex);
vec3 specular = light.specular * (spec * vec3(texture(material.specular1, fs_in.uv_coords)));
return ambient + diffuse + specular;
}
vec3 calc_point_light(PointLight light, vec3 normal, vec3 frag_position, vec3 view_direction) {
vec3 light_direction = normalize(light.position - frag_position);
vec3 reflect_direction = reflect(-light_direction, normal);
float distance = length(light.position - frag_position);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
float diff = max(dot(normal, light_direction), 0.0);
vec3 diff_tex = vec3(texture(material.diffuse1, fs_in.uv_coords));
float spec = pow(max(dot(reflect_direction, view_direction), 0.0), material.shininess);
vec3 ambient = light.ambient * diff_tex * attenuation;
vec3 diffuse = light.diffuse * (diff * diff_tex) * attenuation;
vec3 specular = light.specular * (spec * vec3(texture(material.specular1, fs_in.uv_coords))) * attenuation;
return ambient + diffuse + specular;
}
vec3 calc_spot_light(SpotLight light, vec3 normal, vec3 frag_position, vec3 view_direction) {
vec3 light_direction = normalize(light.position - frag_position);
vec3 reflect_direction = reflect(-light_direction, normal);
float theta = dot(light_direction, normalize(-light.direction));
float epsilon = light.cutoff - light.outer_cutoff;
float intensity = clamp((theta - light.outer_cutoff) / epsilon, 0.0, 1.0);
float diff = max(dot(normal, light_direction), 0.0);
float spec = pow(max(dot(reflect_direction, view_direction), 0.0), material.shininess);
vec3 diff_tex = vec3(texture(material.diffuse1, fs_in.uv_coords));
vec3 ambient = light.ambient * diff_tex;
vec3 diffuse = light.diffuse * (diff * diff_tex) * intensity;
vec3 specular = light.specular * (spec * vec3(texture(material.specular1, fs_in.uv_coords))) * intensity;
return ambient + diffuse + specular;
}

9
shaders/light_frag.glsl Normal file
View File

@ -0,0 +1,9 @@
#version 330 core
out vec4 color;
uniform vec3 light_diffuse;
void main() {
color = vec4(light_diffuse, 1.0);
}

18
shaders/light_vert.glsl Normal file
View File

@ -0,0 +1,18 @@
#version 330 core
#define POINT_LIGHT_COUNT 4
layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 uv;
layout(location=3) in mat4 model;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
void main() {
gl_Position = projection * view * model * vec4(position, 1.0);
};

7
shaders/normal_frag.glsl Normal file
View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 color;
void main() {
color = vec4(1.0, 1.0, 0.0, 1.0);
}

34
shaders/normal_geo.glsl Normal file
View File

@ -0,0 +1,34 @@
#version 330 core
#define MAGNITUDE 0.1
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;
uniform float time;
in VS_OUT {
vec3 vert_normal;
} gs_in[];
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
void generate_line(int index) {
gl_Position = projection * gl_in[index].gl_Position;
EmitVertex();
gl_Position = projection * (gl_in[index].gl_Position + vec4(gs_in[index].vert_normal, 1.0) * MAGNITUDE);
EmitVertex();
EndPrimitive();
}
void main() {
for (int i = 0; i < 3; ++i) {
generate_line(i);
}
}

25
shaders/normal_vert.glsl Normal file
View File

@ -0,0 +1,25 @@
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 uv;
uniform mat3 normal_mat;
uniform mat4 model;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
// interface block
out VS_OUT {
vec3 vert_normal;
} vs_out;
void main() {
vs_out.vert_normal = normal_mat * normal;
gl_Position = view * model * vec4(position, 1.0);
};

17
shaders/pp_frag.glsl Normal file
View File

@ -0,0 +1,17 @@
#version 330 core
struct Material {
sampler2D diffuse1;
sampler2D specular1;
float shininess;
};
in vec2 uv_coords;
uniform Material material;
out vec4 color;
void main() {
color = vec4(vec3(texture(material.diffuse1, uv_coords)), 1.0);
}

12
shaders/pp_vert.glsl Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 uv;
out vec2 uv_coords;
void main() {
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
uv_coords = uv;
}

View File

@ -0,0 +1,24 @@
#version 330 core
in VS_OUT {
vec3 vert_normal;
vec3 frag_position;
vec2 uv_coords;
} fs_in;
uniform samplerCube cubemap;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
out vec4 color;
void main() {
vec3 view_direction = normalize(fs_in.frag_position - camera_position);
vec3 reflect_direction = reflect(view_direction, normalize(fs_in.vert_normal));
color = vec4(texture(cubemap, reflect_direction).rgb, 1.0);
}

View File

@ -0,0 +1,27 @@
#version 330 core
#define AIR_IOR 1.0
#define GLASS_IOR 1.52
in VS_OUT {
vec3 vert_normal;
vec3 frag_position;
vec2 uv_coords;
} fs_in;
uniform samplerCube cubemap;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
out vec4 color;
void main() {
vec3 view_direction = normalize(fs_in.frag_position - camera_position);
vec3 refract_direction = refract(view_direction, normalize(fs_in.vert_normal), AIR_IOR / GLASS_IOR);
color = vec4(texture(cubemap, refract_direction).rgb, 1.0);
}

11
shaders/sb_frag.glsl Normal file
View File

@ -0,0 +1,11 @@
#version 330 core
in vec3 uv_coords;
uniform samplerCube cubemap;
out vec4 color;
void main() {
color = texture(cubemap, uv_coords);
}

21
shaders/sb_vert.glsl Normal file
View File

@ -0,0 +1,21 @@
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 uv;
uniform mat4 sb_view;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
out vec3 uv_coords;
void main() {
vec4 pos = projection * sb_view * vec4(position, 1.0);
gl_Position = pos.xyww;
uv_coords = position;
}

View File

@ -1,15 +1,29 @@
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec2 uv;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 uv;
uniform mat3 normal_mat;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec2 tex_coords;
layout (std140) uniform Common {
mat4 projection;
mat4 view;
vec3 camera_position;
};
// interface block
out VS_OUT {
vec3 vert_normal;
vec3 frag_position;
vec2 uv_coords;
} vs_out;
void main() {
tex_coords = uv;
vs_out.frag_position = vec3(model * vec4(position, 1.0));
vs_out.vert_normal = normal_mat * normal;
vs_out.uv_coords = uv;
gl_Position = projection * view * model * vec4(position, 1.0);
};

1
src/assimp Submodule

@ -0,0 +1 @@
Subproject commit 258cdfd2bc29a920bbe749962abbcd58caa76422

File diff suppressed because it is too large Load Diff