A.D.A. Amiga Demoscene Archive

        Welcome guest!

  

  

  

log in with SceneID

  

Demos Amiga Demoscene Archive Forum / Coding / How to do vertex lighting with light ?

 

Author Message
uzo
Member
#1 - Posted: 31 Aug 2007 15:41
Reply Quote
Hi all,

I got simple fake phong shading (env mapping) up and running, without a light source. This is done by taking the vertex normals and indexing them in a phongmap image. I'd like to add lights to the equation, but I'm kind of lost on the how in terms of calculating the light map u v indexes with the vertexnormal vs light ?

Anyone got some pseudo code ?

/u
winden
Member
#2 - Posted: 4 Sep 2007 10:56
Reply Quote
Hmmm, usually when I've seen dynamic lights there was no env.mapping going on, instead you used a base fixed texture on the object and then over than a gouraud shaded light.

To calc the intensity of the gouraud, there are 2 easy ways:

1. calc. the distance from the vertex position to the light position

2. calc. the angle between the vertex normal and the light position

I'd try the distance one first, then the angle, and then try to calc both and mix them by calcing the max or multiplying them or somesuch.

a good example of the distance based technique with light moving over the scenery can be seen towards the end of "the engine" by ramses, released at euskal party 5.
Kalms
Member
#3 - Posted: 6 Sep 2007 12:54
Reply Quote
Alternatively, if you want to show the light using environment mapping, what you need to do is to rotate your vertex normals again such that they are oriented relative to the light (not relative to the camera). Then you can take the normal's X/Y coordinates as texture coordinates just like before.

You'll get the new rotation matrix by calculating a look-at matrix from object to light.

There is one snag though: your envmap will also light up the side of the object which is facing away from the lightsource. You'll need to do something to keep the 'dark' side dark. Apply a different texture to triangles which are not facing toward the lightsource, or clamp the color values output for those triangles, or... there are many hacks, none of which is both fast and foolproof.
doom
Member
#4 - Posted: 11 Sep 2007 00:04
Reply Quote
For diffuse lighting you want the angle between vertex normal and light position (in fact you probably want the cosine of that, so dot product FTW). For specular highlights you want the angle between the reflection of a ray cast from the camera against the vertex in question and the light source. It's another dot product.

Maybe you want to consider a dynamic envmap instead though. Use (x,y) from the world-space vertex normals to do the envmap, but move the lightsource around in your texture. That way you can get handsome backlighting and multiple lightsources.

 

  Please log in to comment

  

  

  

 

A.D.A. Amiga Demoscene Archive, Version 3.0