Sign in to follow this  
mlt

Transforms in vertex program?

Recommended Posts

I am currently using the vertex program from the Cg Toolkit:
struct vertex_input
{
  float4 position : POSITION;
  float4 normal   : NORMAL;
};

struct vertex_output
{
  float4 position : POSITION;
  float4 color    : COLOR;
};

vertex_output main(
                     vertex_input IN
                   , uniform float4x4 ModelViewProj 
                   , uniform float4x4 ModelViewIT   
                   , uniform float4   light_vector	
             )
{
  vertex_output OUT;  
 
  // Transform vertex position into homogenous clip-space.
  OUT.position = mul(ModelViewProj, IN.position);  
  
  // Transform normal from model-space to view-space/eye-space
  float3 normalVec = normalize(mul(ModelViewIT, IN.normal).xyz);
  
  // Store normalized light vector.
  float3 lightVec  = normalize(light_vector.xyz);
  
  // Calculate half angle vector.
  float3 eyeVec    = float3(0.0, 0.0, 1.0);  
  //float3 halfVec   = normalize(lightVec);
  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
  // diffuse and specular values.
  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;  
}

In the Cg tutorial the chain of spaces are defined in the following way: Object/modelSpc -> WorldSpc -> Eye/ViewSpc -> ClipSpc -> WindowSpc. But in the second statement in the above vertexprogram the normal is transformed into EyeSpc using the "inverse" modelView matrix ModelViewIT. But does ModelViewIT not transform FROM EyeSpc TO ObjectSpc and not the other way around??

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this