2D Water Simulation and Pixels

Hey all,

im trying to port over a Processing sketch I have found that uses a water ripple simulator http://www.openprocessing.org/sketch/43543, but am running into some issues. I think my main issue is in the way I am loading in the pixels and applying the pixels to the unsigned char texture , but I am not sure.

my .h file:

#pragma once

#include "ofMain.h"

#include "ofxOpenCv.h"



class testApp : public ofBaseApp{
    
public:
    void setup();
    void update();
    void draw();
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    public: void disturb(int dx, int dy);
    void newFrame();
    
   
   // water ripples
    ofImage img;
    int size;
    int hwidth;
    int hheight;
    int riprad;
    
    int  *ripplemap;
    int *ripple;
  unsigned char * texture;
    
    int oldind,newind, mapind;
    
    int i,a,b;
    
    unsigned char * pixels;
    ofImageType type;
    

    
};

and my .cpp file:

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    
    
    
    
    img.loadImage("floor.jpg");
   
    
    hwidth = ofGetWidth()>>1;
    hheight = ofGetHeight()>>1;
    riprad=5; //test with 3
    
    size = ofGetWidth() * (ofGetHeight()+2) * 2;
    
    ripplemap = new int[size];
    ripple = new int[ofGetWidth()*ofGetHeight()];
    
    oldind = ofGetWidth();
    newind = ofGetWidth() * (ofGetHeight()+3);
    img.draw(0,0);
    img.getPixels();
  
    
   

    
    
   
    ofSetVerticalSync(true);

    

   
}

//--------------------------------------------------------------
void testApp::update(){
    
 
}

//--------------------------------------------------------------
void testApp::draw(){
    
    img.draw(0,0);

    
    
    int w = img.getWidth();
	int h = img.getHeight();
    
    
    pixels = img.getPixels();
 texture = pixels;
    newFrame();
    for(int y = 0; y < h; y++) {
		for(int x = 0; x < w; x++) {
			int index = y * w + x;
            for (int i = 0; i < pixels[index]; i++) {
                pixels[i] = ripple[i];
            }

		}
	}
    
    
    
    
    
    
	
}

void testApp::disturb(int dx, int dy){
    for (int j=dy-riprad;j<dy+riprad;j++) {
        for (int k=dx-riprad;k<dx+riprad;k++) {
            if (j>=0 && j<ofGetHeight() && k>=0 && k<ofGetWidth()) {
                ripplemap[oldind+(j*ofGetWidth())+k] += 128;   //test with 512
            }
        }
    }
    
}

void testApp::newFrame(){
    i=oldind;
    oldind=newind;
    newind=i;
    
    i=0;
    mapind=oldind;
    for (int y=0;y<ofGetHeight();y++) {
        for (int x=0;x<ofGetWidth();x++) {
            short data = (short)((ripplemap[mapind-ofGetWidth()]+ripplemap[mapind+ofGetWidth()]+ripplemap[mapind-1]+ripplemap[mapind+1])>>1);
            data -= ripplemap[newind+i];
            data -= data >> 5;
            ripplemap[newind+i]=data;
            
            //where data=0 then still, where data>0 then wave
            data = (short)(1024-data);
            
            //offsets
            a=((x-hwidth)*data/1024)+hwidth;
            b=((y-hheight)*data/1024)+hheight;
            
            //bounds check
            if (a>=ofGetWidth()) a=ofGetWidth()-1;
            if (a<0) a=0;
            if (b>=ofGetHeight()) b=ofGetHeight()-1;
            if (b<0) b=0;
            
            ripple[i]=texture[a+(b*ofGetWidth())];
            mapind++;
            i++;
        }
    }
    
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    
	switch (key){
		case ' ':
			bLearnBakground = true;
			break;
		case '+':
			threshold ++;
			if (threshold > 255) threshold = 255;
			break;
		case '-':
			threshold --;
			if (threshold < 0) threshold = 0;
			break;
	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){
    
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
    
    disturb(x,y);

    
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
    
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
    
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
    
}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
    
}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
    
}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 
    
}