Dark Bit Factory & Gravity

GENERAL => Projects => Topic started by: Pixel_Outlaw on February 03, 2010

Title: My first lens effect.
Post by: Pixel_Outlaw on February 03, 2010

Hey Guys I had a night off from college so I decided to code a lens effect. I didn't do any research and it was quite a chore to come up with a good distortion. I was trying to use trig but it I found that the formula was a bit more simple. Basically I take each pixel of the lens object and trace it back to a spot on the back buffer. THANKS Zawran for the wonderful library that let me do this. I still don't know why Bmax does not have image buffers for this sort of thing. It probably has horrid coding under the hood but this was rushed quite a bit.
Title: Re: My first lens effect.
Post by: zawran on February 03, 2010
Nice looking lens effect. Great to see that you still find the library useful. I have been thinking about taking another look at the library. It would give me an reason to set aside some time for coding again :) Your coding looks pretty good to me, its very readable and nicely structured, which is always a good thing.
Title: Re: My first lens effect.
Post by: Clyde on February 03, 2010
Welldone, and welldone on drinking all of that Diet Coke!
See if you can add in some sinus movement.
Title: Re: My first lens effect.
Post by: Shockwave on February 03, 2010
Yep, nice classic lense effect, runs fast and smooth :) Thanks for sharing your source too
Title: Re: My first lens effect.
Post by: Pixel_Outlaw on February 03, 2010
Thanks guys. I'm fairly happy with the effect because it is something I had wanted to do for a long time.
I might play with it a bit when I get a break from school.
Title: Re: My first lens effect.
Post by: Jim on February 03, 2010
Nice effect!  I had a look at the source (K+ for that).  It looks to me like you are calculating all the sums every frame and reloading the background.  It runs very fast already, but one way of making it quicker is instead of using an image buffer for the lens graphic, do exactly what you're doing now, but instead of reading the pixel from the back buffer store the background_x, background_y values.
So, put these values in a 2d array:
Code: [Select]
Local background_x:Float = Cos(direction) * distance
Local background_y:Float = Sin(direction) * distance
This array becomes an 'offsets' buffer.
Code: [Select]
offsets(pixel_x, pixel_y).x = background_x
offsets(pixel_x, pixel_y).y = background_y
You do this just once at the start.

We probably need to set an array of bytes to say if the pixel is in or out of the circle
Code: [Select]
If distance <= radius
 hasOffset(pixel_x, pixel_y)=1
else
 hasOffset(pixel_x, pixel_y)=0
end if

Now, to do the lens effect really fast with no sums, run through your offsets buffer
Code: [Select]
for pixel_y = 0 to radius*2
 for pixel_x = 0 to radius*2
  if hasOffset(pixel_x, pixel_y)=1 then
   background_x = x + offsets(pixel_x, pixel_y).x
   background_y = y + offsets(pixel_x, pixel_y).y
   pix = get_pixel(background_x, background_y)
   set_pixel(x+pixel_x-radius, y+pixel_y-radius, pix)
  end if
 next
next

If you combine that with only loading the background at the start too it should go like lightning!

Jim
Title: Re: My first lens effect.
Post by: benny! on February 03, 2010
Looks good. Well done, buddy!
Title: Re: My first lens effect.
Post by: Pixel_Outlaw on February 04, 2010
Awesome work Jim!


It got late and I just had a small amount of time so I was really pushing to get the effect going at all costs.
Title: Re: My first lens effect.
Post by: Jim on February 04, 2010
You did all the hard work mate :)
Jim
Title: Re: My first lens effect.
Post by: rdc on February 04, 2010
Very nice.