package org.lwjgl.opengl.glu; import org.lwjgl.opengl.GL; /** * Cylinder.java * * * Created 23-dec-2003 * @author Erik Duijs */ public class Cylinder extends Quadric implements GLUConstants { /** * Constructor for Cylinder. */ public Cylinder() { super(); } /** * draws a cylinder oriented along the z axis. The base of the * cylinder is placed at z = 0, and the top at z=height. Like a sphere, a * cylinder is subdivided around the z axis into slices, and along the z axis * into stacks. * * Note that if topRadius is set to zero, then this routine will generate a * cone. * * If the orientation is set to GLU.OUTSIDE (with glu.quadricOrientation), then * any generated normals point away from the z axis. Otherwise, they point * toward the z axis. * * If texturing is turned on (with glu.quadricTexture), then texture * coordinates are generated so that t ranges linearly from 0.0 at z = 0 to * 1.0 at z = height, and s ranges from 0.0 at the +y axis, to 0.25 at the +x * axis, to 0.5 at the -y axis, to 0.75 at the -x axis, and back to 1.0 at the * +y axis. * * @param baseRadius Specifies the radius of the cylinder at z = 0. * @param topRadius Specifies the radius of the cylinder at z = height. * @param height Specifies the height of the cylinder. * @param slices Specifies the number of subdivisions around the z axis. * @param stacks Specifies the number of subdivisions along the z axis. */ public void draw(float baseRadius, float topRadius, float height, int slices, int stacks) { float da, r, dr, dz; float x, y, z, nz, nsign; int i, j; if (super.orientation == GLU_INSIDE) { nsign = -1.0f; } else { nsign = 1.0f; } da = 2.0f * PI / slices; dr = (topRadius - baseRadius) / stacks; dz = height / stacks; nz = (baseRadius - topRadius) / height; // Z component of normal vectors if (super.drawStyle == GLU_POINT) { GL.glBegin(GL.GL_POINTS); for (i = 0; i < slices; i++) { x = cos((i * da)); y = sin((i * da)); normal3f(x * nsign, y * nsign, nz * nsign); z = 0.0f; r = baseRadius; for (j = 0; j <= stacks; j++) { GL.glVertex3f((x * r), (y * r), z); z += dz; r += dr; } } GL.glEnd(); } else if (super.drawStyle == GLU_LINE || super.drawStyle == GLU_SILHOUETTE) { // Draw rings if (super.drawStyle == GLU_LINE) { z = 0.0f; r = baseRadius; for (j = 0; j <= stacks; j++) { GL.glBegin(GL.GL_LINE_LOOP); for (i = 0; i < slices; i++) { x = cos((i * da)); y = sin((i * da)); normal3f(x * nsign, y * nsign, nz * nsign); GL.glVertex3f((x * r), (y * r), z); } GL.glEnd(); z += dz; r += dr; } } else { // draw one ring at each end if (baseRadius != 0.0) { GL.glBegin(GL.GL_LINE_LOOP); for (i = 0; i < slices; i++) { x = cos((i * da)); y = sin((i * da)); normal3f(x * nsign, y * nsign, nz * nsign); GL.glVertex3f((x * baseRadius), (y * baseRadius), 0.0f); } GL.glEnd(); GL.glBegin(GL.GL_LINE_LOOP); for (i = 0; i < slices; i++) { x = cos((i * da)); y = sin((i * da)); normal3f(x * nsign, y * nsign, nz * nsign); GL.glVertex3f((x * topRadius), (y * topRadius), height); } GL.glEnd(); } } // draw length lines GL.glBegin(GL.GL_LINES); for (i = 0; i < slices; i++) { x = cos((i * da)); y = sin((i * da)); normal3f(x * nsign, y * nsign, nz * nsign); GL.glVertex3f((x * baseRadius), (y * baseRadius), 0.0f); GL.glVertex3f((x * topRadius), (y * topRadius), (height)); } GL.glEnd(); } else if (super.drawStyle == GLU_FILL) { float ds = 1.0f / slices; float dt = 1.0f / stacks; float t = 0.0f; z = 0.0f; r = baseRadius; for (j = 0; j < stacks; j++) { float s = 0.0f; GL.glBegin(GL.GL_QUAD_STRIP); for (i = 0; i <= slices; i++) { if (i == slices) { x = sin(0.0f); y = cos(0.0f); } else { x = sin((i * da)); y = cos((i * da)); } if (nsign == 1.0f) { normal3f((x * nsign), (y * nsign), (nz * nsign)); TXTR_COORD(s, t); GL.glVertex3f((x * r), (y * r), z); normal3f((x * nsign), (y * nsign), (nz * nsign)); TXTR_COORD(s, t + dt); GL.glVertex3f((x * (r + dr)), (y * (r + dr)), (z + dz)); } else { normal3f(x * nsign, y * nsign, nz * nsign); TXTR_COORD(s, t); GL.glVertex3f((x * r), (y * r), z); normal3f(x * nsign, y * nsign, nz * nsign); TXTR_COORD(s, t + dt); GL.glVertex3f((x * (r + dr)), (y * (r + dr)), (z + dz)); } s += ds; } // for slices GL.glEnd(); r += dr; t += dt; z += dz; } // for stacks } } }