Tutorials - Lighting in 3D

For those who are interested, I’ve started a series of posts on how to create great 3D lighting effects, here

http://coolcodea.wordpress.com/2013/09/24/112-lighting-in-3d-introduction/

I’m going to cover the main types of light (sunlight, spotlights, a torch beam) and the effects they produce, and provide a code library you can use in your projects.

You need to understand meshes and shaders to follow it, so it is not for absolute beginners*. It is also pretty difficult, and I really struggled with it. I hope these posts make it easier for other people!
(* but if you are a beginner and want to understand lighting, you don’t need a degree in Computer Science - I was in your position 6 months ago - so start learning about meshes, then shaders, I have lots of blog posts on both. Just be patient, it may take you a while).

I don’t claim to be any kind of expert, and it is difficult to judge how clear my explanations of this difficult topic are. So comments and suggestions are welcome.

Very nice. Skimmed through the lot and it takes a good approach to building everything up. The only bit I’d have to play with is your space conversion, because tbh I only got it working one way (everything to object space), and I’m sure you are right for the moving normals to world space, but I found the inverse transpose model matrix worked for moving light vectors from world to object, and it seems the same math to move normals from object to world…

The other subnote… you normalize your normals and light vectors so you don’t have to do the length bits in diffuse and specular lighting, which is good. One gotcha to note, is if your normals are different for the 3 vertices of a triangle they will be interpolated across the triangle. This allows you to give the impression of curved surfaces rather than flat surfaces (good for spheres). Note, the interpolation will not keep them at the unit normalized length and you will need to renormalize them in the fragment shader. If your surfaces are “flat” ie the normals are identical at each vertex you can ignore this problem.

Thanks @spacemonkey. Wrt normals, I included the option to average normals for each vertex, ie if a vertex is used by 3 different triangles, average their normals. This gives a nice smooth feel to terrain.

Yep, I should get the inverse transpose thing working for normals, it just didn’t work for me for some reason. There are lots of gotchas in lighting!!

@Ignatz this looks really helpful, it sounds like you understand it quite well yourself to be explaining it as well as you do. I had a read through it and just being the introduction it’s made me understand a couple things I couldn’t get my head around before, thanks!

@Ignatz yes, averaging the normals between the surrounding triangles is exactly what you want, but then it’s being aware that in the fragment shader they may have moved from the unit (normalized) length due to interpolation.

@spacemonkey - yes, I definitely renormalise them in the fragment shader!