Dark Bit Factory &  Gravity

Dark Bit Factory & Gravity

  • September 03, 2010 *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

Pages: [1]   Go Down

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

0 Members and 1 Guest are viewing this topic.

Pixel_Outlaw

  • Pentium
  • *****
  • Karma: 68
  • Offline Offline
  • Posts: 1206
    • 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.
Logged

zawran

  • Sponsor
  • Pentium
  • *******
  • Karma: 54
  • Offline Offline
  • Posts: 778
    • View Profile
    • WWW
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.
Logged

Clyde

  • A Little Fuzzy Wuzzy
  • Contributing Author
  • Senior Member
  • ******
  • Karma: 71
  • Offline Offline
  • Posts: 6971
    • 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.
Logged
If only I knew then what I know now.

Shockwave

  • good/evil
  • ADMIN
  • Senior Member
  • ********
  • Karma: 357
  • Offline Offline
  • Posts: 15098
  • evil/good
    • View Profile
    • WWW
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
Logged
Shockwave ^ Codigos....  Error: Keyboard not attached.... Press F1 to continue.

Pixel_Outlaw

  • Pentium
  • *****
  • Karma: 68
  • Offline Offline
  • Posts: 1206
    • 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.
Logged

Jim

  • ADMIN
  • Senior Member
  • ********
  • Karma: 326
  • Offline Offline
  • Posts: 4695
    • 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 »
Logged

benny!

  • Senior Member
  • ******
  • Karma: 174
  • Offline Offline
  • Posts: 3439
  • in this place forever!
    • View Profile
    • WWW
Re: My first lens effect.
« Reply #6 on: February 03, 2010 »
Looks good. Well done, buddy!
Logged
[ microBLOG - PHLOCKS - LABS - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Pixel_Outlaw

  • Pentium
  • *****
  • Karma: 68
  • Offline Offline
  • Posts: 1206
    • 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.
Logged

Jim

  • ADMIN
  • Senior Member
  • ********
  • Karma: 326
  • Offline Offline
  • Posts: 4695
    • View Profile
Re: My first lens effect.
« Reply #8 on: February 04, 2010 »
You did all the hard work mate :)
Jim
Logged

rdc

  • Sliderules Rule!
  • MOD
  • Pentium
  • *******
  • Karma: 132
  • Offline Offline
  • Posts: 1439
  • Clark Productions
    • View Profile
    • WWW
Re: My first lens effect.
« Reply #9 on: February 04, 2010 »
Very nice.
Pages: [1]   Go Up
 

 

B l a c k R a i n V.2 by C r i p

Page created in 0.124 seconds with 18 queries.