Log In  

Cart #53994 | 2018-07-07 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
25

I am still very new to PICO-8 and saw a few games rendered with polygons.
This lead me to the Trifill Thunderdome benchmark by Musurca.

One thing this benchmark didn't show was the number of tokens for each method which is an important detail considering the limitations of the platform.

Also, I wrote two triangle rasterizers in 163 and 335 tokens respectively which perform pretty decently. They might be an acceptable alternative to ElectricGryphon's super fast triangle rasterizer if you need an extra 2-400 tokens. If you don't, by all mean use his. It is the fastest one I found, and by a good margin!

Update 20180706: Added the possibility to switch rasterizer and turn the test triangle with the arrow keys. This allows to compare rasterizers at a normal pace. Updated EG's rasterizer to the latest he posted in the previous thread and added the one from Gryphon 3D Engine Library v2 only modified to use normal color() and fillp().

Update 20180708: Added a fill pattern and a toggle for the results and control wireframe.

Hope this helps,

P#53941 2018-07-04 16:42 ( Edited 2018-07-08 17:39)

Looks like some of the new rasterizers have severe artifacts on edges.
@p01 are these yours?

P#53947 2018-07-05 02:59 ( Edited 2018-07-05 06:59)

That would be the one by @creamdog. It uses memset(screenAddress, col * 17, scanlineWidth) which can only set an even number of pixels. Hence the half resolution horizontally and the artifacts.

The triangle you see spinning after the benchmark uses my 335 tokens rasterizer.

P#53949 2018-07-05 04:23 ( Edited 2018-07-05 08:23)

Nice work! I haven't looked at the code yet, but that's a mighty optimization effort right there. (Twice!)

edit: I added some code to switch between renderers on the result screen, there seems to be something not quite right with at least one of the algorithms. (Pretty sure I set things up right, maybe not?)

z changes the renderer, x pauses the rotation, up changes the colour.
You might want to extend this to modify the vertices as well if you're hunting for artefacts.


add this part after the tests array definition, around line 620

functioninfo={}
add(functioninfo, { tests[1], solid_trifill_v3, "solid_trifill_v3" } )
add(functioninfo, { tests[2], function(...) local v={...} musurca_triangle({v[1],v[2]},{v[3],v[4]},{v[5],v[6]},v[7]) end, "musurca_triangle" } )
add(functioninfo, { tests[3], creamdog_tri, "creamdog_tri" } )
add(functioninfo, { tests[4], p01_triangle_335, "p01_triangle_335" } )
add(functioninfo, { tests[5], function(...) gfx_draw({...}) end, "gfx_draw" } )
add(functioninfo, { tests[6], steptri, "steptri" } )
add(functioninfo, { tests[7], p01_triangle_163, "p01_triangle_163" } )

replace everything from ::loop_showing_results:: onwards with this

local method,col,pausetime,paused=3,0,0,false
::loop_showing_results::
cls(1)

local u=t()/8
if btnp(5) then
 if (not paused) pausetime=u
 paused=not paused
end
if (paused) u=pausetime

if (btnp(2)) col=(col+1)%16
local r=80+32*cos(u*1.7)
if btnp(4)  then
 method=(method+1)%#functioninfo
end
local fn=functioninfo[method+1]
fn[2](64+r*cos(u),64+r*sin(u),64+r*cos(u+.33),64+r*sin(u+.33),64+r*cos(u+.67),64+r*sin(u+.67),col)

--zebra = 0
for i=0,#tests do print_result(i) end
print(fn[1].author.."-"..fn[3],0,122,7)
flip()
goto loop_showing_results

P#53956 2018-07-05 11:44 ( Edited 2018-07-07 02:04)

THANKS!

Excellent idea to try the different rasterizers after the benchmark.

I tried this last night and saw some have wild bugs. I updated EG's rasterizer to the last one he posted in the previous thread, and the one from the Gryphon 3D librarry v2 and now my "big" rasterizer comes ahead :U

P#53963 2018-07-06 08:17 ( Edited 2018-07-06 13:48)

Another nice addition would be an overlay of the demo triangle using plain lines (dunno, switch on/off using button).
Not all rasterizers are drawing the triangle at the same place!!

EDIT: just did the test, and @p01 are fast and accurate. Nice job!

...
tests[method_index].fn(1)
-- "native" pico triangle/lines
if btn(4) then
 for i=0,2 do
    local p0,p1=2*i+1,(2*(i+1))%6+1
    line(f[p0],f[p0+1],f[p1],f[p1+1],8)
 end
end
P#53968 2018-07-06 13:00 ( Edited 2018-07-06 17:13)

Thanks. Great idea. I also added a fill pattern to make sure the different rasterizers can draw shaded triangles.

P#53995 2018-07-07 19:45 ( Edited 2018-07-07 23:45)

Hi, @p01
I'm developing a 3D modeling tool with a small specification for PICO-8.
I would like to post on the BBS using your awesome trifill code.

P#74559 2020-04-08 10:52 ( Edited 2020-04-08 10:54)
1

That's the idea ;)
Go right ahead an use this code if it helps your project.

This modeler looks neat!

P#74561 2020-04-08 11:09

Thank you!
I've posted a production!

https://www.lexaloffle.com/bbs/?tid=37287

P#74564 2020-04-08 13:43
6

I created a trifill() for my project.
I'd like to share it because it has a different loop than the other code.
Thank you so much for posting this place!

308 token(top clipping & V-H processing branch)

function pelogen_tri_308(v1,v2,v3,col)
color(col)
local l,c,r,t,m,b=pelogen_sort(v1,v2,v3,2)
if abs(r-l)>abs(b-t) then
v1,v2,v3=l,t,0
local j=(r-l)/(b-t)
while t~=b do
local i=(c-l)/(m-t)
if(t<0) t,l,v1,v3=0,l-i*t,v1-j*(t-v3),m
for t=t,min(m-1,127) do
rectfill(l,t,v1,t)
l+=i
v1+=j
end
c,l,t,m=r,c,m,b
end
else
l,c,r,t,m,b=pelogen_sort(v1,v2,v3,1)
v1,v2,v3=l,t,0
local j=(b-t)/(r-l)
while l~=r do
local i=(m-t)/(c-l)
if(l<0) l,t,v2,v3=0,t-i*l,v2-j*(l-v3),c
for l=l,min(c-1,127) do
rectfill(l,t,l,v2)
t+=i
v2+=j
end
m,t,l,c=b,m,c,r
end
end
end
function pelogen_sort(v1,v2,v3,v)
if(v1[v]>v2[v]) v1,v2=v2,v1
if(v1[v]>v3[v]) v1,v3=v3,v1
if(v2[v]>v3[v]) v3,v2=v2,v3
return flr(v1[1]),flr(v2[1]),v3[1],flr(v1[2]),flr(v2[2]),v3[2]
end

176 token(Enable top clipping only)

function pelogen_tri_176(v1,v2,v3,col)
color(col)
if(v1[2]>v2[2]) v1,v2=v2,v1
if(v1[2]>v3[2]) v1,v3=v3,v1
if(v2[2]>v3[2]) v3,v2=v2,v3
local l,c,r,t,m,b=v1[1],v2[1],v3[1],flr(v1[2]),flr(v2[2]),v3[2]
local i,j,k,r=(c-l)/(m-t),(r-l)/(b-t),(r-c)/(b-m),l
while t~=b do
if(t<0)t,l,r=0,l-i*t,v1 and r-j*t or r
for t=t,min(m-1,127) do
rectfill(l,t,r,t)
r+=j
l+=i
end
l,t,m,i,v1=c,m,b,k
end
end

129 token(minfy-code! Non top clipping) [UPDATE:20221118]

function pelogen_tri_129(l,t,c,m,r,b,col)
color(col)
if(t>m) l,t,c,m=c,m,l,t
if(t>b) l,t,r,b=r,b,l,t
if(m>b) c,m,r,b=r,b,c,m
local i,j,k,r=(c-l)/(m-t),(r-l)/(b-t),(r-c)/(b-m),l
while t~=b do
for t=flr(t),min(flr(m)-1,127) do
rectfill(l,t,r,t)
l+=i
r+=j
end
l,t,m,i=c,m,b,k
end
end

Previous Code

134 token(minfy-code! Non top clipping)

function pelogen_tri_134(l,t,c,m,r,b,col)
color(col)
if(t>m) l,t,c,m=c,m,l,t
if(t>b) l,t,r,b=r,b,l,t
if(m>b) c,m,r,b=r,b,c,m
t,m=flr(t),flr(m)
local i,j,k,r=(c-l)/(m-t),(r-l)/(b-t),(r-c)/(b-m),l
while t~=b do
for t=t,min(m-1,127) do
rectfill(l,t,r,t)
r+=j
l+=i
end
l,t,m,i=c,m,b,k
end
end


These cords have the disadvantage of missing the bottom edge.

P#75530 2020-04-28 00:03 ( Edited 2022-11-17 15:04)
1

Here's my 143 token take on a triangle fill.

-- @Domino_Marama

function domino_tri(x0,y0,x1,y1,x2,y2,col)
 color(col)
 if(y1<y0)x0,x1,y0,y1=x1,x0,y1,y0
 if(y2<y0)x0,x2,y0,y2=x2,x0,y2,y0
 if(y2<y1)x1,x2,y1,y2=x2,x1,y2,y1
 if(y0!=y1) x0,col=trapa(y0,y1,x0,x0,(x1-x0)/(y1-y0),(x2-x0)/(y2-y0))
 if(y1!=y2) trapa(y1,y2,x1,x0,(x2-x1)/(y2-y1),col)
end

function trapa(y1,y2,lx,rx,dl,dr)
 for y=y1,min(y2,128) do
  rectfill(lx,y,rx,y)
  lx+=dl
  rx+=dr
 end
 return rx-dr,dr
end
P#88908 2021-03-13 00:52 ( Edited 2021-03-13 14:52)

i made my own tri function : https://www.lexaloffle.com/bbs/?tid=49930

P#119767 2022-10-28 14:02

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-03-20 14:52:49 | 0.037s | Q:34