Results 1 to 5 of 5

Thread: best way to draw a pyramid

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2002
    Posts
    6

    best way to draw a pyramid

    What is the best way to draw a simple five-faced pyramid in opengl? Should I use a vertex array for it? I have this at the moment:


    glBegin(GL_LINE_LOOP);


    glVertex3f(0.5,-0.5,0.0);
    glVertex3f(0.5,0.5,0.0);
    glVertex3f(-0.5,0.5,0.0);
    glVertex3f(-0.5,-0.5,0.0);
    glEnd();
    //draw the nose
    glBegin(GL_LINES);

    glVertex3f(0.5,-0.5,0.0);
    //glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,0.0,1);

    //glColor3f(1.0,1.0,1.0);
    glVertex3f(0.5,0.5,0.0);
    //glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,0.0,1);

    //glColor3f(1.0,1.0,1.0);
    glVertex3f(-0.5,0.5,0.0);
    //glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,0.0,1);

    //glColor3f(1.0,1.0,1.0);
    glVertex3f(-0.5,-0.5,0.0);
    // glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,0.0,1);
    glEnd();

    thanks, tim.

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

    Re: best way to draw a pyramid

    Here is a pyramid that I used for a little demo program as part of my learning OpenGL. Lot of improvements could be made. As to which way is the best, depends on what you want to do with the final program.

    Now this is a four sided pyramid, but to add a side would not be hard. Also I used GL_TRIANGLE for the side. But for five sided pyramid you can use GL_QUAD for the bottom.


    glTranslate(x, y, x) location of the pyramid
    glRotate() // rotate the pyramid
    glscale() // scale the pyramid
    pyramid() // call out rutine to draw a pyramid

    void pyramid()
    {
    glBegin( GL_TRIANGLES );
    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.f, 0.0f );
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);

    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f);
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);

    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);


    glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f);
    glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);
    glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);

    glEnd();

    }

    [This message has been edited by nexusone (edited 01-31-2002).]

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

    Re: best way to draw a pyramid

    Yes, vertex array's is the most flexible way to define a object or objects.
    This way you need only to change your array data to create a diffrent object.

    here is part of a program I am currently working on with vertex array's.

    typedef struct
    {
    GLfloat xyz[3];

    }Point3F;

    static Point3F dataf[5] = {{1.0f,1.0f,0.0f}, {1.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}, {0.0f,1.0f,0.0f}, {1.0f,1.0f,0.0f}}; // object data

    // here I draw the object
    glPushMatrix();
    glScalef(xscale, yscale, zscale);
    glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
    glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
    glRotatef(rotateZ, 0.0f, 0.0f, 1.0f);
    glTranslatef(object[0].x, object[0].y, object[0].z);
    glBegin(GL_POLYGON);
    glColor3fv( &object[0].Color3f );
    for(i=0;i<=object[0].Sides;i++)
    {
    glVertex3fv( &object[0].Vertex3f[i] );
    }
    glEnd();

  4. #4
    Member Contributor
    Join Date
    Feb 2000
    Location
    Gothenburg, Sweden
    Posts
    68

    Re: best way to draw a pyramid

    nexusone, you should use glDrawElements instead of a loop, it's faster.

    Osku

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

    Re: best way to draw a pyramid

    Thanks for the tip.... I was thinking there should be a way to pass a list of vertex to be drawn.

    Originally posted by Osku:
    nexusone, you should use glDrawElements instead of a loop, it's faster.

    Osku

Similar Threads

  1. Image Pyramid and OpenGL
    By Leonid in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-11-2006, 01:48 AM
  2. Truncated pyramid of frustrum
    By akuda in forum OpenGL: General
    Replies: 11
    Last Post: 08-03-2006, 02:59 PM
  3. Help me : I want to show a pyramid
    By tranckhanh in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-21-2001, 06:11 PM
  4. Pyramid Texture Coords
    By GoatNat in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-24-2001, 04:14 PM
  5. pyramid
    By fox in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-18-2000, 02:09 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