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

**URL:** <https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889>\
**Category:** Coding Questions\
**Created:** [July 28, 2020, 10:28pm UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889 "2020-07-28T22:28:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![capbap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/capbap/32/10112_2.png) [@capbap](https://discourse.processing.org/u/capbap)\
**Post date:** [July 28, 2020, 10:28pm UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/1 "2020-07-28T22:28:21Z")

</div>

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.

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 29, 2020, 2:32am UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/2 "2020-07-29T02:32:48Z")

</div>

This may be of interest:

> [@I need know vertex after rotate](https://discourse.processing.org/t/i-need-know-vertex-after-rotate/12719/16):
>
> Something else I tried… This is rotating shape and updating co-ordinates. I had to rotate the PVector of the vertex. PShape s; float theta; void setup() { size(600, 600, P2D); s = createShape(); s.beginShape(QUADS); s.vertex(0, 0); s.vertex(160, 0); s.vertex(160, 160); s.vertex(0, 160); s.endShape(CLOSE); fill(0); textAlign(CENTER); textSize(16); } void draw() { background(255); translate(width/2, height/2); // theta += TAU/100; for (int i = 0; i \< s.…

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

`:)`

---

<div class="post-metadata">

**Author:** ![capbap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/capbap/32/10112_2.png) [@capbap](https://discourse.processing.org/u/capbap)\
**Post date:** [July 29, 2020, 5:58am UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/3 "2020-07-29T05:58:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [July 29, 2020, 7:01am UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/4 "2020-07-29T07:01:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 29, 2020, 9:50am UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/5 "2020-07-29T09:50:06Z")

</div>

A related discussion:

> [@P3D performance with 62500 boxes](https://discourse.processing.org/t/p3d-performance-with-62500-boxes/3821):
>
> I have a program that I wrote a long time ago using the GLGraphics library and that I’m trying to port into Processing 3.0. It is a program that has 62500 boxes independently moving all over the place. In P151 I used coordinate and color lists and updated them using updateVertices(coords); and updateColors(colors). Even with 62500 boxes it ran perfectly at 60 fps. Now in P3 I have created a PShape(GROUP) called thisModel and it contains 62500 PShapes or boxes. When I try to translate the boxes i…

`:)`

---

<div class="post-metadata">

**Author:** ![capbap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/capbap/32/10112_2.png) [@capbap](https://discourse.processing.org/u/capbap)\
**Post date:** [July 29, 2020, 8:04pm UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/6 "2020-07-29T20:04:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [July 30, 2020, 4:20pm UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/7 "2020-07-30T16:20:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![capbap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/capbap/32/10112_2.png) [@capbap](https://discourse.processing.org/u/capbap)\
**Post date:** [July 30, 2020, 9:25pm UTC](https://discourse.processing.org/t/is-p3d-inherently-slow-and-or-is-there-a-fast-alternative/22889/8 "2020-07-30T21:25:17Z")

</div>

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.
