I had a flash thought this morning about using P3D instructions in OpenGL Mode, and I was right.
Testing with this code, I think that an automatic flush is called when you use P3D instruction in OpenGL rendering mode, thus making it slow quite fast if you have a lot of primitives.
Learnt : either use P3D with processing graphic function, or OPENGL with OpenGL context functions, but not a mix if you want it to be fast
Code:
import javax.media.opengl.*;
import com.sun.opengl.util.*;
import processing.opengl.*;
String Mode = OPENGL;
String RMode = P3D; // Rendering mode, mean do we use processing function to display or GL context functions
PFont fontA;
boolean bFlush = false;
// ____________________
void setup()
{
if ( RMode == OPENGL )
Mode = OPENGL;
size(640, 448, Mode);
if ( Mode == OPENGL )
{
// enable VSync to avoid framerate problems ?
PGraphicsOpenGL pgl;
GL gl;
pgl = (PGraphicsOpenGL)g; //processing graphics object
gl = pgl.beginGL(); //begin opengl
gl.setSwapInterval(1); //set vertical sync on, screen refresh is 60 Hz so framerate will be 60
pgl.endGL(); //end opengl
}
else
{
frameRate(60); // a little more than 60 because 60hz screen and vsync activated ?
}
background(10);
fontA = loadFont("ank-48.vlw");
}
// ____________________
void draw()
{
background(10);
// display framerate
color FillColor = color(204,204,204,255);
fill(FillColor);
textFont(fontA, 14);
text("fps "+frameRate, 15, 15);
text("Viewport Mode = "+Mode,15,30);
text("Instruction Mode (R) = "+RMode, 15, 45);
text("force flush (F) ? "+bFlush, 15, 60);
// send some triangles to display
// unoptimized to test
int psize = 10;
noStroke();
if ( RMode == P3D )
{
for (int i=0; i<2000; i++)
{
pushMatrix();
translate(random(width/3.0)+width/3.0, random(height/3.0)+height/3.0, random(80)+80); // reverse order, translate before rotate
rotateZ(random(2*PI));
beginShape();
fill(color(random(255), random(255), random(255), random(255)));
vertex(-psize/2.0, -psize/2.0);
vertex(psize/2.0, -psize/2.0);
vertex(psize/2.0, psize/2.0);
vertex(-psize/2.0, psize/2.0);
endShape();
popMatrix();
if ( bFlush )
flush();
}
}
else // RMode == OPENGL
{
for (int i=0; i<2000; i++)
{
PGraphicsOpenGL pgl;
GL gl;
pgl = (PGraphicsOpenGL)g; //processing graphics object
gl = pgl.beginGL(); //begin opengl
gl.glPushMatrix();
gl.glTranslatef(random(width/3.0)+width/3.0, random(height/3.0)+height/3.0, random(80)+80);
gl.glRotatef( random(2*PI), 0,0,1 );
gl.glBegin(gl.GL_POLYGON);
gl.glColor3f(random(255)/255.0,random(255)/255.0,random(255)/255.0);
gl.glVertex3f(-psize/2.0, -psize/2.0, 0.0f);
gl.glVertex3f(psize/2.0, -psize/2.0, 0.0f);
gl.glVertex3f(psize/2.0, psize/2.0, 0.0f);
gl.glVertex3f(-psize/2.0, psize/2.0, 0.0f);
gl.glEnd();
gl.glPopMatrix();
if ( bFlush )
gl.glFlush();
pgl.endGL(); //end opengl
}
}
}
// ____________________
void keyPressed()
{
if ( key == 'f' || key == 'F' )
bFlush = !bFlush;
if ( key == 'r' || key == 'R' )
{
if ( Mode == OPENGL )
{
if (RMode == OPENGL )
{
RMode = P3D;
println("going to P3D Rmode");
}
else if ( RMode == P3D )
{
RMode = OPENGL;
println("going to OPENGL Rmode");
}
}
else
println("Can't switch to OpenGL instructions in P3D render mode");
}
}