• Welcome to Dark Bit Factory & Gravity.
 

My first lens effect.

Started by Pixel_Outlaw, February 03, 2010

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Pixel_Outlaw


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.
Challenge Trophies Won:

zawran

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.

Clyde

Welldone, and welldone on drinking all of that Diet Coke!
See if you can add in some sinus movement.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Shockwave

Yep, nice classic lense effect, runs fast and smooth :) Thanks for sharing your source too
Shockwave ^ Codigos
Challenge Trophies Won:

Pixel_Outlaw

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.
Challenge Trophies Won:

Jim

#5
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:

Local background_x:Float = Cos(direction) * distance
Local background_y:Float = Sin(direction) * distance

This array becomes an 'offsets' buffer.

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

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

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
Challenge Trophies Won:

benny!

Looks good. Well done, buddy!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Pixel_Outlaw

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.
Challenge Trophies Won:

Jim

You did all the hard work mate :)
Jim
Challenge Trophies Won:

rdc