#include #include #include #include using namespace std; // A vertex has 3D coordinates vertex.x, vertex.y, and vertex.z, // normal vertex.nx, vertex.ny, and vertex.nz, and color // vertex.R, vertex.G, vertex.B typedef struct { float x, y, z; } vertex; // A poly is made up of a set of vertices typedef struct{ int n; // Number of vertices vertex* v; // Array of n vertices } poly; // You will need to build the polys array within your code. int numpolys=0; poly* polys; int SubdivisionStep = 0; // Has the total number of subdivisions performed so far int lastx,lasty; int xchange,ychange; float spin=0.0; float spinup = 0.0; void YourCode(void); void YourSubdivisionCode(void); void set(vertex& V, const float x, const float y, const float z) { V.x = x; V.y = y; V.z = z; } void display(void) { int i,j; vertex v1; vertex v2; vertex norm; double temp; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); float max = 0.0; for(i=0;i max) max = fabs(polys[i].v[j].x); if (fabs(polys[i].v[j].y) > max) max = fabs(polys[i].v[j].y); if (fabs(polys[i].v[j].z) > max) max = fabs(polys[i].v[j].z); } } glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.75*max, 1.75*max, -1.75*max, 1.75*max, -0.2, 4.0*max); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 2.0*max, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glRotatef(spinup, 1.0, 0.0, 0.0); glRotatef(spin, 0.0, 1.0, 0.0); glEnable(GL_LIGHTING); glLightf(GL_LIGHT0, GL_POSITION, (2.0, 2.0, 1.0)); glLightf(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)); glLightf(GL_LIGHT0, GL_AMBIENT, (0.5, 0.5, 0.5, 1.0)); glLightf(GL_LIGHT1, GL_POSITION, (-2.0, 2.0, 2.0)); glLightf(GL_LIGHT1, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)); glLightf(GL_LIGHT2, GL_POSITION, (2.0, -2.0, 2.0)); glLightf(GL_LIGHT2, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)); glLightf(GL_LIGHT3, GL_POSITION, (-2.0, -2.0, 2.0)); glLightf(GL_LIGHT3, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0)); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glEnable(GL_LIGHT3); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); // Draw polys in grey glColor3f(0.75, 0.75, 0.75); for(i=0;i2) { v1.x = polys[i].v[1].x-polys[i].v[0].x; v1.y = polys[i].v[1].y-polys[i].v[0].y; v1.z = polys[i].v[1].z-polys[i].v[0].z; v2.x = polys[i].v[2].x-polys[i].v[1].x; v2.y = polys[i].v[2].y-polys[i].v[1].y; v2.z = polys[i].v[2].z-polys[i].v[1].z; norm.x = v1.y * v2.z - v1.z * v2.y; norm.y = v1.z * v2.x - v1.x * v2.z; norm.z = v1.x * v2.y - v1.y * v2.x; temp = sqrt(norm.x*norm.x+norm.y*norm.y+norm.z*norm.z); if (temp != 0.0) { norm.x /= temp; norm.y /= temp; norm.z /= temp; } } else { norm.x = 0.0, norm.y=0.0; norm.z=0.0; } for(j=0;j= 360.0) spin -= 360.0; if (spin < 0.0) spin += 360.0; spinup -= ychange/80.0; if (spinup > 89.0) spinup = 89.0; if (spinup < -89.0) spinup = -89.0; glutPostRedisplay(); } void mouse(int button, int state, int x, int y) { switch(button) { case GLUT_LEFT_BUTTON: if (state==GLUT_DOWN) { lastx = x; lasty = y; xchange = 0; ychange = 0; glutIdleFunc(rotateview); } else if (state==GLUT_UP) { xchange = 0; ychange = 0; glutIdleFunc(NULL); } break; case GLUT_RIGHT_BUTTON: if (state==GLUT_DOWN) { SubdivisionStep++; YourSubdivisionCode(); glutPostRedisplay(); } default: break; } } void motion(int x, int y) { xchange = x-lastx; ychange = y-lasty; } void YourSubdivisionCode(void) { // Your Code to do one step of subdivision here // You should update the polygons to be drawn. // See the sample code below in the YourCode() routine. } void YourCode(void) { // Your Code to read in the file and form the // Winged-edge data structure here. // Once you have created the winged-edge structure, you // need to create the polygons to be drawn. // Some sample code is given. /* polys = new poly[3]; numpolys=3; polys[0].n = 3; polys[1].n = 3; polys[2].n = 3; polys[0].v = new vertex[3]; polys[1].v = new vertex[3]; polys[2].v = new vertex[3]; set(polys[0].v[0], 0.0,0.0,0.0); set(polys[0].v[1], 0.5,0.0,0.0); set(polys[0].v[2], 0.25,0.0,0.5); set(polys[1].v[0], 0.0,0.0,0.0); set(polys[1].v[1], -0.5,0.0,0.0); set(polys[1].v[2], -0.25,0.0,0.5); set(polys[2].v[0], 0.0,0.0,0.0); set(polys[2].v[1], 0.5,-0.5,0.0); set(polys[2].v[2], -0.5,-0.5,0.0); */ polys = new poly[6]; numpolys = 6; for (int i=0;i<6;i++) { polys[i].n = 4; polys[i].v = new vertex[4]; } set(polys[0].v[0], 1.0,1.0,1.0); set(polys[0].v[1], -1.0,1.0,1.0); set(polys[0].v[2], -1.0,-1.0,1.0); set(polys[0].v[3], 1.0,-1.0,1.0); set(polys[1].v[0], 1.0,1.0,1.0); set(polys[1].v[1], 1.0,1.0,-1.0); set(polys[1].v[2], -1.0,1.0,-1.0); set(polys[1].v[3], -1.0,1.0,1.0); set(polys[2].v[0], -1.0,1.0,1.0); set(polys[2].v[1], -1.0,1.0,-1.0); set(polys[2].v[2], -1.0,-1.0,-1.0); set(polys[2].v[3], -1.0,-1.0,1.0); set(polys[3].v[0], 1.0,-1.0,-1.0); set(polys[3].v[1], 1.0,-1.0,1.0); set(polys[3].v[2], -1.0,-1.0,1.0); set(polys[3].v[3], -1.0,-1.0,-1.0); set(polys[4].v[0], 1.0,-1.0,-1.0); set(polys[4].v[1], 1.0,1.0,-1.0); set(polys[4].v[2], 1.0,1.0,1.0); set(polys[4].v[3], 1.0,-1.0,1.0); set(polys[5].v[0], 1.0,-1.0,-1.0); set(polys[5].v[1], -1.0,-1.0,-1.0); set(polys[5].v[2], -1.0,1.0,-1.0); set(polys[5].v[3], 1.0,1.0,-1.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("CPSC 645 HW4 - "); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(NULL); YourCode(); glutMainLoop(); return 0; }