Speeding Up Drawing with OpenGL

On some systems, Processing gets really slow when you use large sketches. You may find that your sketch runs ok when it's small (say, 300 pixels wide) but gets really slow when you try to make it larger, like 800x600 or 1024x768 or more. To get faster performance with full screen drawing, you may be able to use hardware acceleration on the graphics card through OpenGL. This only works if you have a graphic card that supports OpenGL, so on some machines you won't get any improvement.

OpenGL lets you use the graphics card to process 3D graphics and images, instead of processing them on the computer's main CPU. Graphics cards are optimized for doing this kind of thing, so faster. This is known as "hardware accelerated" graphics, because it uses specialized hardware. In Processing, you have to do two things to use OpenGL: import the OpenGL library, and switch into OpenGL mode in the size() function.

import processing.opengl.*; // create variables for the Images PImage fire; PImage water; void setup () { // load the images fire = loadImage("fire.jpg"); water = loadImage("water.jpg"); size(800,600,OPENGL); } void draw () { background(0,0,0); float scaling = 800/255; int opacity1 = int(mouseX/scaling); int opacity2 = 255 - opacity1; tint (255,255,255,opacity1); image(water,0,0,800,600); tint (255,255,255,opacity2); image(fire,0,0,800,600); }