#include #include #include #include #include using namespace std; #include GLsizei winWidth=600,winHeight=500; GLfloat LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1.0f,1.0f, 1.0f }; GLfloat LightPosition[]= { 10.0f, 5.0f, 10.0f, 1.0f }; GLfloat LightAmbient1[]= { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat LightDiffuse1[]= { 1.0f, 1.0f,1.0f, 1.0f }; GLfloat LightPosition1[]= { 15.0f, 15.0f, -20.0f, 1.0f }; GLfloat LightAmbient2[]= { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat LightDiffuse2[]= { 1.0f, 1.0f,1.0f, 1.0f }; GLfloat LightPosition2[]= { -15.0f, 10.0f, -15.0f, 1.0f }; GLfloat vertices[] = { 5, 0, 0, 1.545, 0, -4.755, -4.045, 0, -2.939, -4.045, 0, 2.939, 1.545, 0, 4.755, 5, 5, 0, 1.545, 5, -4.755, -4.045, 5, -2.939, -4.045, 5, 2.939, 1.545, 5, 4.755 }; GLubyte indices[] = { 0, 1, 6, 5, 1, 2, 7, 6, 2, 3, 8, 7, 3, 4, 9, 8, 4, 0, 5, 9, 2, 1, 0, 4, 3, 7, 8, 9, 5, 6 }; int nofvertices[] = {4,4,4,4,4,5,5}; int noffaces = 7; unsigned char command[20]; int nofchar = 0; int rot = 0; void init(){ glShadeModel(GL_FLAT); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient1); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse1); glLightfv(GL_LIGHT1, GL_POSITION,LightPosition1); glLightfv(GL_LIGHT2, GL_AMBIENT, LightAmbient2); glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse2); glLightfv(GL_LIGHT2, GL_POSITION,LightPosition2); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glMatrixMode(GL_MODELVIEW); gluLookAt(10.0,15.0,15.0,0.0,0,0.0,0.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluPerspective(60.0, 1.5, 1.0, 50.0); glEnableClientState(GL_VERTEX_ARRAY); } void DrawObject() { int disp; disp = 0; for(int i = 0; i < noffaces; i++) { glDrawElements(GL_POLYGON, nofvertices[i], GL_UNSIGNED_BYTE, indices + disp); disp += nofvertices[i]; } } void DrawPart() { glScalef(0.5, 0.5, 0.5); DrawObject(); glScalef(0.5,2.0,0.5); DrawObject(); } void displayFunc() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glVertexPointer(3, GL_FLOAT, 0, vertices); glMatrixMode(GL_MODELVIEW); DrawObject(); for(int partno=1; partno<=4; partno++) { glPushMatrix(); glRotatef(90.0*partno, 0.0, 1.0, 0.0); glTranslatef(8.0, 0.0, 0.0); DrawPart(); glPopMatrix(); } glFlush(); } void winReshapeFcn(GLint newWidth, GLint newHeight){ // glViewport (0,0,newWidth,newHeight); //winWidth=newWidth; //winHeight=newHeight; } void keyboard(unsigned char key, int x, int y) { // When Command is processed carry out the Xformation if (key == 'x') { printf("%c",key); glutPostRedisplay(); } // Get Command else { printf("%c",key); } } void main(int argc, char **argv){ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(0,0); glutInitWindowSize(winWidth,winHeight); glutCreateWindow("Cmpe 210"); init(); glutDisplayFunc(displayFunc); //calls displayFunc glutReshapeFunc(winReshapeFcn); glutKeyboardFunc(keyboard); glutMainLoop(); }