Is P3D inherently slow and/or is there a fast alternative?

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.

1 Like

This may be of interest:

I modified that example for P3D, added a z and created a textured shape in setup() and changed vertices in draw().

:)

I modified my code to use PShape so that I don’t keep remaking the shape every frame, however it didn’t change the framerate.

I’m not certain it will work but maybe, just a maybe, you could try putting like 2500 faraway shapes in an if statement with a boolean. And the boolean returns true only when the player is at a certain area (close to those shapes).

As I said tho, I don’t know if it will or it could work. Or there could be a better way of doing this.

A related discussion:

:)

3 Likes

I have now tried PJOGL and it seems that it will fix the framerate issues. Unfortunately I don’t know how to use it in processing. I can’t figure out how to draw shapes with textures.

You probably want to batch all shapes using the same texture within a single pair of beginShape() / endShape() calls, and if you are using a lot of different images, consider creating a single image with them all in and use texture coordinates to draw what you want.

2 Likes

Grouping all the texture into one texture file will work well for this application. I seem to have figured out how to get the opengl stuff working so I should be set now.

2 Likes