/* Solution to the material and lighting lab exercise. Based on TeddyBearLit.cc Written by Scott D. Anderson scott.anderson@acm.org Fall 2006 */ #include #include #include bool Light0 = true; bool Light1 = true; void display(void) { twDisplayInit(); twCamera(); // sets up camera based on bounding box coords. glEnable(GL_LIGHTING); glShadeModel(GL_SMOOTH); // smooth shading looks nicer twAmbient(0.1); // turn down ambient to make the lights more obvious // light from upper left; both lights are there. GLfloat lightPos[] = { -4, 5, 8, 1 }; // 50% gray light, for first phase twGrayLight(GL_LIGHT0,lightPos,0.5,0.5,0.5); if(Light0) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0); // pink light, for second phase const GLfloat d = 500; GLfloat lightAmb[] = { 255/d, 182/d, 193/d, 1 }; // pink GLfloat lightDiff[] = { 255/d, 182/d, 193/d, 1 }; GLfloat lightSpec[] = { 255/d, 182/d, 193/d, 1 }; glLightfv(GL_LIGHT1, GL_POSITION, lightPos); glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmb); glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiff); glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpec); if(Light1) glEnable(GL_LIGHT1); else glDisable(GL_LIGHT1); // light blue ball const GLfloat e = 255; GLfloat ballColor[] = { 191/e, 239/e, 255/e }; twColor(ballColor,0.9,20); glPushMatrix(); glTranslatef(3,0,0); // put the sphere to the right boat(); //glutSolidSphere(2.5,40,40); glPopMatrix(); // light green triangle const GLfloat f = 255; GLfloat triangleColor[] = { 144/f, 238/f, 144/f }; twColor(triangleColor,0.5,2); glPushMatrix(); glTranslatef(-1,0,0); // triangle to the left glRotatef(-10,1,0,0); // tilted slightly up glScalef(-2,2,2); // reversed in x direction // The following is a 3,4,5 triangle in the z=0 plane, with normal // sticking out towards the viewer. glBegin(GL_TRIANGLES); { glNormal3f(0,0,1); glVertex3f(0,0,0); glVertex3f(4,0,0); glVertex3f(0,3,0); } glEnd(); glPopMatrix(); glFlush(); glutSwapBuffers(); // necessary for animation } void lightToggle(unsigned char k, int x, int y) { switch(k) { case '1': Light0 = !Light0; break; case '2': Light1 = !Light1; break; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(650,650); glutCreateWindow(argv[0]); twBoundingBox(-5,5.5,-2.5,6,-2.5,2.5); twMainInit(); twKeyCallback('1', lightToggle, "Toggle first light"); twKeyCallback('2', lightToggle, "Toggle second light"); glutDisplayFunc(display); glutMainLoop(); return 0; }