Subdomain Posts
FreeBasic | 2 hours ago
FreeBasic | 18 hours ago
FreeBasic | 20 hours ago
Pascal | 22 hours ago
FreeBasic | 3 days ago
FreeBasic | 3 days ago
XML | 6 days ago
FreeBasic | 7 days ago
PHP | 8 days ago
FreeBasic | 8 days ago
Recent Posts
Bash | 7 sec ago
None | 16 sec ago
None | 36 sec ago
None | 44 sec ago
C | 1 min ago
None | 1 min ago
None | 1 min ago
C# | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Dr_D on the 8th of Feb 2010 01:38:09 AM Download | Raw | Embed | Report
  1. #include "fbgfx.bi"
  2.  
  3. randomize timer
  4. screenres 320,240,32
  5. dim as FB.IMAGE ptr tbuffer = imagecreate(320,240)
  6.  
  7. type vec2f
  8.         as single x, y
  9. end type
  10.  
  11. declare sub triangle_new( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byref colour as uinteger )
  12. declare sub triangle_asm( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byval col as uinteger )
  13. declare sub triangle_old( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byref colour as uinteger )
  14.  
  15.  
  16. dim as integer mode = 1
  17. dim as double ttime, ltime
  18. dim as string fps_string
  19. dim as integer fps
  20.  
  21. ttime = timer
  22. do  
  23.    
  24.     ttime = timer
  25.     if ttime > ltime then
  26.         fps_string = "FPS " & fps
  27.         fps = 0
  28.         ltime = ttime+1
  29.     end if
  30.     fps+=1
  31.    
  32.     if multikey(FB.SC_SPACE) then
  33.         mode +=1
  34.         if mode>3 then mode = 1
  35.         sleep 100,1
  36.     end if
  37.    
  38.     screenlock
  39.     cls
  40.    
  41.     if mode = 1 then
  42.        
  43.         for x as integer = 0 to 500
  44.             triangle_new( 0, type(rnd*320,rnd*240), type(rnd*320,rnd*240), type(rnd*320,rnd*240), rgb( rnd*255, rnd*255, rnd*255) )
  45.         next
  46.        
  47.         locate 1,1
  48.         print "new(integer) algo"
  49.        
  50.     elseif mode = 2 then
  51.        
  52.         for x as integer = 0 to 500
  53.             triangle_asm( 0, type(rnd*320,rnd*240), type(rnd*320,rnd*240), type(rnd*320,rnd*240), rgb( rnd*255, rnd*255, rnd*255) )
  54.         next
  55.        
  56.         locate 1,1
  57.         print "asm(fpu) algo"
  58.  
  59.     elseif mode = 3 then
  60.        
  61.         for x as integer = 0 to 500
  62.             triangle_old( 0, type(rnd*320,rnd*240), type(rnd*320,rnd*240), type(rnd*320,rnd*240), rgb( rnd*255, rnd*255, rnd*255) )
  63.         next
  64.        
  65.         locate 1,1
  66.         print "old fb(fpu) algo"
  67.  
  68.     end if
  69.    
  70.     print fps_String
  71.    
  72.     screenunlock
  73.     sleep 1,1
  74.    
  75. loop until multikey(FB.SC_ESCAPE)
  76.  
  77.  
  78. function min( byval a as integer, byval b as integer, byval c as integer ) as integer
  79.    
  80.         if b<a then
  81.                 swap a, b
  82.     end if
  83.    
  84.         if c<a then
  85.                 swap a, c
  86.     end if
  87.    
  88.         return a
  89.    
  90. end function
  91.  
  92.  
  93. function max( byval a as integer, byval b as integer, byval c as integer ) as integer
  94.        
  95.         if c>b then
  96.                 swap b, c
  97.     end if
  98.    
  99.         if b>a then
  100.                 swap a, b
  101.     end if
  102.    
  103.         return a
  104.    
  105. end function
  106.  
  107.  
  108.  
  109.  
  110. sub triangle_new( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byref colour as uinteger )
  111.  
  112.     dim as uinteger ptr dptr
  113.     dim as integer tw
  114.     dim as integer th
  115.    
  116.     if dst = 0 then
  117.         dptr = screenptr
  118.         screeninfo tw, th
  119.     else
  120.         dptr = cast(uinteger ptr, dst + 1 )
  121.         tw = dst->width
  122.         th = dst->height
  123.     end if
  124.        
  125.         dim as integer y1 = cint(v1.y)*16
  126.         dim as integer y2 = cint(v2.y)*16
  127.         dim as integer y3 = cint(v3.y)*16
  128.         dim as integer x1 = cint(v1.x)*16
  129.         dim as integer x2 = cint(v2.x)*16
  130.         dim as integer x3 = cint(v3.x)*16
  131.         dim as  integer DX12 = X1 - X2
  132.         dim as  integer DX23 = X2 - X3
  133.         dim as  integer DX31 = X3 - X1
  134.         dim as  integer DY12 = Y1 - Y2
  135.         dim as  integer DY23 = Y2 - Y3
  136.         dim as  integer DY31 = Y3 - Y1
  137.         dim as  integer FDX12 = DX12 shl 4
  138.         dim as  integer FDX23 = DX23 shl 4
  139.         dim as  integer FDX31 = DX31 shl 4
  140.         dim as  integer FDY12 = DY12 shl 4
  141.         dim as  integer FDY23 = DY23 shl 4
  142.         dim as  integer FDY31 = DY31 shl 4
  143.         dim as integer minx
  144.         dim as integer maxx
  145.         dim as integer miny
  146.         dim as integer maxy
  147.         minx = (min( x1, x2, x3) +&hf) shr 4
  148.         miny = (min( y1, y2, y3) +&hf) shr 4
  149.         maxx = (max( x1, x2, x3) +&hf) shr 4
  150.         maxy = (max( y1, y2, y3) +&hf) shr 4
  151.        
  152.        
  153.         dim as integer q = 16
  154.        
  155.         minx and = not(q-1)
  156.         miny and = not(q-1)
  157.        
  158.         dim as integer C1 = DY12 * X1 - DX12 * Y1
  159.         dim as integer C2 = DY23 * X2 - DX23 * Y2
  160.         dim as integer C3 = DY31 * X3 - DX31 * Y3
  161.        
  162.         if DY12 < 0 orelse (DY12 = 0 andalso DX12) > 0 then
  163.                 C1+=1
  164.     end if
  165.        
  166.         if DY23 < 0 orelse (DY23 = 0 andalso DX23) > 0 then
  167.                 C2+=1
  168.     end if
  169.        
  170.         if DY31 < 0 orelse (DY31 = 0 andalso DX31) > 0 then
  171.                 C3+=1
  172.     end if
  173.  
  174.     for y as integer = miny to maxy-1 step q
  175.        
  176.         for x as integer = minx to maxx-1 step q
  177.            
  178.             dim as integer x0 = x shl 4
  179.             dim as integer x1 = (x + q - 1) shl 4
  180.             dim as integer y0 = y shl 4
  181.             dim as integer y1 = (y + q - 1) shl 4
  182.            
  183.             dim as integer a00 = -(C1 + DX12 * y0 - DY12 * x0 > 0)
  184.             dim as integer a10 = -(C1 + DX12 * y0 - DY12 * x1 > 0)
  185.             dim as integer a01 = -(C1 + DX12 * y1 - DY12 * x0 > 0)
  186.             dim as integer a11 = -(C1 + DX12 * y1 - DY12 * x1 > 0)
  187.             dim as integer a = (a00 shl 0) or (a10 shl 1) or (a01 shl 2) or (a11 shl 3)
  188.            
  189.             dim as integer b00 = -(C2 + DX23 * y0 - DY23 * x0 > 0)
  190.             dim as integer b10 = -(C2 + DX23 * y0 - DY23 * x1 > 0)
  191.             dim as integer b01 = -(C2 + DX23 * y1 - DY23 * x0 > 0)
  192.             dim as integer b11 = -(C2 + DX23 * y1 - DY23 * x1 > 0)
  193.             dim as integer b = (b00 shl 0) or (b10 shl 1) or (b01 shl 2) or (b11 shl 3)
  194.            
  195.             dim as integer c00 = -(C3 + DX31 * y0 - DY31 * x0 > 0)
  196.             dim as integer c10 = -(C3 + DX31 * y0 - DY31 * x1 > 0)
  197.             dim as integer c01 = -(C3 + DX31 * y1 - DY31 * x0 > 0)
  198.             dim as integer c11 = -(C3 + DX31 * y1 - DY31 * x1 > 0)
  199.             dim as integer c = (c00 shl 0) or (c10 shl 1) or (c01 shl 2) or (c11 shl 3)
  200.            
  201.             if a = &h0 orelse b = &h0 orelse c = &h0 then
  202.                 goto skipiteration
  203.             end if
  204.            
  205.             if a = &h0 and b = &h0 and c = &h0 then
  206.                
  207.                
  208.                 for iy as integer = 0 to q-1
  209.  
  210.                     for ix as integer = x to (x+q)-1
  211.                         dptr[iy*tw+ix] = colour
  212.                     next
  213.                    
  214.                 next
  215.                
  216.             else
  217.                
  218.                 dim as integer CY1 = C1 + DX12 * y0 - DY12 * x0
  219.                 dim as integer CY2 = C2 + DX23 * y0 - DY23 * x0
  220.                 dim as integer CY3 = C3 + DX31 * y0 - DY31 * x0
  221.                
  222.                
  223.                 for iy as integer = y to (y+q)-1
  224.                     dim as integer CX1 = CY1
  225.                     dim as integer CX2 = CY2
  226.                     dim as integer CX3 = CY3
  227.                    
  228.                     for ix as integer = x to (x+q)-1
  229.                        
  230.                        
  231.                         if CX1 > 0 and CX2 > 0 and CX3 > 0 then
  232.                             dptr[iy*tw+ix] = colour
  233.                         end if
  234.                        
  235.                        
  236.                         CX1 -= FDY12
  237.                         CX2 -= FDY23
  238.                         CX3 -= FDY31
  239.                        
  240.                     next
  241.                    
  242.                     CY1 += FDX12
  243.                     CY2 += FDX23
  244.                     CY3 += FDX31
  245.                    
  246.                    
  247.                 next
  248.                
  249.             end if
  250.            
  251.             skipiteration:
  252.            
  253.         next x
  254.        
  255.        
  256.     next y
  257.    
  258. end sub
  259.  
  260.  
  261.  
  262.  
  263.  
  264. sub triangle_asm( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byval col as uinteger )
  265.    
  266.     dim as any ptr dstptr
  267.     dim as integer dw, dh, dp
  268.    
  269.     if dst = 0 then
  270.         dstptr = screenptr
  271.         screeninfo dw,dh
  272.         dp = dw * 4
  273.     else
  274.         dstptr = cast( any ptr, dst + 1 )
  275.         dw = dst->width
  276.         dh = dst->height
  277.         dp = dst->pitch
  278.     end if
  279.    
  280.     dim as single x1 = v1.x
  281.     dim as single x2 = v2.x
  282.     dim as single x3 = v3.x
  283.    
  284.     dim as single y1 = v1.y
  285.     dim as single y2 = v2.y
  286.     dim as single y3 = v3.y
  287.    
  288.     if y2 < y1 then
  289.         swap y1, y2
  290.         swap x1, x2
  291.     end if
  292.    
  293.     if y3 < y1 then
  294.         swap y3, y1
  295.         swap x3, x1
  296.     end if
  297.    
  298.     if y3 < y2 then
  299.         swap y3, y2
  300.         swap x3, x2
  301.     end if
  302.    
  303.     dim as single d1, d2, d3
  304.    
  305.     scope
  306.         dim as integer dx1, dy1
  307.         dx1 = x2 - x1
  308.         dy1 = y2 - y1
  309.         if dy1 <> 0 then
  310.             d1 = dx1 / dy1
  311.         else
  312.             d1 = 0
  313.         end if
  314.     end scope
  315.    
  316.     scope
  317.         dim as integer dx2, dy2
  318.         dx2 = x3 - x2
  319.         dy2 = y3 - y2
  320.         if dy2 <> 0 then
  321.             d2 = dx2 / dy2
  322.         else
  323.             d2 = 0
  324.         end if
  325.     end scope
  326.    
  327.     scope
  328.         dim as integer dx3, dy3
  329.         dx3 = x1 - x3
  330.         dy3 = y1 - y3
  331.         if dy3 <> 0 then
  332.             d3 = dx3 / dy3
  333.         else
  334.             d3 = 0
  335.         end if
  336.     end scope
  337.    
  338.     dim as single lx, rx
  339.    
  340.     lx = x1
  341.     rx = x1
  342.    
  343.     dim as single  lx_incr = any
  344.     dim as single  rx_incr = any
  345.     dim as integer y_start  = any
  346.     dim as integer y_end    = any
  347.    
  348.     for t as integer = 0 to 1
  349.         if t = 0 then
  350.             lx_incr = d1
  351.             rx_incr = d3
  352.             y_start = y1
  353.             y_end   = y2 - 1
  354.         else
  355.             lx_incr = d2
  356.             rx_incr = d3
  357.             y_start = y2
  358.             y_end   = y3
  359.             lx      = x2
  360.         end if
  361.        
  362.         dim as any ptr __dstptr = dstptr + (y_start * dp)
  363.        
  364.         if y_end >= dh then y_end = dh - 1
  365.        
  366.         dim as integer y_draw_count = (y_end - y_start) + 1
  367.         if y_draw_count > 0 then
  368.             dim as integer y = any
  369.            
  370.             asm
  371.                 mov edx, dword ptr [y_draw_count]
  372.                
  373.                 mov eax, dword ptr [y_start]
  374.                 mov dword ptr [y], eax
  375.                
  376.                 fld dword ptr [lx_incr]
  377.                 fld dword ptr [rx_incr]
  378.                 fld dword ptr [lx]
  379.                 fld dword ptr [rx]
  380.                
  381.                 y_inner:
  382.                
  383.                 cmp dword ptr [y], -1
  384.                 jle no_x_draw
  385.                 sub esp, 4
  386.                
  387.                 fld st(1)
  388.                 fistp dword ptr [esp]
  389.                 mov esi, dword ptr [esp]
  390.                
  391.                 fist dword ptr [esp]
  392.                 mov edi, dword ptr [esp]
  393.                
  394.                 add esp, 4
  395.                 cmp esi, edi
  396.                 jle no_swap
  397.                
  398.                 mov eax, esi
  399.                 mov esi, edi
  400.                 mov edi, eax
  401.                
  402.                 no_swap:
  403.                
  404.                 cmp esi, 0
  405.                 jge no_clip_start_x
  406.                
  407.                 mov esi, 0
  408.                
  409.                 no_clip_start_x:
  410.                
  411.                 mov eax, dword ptr [dw]
  412.                 cmp edi, eax
  413.                 jl no_clip_end_x
  414.                
  415.                 dec eax
  416.                 mov edi, eax
  417.                
  418.                 no_clip_end_x:
  419.                
  420.                 mov ebx, esi
  421.                 shl ebx, 2
  422.                 add ebx, dword ptr [__dstptr]
  423.                 mov ecx, edi
  424.                 sub ecx, esi
  425.                 mov eax, dword ptr [col]
  426.                 inc ecx
  427.                 jle no_x_draw
  428.                
  429.                 x_inner:
  430.                 mov dword ptr [ebx], eax
  431.                 add ebx, 4
  432.                 dec ecx
  433.                 jnz x_inner
  434.                
  435.                 no_x_draw:
  436.                
  437.                 fld st(3)
  438.                 faddp st(2), st(0)
  439.                
  440.                 fadd st(2)
  441.                
  442.                 mov eax, dword ptr [dp]
  443.                 add dword ptr [__dstptr], eax
  444.                
  445.                 inc dword ptr [y]
  446.                
  447.                 y_test:
  448.                 dec edx
  449.                 jnz y_inner
  450.                
  451.                 fstp dword ptr [rx]
  452.                 fstp dword ptr [lx]
  453.                
  454.                 emms
  455.             end asm
  456.         end if
  457.     next t
  458.    
  459. end sub
  460.  
  461.  
  462. sub triangle_old( byref dst as fb.image ptr = 0, byref v1 as vec2f, byref v2 as vec2f, byref v3 as vec2f, byref col as uinteger )
  463.    
  464.     dim as uinteger ptr dstptr
  465.    
  466.     dim as integer x, y, dw, dh, dbpp, dp
  467.    
  468.     dim as integer dx1, dx2, dx3, dy1, dy2, dy3, sx, ex
  469.    
  470.     dim as single d1, d2, d3, lx, rx
  471.    
  472.     dim as integer x1 = v1.x
  473.     dim as integer x2 = v2.x
  474.     dim as integer x3 = v3.x
  475.    
  476.     dim as integer y1 = v1.y
  477.     dim as integer y2 = v2.y
  478.     dim as integer y3 = v3.y
  479.    
  480.    
  481.     if dst = 0 then
  482.         dstptr = screenptr
  483.         screeninfo dw,dh,,dbpp, dp
  484.     else
  485.         dstptr = cast( uinteger ptr, dst +1)
  486.         dw = dst->width
  487.         dh = dst->height
  488.         dp = dst->pitch
  489.         dbpp = dst->bpp
  490.     end if
  491.    
  492.     if y2 < y1 then
  493.         swap y1, y2
  494.         swap x1, x2
  495.     end if
  496.    
  497.     if y3 < y1 then
  498.         swap y3, y1
  499.         swap x3, x1
  500.     end if
  501.    
  502.     if y3 < y2 then
  503.         swap y3, y2
  504.         swap x3, x2
  505.     end if
  506.    
  507.     dx1 = x2 - x1
  508.     dy1 = y2 - y1
  509.     if dy1 <> 0 then
  510.         d1 = dx1 / dy1
  511.     else
  512.         d1 = 0
  513.     end if
  514.    
  515.     dx2 = x3 - x2
  516.     dy2 = y3 - y2
  517.     if dy2 <> 0 then
  518.         d2 = dx2 / dy2
  519.     else
  520.         d2 = 0
  521.     end if
  522.    
  523.     dx3 = x1 - x3
  524.     dy3 = y1 - y3
  525.     if dy3 <> 0 then
  526.         d3 = dx3 / dy3
  527.     else
  528.         d3 = 0
  529.     end if
  530.    
  531.     lx = x1
  532.     rx = x1
  533.    
  534.    
  535.     for y = y1 to y2 - 1
  536.         if y>-1 and y<dh then
  537.             sx = lx
  538.             ex = rx
  539.             if sx>ex then swap sx,ex
  540.             if sx<0 then sx = 0
  541.             if ex>dw-1 then ex=dw-1
  542.             for x = sx to ex
  543.                 *cast(uinteger ptr, cast(ubyte ptr, dstptr) + y * dp + x * dbpp) = col
  544.             next
  545.         end if
  546.         lx += d1
  547.         rx += d3
  548.     next
  549.    
  550.     lx = x2
  551.     for y = y2 to y3
  552.         if y>-1 and y<dh then
  553.             sx = lx
  554.             ex = rx
  555.             if sx>ex then swap sx,ex
  556.             if sx<0 then sx = 0
  557.             if ex>dw-1 then ex=dw-1
  558.             for x = sx to ex
  559.                 *cast(uinteger ptr, cast(ubyte ptr, dstptr) + y * dp + x * dbpp) = col
  560.             next
  561.         end if
  562.         lx += d2
  563.         rx += d3
  564.     next
  565.    
  566. end sub
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: