Advertisement

Two triangles are not rendering properly.

Started by May 04, 2018 05:49 PM
0 comments, last by Weird buffalo 11454 6 years, 6 months ago

Capture.PNG.3feb106643e021701ed638a8a75ed8b5.PNG

 

I am getting this weird line between the two rendered triangles.

I have a function that sorts the vertices of the triangle in ascending order in terms of y - coordinate, then it divides the triangle in two halves and render them separately.


void Triangle::drawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, const DWORD &color) {
	Triangle::sortAsecY(v1, v2, v3);

	//Checking if the triangle has only one half
	if (v1.y == v2.y) {
		Triangle::drawBottomTriangle(v3, v1, v2, color);
		return;
	}
	else if (v2.y == v3.y) {
		Triangle::drawTopTriangle(v1, v2, v3, color);
		return;
	}
	else {

		//Dividing the triangle into two halves.
		Vector2 v4(v1.x + (v1.y - v2.y) / (v1.y - v3.y) * (v3.x - v1.x), v2.y);
		Triangle::drawTopTriangle(v1, v2, v4, color);
		Triangle::drawBottomTriangle(v3, v2, v4, color);
	}
}

 

Here's the function to draw top half of the triangle,


void Triangle::drawTopTriangle(Vector2 &v1, Vector2 &v2, Vector2 &v3, const DWORD &color){
	if (v2.x > v3.x) {
		float temp = 0.0f;
		SWAP(v2.x, v3.x, temp);
		SWAP(v2.y, v3.y, temp);
	}

	float invSlope1 = (v1.x - v2.x) / (v1.y - v2.y);
	float invSlope2 = (v1.x - v3.x) / (v1.y - v3.y);

	int y1 = normToScreenCoordinatesY(v1.y);
	int y3 = normToScreenCoordinatesY(v3.y);

	float curX1 = (float)normToScreenCoordinatesX(v2.x);
	float curX2 = (float)normToScreenCoordinatesX(v3.x);

	if (curX1 > curX2) {
		float temp = curX1;
		curX1 = curX2;
		curX2 = temp;
	}

	for (int i = y3; i >= y1; i--) {
		Triangle::drawHLine((int)roundf(curX1), (int)roundf(curX2), i, color);

		curX1 += invSlope1;
		curX2 += invSlope2;
	}
}

 

normToScreenCoordinatesX  and normToScreenCoordinatesY are the functions that converts normalised device coordinates into viewport coordinates. drawHLine draws a horizontal line between two points with same y - coordinates. I am pretty these three are working properly.

I think the error might be in the line "Triangle::drawHLine((int)roundf(curX1), (int)roundf(curX2), i, color);" I think the error is due to using "roundf", I think should be using ceil but I am not sure why I should be doing that ? I want to know the reason for doing so.

 

Edit:

Nevermind what I said about source of error, I am getting same result regardless of using ceilf or floorf. Please help me.

 

 

This topic is closed to new replies.

Advertisement