Hi, I've been toying around with Processing for quite a long time, but only recently I started experimenting with the fancier stuff -- such as OpenGL libraries.
I'm trying to draw a series of quads in a 3D space. Each quad is textured using a PNG image, with no blending. When rendering the quads or moving the camera around them, a couple of issues arise: the transparency of the PNG images doesn't seem to work, and the z-sorting looks a bit off (but I may be wrong on this one). I have no problems whatsoever when rendering with P3D, and using plain image() calls instead of the trickier but more efficient OpenGL methods.
I am posting the full code below. Most of the OpenGL stuff is based on code snippets by Robert Hodgin. Keep in mind that I am totally new to OpenGL black magic in Processing. The relevant part of the applet is in the draw() and glRenderImage() functions.
Code:
import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
import toxi.geom.Vec3D;
import damkjer.ocd.*;
PGraphicsOpenGL pgl;
GL gl;
Texture glParticle;
POV pov;
Particle[] particles;
int numParticles = 500;
float radius = 150;
void setup() {
size(500, 500, OPENGL);
// OpenGL
pgl = (PGraphicsOpenGL)g;
gl = pgl.gl;
gl.setSwapInterval(1);
initGL();
// POV
pov = new POV(this, 0, 0, 400);
pov.ISDRAGGING = true;
// particles
particles = new Particle[numParticles];
for (int i=0; i<numParticles; i++)
particles[i] = new Particle(random(radius/2), radius, random(PI), random(TWO_PI));
// texture
try { glParticle = TextureIO.newTexture(new File(dataPath("particle.png")), true); }
catch (IOException e) { exit(); }
}
void draw() {
background(0);
pov.exist();
gl.glEnable(GL.GL_DEPTH_TEST);
pgl.beginGL();
glParticle.bind();
glParticle.enable();
for (int i=0; i<numParticles; i++) particles[i].render();
glParticle.disable();
pgl.endGL();
}
class Particle {
[...]
void render() {
glRenderImage(pos, diam);
}
}
int squareList;
void initGL() {
pgl.beginGL();
squareList = gl.glGenLists(1);
gl.glNewList(squareList, GL.GL_COMPILE);
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2f(0, 0); gl.glVertex2f(-.5, -.5);
gl.glTexCoord2f(1, 0); gl.glVertex2f(.5, -.5);
gl.glTexCoord2f(1, 1); gl.glVertex2f(.5, .5);
gl.glTexCoord2f(0, 1); gl.glVertex2f(-.5, .5);
gl.glEnd();
gl.glEndList();
pgl.endGL();
}
void glRenderImage(Vec3D pos, float diam) {
gl.glPushMatrix();
gl.glTranslatef(pos.x, pos.y, pos.z);
pov.glReverseCamera();
gl.glScalef(diam, diam, diam);
gl.glCallList(squareList);
gl.glPopMatrix();
}
class POV {
[...]
void glReverseCamera() {
gl.glRotatef(degrees(angleZ), 0, 0, 1.0);
gl.glRotatef(degrees(angleY), 0, 1.0, 0);
}
}