Lens effect

The place for any Hollywood tutorials
Post Reply
jalih
Posts: 245
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Lens effect

Post by jalih » Tue Jan 04, 2011 10:42 pm

Again, not really a tutorial but who cares...

Replace Brush 1 image with your own 640x480 sized picture.

Code: Select all

; Slow Lens Effect
; Based on lens formula by on Abe Racadabra and Processing example.
;
@DISPLAY {Width = 640, Height = 480, Title = "Slow Lens Effect"}

@BRUSH 1, "tausta.png", {LoadAlpha =  True}


Global lensD = 256
Dim lensArray[65536] ; size = 2 * lensD

CreateBrush(2, lensD, lensD)


lens = { x = 1, y = 1, dx = 8,dy = 8}
lens.lensArray = lensArray

Function lens:setup()
	Local magFactor = 80
	Local m, a, b
	
	Local r = Int(lensD / 2)
	Local s = Sqrt(r*r - magFactor*magFactor)
	
	For Local y = -r To r - 1 Step 1
		For Local x = -r To r - 1 Step 1
			If x*x + y*y >= s*s
				a = x
				b = y
			Else
				Local z = Sqrt(r*r - x*x - y*y)
				a = Int(x * magFactor / z + 0.5)
				b = Int(y * magFactor / z + 0.5)				
			EndIf
			
			self.lensArray[(y + r)*lensD + (x + r)] = (b + r) * lensD + (a + r)
		Next
	Next
EndFunction


Function lens:move()
	If self.x + self.dx + lensD > 640 Or self.x + self.dx < 0 Then self.dx = -self.dx
	If self.y + self.dy + lensD > 480 Or self.y + self.dy < 0 Then self.dy = -self.dy
	self.x = self.x + self.dx
	self.y = self.y + self.dy
EndFunction


Function lens:draw()
	
	Dim lens[256][256]

	SelectBrush(2)
	
	DisplayBrushPart(1, self.x, self.y, 0, 0, lensD, lensD)

	For Local j = 0 To 256 - 1 Step 1
		For Local i = 0 To 256 - 1 Step 1
			Local offset = self.lensArray[(j * 256) + i]
			Local x = offset % 256
			Local y = Int(offset/256)
			lens[i][j] = ReadPixel(x, y)
		Next
	Next
	
	EndSelect	


	For Local j = 0 To 256 - 1 Step 1
		For Local i = 0 To 256 - 1 Step 1
			Plot(self.x + i, self.y + j, lens[i][j])
		Next
	Next
			
EndFunction



Function p_mainloop()
	DisplayBrush(1,0,0)
	lens:move()
	lens:draw()
	Flip()
EndFunction




lens:setup()

BeginDoubleBuffer()

SetInterval(1, p_MainLoop, 1000/50) ; 50fps

Repeat
	WaitEvent
Forever

g0blin
Posts: 40
Joined: Tue Oct 04, 2011 9:06 am

Re: Lens effect

Post by g0blin » Tue Jan 31, 2012 12:00 pm

Nice one!

I just noticed that the displayed image tends to warp if it is smaller than the active screen. Maybe a "Cls" could do?

Best Regards
g0blin

Post Reply