Author Topic: My first lens effect.  (Read 4323 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
My first lens effect.
« 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.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: My first lens effect.
« Reply #1 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.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: My first lens effect.
« Reply #2 on: February 03, 2010 »
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:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: My first lens effect.
« Reply #3 on: February 03, 2010 »
Yep, nice classic lense effect, runs fast and smooth :) Thanks for sharing your source too
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: My first lens effect.
« Reply #4 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.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: My first lens effect.
« Reply #5 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
« Last Edit: February 03, 2010 by Jim »
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: My first lens effect.
« Reply #6 on: February 03, 2010 »
Looks good. Well done, buddy!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: My first lens effect.
« Reply #7 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.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: My first lens effect.
« Reply #8 on: February 04, 2010 »
You did all the hard work mate :)
Jim
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: My first lens effect.
« Reply #9 on: February 04, 2010 »
Very nice.