Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Capping end of object

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

    Capping end of object

    So i need to cap the end of a cylinder that uses triangle strip, heres my creation code so far:

    drawCylinder(int numMajor, int numMinor, float height, float radius)
    {
    double majorStep = height / numMajor;
    double minorStep = 2.0 * M_PI / numMinor;
    int i, j;

    for (i = 0; i < numMajor; ++i) {
    GLfloat z0 = 0.5 * height - i * majorStep;
    GLfloat z1 = z0 - majorStep;

    glBegin(GL_TRIANGLE_STRIP);
    for (j = 0; j <= numMinor; ++j) {
    double a = j * minorStep;

    GLfloat x = radius * cos(a);
    GLfloat y = radius * sin(a);

    glNormal3f(x / radius, y / radius, 0.0);
    glTexCoord2f(j / (GLfloat)numMinor, i / (GLfloat)numMajor);
    glVertex3f(x, y, z0);

    glNormal3f(x / radius, y / radius, 0.0);
    glTexCoord2f(j / (GLfloat)numMinor, (i + 1) / (GLfloat)numMajor);
    glVertex3f(x, y, z1);


    }
    glEnd();
    }
    }

  2. #2
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    You can draw a circle at the top and bottom. The calculation of the coordinates is pretty much the same you're already using for your cylinder points. The most convenient approach for a circle is using a triangle fan (GL_TRIANGLE_FAN), use the center of the circle as the first vertex, and then the points on the periphery of the circle as the remaining vertices.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    174
    Using your code and your style of programming, the routine below yields these images.
    I've selected flat shading and wireframe on the backside of polys to show the structure of the cylinder.
    I'm using your own code to compute the bottom and top circles that cap the cylinder.
    There is replication of vertices going on (top and bottom), which is not efficient.
    Also you are generating the cylinder every time you draw it. This is also inefficient.
    It would be better to compute all the vertices once, put them in an array, and render
    the polys using indices into the array. However, what you've done is fine for a small program.
    I doubt that your height parameter is working the way you intended.
    But I leave it up to you to look into that.

    Click image for larger version. 

Name:	Top.jpg 
Views:	16 
Size:	30.6 KB 
ID:	1321 Click image for larger version. 

Name:	Bottom.jpg 
Views:	15 
Size:	36.3 KB 
ID:	1322 Click image for larger version. 

Name:	Inside.jpg 
Views:	18 
Size:	34.8 KB 
ID:	1323

    Code :
    void Draw_Cylinder (int numMajor, int numMinor, float height, float radius)
    {
        int i, j;
        GLfloat x, y, z0, z1;
        const float M_PI = 3.141593;
     
        double majorStep = height / numMajor, a;
        double minorStep = 2.0 * M_PI / numMinor;
     
        for (i = 0; i < numMajor; ++i)  {
           z0 = 0.5 * height - i * majorStep;
           z1 = z0 - majorStep;
        }
     
        glBegin (GL_TRIANGLE_STRIP);
           for (j = 0; j <= numMinor; ++j)  {      // Cylinder Sides
     
              a = j * minorStep;
     
              x = radius * cos(a);
              y = radius * sin(a);
     
              glNormal3f   (x / radius, y / radius, 0.0);
              glTexCoord2f (j / (GLfloat)numMinor, i / (GLfloat)numMajor);
              glVertex3f   (x, y, z0);
     
              glNormal3f   (x / radius, y / radius, 0.0);
              glTexCoord2f (j / (GLfloat)numMinor, (i + 1) / (GLfloat)numMajor);
              glVertex3f   (x, y, z1);
           }
        glEnd ();
     
        glColor3f (1.0, 0.6, 0.2);
        glBegin (GL_POLYGON);                    // Cylinder Bottom - Orange Circle
           glNormal3f (0.0, 0.0, -1.0);
           for (j = 0; j <= numMinor; ++j)  {
              a = j * minorStep;
              x = radius * cos(a);
              y = radius * sin(a);
              glVertex3f (x, y, z0);
           }
        glEnd ();
     
        glColor3f (1.0, 0.6, 1.0);
        glBegin (GL_POLYGON);                    // Cylinder Top - Pink Circle
           glNormal3f (0.0, 0.0, 1.0);
           for (j = 0; j <= numMinor; ++j)  {
              a = j * -minorStep;
              x = radius * cos(a);
              y = radius * sin(a);
              glVertex3f (x, y, z1);
           }
        glEnd ();
     
        return;
    }
    Last edited by Carmine; 05-29-2014 at 12:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •