Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

mapping intensity from dot product to float RGB 0->1

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting the dot product of the surface normal and light position. The weird thing is the dot product is larger than 1. That's problem number 1.

The second problem is I want to map that intensity from 0->1 to Float RGB from 0->1 here is my code

if (the_object->polys[curr_poly].shading == 1)
{

// compute the dot product between the light source vector
// and normal vector to surface

dp = Dot_Product_3D((vector_3d_ptr)&normal,
(vector_3d_ptr)&light_source);

// test if light ray is reflecting off surface

if (dp > 0)
{
// now cos 0 = (u.v)/|u||v| or

intensity = ambient_light + ( dp / (the_object->polys[curr_poly].normal_length));


float r = the_object->polys[curr_poly].color.R*intensity;
float g = the_object->polys[curr_poly].color.G*intensity;
float b = the_object->polys[curr_poly].color.B*intensity;
Color color = Color(r, g, b, 1);
}

 

Share this post


Link to post
Share on other sites
Advertisement
1) you need to find a vector starting from a point on the 3D surface pointing to the light position (light_pos - point_pos) if you are doing point lighting. this vector (as well as the surface normal vector) needs to be normalized before you calculate the dot product. The way you have it set up, it appears you are treating your light as a direct light.

2) the dot product will return a value ranging from -1 to +1. Clamp your dot product result to 0 (before adding the ambient factor).

3) multiplying with the resulting intensity the way that you are doing is the correct way.

Share this post


Link to post
Share on other sites

here is the updated code, there is flickering in the flat shading

if (the_object->polys[curr_poly].shading == 1)
{ 
  float mag;


float normx = normal.x;
float normy = normal.y;
float normz = normal.z;
mag = Vector_Mag_3D((vector_3d_ptr)&normal);
// compute the dot product between the light source vector
// and normal vector to surface
normal.x = normx / mag;
normal.y = normy / mag;
normal.z = normz / mag;






dp = Dot_Product_3D((vector_3d_ptr)&normal,
(vector_3d_ptr)&light_source);


// test if light ray is reflecting off surface
if (dp < 0)
dp = 0;


if (dp > 0)
{
// now cos 0 = (u.v)/|u||v| or


intensity = ambient_light + ( dp / (the_object->polys[curr_poly].normal_length));


float r = (float)the_object->polys[curr_poly].color.R*intensity;
float g = (float)the_object->polys[curr_poly].color.G*intensity;
float b = (float)the_object->polys[curr_poly].color.B*intensity;
Color color = Color(r, g, b, 1);
// intensity now varies from 0-1, 0 being black or grazing and 1 being
// totally illuminated. use the value to index into color table


the_object->polys[curr_poly].shade =  color.ToUInt32();


// printf("\nintensity of polygon %d is %f",curr_poly,intensity);


} // end if light is reflecting off surface
else{
uint32_t color = the_object->polys[curr_poly].color.ToUInt32();
the_object->polys[curr_poly].shade = color;// *(int)ambient_light;
}
} // end if use flat shading

here is a video

Share this post


Link to post
Share on other sites
I should have been more explicit. So it appears that you intend to use your light source as the actual direction vector. I'm quite sure the flickering is because you aren't normalizing your light_source vector, so your intensity is being scaled by the length of light_pos. Try this:


float mag = vector_mag_3D( light_pos );
vector light_dir = light_pos / mag;

float dp = dot_product_3D( light_dir, norm);

if( dp < 0.0 ) dp = 0.0;

Intensity = ambient_light + dp;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!