Results 1 to 4 of 4

Thread: To draw a cylinder

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2002
    Location
    West Lafayette, IN, USA
    Posts
    6

    To draw a cylinder

    I know from the Superbible book that I can draw a cylinder with the following code, but is there more efficient way to draw it? I guess cylinder is a special object, isn't it?
    Thank you!!

    glBegin(GL_QUADS);//for the wall
    for(theta=0; theta+=delta; theta<360)
    {...//points to specify the Quads
    }
    glEnd();

    glBegin(GL_TRIANGLE_FAN)//for the base
    for(theta=0; theta+=delta; theta<360)
    {...//points to specify the Quads
    }
    glEnd();
    I want to become an experienced OpenGL programmer

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    442

    Re: To draw a cylinder

    If you want to generate your own cylindrical geometry heres a quick way to do it:

    Generate points around a circle to the desired resolution with a radius of 1. This will be your circle point cache. Then your cyclinder consists of this point cache copied along the length of your data structure. This way you don't have to calculate sin\cos for the cylinder all the way along.

    Having said that - you really should use a display list of vertex array, rather than immediate mode for a structure like a cylinder - especially if you are interested in speed

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: To draw a cylinder

    You must remember that openGL does not provide routines for primitive shapes, just the means in which to build them.
    A cylinder is not any more special then a circle. Both have to be created from the openGL base vertex's.

    That is why there are openGL helper library's like glu(GL utility library), and GLUT (GL Utility Toolkit). Both provide predefined primitive shapes.

    Example for a cylinder there is the glu routine:
    glucylinder( GLUquadric* quad, GLdouble base, GLdouble top, GLdouble height, GLint slices, GLint stacks );

    one command replaces all of the code you are currently using.


    Originally posted by yang22:
    I know from the Superbible book that I can draw a cylinder with the following code, but is there more efficient way to draw it? I guess cylinder is a special object, isn't it?
    Thank you!!

    glBegin(GL_QUADS);//for the wall
    for(theta=0; theta+=delta; theta<360)
    {...//points to specify the Quads
    }
    glEnd();

    glBegin(GL_TRIANGLE_FAN)//for the base
    for(theta=0; theta+=delta; theta<360)
    {...//points to specify the Quads
    }
    glEnd();



  4. #4
    Junior Member Newbie
    Join Date
    Aug 2002
    Location
    West Lafayette, IN, USA
    Posts
    6

    Re: To draw a cylinder

    I started another thread using the global variable "height" to control a force output. The performance is very bad--vibration happens. If I just use win32's GDI to demo the 2-D picture instead of RenderSence(), the performance is very good. Why? Because of the time-consuming RenderSence()?

    RenderScene() will be called in
    WM_PAINT:
    RenderScene();
    And in
    WM_TIMER://here timer is 20ms
    Invalidate(hwnd,NULL,1);

    void RenderScene(void)
    {
    float fZ,bZ;
    GLdouble baserad=25;
    GLdouble toprad=25;
    GLint slices=16;
    GLint piece=4;
    GLfloat angle, x, y;
    GLUquadricObj* quadr;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    fZ = 30;//100.0f;
    bZ = -30;//-100.0f;

    glPushMatrix();
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    // Set material color, Red
    glRGB(192, 192, 192);
    glBegin(GL_QUADS);
    // Pointing straight out Z
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3f(50.0f, 50.0f, fZ);
    glVertex3f(-50.0f,50.0f,fZ);
    glVertex3f(-50.0f,-50.0f,fZ);
    glVertex3f(50.0f,-50.0f,fZ);

    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-50.0f,50.0f,fZ);
    glVertex3f(50.0f, 50.0f, fZ);
    glVertex3f(50.0f, 50.0f, bZ);
    glVertex3f(-50.0f,50.0f,bZ);
    // Bottom section
    glNormal3f(0.0f,-1.0f,0.0f);
    glVertex3f(-50.0f,-50.0f,fZ);
    glVertex3f(-50.0f,-50.0f,bZ);
    glVertex3f(50.0f, -50.0f,bZ);
    glVertex3f(50.0f, -50.0f,fZ);
    // Left section
    glNormal3f(1.0f, 0.0f, 0.0f);
    glVertex3f(50.0f, 50.0f,fZ);
    glVertex3f(50.0f, -50.0f,fZ);
    glVertex3f(50.0f, -50.0f,bZ);
    glVertex3f(50.0f, 50.0f, bZ);
    // Right Section
    glNormal3f(-1.0f, 0.0f,0.0f);
    glVertex3f(-50.0f, 50.0f,fZ);
    glVertex3f(-50.0f, 50.0f,bZ);
    glVertex3f(-50.0f,-50.0f,bZ);
    glVertex3f(-50.0f,-50.0f,fZ);
    glEnd();

    glFrontFace(GL_CW); glBegin(GL_QUADS);
    glNormal3f(0.0f,0.0f,-1.0f);
    glVertex3f(50.0f, 50.0f, bZ);
    glVertex3f(-50.0f,50.0f,bZ);
    glVertex3f(-50.0f,-50.0f,bZ);
    glVertex3f(50.0f,-50.0f,bZ);
    glEnd();

    glRGB(255, 0, 0);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0.0,0.0,height);
    for(angle=0.0f; angle<(2.01*PI);angle+=(PI/slices))
    {
    x=baserad*sin(angle);
    y=baserad*cos(angle);
    glVertex3f(x,y,height);
    }
    glEnd();

    glFrontFace(GL_CCW); quadr=gluNewQuadric();
    gluCylinder(quadr, baserad, toprad, height, slices, piece);
    // Restore the matrix state
    glPopMatrix();
    // Flush drawing commands
    glFlush();
    }
    I want to become an experienced OpenGL programmer

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 in 3d , given its endpoints.
    By arnott in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 09-07-2000, 09:22 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