Hi All,
I want to simulate the effect of gravity (for example) on an image. I have made a function that moves each pixel in an image down by a certain amount:
void testApp::applyVariableGravity(float delta){
ofxCvColorImage movedCanvas;
float gravity = gui.getValueF("GRAVITY_AMOUNT");
float pixelsToMoveThisFrame = gravity * delta * 100.f; //useable scale
movedCanvas.allocate(canvas.getWidth(),canvas.getHeight());
cvSet(movedCanvas.getCvImage(), cvScalar(0,0,0)); //all black initially
int canvasHeight = movedCanvas.getHeight();
int canvasWidth = movedCanvas.getWidth();
for(int j=0; j < canvasHeight; j++){
for(int i=0; i < canvasWidth; i++){
int positionToCopyToX = i;
int positionToCopyToY = (j+((int)pixelsToMoveThisFrame)) % canvasHeight; //wrap around at the last row
int absolutePositionToCopyTo = 3*(positionToCopyToY*canvasWidth + positionToCopyToX); // 3 * as RGB
int absolutePositionToCopyFrom = 3*(j*canvasWidth + i); // RGB again
movedCanvas.getPixels()[absolutePositionToCopyTo+0] = canvas.getPixels()[absolutePositionToCopyFrom+0]; //Red data
movedCanvas.getPixels()[absolutePositionToCopyTo+1] = canvas.getPixels()[absolutePositionToCopyFrom+1]; //Green data
movedCanvas.getPixels()[absolutePositionToCopyTo+2] = canvas.getPixels()[absolutePositionToCopyFrom+2]; //Blue data
}
}
canvas = movedCanvas;
canvas.flagImageChanged();
}
As you can imagine, as I can only copy to “whole” pixels, this means that even though I am using a time delta value, I don’t get smooth animation, as sometimes, even with constant speed, I might move z pixels one frame and z+1 another.
What I really want is a subpixel accurate method of copying entire rows at a time - I always want to take whole rows at a time, but sometimes I might want to interpolate the destination pixels - “sharing” the colour values between whole pixels in the destination as it were.
http://opencv.willowgarage.com/documentation/geometric-image-transformations.html
Seems to deal with Interpolation of destination pixels:
http://en.wikipedia.org/wiki/Multivariate-interpolation
In the end, I want to use a Perlin noise map to allow individual pixels to move varying amounts downwards on each frame, but this seems to be a good start.
Any thoughts from the forum?
Cheers,
Joel