Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

Mapping intensity in flat shading

This topic is 710 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
Advertisement
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!