Ooops
March 01, 2021, 11:08:09 PM

Author Topic: [bb] Texture Filled Triangle Function by Andy_A [ 1+ years ago ]  (Read 459 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Texture Filled Triangle Function
Author : Andy_A
Posted : 1+ years ago

Description : Fill any triangle with any texture.

The textures in this demo code are just something I whipped up in a couple of minutes. The good news is that you can use any texture you have available.

The code is very redundant and should be optimized for better results.

[update]
Code redundancy removed.


Code :
Code: BlitzBasic
  1. ;     Title: Textured Triangle Fill
  2. ;Programmer: Andy Amaya
  3. ;      Date: 2011.06.12
  4.  
  5. AppTitle "Texture Filled Triangle Function"
  6.  
  7. Global sw%, sh%
  8. sw = 640 : sh = 480
  9. Graphics sw, sh, 32, 2
  10. SeedRnd MilliSecs()
  11.  
  12. ClsColor 32, 192, 255
  13.  
  14. ; these are procedurally generated textures but,
  15. ; you can use any texture you have on your hard drive
  16. img1% = makeTexture1()
  17. img2% = makeTexture2()
  18. img3% = makeTexture3()
  19. img4% = makeTexture4()
  20.  
  21. ;to display the textures being used change to 1
  22. showTextures = 0
  23.  
  24. While MouseHit(1) = 0
  25.         Cls
  26.         If showTextures = 1 Then
  27.                 DrawBlock img1, 10,10
  28.                 DrawBlock img2, 40,10
  29.                 DrawBlock img3, 70,10
  30.                 DrawBlock img4, 100,10
  31.                 Color 0,0,0
  32.                 Text 144,16,"<=== These are the textures used"
  33.         End If
  34.     st = MilliSecs()
  35.     For i = 1 To 4
  36.         x1 = Rand(50,sw-50) : y1 = Rand(50,sh-50)
  37.         x2 = Rand(50,sw-50) : y2 = Rand(50,sh-50)
  38.         x3 = Rand(50,sw-50) : y3 = Rand(50,sh-50)
  39.         Select i
  40.             Case 1
  41.                 textureFill(x1,y1,x2,y2,x3,y3,img1)
  42.             Case 2
  43.                 textureFill(x1,y1,x2,y2,x3,y3,img2)
  44.             Case 3
  45.                 textureFill(x1,y1,x2,y2,x3,y3,img3)
  46.             Case 4
  47.                 textureFill(x1,y1,x2,y2,x3,y3,img4)
  48.         End Select
  49.     Next
  50.     et = MilliSecs()-st
  51.         Color 0,0,0
  52.     Text   5, sh-20,"et: "+et
  53.     Text sw/2, sh-20,"R-click to repeat          L-click to exit",True
  54.         Flip
  55.     WaitMouse() ;L-click to exit ..... R-click to repeat
  56. Wend
  57.  
  58.  
  59. FreeImage img1
  60. FreeImage img2
  61. FreeImage img3
  62. FreeImage img4
  63. End
  64.  
  65.  
  66. Function textureFill(x1#, y1#, x2#, y2#, x3#, y3#, imageHandle%)
  67.         Local slope1#, slope2#, slope3#, x#, y#
  68.         Local imgW%, imgH%, tx%, sy%, ey%, ix%, iy%, vLen%, tLen%
  69.        
  70.         imgW = ImageWidth(imageHandle)
  71.         imgH = ImageHeight(imageHandle)
  72.        
  73.     ;triangle coordinates must be ordered: where x1 < x2 < x3
  74.     If x2 < x1 Then x  = x2 : y  = y2 : x2 = x1 : y2 = y1 : x1 = x  : y1 = y
  75.     ;swap x1, y1, with x3, y3
  76.     If x3 < x1 Then x  = x3 : y  = y3 : x3 = x1 : y3 = y1 : x1 = x  : y1 = y
  77.     ;swap x2, y2 with x3, y3
  78.     If x3 < x2 Then x  = x3 : y  = y3 : x3 = x2 : y3 = y2 : x2 = x  : y2 = y
  79.     If x1 <> x3 Then slope1 = (y3-y1)/(x3-x1)
  80.     ;draw the first half of the triangle
  81.     length = x2 - x1
  82.     If length <> 0 Then
  83.         slope2 = (y2-y1)/(x2-x1)
  84.         For x = 0 To length
  85.             ;add x to coord x1
  86.             tx = x+x1
  87.             ;find modulus of x coord on texture
  88.             ix = tx Mod imgW
  89.             ;calc the starting y pos
  90.             sy = Int(x*slope1+y1)
  91.             ;calc the ending y pos
  92.             ey = Int(x*slope2+y1)
  93.             ;make sure starting y pos is less than ending y pos
  94.             If ey < sy Then tmp=sy: sy = ey: ey = tmp
  95.             ;vLen (vertical length) is delta of starting y pos and ending y pos
  96.             vLen = ey-sy
  97.             ;while vertical length is non-zero add slices of texture (vLen = zero at vertex)
  98.             While vLen > 0
  99.                 ;find the modulus of starting y pos to grab a slice of texture image at correct position
  100.                 iy = sy Mod imgH
  101.                 ;tLen represents length of texture slice to grab
  102.                 tLen = imgH - iy
  103.                 ;if starting y pos plus texture length >= ending y pos of triangle then truncate tLen
  104.                                 If (sy + tLen) >= ey Then tLen = ey-sy
  105.                                 ;grab a 1 by tLen slice of the texture at ix, iy
  106.                                 CopyRect ix, iy,1,tLen, tx, sy, ImageBuffer(imageHandle),BackBuffer()
  107.                 If tLen > 0 Then
  108.                     ;increment the starting y pos by adding texture slice length
  109.                     sy = sy + tLen
  110.                     ;subtract texture slice length from vertical length triangle section
  111.                     ;(this is how we eventually exit the While-Wend loop)
  112.                     vLen = vLen - tLen
  113.                 End If
  114.             Wend
  115.         Next
  116.     End If
  117.     ;draw the second half of the triangle
  118.     y = length*slope1+y1 : length = x3-x2
  119.     If length <> 0 Then
  120.         slope3 = (y3-y2)/(x3-x2)
  121.         For x = 0 To length
  122.             ;add x to coord x2
  123.             tx = x+x2
  124.             ;find modulus of x coord on texture
  125.             ix = tx Mod imgW
  126.             ;calc the starting y pos
  127.             sy = Int(x*slope1+y)
  128.             ;calc the ending y pos
  129.             ey = Int(x*slope3+y2)
  130.             ;make sure start y pos is less than ending y pos
  131.             If ey < sy Then tmp=sy: sy = ey: ey = tmp
  132.             ;vLen (vertical length) is delta of starting y pos and ending y pos
  133.             vLen = ey-sy
  134.             ;while vertical length is non-zero add slices of texture (vLen = zero at vertex)
  135.             While vLen > 0
  136.                 ;find the modulus of starting y pos to grab a slice of texture image at correct position
  137.                 iy = sy Mod imgH
  138.                 ;tLen represents length of texture slice to grab
  139.                 tLen = imgH - iy
  140.                 ;if starting y pos plus texture length >= ending y pos of triangle then truncate tLen
  141.                                 If (sy + tLen) >= ey Then tLen = ey-sy
  142.                                 ;grab a 1 by tLen slice of the texture at ix, iy
  143.                                 CopyRect ix, iy,1,tLen, tx, sy, ImageBuffer(imageHandle),BackBuffer()
  144.                 If tLen > 0 Then
  145.                     ;increment the starting y pos by adding texture slice length
  146.                     sy = sy + tLen
  147.                     ;subtract texture slice length from vertical length triangle section
  148.                     ;(this is how we eventually exit the While-Wend loop)
  149.                     vLen = vLen - tLen
  150.                 End If
  151.             Wend
  152.         Next
  153.     End If
  154. End Function
  155.  
  156. Function makeTexture1()
  157.         Local dh#, diam%, j#, i%
  158.        
  159.         texture% = CreateImage(24,24)
  160.         SetBuffer(ImageBuffer(texture))
  161.         Color 0, 0, 128
  162.         Rect 0, 0, 24, 24, True
  163.         dh = 256./23.
  164.         diam = 24
  165.         j = 91.0
  166.         offset = 0
  167.         While j <= 224.0
  168.                 i = Floor(j)
  169.                 Color i,i,i
  170.                 Oval 0+offset, 0+offset, diam, diam, True
  171.                 diam = diam - 2
  172.                 offset = offset + 1
  173.                 If diam < 1 Then diam = 1: offset = diam/2
  174.                 j = j + dh
  175.         Wend
  176.         SetBuffer(BackBuffer())
  177.         Return texture
  178. End Function
  179.  
  180. Function makeTexture2()
  181.         texture% = CreateImage(21,21)
  182.         SetBuffer(ImageBuffer(texture))
  183.         Color 128, 0, 0
  184.         Rect 0, 0, 21, 3, True
  185.         Color 255,160,0
  186.         Rect 0, 3, 21, 3, True
  187.         Color 255,255,0
  188.         Rect 0, 6, 21, 3, True
  189.         Color 0,255,0
  190.         Rect 0, 9, 21, 3, True
  191.         Color 0,0,255
  192.         Rect 0,12, 21, 3, True
  193.         Color 128,0,255
  194.         Rect 0,15, 21, 3, True
  195.         Color 0,0,128
  196.         Rect 0,18, 21, 3, True
  197.         SetBuffer(BackBuffer())
  198.         Return texture
  199. End Function
  200.  
  201. Function makeTexture3()
  202.         Local dh#, diam%, j#, i%
  203.        
  204.         texture% = CreateImage(24,24)
  205.         SetBuffer(ImageBuffer(texture))
  206.         Color 128, 0, 0
  207.         Rect 0, 0, 24, 24, True
  208.         dh = 256./23.
  209.         diam = 24
  210.         j = 91.0
  211.         offset = 0
  212.         While j <= 224.0
  213.                 i = Floor(j)
  214.                 Color i,i,0
  215.                 Oval 0+offset, 0+offset, diam, diam, True
  216.                 diam = diam - 2
  217.                 offset = offset + 1
  218.                 If diam < 1 Then diam = 1: offset = diam/2
  219.                 j = j + dh
  220.         Wend
  221.         SetBuffer(BackBuffer())
  222.         Return texture
  223. End Function
  224.  
  225. Function makeTexture4()
  226.         texture% = CreateImage(42,24)
  227.         SetBuffer(ImageBuffer(texture))
  228.         Color 0,80,112
  229.         Rect 0,0,42,28,True
  230.         Color 255,255,32
  231.         Text 21, 0,"Blitz",True
  232.         Text 21,11,"Plus",True
  233.         SetBuffer(BackBuffer())
  234.         Return texture
  235. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal