Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
87 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Setting up Direct3D
 Rendering a
 Wireframe
 Isometric View

 Let's Texture This
 Beast!

 Adding Lighting
 Something More
 Impressive!

 Wrapping This
 All Up


 Printable version

 


Adding Lighting to the Engine

DEMO3 adds random colored lights to the renderer. A random color light is applied to each vertex of every tile, on every frame. The result is decidedly trippy, and has a nasty habit of making me think of disco. Despite this, it is a good example of the speed at which Direct3D can apply Gouraud shading on most cards. I remember working hard to do this in regular 2D, with just DirectDraw - and arriving with framerates around 5 fps. Direct3D makes it so ridiculously easy to use this form of lighting that I've been kicking myself ever since for not trying it earlier!

A more advanced form of lighting uses lightmaps. These are basically a grayscale texture, applied as the second texture with SetTexture(1,texture) and a call to ensure that the renderer knows that it should alpha-blend the second texture. Direct3D makes doing this incredibly fast. However, lightmapping is a sufficiently large topic to warrant its own article - so I'm only going to say that it can be done (and quickly, substantially more quickly than any DirectDraw solution I've seen thus far). Vertex lighting should be more than enough to get you started on the Direct3D road.

DEMO3's GameInit and GameDone are unchanged from DEMO2. GameMain calls Demo3Render instead of Demo2Render, but that's really all that changes in the non-rendering code.

Demo3Render itself isn't substantially different from Demo2Render, either. The only difference is that inside the inner loop, before DrawPrimitive is called, I set the color value of each vertex to a random float. D3DRGB meshes red, green and blue floating point values between 0 and 1 into whatever color format Direct3D happens to be using within D3DCOLOR. The entirety of the random lighting code is this (it's a little inelegant, I just wanted to demonstrate the color property's power:

Vertex[0].color = D3DRGB(rand()/5000,rand()/5000,rand()/5000); Vertex[1].color = D3DRGB(rand()/5000,rand()/5000,rand()/5000); Vertex[2].color = D3DRGB(rand()/5000,rand()/5000,rand()/5000); Vertex[3].color = D3DRGB(rand()/5000,rand()/5000,rand()/5000);




Next : Something More Impressive!