Question
I have this functions that draw a triangle, but they aren't drawing in the position I'm entering. I need help to correct that float implicitLineEquation(float
I have this functions that draw a triangle, but they aren't drawing in the position I'm entering. I need help to correct that
float implicitLineEquation(float x, float y, float x1, float x2, float y1, float y2) { return ((y1 - y2) * x) + ((x2 - x1) * y) + (x1 * y2) - (x2 * y1); }
VERTEX FindBarycentric(float x, float y, float xA, float xB, float xC, float yA, float yB, float yC) { float B = implicitLineEquation(xB, yB, xA, xC, yA, yC); float Y = implicitLineEquation(xC, yC, xB, xA, yB, yA); float a = implicitLineEquation(xA, yA, xC, xB, yC, yB); float b = implicitLineEquation(x, y, xA, xC, yA, yC); float y_ = implicitLineEquation(x, y, xB, xA, yB, yA); float a_ = implicitLineEquation(x, y, xC, xB, yC, yB); VERTEX newPoint; newPoint.x = b / (float)B; newPoint.y = y_ / (float)Y; newPoint.z = a_ / (float)a; newPoint.w = 1; return newPoint; }
void BetterBruteTriangle(const VERTEX v1, const VERTEX v2, const VERTEX v3, unsigned int color) { float StartX = min(min(v1.x, v2.x), v3.x); float StartY = min(min(v1.y, v2.y), v3.y); float EndX = max(max(v1.x, v2.x), v3.x); float EndY = max(max(v1.y, v2.y), v3.y); for (float i = StartY; i < EndY; i++) { for (float j = StartX; j < EndX; j++) { VERTEX vec = FindBarycentric(j, i, v1.x, v2.x, v3.x, v1.y, v2.y, v3.y); if (vec.x >= 0 && vec.x <= 1 && vec.y >= 0 && vec.y <= 1 && vec.z >= 0 && vec.z <= 1) { Pixeldraw(j, i, color); } } } }
void DrawTriangle(const VERTEX start, const VERTEX end, const VERTEX third) { VERTEX copy_start; copy_start.x = start.x; copy_start.y = start.y; copy_start.z = start.z; copy_start.w = 1; VERTEX copy_end; copy_end.x = end.x; copy_end.y = end.y; copy_end.z = end.z; copy_end.w = 1; VERTEX copy_third; copy_third.x = third.x; copy_third.y = third.y; copy_third.z = third.z; copy_third.w = 1;
if (VertexShader) { VertexShader(copy_start); VertexShader(copy_end); }
VERTEX finalS; VERTEX finalE; VERTEX finalT; finalS.x = xNDCtoScreen(copy_start.x); finalS.y = yNDCtoScreen(copy_start.y); finalE.x = xNDCtoScreen(copy_end.x); finalE.y = yNDCtoScreen(copy_end.y); finalT.x = xNDCtoScreen(copy_third.x); finalT.y = yNDCtoScreen(copy_third.y);
unsigned int color = 0;
if (start.color == end.color) { color = start.color; }
if (PixelShader) { PixelShader(color); } BetterBruteTriangle(finalS, finalE, finalT, color); }
DrawTriangle(arr[2], arr[0], arr[3]);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started