diff --git a/TheRepo/TheLabs/Lighting.cs b/TheRepo/TheLabs/Lighting.cs index 211200d..1d27795 100644 --- a/TheRepo/TheLabs/Lighting.cs +++ b/TheRepo/TheLabs/Lighting.cs @@ -11,12 +11,21 @@ public struct Lighting public Vector3 diffuseColour; public Vector3 specularColour; + public float constant; + public float linear; + public float quadratic; + public Lighting(Vector3 pos, Vector3 ambient, Vector3 diffuse, Vector3 specular) { position = pos; ambientColour = ambient; diffuseColour = diffuse; specularColour = specular; + + constant = 1.0f; + linear = 0.09f; + quadratic = 0.032f; + } public void Apply(Shader shader) @@ -25,5 +34,10 @@ public struct Lighting GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.ambientColour"), ref ambientColour); GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.diffuseColour"), ref diffuseColour); GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.specularColour"), ref specularColour); + + GL.Uniform1(GL.GetUniformLocation(shader.Handle, "uPointLight.constant"), constant); + GL.Uniform1(GL.GetUniformLocation(shader.Handle, "uPointLight.linear"), linear); + GL.Uniform1(GL.GetUniformLocation(shader.Handle, "uPointLight.quadratic"), quadratic); + } } \ No newline at end of file diff --git a/TheRepo/TheLabs/RenderObject.cs b/TheRepo/TheLabs/RenderObject.cs index d61d833..2b542ed 100644 --- a/TheRepo/TheLabs/RenderObject.cs +++ b/TheRepo/TheLabs/RenderObject.cs @@ -16,6 +16,7 @@ public class RenderObject public Matrix4 Transform = Matrix4.Identity; public Quaternion Rotation = Quaternion.Identity; public Vector3 Scale = Vector3.One; + public bool UseTexture = true; public RenderObject(Mesh mesh, Shader shader, Texture texture, Material material, Lighting lighting) { @@ -38,8 +39,16 @@ public class RenderObject Material mat = new Material(pink * 0.1f, pink * 0.6f, Vector3.One, 32.0f); Shader.Use(); - Texture.Use(); - Shader.SetInt("uTexture", 0); + if (Texture != null && UseTexture) + { + Texture.Use(); + Shader.SetInt("uTexture", 0); + GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 1); + } + else + { + GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 0); + } Material.Apply(Shader); light.Apply(Shader); diff --git a/TheRepo/TheLabs/Shaders/shader.frag b/TheRepo/TheLabs/Shaders/shader.frag index 41adafe..4f37796 100644 --- a/TheRepo/TheLabs/Shaders/shader.frag +++ b/TheRepo/TheLabs/Shaders/shader.frag @@ -22,6 +22,10 @@ struct LightProperty { vec3 ambientColour; vec3 diffuseColour; vec3 specularColour; + + float constant; + float linear; + float quadratic; }; // --- UNIFORMS --- @@ -29,53 +33,52 @@ uniform sampler2D uTexture; // Image Texture uniform vec3 uViewPos; // Camera Position uniform MaterialProperty uMaterial; // Material Properties uniform LightProperty uPointLight; // Light Properties +uniform int uUseTexture; // Flag to indicate if texture should be used +vec3 CalcPointLight(LightProperty light, vec3 normal, vec3 fragPos, vec3 viewDir) { + vec3 lightDir = normalize(light.position - fragPos); + + // 1. Diffuse shading + float diff = max(dot(normal, lightDir), 0.0); + + // 2. Specular shading + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), uMaterial.shininess); + + // 3. Attenuation + float distance = length(light.position - fragPos); + float attenuation = 1.0 / (light.constant + light.linear * distance + + light.quadratic * (distance * distance)); + + // 4. Combine results + vec3 ambient = light.ambientColour * uMaterial.ambientColour; + vec3 diffuse = light.diffuseColour * diff * uMaterial.diffuseColour; + vec3 specular = light.specularColour * spec * uMaterial.specularColour; + + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; + + return (ambient + diffuse + specular); + +} + void main() { - // --- LIGHTING CALCULATIONS --- - // Lighting Setup (hardcoded for simplicity) - vec3 lightPos = vec3(1.2f, 1.0f, 2.0f); - vec3 lightColor = vec3(1.0f, 1.0f, 1.0f); - // 1. Get Texture Color - // Sample the texture at the given texture coordinates - vec4 objColor = texture(uTexture, TexCoord); - - // 2. Ambient strength - // A constant, dim light that ensure s object are not completely dark - // It simply multiplies the light color by a small factor - float ambientStrength = 0.1f; - vec3 ambient = uMaterial.ambientColour * uPointLight.ambientColour; - - // 3. Diffuse - // Creates the "matte" effect by calculating the angle between the light direction and the surface normal - - // Normalize the normal vector and calculate the light direction + // 1. Setup Vectors vec3 norm = normalize(Normal); - vec3 lightDir = normalize(uPointLight.position - FragPos); - - // The dot product gives us the cosine of the angle between the two vectors - // The max function ensures we don't get negative values (light coming from behind the surface) - float diff = max(dot(norm, lightDir), 0.0f); - vec3 diffuse = (uPointLight.diffuseColour * diff) * uMaterial.diffuseColour; + vec3 viewDir = normalize(uViewPos - FragPos); - // 4. Specular - // Creates the shiny highlights on the surface - float shininess = 32.0f; - // Calculate the view direction and the reflection direction - vec3 viewDirection = normalize(uViewPos - FragPos); - // If the light is coming from the light direction, we need to negate it for reflection - vec3 reflectDirection = reflect(-lightDir, normalize(Normal)); - - // Calculate the specular component using the dot product and shininess factor - float spec = pow(max(dot(viewDirection, reflectDirection), 0.0f), shininess); - vec3 specular = uMaterial.specularColour * spec * uPointLight.specularColour; + // 2. Calculate Lighting using the Function + vec3 result = CalcPointLight(uPointLight, norm, FragPos, viewDir); - // 5. Combine - // We multiply (Ambient + Diffuse) by the Texture Color. - // We ADD Specular afterwards so the highlights look bright white (like plastic/metal). - vec3 result = (ambient + diffuse) * objColor.rgb + specular; + // 3. Apply Texture Switch + if (uUseTexture == 1) { + outputColor = vec4(result, 1.0) * texture(uTexture, TexCoord); + } else { + outputColor = vec4(result, 1.0); + } - outputColor = vec4(result, objColor.a); } \ No newline at end of file