mirror of
https://github.com/UOH-CS-Level5/551455-graphics-programming-2526-the-repo-Zyb3rWolfi.git
synced 2025-11-29 00:43:08 +00:00
refracted lighting again
This commit is contained in:
parent
fe63a6d938
commit
d2b3027a11
@ -11,12 +11,21 @@ public struct Lighting
|
|||||||
public Vector3 diffuseColour;
|
public Vector3 diffuseColour;
|
||||||
public Vector3 specularColour;
|
public Vector3 specularColour;
|
||||||
|
|
||||||
|
public float constant;
|
||||||
|
public float linear;
|
||||||
|
public float quadratic;
|
||||||
|
|
||||||
public Lighting(Vector3 pos, Vector3 ambient, Vector3 diffuse, Vector3 specular)
|
public Lighting(Vector3 pos, Vector3 ambient, Vector3 diffuse, Vector3 specular)
|
||||||
{
|
{
|
||||||
position = pos;
|
position = pos;
|
||||||
ambientColour = ambient;
|
ambientColour = ambient;
|
||||||
diffuseColour = diffuse;
|
diffuseColour = diffuse;
|
||||||
specularColour = specular;
|
specularColour = specular;
|
||||||
|
|
||||||
|
constant = 1.0f;
|
||||||
|
linear = 0.09f;
|
||||||
|
quadratic = 0.032f;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply(Shader shader)
|
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.ambientColour"), ref ambientColour);
|
||||||
GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.diffuseColour"), ref diffuseColour);
|
GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.diffuseColour"), ref diffuseColour);
|
||||||
GL.Uniform3(GL.GetUniformLocation(shader.Handle, "uPointLight.specularColour"), ref specularColour);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -16,6 +16,7 @@ public class RenderObject
|
|||||||
public Matrix4 Transform = Matrix4.Identity;
|
public Matrix4 Transform = Matrix4.Identity;
|
||||||
public Quaternion Rotation = Quaternion.Identity;
|
public Quaternion Rotation = Quaternion.Identity;
|
||||||
public Vector3 Scale = Vector3.One;
|
public Vector3 Scale = Vector3.One;
|
||||||
|
public bool UseTexture = true;
|
||||||
|
|
||||||
public RenderObject(Mesh mesh, Shader shader, Texture texture, Material material, Lighting lighting)
|
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);
|
Material mat = new Material(pink * 0.1f, pink * 0.6f, Vector3.One, 32.0f);
|
||||||
|
|
||||||
Shader.Use();
|
Shader.Use();
|
||||||
|
if (Texture != null && UseTexture)
|
||||||
|
{
|
||||||
Texture.Use();
|
Texture.Use();
|
||||||
Shader.SetInt("uTexture", 0);
|
Shader.SetInt("uTexture", 0);
|
||||||
|
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GL.Uniform1(GL.GetUniformLocation(Shader.Handle, "uUseTexture"), 0);
|
||||||
|
}
|
||||||
|
|
||||||
Material.Apply(Shader);
|
Material.Apply(Shader);
|
||||||
light.Apply(Shader);
|
light.Apply(Shader);
|
||||||
|
|||||||
@ -22,6 +22,10 @@ struct LightProperty {
|
|||||||
vec3 ambientColour;
|
vec3 ambientColour;
|
||||||
vec3 diffuseColour;
|
vec3 diffuseColour;
|
||||||
vec3 specularColour;
|
vec3 specularColour;
|
||||||
|
|
||||||
|
float constant;
|
||||||
|
float linear;
|
||||||
|
float quadratic;
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- UNIFORMS ---
|
// --- UNIFORMS ---
|
||||||
@ -29,53 +33,52 @@ uniform sampler2D uTexture; // Image Texture
|
|||||||
uniform vec3 uViewPos; // Camera Position
|
uniform vec3 uViewPos; // Camera Position
|
||||||
uniform MaterialProperty uMaterial; // Material Properties
|
uniform MaterialProperty uMaterial; // Material Properties
|
||||||
uniform LightProperty uPointLight; // Light 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()
|
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
|
// 1. Setup Vectors
|
||||||
// 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
|
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 lightDir = normalize(uPointLight.position - FragPos);
|
vec3 viewDir = normalize(uViewPos - FragPos);
|
||||||
|
|
||||||
// The dot product gives us the cosine of the angle between the two vectors
|
// 2. Calculate Lighting using the Function
|
||||||
// The max function ensures we don't get negative values (light coming from behind the surface)
|
vec3 result = CalcPointLight(uPointLight, norm, FragPos, viewDir);
|
||||||
float diff = max(dot(norm, lightDir), 0.0f);
|
|
||||||
vec3 diffuse = (uPointLight.diffuseColour * diff) * uMaterial.diffuseColour;
|
|
||||||
|
|
||||||
// 4. Specular
|
// 3. Apply Texture Switch
|
||||||
// Creates the shiny highlights on the surface
|
if (uUseTexture == 1) {
|
||||||
float shininess = 32.0f;
|
outputColor = vec4(result, 1.0) * texture(uTexture, TexCoord);
|
||||||
// Calculate the view direction and the reflection direction
|
} else {
|
||||||
vec3 viewDirection = normalize(uViewPos - FragPos);
|
outputColor = vec4(result, 1.0);
|
||||||
// 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;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
outputColor = vec4(result, objColor.a);
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user