احمد - 7 months ago 44

C Question

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);

}

Answer

I assume you understand what scalar products are, in any case they are `|a|*|b|*cos Θ`

. they could certainly be over or under 1 since cos Θ is at most 1, multiplying the vectors, unless they are unit vectors may certainly lead to various numbers.

also, to map between intensity to RGB, (if RGB is in standard 0-255 range), multiplying it won't help you much, you need to normalize it between the range, like this:

`(color / 255) * intensity`

you could also clamp the color between the values so you don't get overexposure or the likes.

please clarify further the issues if this does not help. (I can't comment yet so I answered as much as I could).

Source (Stackoverflow)