#### Archived

This topic is now archived and is closed to further replies.

# Basic lighting help needed

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m having some problems understanding some basic lighting concepts. I know this is VERY basic, but i do need some explanation. Thanks for your time, here it goes: Looking at shaders recently i decided to start by doing the old style vertex lighting in a vertex program and then doing pixel lighting on an fragment program as my second step with one point light. I did this from what i remembered of the usual lighting procedures (LightVec dot Normal, etc). I did it in a way that my calculations where done in untransformed space (don''t know the term used here, but i mean simply the untransformed points for the model''s vertices and light position... is this calles Model Sapce?), pasing the eye position as a parameter to the shadwer. The results are correct, because no transformation is applied to anything in the scene, except for the camara and projection wich affects the whole scene, hence no change. I guess that this is not the correct way of doing. I suppose i should transform all vertices and normals to eye space (is it called this way? I mean multiplying by the modelview matrix so that vertices and normals are relative to the camera), as well as the light position so the calculations are right no matter what transformation there is. Is this correct? On the other hand, i took a look at the basic example that comes with the Cg Toolkit docs. This is it:
// Define inputs from application.

struct appin
{
float4 Position : POSITION;
float4 Normal : NORMAL;
};
// Define outputs from vertex shader.

struct vertout
{
float4 HPosition : POSITION;
float4 Color : COLOR;
};
vertout main(appin IN,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewIT,
uniform float4 LightVec)
{
vertout OUT;
// Transform vertex position into homogenous clip-space.

OUT.HPosition = mul(ModelViewProj, IN.Position);
// Transform normal from model-space to view-space.

float3 normalVec = normalize(mul(ModelViewIT,
IN.Normal).xyz);
// Store normalized light vector.

float3 lightVec = normalize(LightVec.xyz);
// Calculate half angle vector.

float3 eyeVec = float3(0.0, 0.0, 1.0);
float3 halfVec = normalize(lightVec + eyeVec);
// Calculate diffuse component.

float diffuse = dot(normalVec, lightVec);
// Calculate specular component.

float specular = dot(normalVec, halfVec);
// Use the lit function to compute lighting vector from

float4 lighting = lit(diffuse, specular, 32);
// Blue diffuse material

float3 diffuseMaterial = float3(0.0, 0.0, 1.0);
// White specular material

float3 specularMaterial = float3(1.0, 1.0, 1.0);
// Combine diffuse and specular contributions and

// output final vertex color.

OUT.Color.rgb = lighting.y * diffuseMaterial +
lighting.z * specularMaterial;
OUT.Color.a = 1.0;
return OUT;
}

The whole lighting idea i do understand as I said before, but my problem with this algo is where it says it transforms the Normal from model space (Untransformed??) to View Space (Camera transformation?)... it multyplies the normal by the Inverse Transpose of the modelview matrix. Eing? I dont get this... if i understand well, that will the the oposite, backwards transformation!! It''s like if the normal WAS already transformed from its untrasformed state and we would wan''t it back to it again. Shouldn''t one do this by just multiplying the normal with the ModelView matrix? On the other hand, this algo gets the light vector. I''m using point lights (as an excercise, this is nothing serious so performance is not important), so i guess I need to get the light vector (lightPos - vertexPos untransformed) and then transform this vector to camera space. So, can someone explain WHERE do i fail to understand things? I do understand the basic algebra things (vectors, operations between them, matrix multiplications for changing vector bases, etc), but I still have a long way to go and somehow this is getting me really confused. I need some light here! Thank you very much, really.

• ### Forum Statistics

• Total Topics
628645
• Total Posts
2984028

• 9
• 9
• 9
• 10
• 21