Per pixel lighting
In this tutorial we’re going to make the previous tutorial look way better. The previous tutorial used only the vertex information to calculate the lighting on the cube. This is a good way of doing it when you want to have your performance high, but if you want a better looking solution, you should take the per pixel lighting. This lighting system calculates the light for each pixel instead of interpolating it between the vertices.

What do we need?
- We are going to use a normal map, since that makes the object look even better. A normal map defines the normals per pixel. A normal defines in what direction the surface points. Well, it’s perpendicular to that surface. To find out more about normal maps, you should look at this web page: wiki surface normal.
- Of course we want to keep track of the normal texture’s ID. That’s why we add an integer IDNormal.
How do we set things up?
- We’re going to apply the normal map to the mesh by calling:
This function is pretty handy. As you see, it’s slightly different from SetTexture. It takes 1 parameter more, that’s the first parameter. You can choose in what layer of textures you want to add the texture. In this case we want to apply it to the normal map layer.
- We need to tell the engine to use the normal map. We do it by calling:
This means it’ll light the mesh per pixel and use the normal map to calculate how the light affects that specific pixel. As you can imagine, this is making it harder for your computer to calculate everything.
How do we render this?
It’s exactly the same as last time, since we didn’t add any new objects.
Things you can add/change yourelf
- You might have asked yourself how normal maps are being made. There are multiple ways of doing this, but the most common are these two:
Buying/download a trial of CrazyBump: crazybump This program really does make it really easy to create normal maps.
Using the nVidia’s photoshop plugin found at: developer.nvidia.com This usually gives a less result than CrazyBump, but you can make the bumping better and higher if you follow this tutorial: cgtextures
- I suggest making a nice texture and normal map and then apply it to our mesh. Be carefull with the size of the texture, since a higher texture requires a longer loading time and most graphics cards don’t support a non power of two texture (so 128×128, 256×256, 512×512 or 1024×1024 should do fine everywhere).