Pixel Manipulation


In a previous post we saw how to simulate a 3d road, or world in first person using only pixel manipulations. Today i'd like to show another example - a sort of screen saver that simulates depth by playing around with pixels.

We need 2 images - a picture to sample pixels from, and a blank canvas to "paint" to. As show in the video, we're going to simulate a moving image that seemingly flies around in space.

We define a percentage value for each of the four sides of the destination image. For instance, we want the top row to be at a width of 50% of the original image, the bottom row to be 100% (as show here)

0,0,0,1,1,1,1,1, 0, 0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,1 , 1 ,1 ,1,1,1,1,1 ,1,

We then calculate the distance between the rows, and the required width of each row along the way using percentages (between 0.5 and 1).

We can do the same thing with columns to manipulate the x axis as well.
In order to sample the correct pixel at every row, we take into account the width percentage of the current row / col. For instance, if the current row it set at 50% of the original size, that means we need to "jump" 2 pixels between columns when sampling the source image.

In order to achieve the animation effect we again use percentages and run the code in a update function. We calculate random percentages that we aspire to get to, and the number of steps we want to travel until we get there. Then we use percentages to know what should be the width and height of each image side at each frame - according to the current frame's percentage from the total number of defined steps.

Written in C++ using sfml.

The Code:
https://drive.google.com/open?id=1QcMRW5nSiiuaHG1q91PRagPkht0sJDyO

תגובות

פוסטים פופולריים מהבלוג הזה

Thoughts on HTML5, a Flash Developers hopeless rant

Pseudo 3d Isometric view

Veroni Noise