Dark Bit Factory & Gravity
GENERAL => Projects => Topic started 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.
-
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.
-
Welldone, and welldone on drinking all of that Diet Coke!
See if you can add in some sinus movement.
-
Yep, nice classic lense effect, runs fast and smooth :) Thanks for sharing your source too
-
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.
-
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
-
Looks good. Well done, buddy!
-
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.
-
You did all the hard work mate :)
Jim
-
Very nice.