Loading...
Logo
Processing Forum
Hi all,

I'm trying to optimize my game using retained mode, i.e.drawing a lot of my geometry using PShapes, which I understand are implemented as VBOs in Processing 2.x. Before this I was drawing a quad-based heightmap terrain in immediate mode, within which some of the quads (tiles) are differently textured. I found that it was really slow when it had to switch between textures from tile to tile, so I thought maybe moving to using blocks of PShapes to draw the terrain would be faster. Unfortunately using textures with the PShape is a lot slower. Here's the code that generates a block of textured terrain:

void createGroundVBOModel()
    {
        terrainModel = createShape(GROUP);
   
             for(int i =0; i < numGroundQuads-2; i++)
             for(int j =0; j < numGroundQuads-2; j++)
             if(true) //ignore this, it will be used for checking frustum culling later
             {
                 
                 PShape thisShape = createShape();
                 
                      Quad quad = terrainQuads.get(i + j*(numGroundQuads-1));
               
                      //if it's bitgrass, get the right tile
                      PImage bitGrassTex;
                     
                      bitGrassTex = getShoreQuadTexFromTileset(i, j, bitGrass);
                      //bitGrassTex = blackWater;
                      thisShape.texture(bitGrassTex);
                     
                 
                      //if all four verts of this quad are at sea level then texture this is sea
                     if(quad.isWater)
                     {
                         
                         thisShape.texture(blackWater);
                     }
                     
                     thisShape.noFill();
                     thisShape.noStroke();
                     
                        thisShape.vertex(quad.tvert1.x,quad.tvert1.y, quad.tvert1.z, 0, 0);
                        thisShape.vertex(quad.tvert2.x, quad.tvert2.y, quad.tvert2.z, 64, 0);
                        thisShape.vertex(quad.tvert3.x, quad.tvert3.y, quad.tvert3.z, 64, 64);
                        thisShape.vertex(quad.tvert4.x, quad.tvert4.y, quad.tvert4.z, 0, 64);
                     
                      thisShape.end();
                     
                      terrainModel.addChild(thisShape);
                   
                  }
            
            // terrainModel.end();
    }

I then simply call shape(terrainModel); in my draw function (amongst a lot of other things related to the camera movement).

Any idea why this would happen?

Thanks a lot!

Replies(2)

Hello, you are correct, PShape uses VBOs with the P2D and P3D renderers, which in general makes rendering faster, specially when the geometry is static. When you create a group shape, like terrainModel in your case, with several child shapes, Processing will try to aggregate all the geometry of the children into a single VBO so the rendering happens in a single call, which results in optimal performance. However, this aggregation is not possible if each child shape is textured with a different image since in that case each shape has to be rendered separately. What you could do in this situation is to combine all your terrain tiles into a single large texture, and then set the appropriate texture coordinates for each of the quad shapes so they read the texture at the correct location.
Interesting! Thanks a lot for your response, I thought I'd be left in the dark with this one. I'll try what you described.