Results 1 to 4 of 4

Thread: How to Draw Cylinder with better performance

  1. #1
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    1

    Exclamation How to Draw Cylinder with better performance

    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
    legooo_zpse480796e.jpg
    Capture_zpsac8b6059.jpg

    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

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2012
    Location
    Oldenburg, Germany
    Posts
    190
    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.

  3. #3
    Member Newbie
    Join Date
    Apr 2014
    Posts
    47
    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.
    Code :
    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.
    Code :
    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!)

  4. #4
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    394
    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.

    Attachment 1395

Similar Threads

  1. Draw Cylinder
    By JohnBruner in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-09-2015, 02:26 PM
  2. Draw Cylinder
    By noobatopengl in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-11-2015, 01:16 AM
  3. How to draw a cylinder
    By Ginger in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-30-2010, 08:24 AM
  4. How to draw 3D rotating Cylinder
    By Ravikishore in forum OpenGL: Basic Coding
    Replies: 60
    Last Post: 02-19-2010, 11:49 PM
  5. To draw a cylinder
    By yang22 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-16-2002, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean