# problems with rasterizing

This topic is 3898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, I've been trying to find the bug in this code for a while now, it's to rasterize a triangle using Bresenham's line algorithm but there's somthing wrong somewhere, I'm using it for perspective mapping but i've cut all the code for that out because i'm sure its somthing to do with the rasterizing part, here's a screen shot: I've highlighted the part's that don't look right. When I use the texture mapping code the UV coordinates are out of bounds around the edges aswell. here's the code(I got most of this from chris heckers website)

struct edge
{
edge(const Vertex * vertices, int Top, int Bottom);
inline void Step();
long X, XStep;
long Numerator, Denominator; // DDA info for x
long ErrorTerm;
int Y, Height; // current y and vert count
float xStep; // to determine midlle - left or right
};

edge(const Vertex * vertices, int Top, int Bottom)
{
Y = vertices[Top].y;
int YEnd = vertices[Bottom].y;
Height =  YEnd - Y;
int Width = vertices[Bottom].x - vertices[Top].x;

if(Height != 0)
xStep = Width/(float)Height;
else xStep = Width;

if(Height) {
ErrorTerm = 0;
X = vertices[Top].v.x + 1;

FloorDivMod(Width,Height,XStep,Numerator);
Denominator = Height;
}
}

inline void FloorDivMod(long Numerator, long Denominator, long &Floor, long &Mod)
{
assert(Denominator > 0);
if(Numerator >= 0) { // positive case, C is okay
Floor = Numerator / Denominator;
Mod = Numerator % Denominator;
} else { // Numerator is negative
Floor = -((-Numerator) / Denominator);
Mod = (-Numerator) % Denominator;
if(Mod) { // there is a remainder
Floor--;
Mod = Denominator - Mod;
}
}
}

inline void edge::Step()
{
X += XStep;
Y++;
Height--;

ErrorTerm += Numerator;
if(ErrorTerm >= Denominator) {
X++;
ErrorTerm -= Denominator;
}
}

void DrawScanLine(edge * pLeft, edge * pRight, UINT * pDestBits, int mem_pitch)
{
int XStart = pLeft->X;
int Width = pRight->X - XStart;

pDestBits += (pLeft->Y * mem_pitch) + XStart;

while(Width-- > 0) {
*(pDestBits++) = 100;
}

}

void Triangle(Vertex * vertices UINT * video_buffer, int mem_pitch)
{
int Top, Middle, Bottom;
float Y0 = vertices[0].v.y;
float Y1 = vertices[1].v.y;
float Y2 = vertices[2].v.y;

// sort vertices in y
if(Y0 < Y1) {
if(Y2 < Y0) {
Top = 2; Middle = 0; Bottom = 1;
} else {
Top = 0;
if(Y1 < Y2) {
Middle = 1; Bottom = 2;
} else {
Middle = 2; Bottom = 1;
}
}
} else {
if(Y2 < Y1) {
Top = 2; Middle = 1; Bottom = 0;
} else {
Top = 1;
if(Y0 < Y2) {
Middle = 0; Bottom = 2;
} else {
Middle = 2; Bottom = 0;
}
}
}

edge TopToBottom(vertices,Top,Bottom);
edge TopToMiddle(vertices,Top,Middle);
edge MiddleToBottom(vertices,Middle,Bottom);
edge *pLeft, *pRight;
int MiddleIsLeft;

// determine whether middle is on left side or right
if(TopToMiddle.xStep > TopToBottom.xStep) {
MiddleIsLeft = 0;
pLeft = &TopToBottom; pRight = &TopToMiddle;
} else {
MiddleIsLeft = 1;
pLeft = &TopToMiddle; pRight = &TopToBottom;
}

int Height = TopToMiddle.Height;

while(Height--) {
DrawScanLine(pLeft, pRight, video_buffer, mem_pitch);
TopToMiddle.Step();
TopToBottom.Step();
}

Height = MiddleToBottom.Height;

if(MiddleIsLeft) {
pLeft = &MiddleToBottom;
pRight = &TopToBottom;
} else {
pLeft = &TopToBottom;
pRight = &MiddleToBottom;
}

while(Height--) {
DrawScanLine(pLeft, pRight, video_buffer, mem_pitch);
MiddleToBottom.Step();
TopToBottom.Step();
}

}


can anyone see what's wrong? [Edited by - staticVoid2 on March 14, 2008 9:14:38 AM]

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011308
• ### Who's Online (See full list)

There are no registered users currently online

×