PDA

View Full Version : How to Draw Cylinder with better performance



Enzo19
08-09-2014, 01:33 PM
Hello there, I am new to Opengl
I am creating building of LEGO Blocks, so each lego block will have small cylinder on top.

it will be look like this
1389
1390

I had create cylinder using gluCylinder as follow.


glPushMatrix();
glTranslatef(-0.5f,0.0f, 0.0f);
gluCylinder(pObj, 0.25f, 0.25f, 0.2f, 26, 13);
glTranslatef( 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.25f, 0.25f, 0.2f, 26, 13);
glPopMatrix();


BUT Once my building become bigger and bigger, it is become very very lag.
i tried temporary remove the cylinder and it is become very smooth, even when i put 10 big building with many blocks.

is there any way to improve the speed???
may i create cylinder using vertex??

thanks in advance

hlewin
08-09-2014, 02:16 PM
For simplicity - try putting the cylinder in a display list.
More advanced: Generate the geometry by yourself and put the Vertices into buffers and draw from there.

MtRoad
08-09-2014, 02:24 PM
Another thing to reduce the number of stacks/slices you are using, or determine the number of stacks/slices to use based on the distance from the camera so farther cylinders use fewer stacks/slices.

I'd recommend reducing the number of stacks you are using by at least half. Because the cylinders are small compared to the distance to your light sources, I doubt lighting changes much over the height of the cylinder from end to end so you shouldn't see much difference.


const int stacks = 5;
const int slices = 26;
glPushMatrix();
glTranslatef(-0.5f,0.0f, 0.0f);
gluCylinder(pObj, 0.25f, 0.25f, 0.2f, slices, stacks);
glTranslatef( 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.25f, 0.25f, 0.2f, slices, stacks);
glPopMatrix();


Check other performance optimizations such as ensuring you have fall culling on.


glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);


If you want to go super-advanced, you could use parallax (relief) mapping on the top of your geometry to represent the cylinders (never done it before, but if you will always view geometry from above and never from the side it might work. Disclaimer: never done it before myself... yet!)

Carmine
08-13-2014, 11:59 AM
The 'glu' functions are o.k. for small applications or applications where they are only called a few times. But it's not a good idea to use them when they are going to be called 100's or 1000's of times. This is because they calculate vertex coordinates each time they are called. This sucks up CPU time. Those coordinates only have to be computed once. I would do as Hlewin suggested. MtRoad also has a good suggestion - only 1 stack is needed in your situation.

Another more basic issue is that gluCylinder does not display the tops and bottoms of cylinders. This is evident even in your small image, where it looks like only the inner facing sides of the cylinders are displayed. What you really need is to display the outer facing side surfaces of the cylinders, plus a top which can be put on using gluDisk. The image below demonstrates what I'm saying. All of the cylinders were generated with 1 stack and 24 slices.

However, repeating what I said above, you shouldn't be using glu functions in your application.

1395