What are the fastest algorithms for drawing solid, filled polygons? Thanks.
Dan East
Dan East
A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a>b)?(a):(b))
#define MaxPoint(a,b) ((a.y > b.y) ? a : b)
#define MinPoint(a,b) ((b.y > a.y) ? a : b)
#define MaxPoint3(a,b,c) MaxPoint(MaxPoint(a,b),MaxPoint(b,c))
#define MidPoint3(a,b,c) MaxPoint(MinPoint(a,b),MinPoint(a,c))
#define MinPoint3(a,b,c) MinPoint(MinPoint(a,b),MinPoint(b,c))
typedef struct {
short r,g,b;
} _Color;
typedef struct {
short x, y;
_Color Color;
} _2D;
/**
** Flat Shader
*/
void polygonFlat(_2D p1,_2D p2,_2D p3) {
PIXEL* buffer=getbuffer();
_2D p1d,p2d,p3d;
int xd1,yd1,xd2,yd2,i;
int Lx,Rx;
p1d = MinPoint3(p1,p2,p3);
p2d = MidPoint3(p2,p3,p1);
p3d = MaxPoint3(p3,p1,p2);
if(p2.y<p1.y) {
p1d=MinPoint3(p2,p1,p3);
p2d=MidPoint3(p1,p3,p2);
}
xd1=p2d.x-p1d.x;
yd1=p2d.y-p1d.y;
xd2=p3d.x-p1d.x;
yd2=p3d.y-p1d.y;
if(yd1)
for(i=p1d.y;i<=p2d.y;i++) {
Lx=p1d.x+((i-p1d.y)*xd1)/yd1;
Rx=p1d.x+((i-p1d.y)*xd2)/yd2;
if(Lx!=Rx)
FLAT_HLine(buffer,MIN(Lx,Rx),MAX(Lx,Rx),i,p1.Color.r,p1.Color.g,p1.Color.b);
}
xd1=p3d.x-p2d.x;
yd1=p3d.y-p2d.y;
if(yd1)
for(i=p2d.y;i<=p3d.y;i++) {
Lx=p1d.x+((i-p1d.y)*xd2)/yd2;
Rx=p2d.x+((i-p2d.y)*xd1)/yd1;
if(Lx!=Rx)
FLAT_HLine(buffer,MIN(Lx,Rx),MAX(Lx,Rx),i,p1.Color.r,p1.Color.g,p1.Color.b);
}
}
void FLAT_HLine(PIXEL* buffer,int x1,int x2,int y,int r,int g,int b) {
int x;
int rgb=(r<<11)+(g<<5)+b;
buffer+=(x1+y*240);
for(x=x1;x<=x2;x++)
*buffer++=rgb;
}
void DrawTriangle(Point p1,Point p2,Point p3, unsigned long c)
{
Fixed dx1, dx2, i;
Fixed Lx,Rx;
if(p2.y < p1.y){
Lx = p1.x;
Rx = p1.y;
p1.x = p2.x;
p1.y = p2.y;
p2.x = Lx;
p2.y = Rx;
}
if(p3.y < p2.y){
Lx = p2.x;
Rx = p2.y;
p2.x = p3.x;
p2.y = p3.y;
p3.x = Lx;
p3.y = Rx;
}
if(p2.y < p1.y){
Lx = p1.x;
Rx = p1.y;
p1.x = p2.x;
p1.y = p2.y;
p2.x = Lx;
p2.y = Rx;
}
dx1 = fixdiv( (p2.x - p1.x),(p2.y - p1.y) );
dx2 = fixdiv( (p3.x - p1.x),(p3.y - p1.y) );
Lx = p1.x;
Rx = p1.x;
if(!dx1)
Lx = p2.x;
for(i=p1.y; i<=p2.y; i+=FIXED_ONE)
{
HLine2(
(MIN(Lx,Rx)+FIXED_HALF)>>16,
(MAX(Lx,Rx)+FIXED_HALF)>>16,
i,
c);
Lx += dx1;
Rx += dx2;
}
Lx -= dx1;
dx1 = fixdiv( (p3.x - p2.x),(p3.y - p2.y) );
Lx += dx1;
for(i = p2.y + FIXED_ONE; i <= p3.y; i+=FIXED_ONE)
{
HLine2(
(MIN(Lx,Rx)+FIXED_HALF)>>16,
(MAX(Lx,Rx)+FIXED_HALF)>>16,
i,
c);
Lx += dx1;
Rx += dx2;
}
}
onedivtab
DCD 0x00000000,0x00000000,0x80000000,0x55555555,0x40000000,0x33333333,0x2AAAAAAA,0x24924924
DCD 0x20000000,0x1C71C71C,0x19999999,0x1745D174,0x15555555,0x13B13B13,0x12492492,0x11111111
DCD 0x10000000,0x0F0F0F0F,0x0E38E38E,0x0D79435E,0x0CCCCCCC,0x0C30C30C,0x0BA2E8BA,0x0B21642C
DCD 0x0AAAAAAA,0x0A3D70A3,0x09D89D89,0x097B425E,0x09249249,0x08D3DCB0,0x08888888,0x08421084
DCD 0x08000000,0x07C1F07C,0x07878787,0x07507507,0x071C71C7,0x06EB3E45,0x06BCA1AF,0x06906906
DCD 0x06666666,0x063E7063,0x06186186,0x05F417D0,0x05D1745D,0x05B05B05,0x0590B216,0x0572620A
DCD 0x05555555,0x05397829,0x051EB851,0x05050505,0x04EC4EC4,0x04D4873E,0x04BDA12F,0x04A7904A
DCD 0x04924924,0x047DC11F,0x0469EE58,0x0456C797,0x04444444,0x04325C53,0x04210842,0x04104104
And so forth, up to 4096 entries.
; r0 = dividend r1 = divisor
; works on 16:16 fixed point
fixdiv
stmfd sp!,{r2-r4} ; push some registers
;; r4 = signflag
mov r4,#0
tst r1,#0x80000000 ; see if neg bit is set
subne r1,r4,r1 ; negate r1 if r1 is negative
subeq r4,r4,#1 ; set r4 to -1 if unsigned else 0 if signed
ldr r2,=0xFFF00000
mov r3,#0
tst r1,r2
bne fixdiv_highbitvalue
tst r1,#0x0000F000
movne r3,#4
tst r1,#0x000F0000
movne r3,#8
b fixdiv_knowbits
fixdiv_highbitvalue
mov r3,#12
tst r1,#0x0F000000
movne r3,#16
tst r1,#0xF0000000
movne r3,#20
fixdiv_knowbits
; r3 = divisior rightshifter
mov r1,r1,lsr r3
ldr r2,=onedivtab
ldr r2,[r2,r1,lsl #2]
mov r2,r2,lsr r3
smull r0,r1,r2,r0
mov r0,r0,lsr #16 ; shift right of low
add r0,r0,r1,lsl #16 ; shift left of high
tst r4,r4
subeq r0,r4,r0 ; negate result if r4=0 (ie signed number)
ldmfd sp!,{r2-r4} ; pop some registers
bx lr
; mov pc,lr ; this is for return to a 32bit ARM code.
; "Optimised" (generally towards a StrongARM) by Refractor
; r0 = dividend r1 = divisor
; works on 16:16 fixed point
fixdiv
stmfd sp!,{r2-r3,r12} ; push some registers
;R using the ARM procedure call standard, r2 and r3 (maybe r12) don't need to be saved in the above
;R (or unstacked below)
;R if using this from the embedded Visual C++, R12 is defined as a temporary register, (this may be true in the ARM PCS too... I can't remember)...
;R if so we can use R12 and not stack R4 (or R12)... then we don't need to use the stack *at all*
mov r12,r1 ;R we can just store the number rather than the 0/-1 flag
cmp r1,#0 ;R
rsbmi r1,r1,#0 ;R if it's negative, negate it ... one cycle saved
ldr r2,=0xFFF00000
tst r1,r2
bne fixdiv_highbitvalue
mov r3,#0 ; moved after the comparison .. one cycle saved for the highbitvalue
tst r1,#0x0000F000
movne r3,#4
tst r1,#0x000F0000
movne r3,#8
b fixdiv_knowbits
fixdiv_highbitvalue
mov r3,#12
tst r1,#0x0F000000
movne r3,#16
tst r1,#0xF0000000
movne r3,#20
fixdiv_knowbits
; r3 = divisor rightshifter
mov r1,r1,lsr r3
ldr r2,=onedivtab
ldr r2,[r2,r1,lsl #2]
mov r2,r2,lsr r3
smull r0,r1,r2,r0
cmp r12,#0 ;R doing this here saves us one cycle (StrongARM, maybe others) because we have to wait
;R for the result of the SMULL for 1-3 cycles anyway.
;R So, anything that doesn't use the result can go in the wait period.
mov r0,r0,lsr #16 ; shift right of low
add r0,r0,r1,lsl #16 ; shift left of high
; removed tst r4,r4
; removed subeq r0,r4,r0 ; negate result if r12=0 (ie signed number)
rsbmi r0,r0,#0 ;R I *presume* that it was supposed to re-negate the number we negated earlier
ldmfd sp!,{r2-r3, r12} ; pop
;R some/all registers (may not be necessary, see above)
; targetting at a non-thumb processor
; bx lr
mov pc,lr ; this is for return to a 32bit ARM code.