Dr_Davenstein

Antialiased Wormhole

May 4th, 2021 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include once "fbgfx.bi"
  2. randomize timer
  3.  
  4. declare sub antialias( byref src as FB.image ptr )
  5.  
  6. const as integer scr_width = 640, scr_height = 480
  7.  
  8. const as integer alevel = 2
  9.  
  10. const as single pi = 3.1415926, pi2 = pi*2
  11.  
  12. screenres scr_width,scr_height,32,,FB.GFX_NULL
  13.  
  14. dim as fb.image ptr image = imagecreate( scr_width * alevel, scr_height * alevel, 0, 32 )
  15.  
  16. screenres scr_width,scr_height,32,,FB.GFX_HIGH_PRIORITY' or FB.GFX_FULLSCREEN
  17.  
  18. dim as single wobble
  19. dim as single iFin = pi2
  20. dim as single iStep = iFin/16
  21.  
  22.  
  23. do
  24.  
  25.     dim as double nTime = timer / 10
  26.  
  27.     line image, (0,0)-(scr_width*alevel, scr_height*alevel), 0, bf
  28.  
  29.     dim as single mStep = 1
  30.    
  31.     dim as integer xm = (scr_width/2)*alevel
  32.     dim as integer ym = (scr_height/2)*alevel
  33.    
  34.     for i as single = 0 to iFin step iStep
  35.                
  36.         dim as single xs = xm, oxs = xm
  37.         dim as single ys = ym, oys = ym
  38.        
  39.         for s as single = 0 to 50 step mStep
  40.            
  41.             oxs += s*sin((i-iStep)+wobble)
  42.             oys += s*cos((i-iStep)+wobble)
  43.             xs += s*sin(i+wobble)
  44.             ys += s*cos(i+wobble)
  45.             wobble = sin((s/10)+(nTime*12))
  46.            
  47.             dim as integer tCol = 128+127*(sin((i+wobble)*9))
  48.            
  49.             line image, ( xs, ys ) - ( xs + s*sin(i+wobble), ys + s*cos(i+wobble) ),rgb(tCol, tCol, 255 and wobble)
  50.            
  51.             line image, -( oxs + s * sin((i-iStep)+wobble), oys + s * cos((i-iStep)+wobble) ),rgb(tCol, tCol, 255 and wobble)
  52.            
  53.         next
  54.  
  55.     next
  56.    
  57.     screenlock
  58.     antialias( image )
  59.     screenunlock
  60.    
  61.     sleep( 1, 1 )
  62.  
  63. loop until multikey(FB.SC_ESCAPE)
  64.  
  65.  
  66. 'basically this function iterates through every other pixel of the larger image
  67. 'add the color to a variable, take the average of those colors
  68. 'and plot the pixel in the smaller image using that averaged color
  69. 'the essence of downsampling, aka 'antialiasing'    
  70. sub antialias( byref src as FB.image ptr )
  71.  
  72.     dim as uinteger ptr sptr = screenptr
  73.     dim as uinteger ptr iptr = cast( uinteger ptr, src + 1 )
  74.  
  75.     dim as integer avgcol
  76.     dim as integer hits
  77.     dim as uinteger r,g,b
  78.     dim as uinteger sw = src->width-1, sh = src->height-1
  79.     dim as integer x = any, y = any, x2 = any, y2 = any
  80.     dim as integer y2mul, y1mul
  81.     const as single recip = 1/9
  82.  
  83.     for y = 1 to sh-1 step 2
  84.  
  85.         y1mul = ((y shr 1) * SCR_WIDTH)
  86.  
  87.         for x = 1 to sw-1 step 2
  88.  
  89.             avgcol = 0
  90.             hits = 0
  91.             r = 0
  92.             g = 0
  93.             b = 0
  94.  
  95.             for y2 = y - 1 to y + 1
  96.  
  97.                 'if y2>sh then continue for
  98.  
  99.                 y2mul = y2*src->width
  100.  
  101.                 for x2 = x - 1 to x + 1
  102.  
  103.                     'if x2>sw then continue for
  104.  
  105.                     avgcol = iptr[ y2mul + x2 ]
  106.                     r+= (avgcol shr 16) and 255
  107.                     g+= (avgcol shr 8) and 255
  108.                     b+= (avgcol) and 255
  109.  
  110.                 next
  111.  
  112.             next
  113.  
  114.             sptr[ y1mul + (x shr 1) ] = rgb( r * recip, g * recip, b * recip )
  115.  
  116.         next
  117.     next
  118.  
  119. end sub
  120.  
  121.  
Add Comment
Please, Sign In to add comment