PDA

View Full Version : Dinamic lights



Coluna
08-04-2003, 08:11 PM
I'm writing a demo which will use dinamic lights. I read that u should use binormals and tangent(used for bump mapping too) from the faces to do that in the right way. What's the better way to store the vertexes and faces (the structs/classes)?
i'm thinking this way:
class cVECTOR3D
{
float x,y,z;
};

class cFACE
{
cVECTOR3D verts[3]; //or
int verts[3]; //to eliminate
//doubled entries
int tex_coord[2];

cVECTOR3D normal;
cVECTOR3D binormal;
cVECTOR3D tangent;
};
Can i use binormal and tangent to calculate both dinamic lights and bump mapping? Is those structs rights or is it missing something?
Thanx (and sorry about my english)

Jan
08-05-2003, 12:12 AM
1. You donīt need binormals and tangents for dynamic lighting, only for bump-mapping.

2. You should add a float for the plane-equation of that triangle, to be able to do backface-culling.

Jan.

Coluna
08-05-2003, 09:56 AM
But when using 2d textures, how do i calculate the S and T coordinates from the dinamic light without binormal and tangent?

Jan
08-05-2003, 12:07 PM
First you create a 3D lighting texture.

Then you put your base texture into Texture Unit 0 and your 3D lightmap into Texture Unit 1.

Put into the texcoords of TU 0 your usual texcoords and into the texcoords of TU 1 you put the vertex-position (so you need 3 texcoords on TU 1).

The activate TU 1 and call:

glMatrixMode (GL_TEXTURE);
glLoadIdentity ();
glScalef (1 / Lighradius, 1 / Lightradius, 1 / Lightradius);
glTransform (-Lightpos.x + (Lightradius/2), -Lightpos.y + (Lightradius/2), -Lightpos.z + (Lightradius/2));
glMatrixMode (GL_MODELVIEW);

Thatīs it. With this setup you can perform dynamic lighting (without bumpmapping). When doing bumpmapping, it gets a bit harder. For bumpmapping vertex-shaders come in VERY handy + you need to use register combiners or fragment programs (nVidia vs. ATI).

Anyway, you might like to read this article:
http://www.ronfrazier.net/apparition/

(under "Research Project"). Itīs quite helpful.

Hope that helps you.
Jan.

EDIT:

With this line iīm not so sure:

glScalef (1 / Lighradius, 1 / Lightradius, 1 / Lightradius);
glTransform (-Lightpos.x + (Lightradius/2), -Lightpos.y + (Lightradius/2), -Lightpos.z + (Lightradius/2));

It could be, that you first have to transfom and than to scale (but i donīt think so) and it could be, that you donīt need to negate the Lightposition in the transformation. But i think itīs shown correctly in the article i mentioned.

[This message has been edited by Jan2000 (edited 08-05-2003).]

Coluna
08-05-2003, 07:14 PM
Thanx Jan...i helps me, but the problem is that i wanna do it with only 2d textures for lighting ... and other question: if my texture units were full, can i do the lighting (with 3d texture) in a different pass, using blend?

Coluna
09-04-2003, 10:53 AM
I used a 3d texture for lighting (instead of 2 textures), but in a radeon 7500 (that is supposed to support it), it runs very slowly...but i saw a demo (but with directx) that runs at mid-speed...is 3d textures done by software in radeon 7500? or could it be another error im doing?

Coluna
09-04-2003, 10:57 AM
sorry...i found out that using GL_CLAMP_TO_EDGE was the problem...now im using only GL_CLAMP and it works fine...

Jan
09-04-2003, 02:25 PM
I donīt know much about ATI cards, so i canīt help you with that.

However i think in the article i mentioned, he does use a 1D and a 2D texture instead of a 3D texture. Both solutions should be equally fast, but both have their drawbacks:
A 3D texture uses a LOT of memory, so you cannot use a very highres texture (i would say more than 128*128*128 is insane), BUT you only need on texture-unit (which can be very important).
With the other approach you can use much higher resolutions, so the light quality will be better, but on cards with only 2 or 4 texture-units, you will pull your hair out, when you want to do some more advanced stuff and need that one additional texture-unit!

And yes, you can do several passes for lighting and stuff. This is how i do it:

1. z-only pass

then for every light

2. diffuse, bumpmapped, attentuated light
stored in alpha channel

3. base-texture blended with the alpha-channel (plus some other effects)

4. specular lighting, attentuated by the value which is stored in the alpha-channel

Actually my bumpmapped specular lighting is not physically correct. However it is high-quality "specular" lighting and looks pretty good. Real bumpmapped specular lighting is not possible without fragment programs (i have a Geforce 4). At least not high-quality bumpmapped specular lighting (and donīt tell me about texture-shaders and stuff, i tried it all!)

I think itīs strange that GL_CLAMP_TO_EDGE is a slowdown on a Radeon. I use clamp-to-edge and it doesnīt slow down a bit. But with GL_CLAMP it should just work fine. But make sure that your texture-border color is always set to black, or you might get artifacts.

If it helps you: "Game Programming Gems 1" has an article about attentuation maps (1D + 2D textures) and explains how to use them in OpenGL for dynamic lighting.

Dynamic lighting is real fun, but itīs not easy to optimize. Get used to framerates of 20 or 30 FPS.

Hope i could help you.
Jan.