Loading...
Logo
Processing Forum

OpenGL slow, P2D fast?

in Programming Questions  •  3 years ago  
Greetings!

The following code fades an image (src) to white, and then fades back from white with a new image (dest). It's reasonably fast using P2D, but with OpenGL the frame rate plummets down to around 20 FPS. Why is that? Any thoughts or suggestions to improve this code is greatly appreciated.

Copy code
  1.       src.loadPixels();
  2.       dest.loadPixels();
  3.       int totalColor = src.width * src.height * 3 * 255;
  4.       int currentTotal = 0;
  5.       // fade the source image to white
  6.       for (int i = 0; i < src.width * src.height; i++) {
  7.         color rgb = src.pixels[i];
  8.         int r = (rgb >> 16) & 0xFF;
  9.         int g = (rgb >> 8) & 0xFF;
  10.         int b = rgb & 0xFF;
  11.         currentTotal += (r + b + g);        

  12.         if (currentTotal < totalColor && !stageTwo) {
  13.           r += speed;
  14.           g += speed;
  15.           b += speed;
  16.           color fin = color(r, g, b);
  17.           src.pixels[i] = fin;
  18.         }
  19.         else {
  20.           stageTwo = true;
  21.         }

  22.         // fade in the new image
  23.         if (stageTwo) {
  24.           color picked = src.pixels[i];
  25.           color target = dest.pixels[i];
  26.           if (((picked >> 16) & 0xFF) > ((target >> 16) & 0xFF)) r -= speed;
  27.           if (((picked >> 8) & 0xFF) > ((target >> 8) & 0xFF)) g -= speed;
  28.           if ((picked & 0xFF) > (target & 0xFF)) b -= speed;
  29.           src.pixels[i] = color(r, g, b);
  30.           currentTotal -= (r + b + g);
  31.         }
  32.       }
  33.       src.updatePixels();
  34.       dest.updatePixels();

Replies(3)

I'm not sure, but i am guessing it somewhat related with this. Basically opengl and the pixels array aren't very good friends.

edit: come to think of it, if opengl stores the images in the gpu memory, accessing the image will naturally be slow. The gpu is a one way superhighway, if you want to go the other direction you'll have to walk, but then again if opengl doesn't store the images in the gpu then i dunno what's up...
I too have found this. My work around is create a color [] stack where each line point in the buffer relates to a colour. That way when I make reference to the starting point of the line you can use that colour. Enabling opengl smoothing for some strange reason makes it faster. I believe that OPENGL is designed with smoothing by default we usually have to disable Processing 2X smoothing and then renable the Opengl 4x smoothing.


Sometimes OPENGL is faster, it all depends on what you are doing.
Thank you for your suggestions. I have temporarily "fixed" the problem by reducing the dimension of the images. I'll look into other solutions later on.