Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lighting

Started by CRACK123 Dec 29, 2003 at 1:28 AM 3 replies 550+ views
Original Post
CRACK123
CRACK123
Hi, I am trying to do my own lighting routines and facing some issues with it. I am trying to follow Foley to do the lighting. But it does not seem to be correct. I am trying to get a something similar to the default GL lighting. As from Foley I understand that intensity
quote:
I = IaKa + IdKdcos(phi) and cos(phi) = DotProduct(Surface_Normal, Light_Direction) Hence I = IaKa + IdKd * DotProduct(Surface_Normal, Light_Direction)
Now I setup a structure to get r, g, b, and a value using the following structure
// vector4 is a class having x, y, z, w as floats

// vector3 is a class having x, y, z as floats


typedef struct {
   vector4 ambient, diffuse;     // ambient and diffuse components

   float ambFactor, diffFactor; // coefficents

   vector4 normals;             
   vector4 finalColor;          // final color to be used

} Light;
Then I went and did the calculations for the final color doing the following
vector4 SetLight(vector3 position, Light &light)
{
    light.finalColor.set(0, 0, 0, 1);

    light.ambient.set(0, 0, 0, 1);
    light.ambFactor = 1.0f;
    light.ambient *= light.ambFactor;

    light.diffuse.set(1, 1, 1, 0);
    light.diffFactor = 1.0f;
    light.diffuse *= light.diffFactor;

    light.position.set(0, 0, 1, 0);
    vector4 pos(position);
    vector4 dir = light.position - pos;
    vector3 n = Normalized(position);
    vector4 normal(n);

    float theta = DotProduct(normal, dir.normalize());

    light.diffuse *= theta;

    light.finalColor += light.ambient + light.diffuse;

    return light.finalColor;    
}
It however does not seem to be doing it right. I am not sure if I am doing the calculations right of if my undertsanding of doing the lighting calculations is flawed. I would like to know if my calculations are right or not or if something needs to be done differently ? Thank you.
The more applications I write, more I find out how less I know
quasar3d
quasar3d
I don't really understand where you get your normal from. The normal is a vector that's perpendicular to the plane you are lighting. It doesn't belong to the light, it belongs to the surface. And why is the finalColour in the light struct? Every point you light will get it's own finalColour.

You seem to want to do diffuse and ambient lighting. This is pretty easy:

For every point you want to light, you need the normal. If you don't want smooth shading, this is just the normal of the polygon that point lays on.
Now you need two vecters. First the light vector. This vector points in the direction of the light, and has length one:

VECTOR3D lightVec = light.pos - point.pos;
lightVec.Normalize();

the normal is usually precalculated

Now we take the (3D) dot product of these two vectors to calculate the light intensity:
float theta = DotProduct(lightVec,point.normal);

finalColour = diffuse * theta + ambient;

This takes only the incidence angle into account. If you want it to look good, you need some sort of attenuation. By example linear attenuation:

float lightDist = sqrtf(lightVec.x * lightVec.x + lightVec.y * lightVec.y + lightVec.z * lightVec.z);
float falloff = 1 - lightDist / range;
if(falloff < 0)
falloff = 0;

finalColour = (diffuse * theta + ambient) * falloff;

This is for point lights btw. for directional lights it's even easier. The lightVec is a fixed vector, and there's no attenuation.

My Site

[edited by - Quasar3D on December 29, 2003 8:31:08 AM]
CRACK123
CRACK123
quote:
Original post by quasar3d
I don''t really understand where you get your normal from. The normal is a vector that''s perpendicular to the plane you are lighting. It doesn''t belong to the light, it belongs to the surface. And why is the finalColour in the light struct? Every point you light will get it''s own finalColour.

You seem to want to do diffuse and ambient lighting. This is pretty easy:




Thats correct, I want to only do diffuse and ambient lighting at the moment before going further. The finalColor contains x,y,z,w which would analogous to r, g, b, a.

quote:
Original post by quasar3d
For every point you want to light, you need the normal. If you don''t want smooth shading, this is just the normal of the polygon that point lays on.



I am doing it in using smooth shading so I would assume I need a normal for every vertex.

quote:
Original post by quasar3d
Now you need two vecters. First the light vector. This vector points in the direction of the light, and has length one:

VECTOR3D lightVec = light.pos - point.pos;
lightVec.Normalize();

the normal is usually precalculated

Now we take the (3D) dot product of these two vectors to calculate the light intensity:
float theta = DotProduct(lightVec,point.normal);

finalColour = diffuse * theta + ambient;

This takes only the incidence angle into account. If you want it to look good, you need some sort of attenuation. By example linear attenuation:

float lightDist = sqrtf(lightVec.x * lightVec.x + lightVec.y * lightVec.y + lightVec.z * lightVec.z);
float falloff = 1 - lightDist / range;
if(falloff < 0)
falloff = 0;

finalColour = (diffuse * theta + ambient) * falloff;

This is for point lights btw. for directional lights it''s even easier. The lightVec is a fixed vector, and there''s no attenuation.



I did it like this without the falloff and partly it seems to work. But it seems to do constant coloring everywhere. This I do not wish to happen. The coloring seems to be constant. Should I be using face normals or vertex normals. At the point I am calculating normals per vertex (I guess you call these psuedo normals or something).
The more applications I write, more I find out how less I know
quasar3d
quasar3d
quote:
Original post by CRACK123
light.position.set(0, 0, 1, 0);
vector4 pos(position);
vector4 dir = light.position - pos;
vector3 n = Normalized(position);
vector4 normal(n);

float theta = DotProduct(normal, dir.normalize());
[/source]



Why do you set the normal to the normalized position here? The normal is included in the vertex, right?
CRACK123
CRACK123
quote:
Original post by quasar3d
quote:
Original post by CRACK123
light.position.set(0, 0, 1, 0);
vector4 pos(position);
vector4 dir = light.position - pos;
vector3 n = Normalized(position);
vector4 normal(n);

float theta = DotProduct(normal, dir.normalize());
[/source]



Why do you set the normal to the normalized position here? The normal is included in the vertex, right?


Hey, I made a mistake there. Yes, the normal is already already there. I am using the normal of the 3D model file itself. But using the normals too it colors everything, it doesn't show any kind of lighting effect. It seems to apply same color to all vertices. So its like constant everywhere.

[edited by - CRACK123 on December 30, 2003 6:07:00 PM]
The more applications I write, more I find out how less I know

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.