-
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
3 changed files
with
96 additions
and
69 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 |
---|---|---|
@@ -1,9 +1,55 @@ | ||
#version 330 core | ||
|
||
out vec4 FragColor; | ||
out vec4 FragColor; // Final fragment color | ||
|
||
in vec3 VertexColor; // Interpolated color from the vertex shader | ||
in vec3 FragPos; // Fragment position in world space | ||
in vec3 Normal; // Normal in world space | ||
|
||
uniform vec3 viewPos; // Camera position | ||
|
||
// Light structure | ||
struct Light { | ||
vec3 position; // Light position | ||
vec3 ambient; // Ambient light color | ||
vec3 diffuse; // Diffuse light color | ||
vec3 specular; // Specular light color | ||
}; | ||
|
||
uniform int numLights; // Number of active lights | ||
uniform Light lights[16]; // Array of lights (supporting up to 16 point lights) | ||
|
||
// Material structure | ||
struct Material { | ||
vec3 diffuse; // Diffuse material color | ||
vec3 specular; // Specular material color | ||
float shininess; // Shininess factor | ||
}; | ||
|
||
uniform Material material; // Material properties | ||
|
||
void main() { | ||
FragColor = vec4(VertexColor, 1.0); // Output the interpolated color | ||
vec3 result = vec3(0.0); // Initialize lighting result | ||
|
||
// Process each light source | ||
for (int i = 0; i < numLights; ++i) { | ||
// Ambient component | ||
vec3 ambient = lights[i].ambient * material.diffuse; | ||
|
||
// Diffuse component | ||
vec3 lightDir = normalize(lights[i].position - FragPos); | ||
float diff = max(dot(Normal, lightDir), 0.0); | ||
vec3 diffuse = lights[i].diffuse * diff * material.diffuse; | ||
|
||
// Specular component | ||
vec3 viewDir = normalize(viewPos - FragPos); | ||
vec3 reflectDir = reflect(-lightDir, Normal); | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); | ||
vec3 specular = lights[i].specular * spec * material.specular; | ||
|
||
// Accumulate the lighting | ||
result += ambient + diffuse + specular; | ||
} | ||
|
||
FragColor = | ||
vec4(result, 1.0); // Set the final fragment color with full opacity | ||
} |
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 |
---|---|---|
@@ -1,63 +1,21 @@ | ||
#version 330 core | ||
|
||
layout(location = 0) in vec3 position; | ||
layout(location = 1) in vec3 normal; | ||
layout(location = 0) in vec3 position; // Vertex position | ||
layout(location = 1) in vec3 normal; // Vertex normal | ||
|
||
uniform mat4 modelMatrix; | ||
uniform mat4 viewMatrix; | ||
uniform mat4 projectionMatrix; | ||
uniform mat4 modelMatrix; // Model matrix | ||
uniform mat4 viewMatrix; // View matrix | ||
uniform mat4 projectionMatrix; // Projection matrix | ||
|
||
out vec3 FragPos; | ||
out vec3 Normal; | ||
out vec3 VertexColor; // Color output for Gouraud shading | ||
|
||
// Light structure | ||
struct Light { | ||
vec3 position; // Light position | ||
vec3 ambient; | ||
vec3 diffuse; | ||
vec3 specular; | ||
}; | ||
|
||
// Material structure | ||
struct Material { | ||
vec3 diffuse; | ||
vec3 specular; | ||
float shininess; | ||
}; | ||
|
||
uniform int numLights; // Number of active lights | ||
uniform Light lights[16]; // Array of lights (supporting up to 16 point lights) | ||
uniform Material material; // Material properties | ||
uniform vec3 viewPos; // Camera position | ||
out vec3 FragPos; // Fragment position in world space | ||
out vec3 Normal; // Normal in world space | ||
|
||
void main() { | ||
FragPos = vec3(modelMatrix * vec4(position, 1.0)); // World space position | ||
FragPos = vec3(modelMatrix * | ||
vec4(position, 1.0)); // Transform position to world space | ||
Normal = normalize(mat3(transpose(inverse(modelMatrix))) * | ||
normal); // Normal in world space | ||
|
||
// Initialize lighting result for Gouraud shading | ||
vec3 result = vec3(0.0); | ||
vec3 viewDir = | ||
normalize(viewPos - FragPos); // Direction from fragment to view | ||
|
||
// Process each light | ||
for (int i = 0; i < numLights; ++i) { | ||
vec3 ambient = lights[i].ambient * material.diffuse; | ||
|
||
vec3 lightDir = normalize(lights[i].position - FragPos); | ||
float diff = max(dot(Normal, lightDir), 0.0); | ||
vec3 diffuse = lights[i].diffuse * diff * material.diffuse; | ||
|
||
vec3 reflectDir = reflect(-lightDir, Normal); | ||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); | ||
vec3 specular = lights[i].specular * spec * material.specular; | ||
|
||
result += ambient + diffuse + specular; // Accumulate the lighting | ||
} | ||
|
||
VertexColor = result; // Pass the computed color to the fragment shader | ||
normal); // Transform normal to world space | ||
|
||
gl_Position = | ||
projectionMatrix * viewMatrix * vec4(FragPos, 1.0); // MVP transform | ||
// Transform the vertex position to clip space | ||
gl_Position = projectionMatrix * viewMatrix * 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