/* Mala Sarkar CS307 Fall 2005 Demo of Pillow This doesn't actually use a library object, but it might become one someday. */ #include #include #include //#include "msarkar.cc" //default color for pillow twTriple duskyLavender = {0.73,0.67,0.75}; bool Wire = false; float s = 5; GLfloat pillowFront[12][3] = { {s,-s,0}, {s,0,0}, {s,s,0}, {0,-s,0}, {0,0,1.75*s}, {0,s,0}, {-s,-s,0}, {-s,0,0}, {-s,s,0}, }; GLfloat pillowBack[12][3] = { {s,-s,0}, {s,0,0}, {s,s,0}, {0,-s,0}, {0,0,-1.75*s}, {0,s,0}, {-s,-s,0}, {-s,0,0}, {-s,s,0}, }; // This draws the whole surface, using a mesh with `nsteps' in each direction void draw_bezier_surface(int nsteps, GLfloat* control_points) { glEnable(GL_AUTO_NORMAL); // automatically compute the normals glMap2f(GL_MAP2_VERTEX_3, 0,1,3,3, 0,1,9,3, control_points); glEnable(GL_MAP2_VERTEX_3); // set up grid and generate the desired surface glMapGrid2f(nsteps, 0, 1, nsteps, 0, 1); glEnable(GL_NORMALIZE); if(Wire) glEvalMesh2(GL_LINE, 0, nsteps, 0, nsteps); else glEvalMesh2(GL_FILL, 0, nsteps, 0, nsteps); } void pillowMaterial() { glShadeModel(GL_SMOOTH); GLfloat ambient[] = { 155/255.0, 155/255.0, 255/255.0, 0 }; GLfloat diffuse[] = {0.2,0.3,0.1,0}; glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); } void drawPillow(twTriple pillowColor) { glPushMatrix(); glShadeModel(GL_SMOOTH); twColor(pillowColor,0,0); pillowMaterial(); draw_bezier_surface(10,pillowBack[0]); draw_bezier_surface(10,pillowFront[0]); glPopMatrix(); } void setLight () { glEnable(GL_LIGHTING); //directional light GLfloat light0[4]={1,1,1,0}; twGrayLight(GL_LIGHT0,light0 ,0.2,0.6, 0.3); } void display(void) { twDisplayInit(); twCamera(); //set up camera based on bounding box coordinates setLight(); //draw pillow drawPillow(duskyLavender); glFlush(); glutSwapBuffers(); // necessary for animation } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(650,650); glutCreateWindow(argv[0]); glutDisplayFunc(display); //create a bounding box tailored to fit the pillow dimensions twBoundingBox(-s,s,-s,s,-0.45*s,0.45*s); twMainInit(); glutMainLoop(); return 0; }