/* Model's filename is: mymodel.dat Commands -------- 1: Turn on the directional light. 2: Turn on the positional light. 0: Turn off the directional and positional light. A: Increase the ambient light. a: Decrease the ambient light. D: Increase the diffuse light. d: Decrease the diffuse light. S: Increase the specular component of the positional light. s: Decrease the specular component of the positional light. H: Increase the shininess of the object. h: Decrease the shininess of the object. E: Increase the emissive light of the object. e: Decrease the emissive light of the object. +: Increase the cutoff angle of the positional light. -: Decrease the cutoff angle of the positional light. P, p: Rotate the positional light about the Z-axis that is on the XY principal plane. O, o: Rotate the objects about the Y-axis about the center of objects. */ #include #include #include #define MAXLINE 161200 static GLint width, height; static GLfloat LR = -3.0; GLfloat light1_position[] = {2.0, 0.0, 0.0, 1.0}; GLfloat light2_position[] = {-2.0, 0.0, 0.0, 1.0}; GLfloat ambient_light[] = {0.5, 0.5, 0.5, 1.0}; GLfloat diffuse_light[] = {0.5, 0.5, 0.5, 1.0}; GLfloat specular_light[] = { 0.5f, 0.5f, 0.5f, 1.0f}; GLfloat specular_property[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat emission_light[] = {0.5, 0.5, 0.5, 1.0}; GLfloat light1_diffuse_light[] = {1.0, 0.0, 0.0, 1.0}; GLfloat light2_diffuse_light[] = {0.0, 1.0, 0.0, 1.0}; GLint shininess = 5; GLint count; GLint numPoints; FILE *fp; float target[MAXLINE][3]; float maxX, maxY, maxZ, minX, minY, minZ, avgX, avgY, avgZ; //Mouse movement stuff static int left_click = GLUT_UP; static int right_click = GLUT_UP; static int xold; static int yold; static float rotate_x = 30; static float rotate_y = 15; static float alpha = 0; static float beta = 0; //Object and viewport manipulation GLfloat xRot, yRot, zRot; GLfloat xPos, yPos, zPos; GLfloat vpXPos, vpYPos, vpZPos; GLfloat scale, vpScale; /* Prompts to enter a filename, and reads it if it is found and displays an error message if not found */ void prompt_open_file() { int flag = 1; int file_name_length; char file_name[256] = "mymodel.dat"; while(flag) { printf("Enter file name : "); scanf("%s", file_name); if ((fp = fopen(file_name, "r")) == NULL) printf("\n%s file not found!\n", file_name); else flag = 0; } } /* Reads the data from the filename and calculates the model's minimum and maximum sizes */ void read_data() { char buffer[256]; int x, y, z; maxX = maxY = maxZ = -9999; minX = minY = minZ = 9999; avgX = avgY = avgZ = 0; numPoints = 0; while ((fgets(buffer, 256, fp)) != NULL) { if (buffer[0] == 'j' || buffer[0] == 'J') { target[count][0] = -9999; count++; } else { sscanf(buffer, "%d %d %d", &x, &y, &z); target[count][0] = x; target[count][1] = y; target[count][2] = z; avgX += x; avgY += y; avgZ += z; if (x > maxX) maxX = x; if (y > maxY) maxY = y; if (z > maxZ) maxZ = z; if (x < minX) minX = x; if (y < minY) minY = y; if (z < minZ) minZ = z; count++; numPoints++; } } avgX = avgX / numPoints; avgY = avgY / numPoints; avgZ = avgZ / numPoints; for (int i = 0; i < count; i++) { if (target[i][0] == -9999) { continue; } else { target[i][0] -= avgX; target[i][1] -= avgY; target[i][2] -= avgZ; } } } /* Reading the file */ void read_file() { prompt_open_file(); read_data(); } /* Creating the model */ void createModel (void) { int i, flag; flag = 1; //Perform required manipulation glColor3f(0.0f, 1.0f, 0.0f); glTranslatef(xPos, yPos, zPos); glScalef(scale, scale, scale); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glRotatef(zRot, 0.0f, 0.0f, 1.0f); glRotatef (beta, 1, 0, 0); glRotatef (alpha, 0, 1, 0); //Create the model based on the information read in glLineWidth(1.0); for (i = 0; i < count; i++) { if (target[i][0] == -9999) { flag = 0; } else { if (flag == 1) { glBegin(GL_POLYGON); glVertex3f(target[i - 1][0], target[i - 1][1], target[i - 1][2]); glVertex3f(target[i][0], target[i][1], target[i][2]); glEnd(); } else flag = 1; } } } /***TEST***/ void other_init() { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMatrixMode(GL_MODELVIEW); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse_light); //Directional Light glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, light1_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light); //Positional Light glLoadIdentity(); glLightfv(GL_LIGHT1, GL_POSITION, light2_position); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse_light); glLightfv(GL_LIGHT1, GL_AMBIENT, ambient_light); glLightfv(GL_LIGHT1, GL_SPECULAR, specular_light); glMaterialfv(GL_FRONT, GL_SPECULAR,specular_property); //glMaterialfv(GL_FRONT, GL_EMISSION, emission_light); glEnable(GL_LIGHTING); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glMateriali(GL_FRONT,GL_SHININESS,shininess); glEnable(GL_DEPTH_TEST); } void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); /* glTranslatef(-1.2, 1.2, 0.0); glutSolidTeapot(0.8); glTranslatef(2.0, -2.0, 0.0); glutSolidSphere (0.8, 20, 16); */ createModel(); //New glPopMatrix(); glFlush (); } void keyboard(unsigned char key, int x, int y) { switch (key) { //Light Switches case '0': glDisable(GL_LIGHT0); glDisable(GL_LIGHT1); break; case '1': glEnable(GL_LIGHT0); break; case '2': glEnable(GL_LIGHT1); break; //Ambient Light case 'A': if (ambient_light[0] < 1.0) { ambient_light[0] += 0.1; ambient_light[1] += 0.1; ambient_light[2] += 0.1; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light); } break; case 'a': if (ambient_light[0] >= 0.1) { ambient_light[0] -= 0.1; ambient_light[1] -= 0.1; ambient_light[2] -= 0.1; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light); } break; //Diffuse Light (Not working) case 'D': if (diffuse_light[0] < 1.0) { diffuse_light[0] += 0.1; diffuse_light[1] += 0.1; diffuse_light[2] += 0.1; } break; case 'd': if (diffuse_light[0] >= 0.1) { diffuse_light[0] -= 0.1; diffuse_light[1] -= 0.1; diffuse_light[2] -= 0.1; } break; //Specular component **INCOMPLETE** case 's': break; case 'S': break; //Emissive light of the object case 'e': break; case 'E': break; //Shiness of the object case 'h': if (shininess > 2) shininess -= 2; glMateriali(GL_FRONT,GL_SHININESS,shininess); break; case 'H': if (shininess < 126) shininess += 2; glMateriali(GL_FRONT,GL_SHININESS,shininess); break; case 'Q': case 'q': exit(1); } glutPostRedisplay(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(LR, -LR, LR * (GLfloat) h / (GLfloat) w, -LR * (GLfloat) h / (GLfloat) w, LR, -LR); else glOrtho(LR * (GLfloat) w / (GLfloat) h, -LR * (GLfloat) w / (GLfloat) h, LR, -LR, LR, -LR); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); width = w; height = h; } void init_window(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); width = height = 500; glutInitWindowSize(width, height); glutInitWindowPosition(50, 50); glutCreateWindow("Project 5"); } void main(int argc, char** argv) { read_file(); fclose(fp); //Defaulting some values xRot = yRot = zRot = 0.0f; xPos = yPos = zPos = 0.0f; vpXPos = width/4; vpYPos = height/4; scale = 1.0f; vpScale = 1.0f; init_window(argc,argv); other_init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutMainLoop(); }