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

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

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:
[b]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();

[/b]

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&lt;(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();
}