From 6c581ff56dfcc22c25538e305e58efd967dd640a Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:54:55 +1100 Subject: lights, camera, action --- assets/shaders/blinn_phong.frag | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'assets') diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index a284948..095b19a 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -9,6 +9,25 @@ struct Material { float shininess; }; +struct DirLight { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct PointLight { + vec3 position; + // fall off properties + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + in VS_OUT { vec3 FragPos; vec3 Normal; @@ -16,6 +35,61 @@ in VS_OUT { vec4 FragPosLightSpace; } fs_in; +// --- Uniforms +#define NUM_POINT_LIGHTS 4 +uniform vec3 viewPos; +uniform Material material; +uniform DirLight dirLight; +uniform PointLight pointLights[NUM_POINT_LIGHTS]; + +// --- Function prototypes +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir); + void main() { - FragColor = vec4(1.0); + vec3 norm = normalize(fs_in.Normal); + vec3 viewDir = normalize(viewPos - fs_in.FragPos); + + vec3 result = CalcDirLight(dirLight, norm, viewDir); + for (int i = 0; i < 4; i++) { + result += CalcPointLight(pointLights[i], norm, fs_in.FragPos, viewDir); + } + + FragColor = vec4(result, 1.0); +} + +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir) +{ + vec3 lightDir = normalize(-light.direction); + // diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + // specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // combine result + vec3 ambient = light.ambient * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, fs_in.TexCoords)); + return (ambient + diffuse + specular); +} + +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse + float diff = max(dot(normal, lightDir), 0.0); + // specular + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attentuation + float distance = length(light.position - fragPos); + float attentuation = 1.0 / (light.constant + light.linear * distance * light.quadratic * (distance * distance)); + // result + vec3 ambient = light.ambient * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, fs_in.TexCoords)); + ambient *= attentuation; + diffuse *= attentuation; + specular *= attentuation; + return (ambient + diffuse + specular); } \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 2b83174a87f5a1e4991cc9153309ad0f73450b44 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 22:29:15 +1100 Subject: fix cube texture path now that we can load relative path --- assets/models/obj/cube/cube.mtl | 2 +- src/resources/obj.c | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'assets') diff --git a/assets/models/obj/cube/cube.mtl b/assets/models/obj/cube/cube.mtl index 9a17b69..aa04436 100644 --- a/assets/models/obj/cube/cube.mtl +++ b/assets/models/obj/cube/cube.mtl @@ -10,4 +10,4 @@ Ke 0.0 0.0 0.0 Ni 1.450000 d 1.000000 illum 2 -map_Kd models/obj/cube/container.jpg +map_Kd container.jpg diff --git a/src/resources/obj.c b/src/resources/obj.c index 710d5f0..56f885f 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -45,11 +45,8 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y if (!relative_path.has_value) { WARN("Couldnt get a relative path for the path to use for loading materials & textures later"); } - printf("Relative path: %s\n", relative_path.path.buf); const char *file_string = string_from_file(path); - // TODO: store the relative path without the name.obj at the end - model model = { 0 }; model.name = str8_cstr_view(path); model.meshes = mesh_darray_new(1); -- cgit v1.2.3-70-g09d2