This topic is now archived and is closed to further replies.


Triangle Color

Recommended Posts

hi, I''m coding an small Software Rasterizer, only for learn how does it work. I run into an problem while im coding the triangle rasterizer. I''ve an method like these on : VOID cRasterizer::DrawBottomFlat (Int32 X[3], Int32 Y[3], uInt32 Color[3]); this will draw an Botom Flat Triangle only, it works but only with on color, not with all edge colors, so here is my question : how must i interpolate the color values for ever edge of the triangle? And works this similar for texture and the Z-buffer ??? PS : I work in 32bit mode, mean the color is already convert! pls help me out here :D Thx Imp

Share this post

Link to post
Share on other sites
Can you do vertex colors (as different from edge colors)? If you''ve gotten that far, then you can just, within your edge color code, internally split the triangle into three triangles. If M is the intersection of the medians (side bisectors) of triangle ABC, then split it into triangles MAB, MAC, and MCB. Average the colors, and set the vertex color of each triangle''s vertex at M to be that value. Aside from that, then just have each other vertex be the color of the edge it''s on. I know you''re doing this in software, but the technique I described could also be used in OpenGL, since it has hardware vertex interpolation features.

Share this post

Link to post
Share on other sites
thanks TerranFury for your answer, but i don''t understand this not completely, hmm, is there an other easier way to do this??
Did you have some links or something code snipped or so.. thats show me how does it work?

Thanks anyway Imp

Share this post

Link to post
Share on other sites

I''ve posted here my Bottom Flat rasterizer routine, while i hope you can help me to find my bug inside the color interpolation.
Well, it''s not totaly wrong the grafik output, but there where funy musters, inside my rendered triangle :D

Attention code snipped :D

VOID cRasterizer::DrawBottomFlat (Int32 X[3], Int32 Y[3], uInt32 Color[3])

uInt32 *puiBuffer = m_pOutputDevice->GetDataPtr ();
Int32 iRight = X[0];
Int32 iLeft = iRight;
Float32 fRight = (Float32) iRight;
Float32 fLeft = (Float32) iRight;
Float32 fHeight = (Float32) (Y[2] - Y[0]);
Float32 fWidth = (Float32) (X[2] - X[1]);
Float32 fDeltaLeft = (Float32) (X[1] - X[0]) / fHeight; //(Float32) (Y[1] - Y[0]);
Float32 fDeltaRight = (Float32) (X[2] - X[0]) / fHeight; //(Float32) (Y[2] - Y[0]);

Int32 uiColor = Color[0];

if ((fHeight <= 0.5f && fHeight >= -0.5f) || (fWidth <= 0.5f && fWidth >= -0.5f))

Int32 y = Y[0];
if (y > -1 && y < m_iHeight)
// out of screen coordinate ?
if (!(iLeft < 0 && iRight < 0) &&
!(iLeft > m_iWidth && iRight > m_iWidth))
// flip sides
if (iLeft > iRight)
Int32 iTemp = iRight;
iRight = iLeft;
iLeft = iTemp;
uInt32 uiTempColor = Color[1];
Color[1] = Color[0];
Color[0] = uiTempColor;

if (iLeft < 0)
iLeft = 0;

if (iRight > m_iWidth)
iRight = m_iWidth;

Int32 iPitch = y * m_iWidth;
Int32 x = iLeft;

// Y color deltas
Int32 iYC1 = Color[0] + (y-Y[0]) * ((Color[0] - Color[1]) / fHeight);
Int32 iYC2 = Color[0] + (y-Y[0]) * ((Color[0] - Color[2]) / fHeight);

// X delta color
Int32 iXC1 = (iYC1 - iYC2) / fWidth;

do {
uiColor = iYC1 + (x-iRight) * iXC1;
*(puiBuffer + x + iPitch) = uiColor;
} while (x <= iRight);
fLeft += fDeltaLeft;
fRight += fDeltaRight;
iLeft = (Int32) fLeft;
iRight = (Int32) fRight;
} while (y <= Y[1]);


but it''s totaly unoptimized...*sigh*

pls help, thx Imp

Share this post

Link to post
Share on other sites
What I meant was simply this: divide each triangle into three triangles around the center of the original triangle, and set the color of that central vertex to the average of the colors of the three original verteces. Then just draw those three triangles using a normal vertex-colored method.

Another (sloooooooow) technique might be to, for eac pixel you draw, find it''s distance from each of the edges (there''s a formula for point-to-line distance; I don''t know it offhand though), and interpolate that way.

Also, I''m sorry I didn''t deal with the code you posted, but I''m really, really bad at reading other people''s code.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Why would someone use edge colors ? You can do everything with vertex colors !

Share this post

Link to post
Share on other sites
thanks TerranFury i will try your solution :D
and Anonymous it''s true, you can all do with colors, but how do it right , thats the question *g*

Thx Imp

Share this post

Link to post
Share on other sites