// GL_Window.cpp: implementation of the CGL_Window class. // ////////////////////////////////////////////////////////////////////// #include "gl_window.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CGL_Window::CGL_Window(int x, int y, int w, int h, const char *l) : Fl_Gl_Window(x,y,w,h,l) { //set up the rotation values rtx = 0; rty = 0; rtz = 0; zm =-1; obj.num_elems =0; } CGL_Window::~CGL_Window() { } /******************************************/ /* Do your Gl work here */ /******************************************/ void CGL_Window::drawPicture(void) { //currently draws 3 axes just for demonstration glLineWidth(2.0); // rotate object amount on sliders // zoom glTranslatef(0,0,zm); // rotate glRotated(rtx,1,0,0); glRotated(rty,0,1,0); glRotated(rtz,0,0,1); // draw axis glBegin(GL_LINES); glColor3f(10.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(10.0, 0.0, 0.0); glColor3f(0.0, 10.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 10.0, 0.0); glColor3f(0.0, 0.0, 10.0); glVertex3f (0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 10.0); glEnd(); // draw objects draw_obj(); } void CGL_Window::draw() { int width, height; glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); width=w(); height=h(); glViewport(0, 0, width, height); gluPerspective(60, (float)width/height, .0001, 360.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // clear stuff on the canvas glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); drawPicture(); glPopMatrix(); } int CGL_Window::handle(int event) { static int num,x,y; switch(event){ case FL_PUSH: num=Fl::event_button(); x=Fl::event_x(); y=Fl::event_y(); //printf("pushed %d x %d y %d\n",num,x,y); return 1; case FL_DRAG: //... mouse moved while down event ... return 1; case FL_RELEASE: //... mouse up event ... return 1; case FL_FOCUS : return 1; case FL_UNFOCUS : //... Return 1 if you want keyboard events, 0 otherwise return 1; case FL_KEYBOARD: //... keypress, key is in Fl::event_key(), ascii in Fl::event_text() //... Return 1 if you understand/use the keyboard event, 0 otherwise... return 1; case FL_SHORTCUT: //... shortcut, key is in Fl::event_key(), ascii in Fl::event_text() //... Return 1 if you understand/use the shortcut event, 0 otherwise... return 1; default: // tell FLTK that I don't understand other events return 0; } } void CGL_Window::load(char *file) { cout << "Loading file\n"; read_coords("enterprise.coor"); read_poly("enterprise.poly"); } /*********************************************************************/ /* Read in the list of points defined in the file specified by */ /* filename. The result is put in the_coords. */ /*********************************************************************/ void CGL_Window::read_coords(char *filename) { int i, j; float x, y, z; FILE *f; obj.min_pt.x = obj.min_pt.y = obj.min_pt.z = MAXFLOAT; obj.max_pt.x = obj. max_pt.y = obj.max_pt.z = MINFLOAT; f = fopen(filename, "r"); if (!f) { cout << "Could not open file " << filename << endl; exit(-1); } if (fscanf(f, "%d", &( obj.num_coords )) != 1) { cout << "Could not read first line of " << filename << endl; exit(-1); } obj.the_coords = (coord *)malloc((obj. num_coords + 1) * sizeof(coord)); for( i = 0 ; i < obj.num_coords ; i++) { if (fscanf(f, "%d,%f,%f,%f", &j, &x, &y, &z) != 4) { cout << "Could not read from \n"; exit(-1); } if ((j < 0) || (j > obj.num_coords)) { cout << "Illegal index \n"; exit(-1); } if (x > obj.max_pt.x) obj.max_pt.x = x; if (x < obj.min_pt.x) obj.min_pt.x = x; if (y > obj.max_pt.y) obj.max_pt.y = y; if (y < obj.min_pt.y) obj.min_pt.y = y; if (z > obj.max_pt.z) obj.max_pt.z = z; if (z < obj.min_pt.z) obj.min_pt.z = z; obj.the_coords[j].x = x; obj.the_coords[j].y = y; obj.the_coords[j].z = z; } fclose(f); obj.center_pt.x =( obj.max_pt.x + obj.min_pt.x ) / 2; obj.center_pt.y =( obj.max_pt.y + obj.min_pt.y ) / 2; obj.center_pt.z =( obj.max_pt.z + obj.min_pt.z ) / 2; } /* end function read_coords */ /*********************************************************************/ /* Read in the list of polygons defined in the file specified by */ /* filename. The result is put in the_elems. */ /*********************************************************************/ void CGL_Window::read_poly(char *filename) { int i, pt, res, index; char c; FILE *f; f = fopen(filename, "r"); if (!f) { cout << "Could not open file " << filename << endl; exit(-1); } if (fscanf(f, "%d", &( obj.num_elems )) != 1) { cout << "Could not read first line of " << filename << endl; exit(-1); } obj.the_elems = (elem *) malloc ((obj.num_elems) * sizeof(elem)); for( i = 0 ; i < obj.num_elems ; i++ ) { do c = fgetc(f); while( !feof(f) && (c != ' ') ); index = 0; do { res = fscanf( f, "%d", &pt ); if (res) { obj.the_elems[i].points[index] = pt; index++; } if (index >= MAX_PTS) { cout << "Too many indices\n"; cout << "polygon "<< i << endl; exit(-1); } } while(!feof(f) && res); obj.the_elems[i].num_points = index; } fclose(f); } /* end function read_elems */ /*********************************************************************\ * Call to display an_object \*********************************************************************/ void CGL_Window::draw_obj(void) { int index; glPushMatrix(); glScalef( .25, .25, .25 ); glLineWidth(1); glColor3f( 1.0, 1.0, 1.0 ); for( index = 0; index < obj.num_elems; index++ ) { draw_wire_polygon(obj.the_elems[index]); } glPopMatrix(); } /*********************************************************************\ * Call to display a wireframe polygon, called in loop by draw_object \*********************************************************************/ void CGL_Window::draw_wire_polygon(elem curr_polygon ) { int index; glBegin( GL_LINE_LOOP ); for( index = 0; index < ( curr_polygon.num_points - 1 ); index++ ) { glVertex3f( obj.the_coords[curr_polygon.points[index]].x, obj.the_coords[curr_polygon.points[index]].y, obj.the_coords[curr_polygon.points[index]].z ); glVertex3f( obj.the_coords[curr_polygon.points[index+1]].x, obj.the_coords[curr_polygon.points[index+1]].y, obj.the_coords[curr_polygon.points[index+1]].z ); } glEnd( ); }