Lighting and shading are crucial components in 3D graphics that bring realism and depth to rendered scenes. This section will cover the fundamental concepts, types of lighting models, shading techniques, and practical examples to help you understand and apply these principles in 3D graphics.

Key Concepts

  1. Lighting Models: Mathematical models that simulate how light interacts with surfaces.
  2. Shading Techniques: Methods to determine the color of pixels based on lighting models.
  3. Light Sources: Different types of lights used in 3D scenes.
  4. Surface Properties: Characteristics of surfaces that affect how they reflect light.

Types of Light Sources

  1. Ambient Light: General light present in the scene, affecting all objects equally.
  2. Point Light: Emits light from a single point in all directions.
  3. Directional Light: Emits parallel light rays in a specific direction, simulating a distant light source like the sun.
  4. Spotlight: Emits light in a cone shape, with a specific direction and angle.
Light Source Description Example Use Case
Ambient Light Uniform light affecting all objects General scene illumination
Point Light Light radiating from a single point Light bulb
Directional Light Parallel rays from a distant source Sunlight
Spotlight Light in a cone shape Flashlight, stage lighting

Lighting Models

  1. Phong Lighting Model: A widely used model that includes ambient, diffuse, and specular reflections.
  2. Blinn-Phong Model: A variation of the Phong model that uses a different approach for specular highlights.
  3. Gouraud Shading: Calculates lighting at vertices and interpolates the results across the surface.
  4. Phong Shading: Calculates lighting per pixel, providing more accurate and smoother results.

Phong Lighting Model

The Phong lighting model combines three components:

  1. Ambient Reflection: Simulates indirect light. \[ I_a = k_a \cdot I_{ambient} \]
  2. Diffuse Reflection: Simulates direct light scattered equally in all directions. \[ I_d = k_d \cdot (L \cdot N) \cdot I_{light} \]
  3. Specular Reflection: Simulates the bright spot of light that appears on shiny surfaces. \[ I_s = k_s \cdot (R \cdot V)^n \cdot I_{light} \]

Where:

  • \( k_a, k_d, k_s \) are the ambient, diffuse, and specular reflection coefficients.
  • \( L \) is the light direction vector.
  • \( N \) is the normal vector at the surface point.
  • \( R \) is the reflection vector.
  • \( V \) is the view direction vector.
  • \( n \) is the shininess coefficient.

Example Code: Phong Lighting in GLSL

// Vertex Shader
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

out vec3 FragPos;
out vec3 Normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    FragPos = vec3(model * vec4(aPos, 1.0));
    Normal = mat3(transpose(inverse(model))) * aNormal;  
    gl_Position = projection * view * vec4(FragPos, 1.0);
}

// Fragment Shader
#version 330 core
out vec4 FragColor;

in vec3 FragPos;
in vec3 Normal;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;

void main()
{
    // Ambient
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;
    
    // Diffuse 
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;
    
    // Specular
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);  
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    vec3 specular = specularStrength * spec * lightColor;  
    
    vec3 result = (ambient + diffuse + specular) * objectColor;
    FragColor = vec4(result, 1.0);
}

Explanation

  • Vertex Shader: Transforms vertex positions and normals to world space.
  • Fragment Shader: Implements the Phong lighting model by calculating ambient, diffuse, and specular components.

Practical Exercises

Exercise 1: Implementing Phong Lighting

  1. Objective: Implement the Phong lighting model in a 3D scene.
  2. Steps:
    • Set up a basic 3D scene with a single object.
    • Implement vertex and fragment shaders as shown in the example.
    • Adjust the light position, view position, and material properties.
  3. Solution: Follow the provided example code and modify it to suit your scene.

Exercise 2: Comparing Shading Techniques

  1. Objective: Compare Gouraud and Phong shading techniques.
  2. Steps:
    • Implement both Gouraud and Phong shading in your 3D scene.
    • Render the scene using each technique and observe the differences.
  3. Solution:
    • Gouraud Shading: Calculate lighting in the vertex shader and interpolate.
    • Phong Shading: Calculate lighting in the fragment shader for more accuracy.

Common Mistakes and Tips

  1. Incorrect Normal Calculation: Ensure normals are correctly transformed to world space.
  2. Lighting Direction: Verify the direction vectors are normalized.
  3. Shininess Coefficient: Adjust the shininess coefficient to achieve the desired specular highlight.

Conclusion

In this section, we covered the basics of lighting and shading in 3D graphics. We explored different types of light sources, lighting models, and shading techniques. By implementing the Phong lighting model and comparing shading techniques, you can enhance the realism of your 3D scenes. Practice these concepts through the provided exercises to solidify your understanding and application of lighting and shading in 3D graphics.

© Copyright 2024. All rights reserved