You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When drawing a line, the result changes depending on which endpoint is specified first. This bug only occurs if one of the endpoints is offscreen.
Actual behavior: The left endpoint of the lines is separated somehow in the process of clipping to screen, so the two lines are drawn visibly differently.
Expected behavior: They should draw the same way, so this code would only show a red line.
#include <ti/getcsc.h>
#include <graphx.h>
int main(void)
{
gfx_Begin();
gfx_SetColor(7); // green
gfx_Line(-100, 20, 30, 40);
gfx_SetColor(224); // red
gfx_Line(30, 40, -100, 20); // same numbers as above, just flipped order
while (!os_GetCSC());
gfx_End();
return 0;
}
Result:
Closeup where you can see the divergence:
It may look minor here, but in my game I am often doing this with different colors connecting to things off screen when the camera is moving, and this bug looks bad and causes color flickering.
The text was updated successfully, but these errors were encountered:
LukeBorowy
changed the title
gfx_Line Draws a different line when endpoints are reversed and one is offscreen
gfx_Line Draws a different line when endpoints are swapped and one is offscreen
Jan 23, 2024
Note that this occurs on all 4 edges of the screen, not just going off to the left. For example, going off the top or bottom will vary in the x-coordinate instead.
Ok, I've been testing things out to try to pinpoint this bug. I saw that the graphx code was using the Cohen Sutherland algorithm to clip the lines into frame. I found a C implementation and was testing to see if it exhibited the same bug as the graphx one.
The issue seems to occur here in the algorithm:
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
It did show the same bug, although it was only off by 1 pixel when graphx was off by 2. However, I think the underlying cause is the same: Signed integer division truncates towards 0. This means that when the denominator is negative, as in the red case, 0.61 pixel is not subtracted, so it ends up with y being 1 greater, and the lines don't match up. Maybe in the assembly version this happens twice for some reason so it is off by 2.
I think that if there was a division that rounds strictly down instead of toward 0, and used instead of _DivideHLBC in the gfx_Line function, it would solve this bug. However, I don't know assembly or if that would be significantly slower.
Alternatively, I've just been using this:
void gfx_ConsistentLine(int x0, int y0, int x1, int y1) {
if (x0 < x1) {
gfx_Line(x0, y0, x1, y1);
} else {
gfx_Line(x1, y1, x0, y0);
}
}
It will make sure the same endpoint is put first. In the case of a vertical line it couldn't tell, but the bug doesn't happen with a vertical line so it's ok.
When drawing a line, the result changes depending on which endpoint is specified first. This bug only occurs if one of the endpoints is offscreen.
Actual behavior: The left endpoint of the lines is separated somehow in the process of clipping to screen, so the two lines are drawn visibly differently.
Expected behavior: They should draw the same way, so this code would only show a red line.
Result:
![image](https://private-user-images.githubusercontent.com/28664080/298815417-01c0b545-e46e-47b1-947f-a3c0a8cb75fd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA1Mjk5NzYsIm5iZiI6MTcyMDUyOTY3NiwicGF0aCI6Ii8yODY2NDA4MC8yOTg4MTU0MTctMDFjMGI1NDUtZTQ2ZS00N2IxLTk0N2YtYTNjMGE4Y2I3NWZkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA5VDEyNTQzNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUzZDI2ZDUyNjlkYzg5MmU0ZGIzM2UxY2YwYjQyNzZjODExMTE2ZTk5Y2Q0ZWFhYzNiNDQxMWRjNjMyMzdkYzEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.yK6bWNEWiHxsPjpL8bfSerCL1EdSZ60OaNUMBWLtbQE)
Closeup where you can see the divergence:
![image](https://private-user-images.githubusercontent.com/28664080/298815957-c9b029d2-4129-4f43-bd2f-ec30491c06cc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA1Mjk5NzYsIm5iZiI6MTcyMDUyOTY3NiwicGF0aCI6Ii8yODY2NDA4MC8yOTg4MTU5NTctYzliMDI5ZDItNDEyOS00ZjQzLWJkMmYtZWMzMDQ5MWMwNmNjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA5VDEyNTQzNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNkYjczNTQ3MTE1ZTk5NWFlMDJjZWFmMzMzZGVkNmQxOWNhMjA0MjE0NzBlMGVkZjBmZjZmN2I3YzRkZTdlYmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.bCiTlXzCTLfKKwjBlcQ8K4g-kC_B70GvIbK-dT-X38Y)
It may look minor here, but in my game I am often doing this with different colors connecting to things off screen when the camera is moving, and this bug looks bad and causes color flickering.
The text was updated successfully, but these errors were encountered: