How To Render Light Maps in XNA

If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

Light mapping is a method for handling the lighting of a surface, including shadows, for static geometry. There are two mainstream techniques, (shadow mapping and shadow volumes) that can render
dynamic models and static models in real-time, but they cost some performance. Ray-tracing is the highly-realistic method for calculating the lighting properties of objects, but the cost in time is
very high and it is hard to render a complex scene in real-time. For static models, light mapping is a better method, because it requires just a light map texture and a second set of texture
coordinates in the model file (such as a .X file, exported by Panda 3ds Max plug-in. Light mapping can be used with multiple light sources at no additional cost. For example, shadow mapping will add
some extra rendering passes to the scene with multiple light sources).

How can we generate a light map? We can use the "render to texture" function of 3ds Max to generate a light map and save the texture coordinates in UV Channel 2. The light map is storing the
intensities of light on the object’s surface. It can be an RGB color texture. When we render a model with light map, each pixel from the usual color map is modulated by the color stored in the
light map (access via the second texture coordinate stored in the FVFData block of a .X file).

You can find the complete source code (for XNA Game Studio 3.0) for this article and 3ds Max files here

W,S,A,D to move the camera, click and drag mouse middle button to rotate the camera

Part 1: Create 3D models and their light maps

Step1: (file: Content\step1, original material.max)

Add Box and Cylinder - Assign textures to their Diffuse Maps

Add Target Directional Light

Shadows:

Lenable Shadows [V] on

select "Ray Traced Shadows" or "Shadow Map"

Directional Parameters - use proper Hotspot/Beam

Step 2: (file: Content\step2,render t texture.max)

Selected Box and Cylinder

Rendering -> Render to Texture

Output Page:

push “Add” button and select ”LightingMap” in Add Texture Elements

set Target Map Slot as “Self-Illumination”

Baked Material Page:

click the radio of “Output Into Source“

Objects to Bake Page -> Mapping Coordinates Option:

Object==>set “Use Automatic Unwrap“

set Channel as2

Click “Render” button

Delete the ”Target Directional Light”

The light map will be generated and assigned automatically in the “Self-Illumination” map

Push F9 or click “Quick Render”, the shadow will appear on Box01 and Cylinder01

Step3: (file: Content\step3,use shader.max)

Select Box1 and Change Material to be Direct Shader and "Keep old material as sub-material"

Select the Shader of "Content\LightMapEffect.fx"

Assign the Diffuse Map and its Light Map and Select the Technique as "Technique1"

Push F3 to show the Box01

Push F9 or click “Quick Render”, the shadow will appear on Box01 and Cylinder01(the scene without light object)

Step4: Export 3D Models with Panda plug-in

At last, you can export the model using the Panda plug-in; the second texture coordinate will be written into the FVFData block in
the .X file. Remember to enable the "Include .fx file" and "Include .fx parameters" options.

Step5: Show the light map result

Use the max file of Step2

If you want to see the light map in 3ds max with sharp shadow, you must re-assign the new materials (with DirectX Shader – LightMap) to Box01 and Cyliner01.