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
- Lighting Models: Mathematical models that simulate how light interacts with surfaces.
- Shading Techniques: Methods to determine the color of pixels based on lighting models.
- Light Sources: Different types of lights used in 3D scenes.
- Surface Properties: Characteristics of surfaces that affect how they reflect light.
Types of Light Sources
- Ambient Light: General light present in the scene, affecting all objects equally.
- Point Light: Emits light from a single point in all directions.
- Directional Light: Emits parallel light rays in a specific direction, simulating a distant light source like the sun.
- 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
- Phong Lighting Model: A widely used model that includes ambient, diffuse, and specular reflections.
- Blinn-Phong Model: A variation of the Phong model that uses a different approach for specular highlights.
- Gouraud Shading: Calculates lighting at vertices and interpolates the results across the surface.
- Phong Shading: Calculates lighting per pixel, providing more accurate and smoother results.
Phong Lighting Model
The Phong lighting model combines three components:
- Ambient Reflection: Simulates indirect light. \[ I_a = k_a \cdot I_{ambient} \]
- Diffuse Reflection: Simulates direct light scattered equally in all directions. \[ I_d = k_d \cdot (L \cdot N) \cdot I_{light} \]
- 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
- Objective: Implement the Phong lighting model in a 3D scene.
- 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.
- Solution: Follow the provided example code and modify it to suit your scene.
Exercise 2: Comparing Shading Techniques
- Objective: Compare Gouraud and Phong shading techniques.
- Steps:
- Implement both Gouraud and Phong shading in your 3D scene.
- Render the scene using each technique and observe the differences.
- 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
- Incorrect Normal Calculation: Ensure normals are correctly transformed to world space.
- Lighting Direction: Verify the direction vectors are normalized.
- 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.
Mathematics 3D
Module 1: Fundamentals of Linear Algebra
- Vectors and Vector Spaces
- Matrices and Determinants
- Systems of Linear Equations
- Eigenvalues and Eigenvectors
Module 2: Linear Transformations
- Definition and Properties
- Transformation Matrices
- Rotations, Translations, and Scalings
- Composition of Transformations
Module 3: Geometry in 3D Space
- Coordinates and Planes
- Vectors in 3D Space
- Dot Product and Cross Product
- Equations of Planes and Lines