# 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.

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).]

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();

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

Osku

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:
[b]nexusone, you should use glDrawElements instead of a loop, it’s faster.

Osku[/b]