Advanced OpenGL

From Processing

Jump to: navigation, search
Versions: 1.0+
Contributors: REAS, JeffG
Started: 2010-06-06


Using the OpenGL Renderer is for advanced users only!

Generally, this should be only considered a last-ditch way to get at a particular GL-specific feature. It can make your code incompatible with everything else (such as future versions of Processing or other 3D renderers) and will confuse the hell outta people when you post your code to the web. Again, if this works for you great, but if not, we aren't responsible. If you want to write code for Java and JOGL, then find yourself a good Java IDE and get JOGL installed, Processing is probably not the way to do it because that's the kind of confusing mess we're trying to insulate you from.

Contents

Source Code

Processing 1.x

To get access to the GL object, use the following:

GL gl = ((PGraphicsOpenGL)g).gl;

This will give you JOGL's GL object that talks directly to OpenGL and will let you make OpenGL calls directly.

Again, this is not supported, in the sense that if you get weird problems with a GL-based sketch that uses this method, you're on your own.

Because most of the work in drawing is handled by Processing, most OpenGL calls will not work, because most things won't be set up properly. Therefore, using the GL object may only be useful for tweaking obscure OpenGL parameters.

Another option for talking directly to OpenGL in release 0116 and later is to use the beginGL() and endGL() methods. These were added against my better judgment, and handle setting up the camera matrices (though still no lighting or other parameters). Many features still will not work, but it opens up lots of control for geometry. All the above caveats about "don't cry when it breaks" apply.

import javax.media.opengl.*;
import processing.opengl.*;
 
float a; 
 
void setup() {
  size(800, 600, OPENGL);
}
 
void draw() {
  background(255);
 
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  // g may change
  GL gl = pgl.beginGL();  // always use the GL object returned by beginGL
 
  // Do some things with gl.xxx functions here.
  // For example, the program above is translated into:
  gl.glColor4f(0.7, 0.7, 0.7, 0.8);
  gl.glTranslatef(width/2, height/2, 0);
  gl.glRotatef(a, 1, 0, 0);
  gl.glRotatef(a*2, 0, 1, 0);
  gl.glRectf(-200, -200, 200, 200);
  gl.glRotatef(90, 1, 0, 0);
  gl.glRectf(-200, -200, 200, 200);
 
  pgl.endGL();
 
  a += 0.5;
}

Always do the assignment/cast from g to pgl inside draw(), because 'g' may change from time to time. In addition, use the GL object returned by beginGL() for the same reason, that the GL context itself may have changed. In practice, I've not really seen this happen, but this is strongly encouraged to prevent things from crashing in a bad way (think blue screen/kernel panic kinds of bad ways).

Processing 2.x

To get access to the GL2 object, use the following:

GL2 gl = ((PGraphicsOpenGL)g).beginPGL().gl.getGL2();

This will give you JOGL's GL2 object that talks directly to OpenGL and will let you make OpenGL calls directly.

Again, this is not supported, in the sense that if you get weird problems with a GL-based sketch that uses this method, you're on your own.

Because most of the work in drawing is handled by Processing, most OpenGL calls will not work, because most things won't be set up properly. Therefore, using the GL object may only be useful for tweaking obscure OpenGL parameters.

Another option for talking directly to OpenGL in release 2.x and later is to use the beginPGL() and endPGL() methods. These were added against my better judgment, and handle setting up the camera matrices (though still no lighting or other parameters). Many features still will not work, but it opens up lots of control for geometry. All the above caveats about "don't cry when it breaks" apply.

import javax.media.opengl.GL2;
 
float a; 
GL2 gl; 
PGraphicsOpenGL pgl;
float[] projMatrix;
float[] mvMatrix;
 
void setup() {
  size(800, 600, OPENGL);
  pgl = (PGraphicsOpenGL) g;  // g may change
  gl = pgl.beginPGL().gl.getGL2();
  projMatrix = new float[16];
  mvMatrix = new float[16]; 
}
 
void draw() {
  background(255);
  loadMatrix();
  pgl.beginPGL();
  // Do some things with gl.xxx functions here.
  // For example, the program above is translated into:
  gl.glColor4f(0.7, 0.7, 0.7, 0.8);
  gl.glTranslatef(width/2, height/2, 0);
  gl.glRotatef(a, 1, 0, 0);
  gl.glRotatef(a*2, 0, 1, 0);
  gl.glRectf(-200, -200, 200, 200);
  gl.glRotatef(90, 1, 0, 0);
  gl.glRectf(-200, -200, 200, 200);
 
  pgl.endPGL();
 
  a += 0.5;
}
 
 
void loadMatrix() {
 
  gl.glMatrixMode(GL2.GL_PROJECTION);
  projMatrix[0] = pgl.projection.m00;
  projMatrix[1] = pgl.projection.m10;
  projMatrix[2] = pgl.projection.m20;
  projMatrix[3] = pgl.projection.m30;
 
  projMatrix[4] = pgl.projection.m01;
  projMatrix[5] = pgl.projection.m11;
  projMatrix[6] = pgl.projection.m21;
  projMatrix[7] = pgl.projection.m31;
 
  projMatrix[8] = pgl.projection.m02;
  projMatrix[9] = pgl.projection.m12;
  projMatrix[10] = pgl.projection.m22;
  projMatrix[11] = pgl.projection.m32;
 
  projMatrix[12] = pgl.projection.m03;
  projMatrix[13] = pgl.projection.m13;
  projMatrix[14] = pgl.projection.m23;
  projMatrix[15] = pgl.projection.m33;
 
  gl.glLoadMatrixf(projMatrix, 0);
 
  gl.glMatrixMode(GL2.GL_MODELVIEW);
  mvMatrix[0] = pgl.modelview.m00;
  mvMatrix[1] = pgl.modelview.m10;
  mvMatrix[2] = pgl.modelview.m20;
  mvMatrix[3] = pgl.modelview.m30;
 
  mvMatrix[4] = pgl.modelview.m01;
  mvMatrix[5] = pgl.modelview.m11;
  mvMatrix[6] = pgl.modelview.m21;
  mvMatrix[7] = pgl.modelview.m31;
 
  mvMatrix[8] = pgl.modelview.m02;
  mvMatrix[9] = pgl.modelview.m12;
  mvMatrix[10] = pgl.modelview.m22;
  mvMatrix[11] = pgl.modelview.m32;
 
  mvMatrix[12] = pgl.modelview.m03;
  mvMatrix[13] = pgl.modelview.m13;
  mvMatrix[14] = pgl.modelview.m23;
  mvMatrix[15] = pgl.modelview.m33;
  gl.glLoadMatrixf(mvMatrix, 0);
}

Downloads

Related Links


See also

Personal tools