I am trying to implement triangle rasterization using this article as reference
as such I have implemented an edge function with an input of 3 points. 2 defining a line and one as the point to be tested, annotated as p
bool SoftwareRendererImp::edgeFunction(float xa, float ya,
float xb, float yb,
float xp, float yp)
{
return ((xp - xa) * (yb - ya) - (yp - ya) * (xb - xa) >= 0);
}
in my rasterization function I am iterating over a set of integers ranging from the lowest to highest x and y values. This is to try and iterate over a box of best fit containing the triangle and test each point to see if it is contained in the triangle.
void SoftwareRendererImp::rasterize_triangle(float x0, float y0,
float x1, float y1,
float x2, float y2,
Color color)
{
// Task 3:
// Implement triangle rasterization
cout << "triangle----------------------------------------------------------------------------------------------------------------------- \n";
float minX = std::min(x0, x1);
minX = std::min(minX, x2);
float minY = std::min(y0, y1);
minY = std::min(minY, y2);
float maxX = std::max(x0, x1);
maxX = std::max(maxX, x2);
float maxY = std::max(y0, y1);
maxY = std::max(maxY, y2);
bool inside;
float px, py;
for (int x = minX; x < maxX; x )
{
for (int y = minY; y < maxY; y )
{
inside = true;
px = x 0.5f;
py = y 0.5f;
inside &= SoftwareRendererImp::edgeFunction(x0, y0, x1, y1, px, py);
inside &= SoftwareRendererImp::edgeFunction(x1, y1, x2, y2, px, py);
inside &= SoftwareRendererImp::edgeFunction(x2, y2, x0, y0, px, py);
if (inside)
{
SoftwareRendererImp::rasterize_point(x, y, color);
cout << "inside: " << x << ", " << y << "\n";
}
else
{
// cout << "outside: " << x << ", " << y << "\n";
}
}
}
}
From my understanding of the linked article this should work however it is not. Can anyone spot what it is that I am screwing up?
CodePudding user response:
this expression was backwards
(xp - xa) * (yb - ya) - (yp - ya) * (xb - xa)
it should have been
(yp - ya) * (xb - xa) - (xp - xa) * (yb - ya)