I started making a simple 3d game in processing using P3D, but it lags severely when drawing more than about 5,000 textured rectangles. Here is the code that I am using to draw the rectangles.
void disp(PImage img) {
noStroke();
beginShape();
texture(img);
vertex(c1.x, c1.y, c1.z, 0, 0); vertex(o1.x, o1.y, o1.z, 1, 0); vertex(c2.x, c2.y, c2.z, 1, 1); vertex(o2.x, o2.y, o2.z, 0, 1);
endShape();
}
I believe the reason P3D is slow is because every shape must be in the draw() loop and thus must be handled by the cpu every frame. Though I am not sure.
I am running windows 10 on a ryzen 5 3600 with a gtx 1070 and I am using processing version 3.5.4.
EDIT:
The solution is a bit hard to follow so I’ll summarize for anybody with a similar problem.
Instead of using processing to draw the shapes using OpenGL is faster. In processing OpenGL is used something like this.
import com.jogamp.opengl.GL2; //required to use OpenGL
import com.jogamp.opengl.util.texture.*; //required to use textures in OpenGL (I think)
com.jogamp.opengl.util.texture.Texture texture; //should just be "Texture texture" but there is a "Texture" class in both imports so I have to specify which I'm using (there is probably a way to import things that avoids this)
void settings() {
fullScreen(P3D);
PJOGL.profile = 1;
}
void setup() {
try {
texture=TextureIO.newTexture(createInput("texture.png"), true, "png");
} catch (IOException ex) {
println("error");
}
}
void draw() {
pg = (PGraphicsOpenGL) g; //some or all of these 6 lines can probably go in setup instead of draw
pgl = beginPGL();
gl = ((PJOGL) pgl).gl.getGL2();
gl.glEnable(gl.GL_TEXTURE_2D);
gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST); //this makes the textures sharp which is great for pixel art (which I am using)
tex.bind(gl);
drawQuad();
}
void drawQuard() {
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2f(tx1, ty1);
gl.glVertex3f(x1, y1, z1);
gl.glTexCoord2f(tx2, ty2);
gl.glVertex3f(x2, y2, z2);
gl.glTexCoord2f(tx3, ty3);
gl.glVertex3f(x3, y3, z3);
gl.glTexCoord2f(tx4, ty4);
gl.glVertex3f(x4, y4, z4);
gl.glEnd();
}
It seems that you can also use OpenGL 3 and 4 in processing, but I’m not sure if that’s better or worse or if it works exactly the same so I’m using OpenGL 2.