how to optmize draw ofImage?

hey there,

some days ago i started a project using processing. in fact, it’s a simple stuff: it would allow me to compose a certain image using others as “pixels”. this image explains a little bit more what i mean:
http://twitpic.com/oat56
although the simple “animations” i’ve made (translating z-axis), the whole stuff worked fine, with a good framerate.

now i’m trying to do tha same thing with OF. first i loaded all the images as ofImages (about 72 images) and drawing them after that (there are something arround “10000 pixels”. each one is represented by one that 72 ofImages). everything was going fine when i noticed that i couldn’t get more than 7 FPS. here’s what i am doing:

  
  
  
void draw(){  
  
	glEnable(GL_DEPTH_TEST);	  
	ofBackground(0, 0, 0);  
  
	int index = 0;  
	int cellsize = 15;  
	int columns = img.width / cellsize;  // Calculate # of columns  
    int rows = img.height / cellsize;  // Calculate # of rows  
	  
	unsigned char* pixels = img.getPixels();  
  
	ofFill();  
	  
	// Begin loop for columns  
    for ( int i = 0; i < columns; i++) {  
      // Begin loop for rows  
      for ( int j = 0; j < rows; j++) {  
        int x = i*cellsize;  // x position  
        int y = j*cellsize;  // y position  
        int loc = (x + y*img.width)*3;  // Pixel array location  
          
		  
		int r = pixels[loc];  // Grab the color  
		int g = pixels[loc+1];  
		int b = pixels[loc+2];  
		  
		float brightness = (0.2126*r) + (0.7152*g) + (0.0722*b);  
		float factor = timer;  
        int z = (factor) * (brightness)-10;  
  
		  
		ofPushMatrix();  
		ofTranslate(x, y, (z*30));  
		//ofSetColor(255, 255, 255);  
        ofSetColor(r, g, b, 255);  
        index = ((index+1)%nImages);       
		img_pixels[index].draw(0, 0, cellsize, cellsize);  
		  
        ofPopMatrix();  
		  
      }  
    }  
	  
	glDisable(GL_DEPTH_TEST);	  
	  
}  
  
  
  

the problem is in line

  
  
img_pixels[index].draw(0, 0, cellsize, cellsize);  
  

removing it gives me high fps again, but not what i want… how could i do the same thing, but with a greater framerate?

ps: how could this be slower than a processing sketch? ideas of why?

btw, i’m a big fan of oF: what a awesome project!
thanks to everybody involved on this (and for that that eventually could help me)!

hi,

at the moment, due to the architecture of the system, oF is not so good at heavy-duty pixel-pushing. (why? because oF is based on OpenGL, which is optimised for uploading textures once at boot time and then doing all the heavy lifting through geometry data; Processing on the other hand uses pixel data all the way to the end.)

i think the main thing that’s killing your framerate here is the piece-wise copying of data between system memory and GPU memory.

you might be able to optimise things by pre-scaling your source images to be square and with power of two dimensions (eg 16x16, 32x32, 64x64, 128x128 etc), ideally about the same size as your cellsize.

alternatively, you could allocate a huge chunk of system memory and build up the image locally, then just upload the whole thing to video memory in a giant ofTexture, and draw that.

thank you, damian. your answer was very helpful.
i’ve decided for now to go on with my processing sketch.
however, based on this kind of experiments with oF, i’ve deveped this-work! and it just ran fine!

thanks again,