#include #include // function prototypes void display(void); void reshape(int width, int height); void keyboard(unsigned char key, int x, int y); void init(void); void idle(void); // global variable -- yuck // you should create different arrays for diffuse // and specular -- this is only so that // they can be easily indexed for change float mat1[]={1.0f, 0.0f, 0.0f, 1.0f}; float mat2[]={0.0f, 0.0f, 1.0f, 1.0f}; int main(int argc, char **argv) { // set up window glutInitWindowSize(400, 400); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Materials Lab"); // register callback functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutIdleFunc(idle); // initalize opengl parameters init(); // loop until something happens glutMainLoop(); return 0; } void idle() { glutPostRedisplay(); } void init() { // initialize viewing system glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 10.0, 100.0); glMatrixMode(GL_MODELVIEW); // initialize background color to black glClearColor(0.0,0.0,0.0,0.0); // enable depth buffering glEnable(GL_DEPTH_TEST); // shading model is smooth glEnable(GL_SMOOTH); // normalize normals glEnable(GL_NORMALIZE); // initialize light GLfloat lightPosition[] = {20.0f, 20.0f, -10.0f}; // set light position glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); // this will make it a // positional light GLfloat whiteLight[] = {1.0f, 1.0f, 1.0f, 1.0f}; // set the light color to white glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight); // for diffuse glLightfv(GL_LIGHT0, GL_SPECULAR, whiteLight); // and specular glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); // light both sides // enable lighting and the light we defined glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } void reshape(int width, int height) { if (width< height) glViewport(0,0,width, width); else glViewport(0,0,height,height); } void display() { static float angle; // clear the buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // initialize modelview matrix glLoadIdentity(); //move teapot into scene glTranslatef(0.0f, 0.0f, -20.0f); // rotate about the y axis glRotatef(angle,0,1,0); angle += 1; // set material proprties glMaterialfv(GL_FRONT, GL_DIFFUSE, mat1); //draw triangle glBegin(GL_TRIANGLES); glNormal3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(10.0f,0.0f,0.0f); glVertex3f(8.0f, 4.0f,0.0f); glEnd(); // draw to screen glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { }