Sign in to follow this  

Mapping intensity in flat shading

This topic is 402 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 have a 16bit RGB565 and I have the result from dot product and its 0->1.

I would like to map the dot product value from light source to the normal vector in a mesh to 16bit color value.

uint16_t floatToRGB565(float input)
{
uint8_t x = input*255;
uint8_t r = (x&248u);//0xff-0x07, 5msb mask
uint8_t g = (x&252u);//0xff-0x03, 6msb mask
uint8_t b = (x&248u);//0xff-0x07, 6msb mask

return (r<<8)|(g<<3)|(b>>3);// assuming r is at msb
}



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

// test if light ray is reflecting off surface


if (dp > 0)
{

float intens ;
uint16_t colorMod;
int r,g,b;
float Red, Green, Blue, result;
intensity = ambient_light + dp;

if (intensity > 1)
intensity = 1;
if (intensity < 0)
intensity = 0;
r = the_object->polys[curr_poly].color >> 11;
g = (the_object->polys[curr_poly].color >>5)&0x00FF;
b = the_object->polys[curr_poly].color & 0x0000FF;
Red = r*intensity;
Green = g*intensity;
Blue = b*intensity;
result = (int)(Red * 31) << 11 | (int)(Green * 63) << 5 | (int)(Blue * 31);
colorMod = floatToRGB565(result);
the_object->polys[curr_poly].shade = colorMod;
// printf("\nintensity of polygon %d is %f",curr_poly,intensity);

} // end if light is reflecting off surface
else{
int r,g,b;
float Red, Green, Blue, result;
float colorModulate;
uint16_t colorMod;
r = the_object->polys[curr_poly].color >> 11;
g = (the_object->polys[curr_poly].color >>5)&0x00FF;
b = the_object->polys[curr_poly].color & 0x0000FF;
Red = r*ambient_light;
Green = g*ambient_light;
Blue = b*ambient_light;
colorModulate = (int)(Red * 31) << 11 | (int)(Green * 63) << 5 | (int)(Blue * 31);
colorMod = floatToRGB565(colorModulate);
the_object->polys[curr_poly].shade = colorMod;
}
} // end if use flat shading

Share this post


Link to post
Share on other sites

This topic is 402 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.

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