/* This program displays a curve looking vaguely like the outline of a Coke bottle. The bottle silhouette is three Bezier curves, with the transitions at the upper bulge and lower dent, since I'll assume that the tangent is vertical at that point. This version makes the control point arrays 1D, thereby eliminating a horrendous cast that the old version had when passing it to OpenGL. Scott D. Anderson April 1999 Original Fall 2003 revised to use TW */ #include #include #include #include #include /* ********************************************************************** Global variables, parameters and constants. */ /* Upper curve, from diameter of 0.75in at height 5in to diameter of 1.5in at height 2.5in. */ GLfloat upper_cp[] = {0.5/2, 5.0, 0.0, 0.5/2, 4.0, 0.0, 1.5/2, 3.0, 0.0, 1.5/2, 2.5, 0.0}; /* Middle curve, from upper bulge (see previous) to dent with diameter of 1.25in at height of 1.25in. */ GLfloat middle_cp[] = {1.5/2, 2.5, 0.0, 1.5/2, 2.0, 0.0, 1.25/2, 1.75, 0.0, 1.25/2, 1.25, 0.0}; /* Lower curve, from dent to base, with a radius the same as the bulge. */ GLfloat lower_cp[] = {1.25/2, 1.25, 0.0, 1.25/2, 0.75, 0.0, 1.5/2, 0.50, 0.0, 1.5/2, 0.00, 0.0}; void draw_bezier_curve(GLfloat* cp) { int steps = 8; glMap1f(GL_MAP1_VERTEX_3, 0, 1, 3, 4, cp); glEnable(GL_MAP1_VERTEX_3); glMapGrid1f(steps,0.0,1.0); glEvalMesh1(GL_LINE,0,steps); } void reflect() { int i; // reflect across the yz plane, by negating the x value for(i=0;i<12;i+=3) { upper_cp[i] = - upper_cp[i]; middle_cp[i] = - middle_cp[i]; lower_cp[i] = - lower_cp[i]; } } void draw_silhouette() { glPushAttrib(GL_ALL_ATTRIB_BITS); glLineWidth(3.0); twColorName(TW_MAGENTA); draw_bezier_curve(upper_cp); twColorName(TW_YELLOW); draw_bezier_curve(middle_cp); twColorName(TW_CYAN); draw_bezier_curve(lower_cp); // reflect(); glPushMatrix(); glScalef(-1,1,1); draw_bezier_curve(upper_cp); draw_bezier_curve(middle_cp); draw_bezier_curve(lower_cp); glPopMatrix(); //reflect(); glPopAttrib(); } void display(void) { twDisplayInit(); twCamera(); draw_silhouette(); glFlush(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(300,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(-1.5/2,1.5/2,0,5,-0.25,+0.25); twMainInit(); glutMainLoop(); }