-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
308 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#version 410 core | ||
|
||
// Inputs from vertex shader | ||
in vec3 FragPos; // Position in world space | ||
in vec3 Normal; // Normal in world space | ||
in vec3 ViewDir; // View direction | ||
in vec3 textCoords; //Texture | ||
in vec3 Tangent; //Tangent | ||
in vec3 Bitangent; //Bitangent | ||
// Uniforms | ||
uniform vec3 light_position; | ||
uniform vec3 diffuse_colour; | ||
uniform vec3 specular_colour; | ||
uniform vec3 ambient_colour; | ||
uniform float shininess_value; | ||
uniform bool use_normal_mapping; | ||
|
||
uniform sampler2D demo_diffuse_texture; | ||
uniform sampler2D demo_specular_texture; | ||
uniform sampler2D demo_normal_texture; | ||
// Output color | ||
out vec4 FragColor; | ||
|
||
void main() | ||
{ | ||
vec3 ambient = ambient_colour; | ||
|
||
if(use_normal_mapping){ | ||
vec3 T = normalize(Tangent); | ||
vec3 B = normalize(Bitangent); | ||
vec3 N = normalize(Normal); | ||
mat3 TBN = mat3(T, B, N); | ||
|
||
vec3 normalMap = texture(demo_normal_texture, textCoords.xy).rgb; | ||
normalMap = normalize(normalMap * 2.0 - 1.0); | ||
vec3 normal = normalize(TBN * normalMap); | ||
|
||
vec3 light_dir = normalize(light_position - FragPos); // Light direction | ||
float diff = max(dot(normal, light_dir), 0.0); // Angle between light and normal | ||
vec3 diffuse = diff * texture(demo_diffuse_texture, textCoords.xy).rgb; | ||
|
||
// Specular lighting (Phong reflectance) | ||
vec3 reflect_dir = reflect(-light_dir, normal); // Reflect the light direction around the normal | ||
float spec = pow(max(dot(ViewDir, reflect_dir), 0.0), shininess_value); // Specular intensity | ||
vec3 specular = spec * texture(demo_specular_texture, textCoords.xy).rgb; | ||
// Combine all three components (ambient + diffuse + specular) | ||
vec3 result = (ambient + diffuse + specular); | ||
FragColor = vec4(result, 1.0); | ||
} else | ||
{ | ||
vec3 normal = normalize(Normal); | ||
|
||
vec3 light_dir = normalize(light_position - FragPos); // Light direction | ||
float diff = max(dot(normal, light_dir), 0.0); // Angle between light and normal | ||
vec3 diffuse = diff * texture(demo_diffuse_texture, textCoords.xy).rgb; | ||
|
||
// Specular lighting (Phong reflectance) | ||
vec3 reflect_dir = reflect(-light_dir, normal); // Reflect the light direction around the normal | ||
float spec = pow(max(dot(ViewDir, reflect_dir), 0.0), shininess_value); // Specular intensity | ||
vec3 specular = spec * texture(demo_specular_texture, textCoords.xy).rgb; | ||
|
||
// Combine all three components (ambient + diffuse + specular) | ||
vec3 result = (ambient + diffuse + specular); | ||
|
||
// Output the final color (with alpha value of 1.0) | ||
FragColor = vec4(result, 1.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#version 410 core | ||
|
||
// Vertex attributes | ||
layout (location = 0) in vec3 position; | ||
layout (location = 1) in vec3 normal; | ||
layout (location = 2) in vec3 textures; | ||
layout (location = 3) in vec3 tangent; | ||
layout (location = 4) in vec3 bitangent; | ||
|
||
// Uniforms | ||
uniform mat4 normal_model_to_world; | ||
uniform mat4 vertex_model_to_world; | ||
uniform mat4 vertex_world_to_clip; | ||
uniform vec3 camera_position; // Camera position (for specular reflections) | ||
// Outputs to fragment shader | ||
out vec3 FragPos; // Position in world space | ||
out vec3 Normal; // Normal in world space | ||
out vec3 ViewDir; // View direction (camera position - fragment position) | ||
out vec3 textCoords; | ||
out vec3 Tangent; | ||
out vec3 Bitangent; | ||
void main() | ||
{ | ||
// Transform the vertex position to world space | ||
FragPos = vec3(vertex_model_to_world * vec4(position, 1.0)); | ||
|
||
// Pass the normal in world space | ||
Normal = (normal_model_to_world * vec4(normal, 0.0)).xyz; | ||
|
||
Tangent = (normal_model_to_world * vec4(tangent, 0.0)).xyz; | ||
|
||
Bitangent = (normal_model_to_world * vec4(bitangent, 0.0)).xyz; | ||
|
||
// Calculate view direction (camera position - fragment position) | ||
ViewDir = normalize(camera_position - FragPos); | ||
|
||
textCoords = textures; | ||
|
||
// Transform the position to clip space for rasterization | ||
gl_Position = vertex_world_to_clip * vec4(FragPos, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.