XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
My XNA Book
       Go to section on this site

Additional info

Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


XNA 2.0 Game Programming Recipes

Riemer Grootjans

Chapter 6: Adding Light to Your Scene in XNA 2.0 (60 pages)

Lighting a scene sounds very easy: set the positions of the objects in your 3D world, define the positions of your lights, and—bingo!—you expect your 3D scene to be lit correctly. Although it seems trivial to have your graphics card do this for you, it definitely is not.

For each surface of your objects, the graphics card needs to calculate the amount of lighting that is received by the surface. This amount is based on the angle between the direction of the light and the normal direction of the triangle. Luckily, the XNA Framework comes with the BasicEffect, which is capable of performing all these calculations for you. The first part of this chapter shows you how you can use the BasicEffect to lighten up your scene.

However, as its name implies, the BasicEffect can be used only for some basic lighting effects. What if you want to have point lights in your scene, such as candles? Or what if you want to have your scene lit by many lights? And why don’t your objects cast any shadows?

To answer these questions, you’ll have to code your own effects in HLSL. The second part of this chapter starts by showing you how you can create HLSL effects with the same functionality of the BasicEffect.

This chapter doesn’t stop where the BasicEffect stops, however. You’ll learn how to implement additional functionality, such as support for point lights and spotlights. Even more, the final recipes introduce you to the world of deferred rendering, allowing you to light your scene by a large number of lights simultaneously. To top it off, you’ll learn how you can combine the
shadow mapping technique with deferred rendering so all your objects cast shadows.

6-1. Define Normals and Use the BasicEffect
This recipe shows you what normals are, how you can find them, and why they are absolutely crucial when you want your 3D scene to be lit correctly.

6-2. Share Normals Between Vertices
To obtain smooth lighting on your objects, vertices that are shared between multiple triangles should also share the normal vectors of these triangles. This recipe shows you the difference in resulting lighting between per-triangle vertices and shared vertices.

6-3. Add Higher Detail to Your Lighting: Per-Pixel Lighting
In case your objects have sharp corners or are made out of large triangles, you will need per-pixel lighting to correctly light your objects. This recipe explains why and how this can be done using the BasicEffect.

6-4. Add Specular Highlights to Reflective Surfaces
Specular Highlights are regions of high reflectivity, found on shiny surfaces such as metal or hard plastics.

6-5. Add HLSL Vertex Shading
Taking one step further, you learn how you can implement the basic lighting functionality of the BasicEffect using your own HLSL code.

6-6. Define a Point Light Using HLSL
The BasicEffect only support directional lights, like the sunlight. In this recipe you learn how you can add a point light to your 3D scene using a HLSL effect, which is a point emitting light towards each direction.

6-7. Add HLSL Per-Pixel Lighting
In this recipe you learn why and how you should calculate the lighting in the pixel shader.

6-8. Define a Spotlight Using HLSL
A spotlight is much like a directional light, but instead of shining into all directions, it transmits light only into a certain cone.

6-9. Add HLSL Specular Highlights
Learn how your HLSL effects can add specular highlights to the surfaces of your objects.

6-10. Add Multiple Lights to Your Scene Using Deferred Shading
In this recipe, you learn a totally different approach to rendering your 3D world. You will no longer do the (lighting) calculations in each pixel as your 3D scene is being transformed into a 2D image. Instead, you will first render your 3D scene into a 2D image with no special effects whatsoever, and add these special effects such as lighting later on. Since this is a 2D operation, it allows you to use multiple effects (such as multiple lights) without causing slowdowns.

6-11. Add Shadowing Capability to Your Deferred Shading Engine
This recipe builds on the previous one, as it shows how you can extend the Deferred Rendering technique to support shadows.

Recipe 6-11: Multiple lights simultaneously lighting a 3D scene using Deferred Rendering, casting correct shadows.


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !

- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award

2007 - 2011 MVP Award
DirectX - XNA


XNA 2.0 Recipes Book (8)
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
XNA 3.0 Recipes Book (8)
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
-- Expand all --

Thank you!

Support this site --
any amount is welcome !

Stay up-to-date

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!