Blazestorm
Supreme [H]ardness
- Joined
- Jan 17, 2007
- Messages
- 6,940
I'm working on an assignment for a 3D graphics class... it's pretty much done and was just seeing if there was anyway to improve this.
Part of it is a triangle rasterizer which takes three 3D points and draws the triangle that fits between all 3.
One of the things is color interpolation (or anything really...). And right before the triangle rasterizer sets the pixel it converts the color (which is stored as 3 floats, red, green, blue) into unsigned chars by multiplying by 255 and casting to an unsigned char. But what happens is if the value is greater than 255 it rolls over and goes to zero. Well this is an issue with colors because it goes from a solid red to black. This causes artifacts usually on the edges of triangles. And it really only happens if I interpolate between like solid red, and solid green because the values start at 1.0f. If I do some colors inbetween it never rolls-over the size of an unsigned char.
When I have these 6 lines (well 12 if it was "properly" formatted) it takes up 5-6% of my programs processing according to a profiler. Because with 300,000 pixels that's a lot of checks to do.
So I'm just wondering if there's a faster way to do it... some little trick I don't know about...
or this... both have the same performance roughly.
Part of it is a triangle rasterizer which takes three 3D points and draws the triangle that fits between all 3.
One of the things is color interpolation (or anything really...). And right before the triangle rasterizer sets the pixel it converts the color (which is stored as 3 floats, red, green, blue) into unsigned chars by multiplying by 255 and casting to an unsigned char. But what happens is if the value is greater than 255 it rolls over and goes to zero. Well this is an issue with colors because it goes from a solid red to black. This causes artifacts usually on the edges of triangles. And it really only happens if I interpolate between like solid red, and solid green because the values start at 1.0f. If I do some colors inbetween it never rolls-over the size of an unsigned char.
When I have these 6 lines (well 12 if it was "properly" formatted) it takes up 5-6% of my programs processing according to a profiler. Because with 300,000 pixels that's a lot of checks to do.
So I'm just wondering if there's a faster way to do it... some little trick I don't know about...
Code:
if(color.r > 1.0f) color.r = 1.0f;
if(color.g > 1.0f) color.g = 1.0f;
if(color.b > 1.0f) color.b = 1.0f;
if(color.r < 0.0f) color.r = 0.0f;
if(color.g < 0.0f) color.g = 0.0f;
if(color.b < 0.0f) color.b = 0.0f;
or this... both have the same performance roughly.
Code:
color.r = (color.r > 1.0f) ? 1.0f : color.r;
color.g = (color.g > 1.0f) ? 1.0f : color.g;
color.b = (color.b > 1.0f) ? 1.0f : color.b;
color.r = (color.r < 0.0f) ? 0.0f : color.r;
color.g = (color.g < 0.0f) ? 0.0f : color.g;
color.b = (color.b < 0.0f) ? 0.0f : color.b;