Implementing whole GL lighting equation

Full GL lighting is calculated as:

vertex_col =
emission +
ambient_colambient_material +
diffuse_col
diffuse_material +
specular_something*…

…and so on.

Is there a vertex/fragment program(s) which implement FULL lighting equation, no just diffuse lighting?

[This message has been edited by EnemyOfTheStateMachine (edited 01-06-2004).]

[This message has been edited by EnemyOfTheStateMachine (edited 01-06-2004).]

I have done it for point and directional lights in vp: http://www.geocities.com/vmelkon/verybasicvp.html

(the site seems to be down. Says too many visitors)

I’ve also done in fp, but haven’t uploaded it.
Also did it for spot lights in fp. It looks a few million times better in fp.

Yeah, it’s not especially difficult. Once you figure out how to get the light positions and eye position properly transformed, it’s just a matter of turning the equation into code – which isn’t all all hard, especially in GLSL.

Note that the full lighting equation (with specular) is one of the samples in the ARB_ program specifications.

Also, the math itself is defined in the OpenGL specification, and written out in in the red book. Writing the code is an excercise in typing, mostly. Especially if you do it in Cg and compile to ARB_ assembly :slight_smile:

Note that you need a (slightly) different program for each light type: point light, spot light and directional light. Some of them can sometimes be merged using cute tricks, but it’s more robust if you use different programs specialized for each kind.

I think I’m not computing attenuation in there. So you may need to plug in a few extra instructions.

With NV_vp2, it’s possible to code all light types in a single program.

Also, if you want to implement the whole GL lighting in fp, then you can do at most 2 lights per pass because of instruction limitations on ATI. 2 point lights with no attenuation that is.

If you really need the FULL OpenGL lighting equation you also need to handle two sided lighting correctly. Besides the asembly extension spec there’s also cod3e for this in the GLSL spec if I remember correctly.

Originally posted by EnemyOfTheStateMachine:
Is there a vertex/fragment program(s) which implement FULL lighting equation, no just diffuse lighting?

As others have said there are examples out there. However you will not be able to have a vertex program fully emulate the standard OpenGL lighitng model since you can’t do something like this yet:
for(i = 0; i < 8; i++)
{
switch(light[i].type)
{
case directional:

break;
case point:

break;
case spot:

break;
}
}

I haven’t been following the rumours very well. When do the rumours claim we’ll be able to do something like this? Next generation?

I admit I havn’t used NV_vp2 yet but I think that by using this extension, that code there can be done by replacing the switch and case with ifs.

EDIT: Um yeah I forgot to mention that I’m talking about using Cg with the profile that spits out NV_vp2 code. Not by typing ifs in a NV_vp2 program directly, as the compiler would go bananas.

-SirKnight

[This message has been edited by SirKnight (edited 01-10-2004).]

Titan,

To “fully” handle the pipeline, you have to split the shader in two for front- and back-facing lighting (if you use two-sided). You can then just assemble the vertex program (and/or fragment program) using text snippets that are written to handle point, spot and directional lights, for all enabled lights.

Thus, it IS possible, although depending on the quality you want to use per light (iterate on root finding, etc) you may run out of shader instructions on some cards.