[WIP] adding clipping into azur line shader

This commit is contained in:
Sylvain PILLOT 2023-05-09 22:49:25 +02:00
parent 0d014a542a
commit 5907c27706
1 changed files with 55 additions and 14 deletions

View File

@ -52,26 +52,66 @@ void azrp_line(int xA, int yA, int xB, int yB, int color)
{
prof_enter(azrp_perf_cmdgen);
int x1, x2;
int y1, y2;
//clipping algorithm as per "Another Simple but Faster Method for 2D Line Clipping"
//from Dimitrios Matthes and Vasileios Drakopoulos
//International Journal of Computer Graphics & Animation (IJCGA) Vol.9, No.1/2/3, July 2019
int xmin = 0;
int xmax = azrp_width;
int ymin = 0;
int ymax = azrp_height;
//step 1 line are fully out of the screen
if ((xA<xmin && xB<xmin) || (xA>xmax && xB>xmax) || (yA<ymin && yB<ymin) || (yA>ymax && yB>ymax)) {
prof_leave(azrp_perf_cmdgen);
return;
}
int x1, x2, y1, y2;
// we swap to always start with the point on top as the fragment are updated from top to bottom
if (yA <= yB)
{
x1 = xA;
y1 = yA;
x2 = xB;
y2 = yB;
// (x1,y1) is the most top point and (x2,y2) is the most bottom point (rankig as per y values only
if (yA <= yB) {
x1 = xA; y1 = yA;
x2 = xB; y2 = yB;
}
else
{
x1 = xB;
y1 = yB;
x2 = xA;
y2 = yA;
else {
x1 = xB; y1 = yB;
x2 = xA; y2 = yA;
}
//step 2 line clipping within the box (xmin,ymin) --> (xmax,ymax)
int x[2];
int y[2];
x[0] = x1; x[1] = x2;
y[0] = y1; y[1] = y2;
for(int i=0; i<2; i++) {
if (x[i] < xmin) {
x[i] = xmin; y[i] = ((y2-y1) * (xmin-x1)) / (x2-x1) + y1;
}
else if (x[i] > xmax) {
x[i] = xmax; y[i] = ((y2-y1) * (xmax-x1)) / (x2-x1) + y1;
}
if (y[i] < ymin) {
x[i] = ((x2-x1) * (ymin-y1)) / (y2-y1) + x1; y[i] = ymin;
}
else if (y[i] > ymax) {
x[i] = ((x2-x1) * (ymax-y1)) / (y2-y1) + x1; y[i] = ymax;
}
}
if((x[0] < xmin && x[1] < xmin) || (x[0] > xmax && x[1] > xmax)) {
prof_leave(azrp_perf_cmdgen);
return;
}
x1 = x[0];
y1 = y[0];
x2 = x[1];
y2 = y[1];
int frag_first = y1 >> 4;
int frag_last = y2 >> 4;
@ -93,6 +133,7 @@ void azrp_line(int xA, int yA, int xB, int yB, int color)
cmd.i = 0;
cmd.cumul = (cmd.dx >= cmd.dy) ? cmd.dx >> 1 : cmd.dy >> 1;
exit:
azrp_queue_command(&cmd, sizeof cmd, frag_first, frag_count);
prof_leave(azrp_perf_cmdgen);
}