/***************************************************************************** Some simple SGI GL-based routines for CS480 ****************************************************************************** Author: Stan Sclaroff Boston University Computer Science Dept. January 27, 1995 ****************************************************************************** Modifications: Mar. 20, 1995, Modified for programming assignment 3 Added: support for stereo drawing of polygon object Removed: some line object routines Chanded: now RGB colors rather than color-map Feb. 15, 1995, Modified for programming assignment 2 Added: support for Lines and rectangular frames Removed: pixmap stuff Changed: line drawing to use GL line drawing routines ******************************************************************************/ #include #include #include #include #include "draw.h" /* This routine opens a window. Returns window id. If window open fails, then it returns window id = 0 */ long open_window(width,height,name) long width,height; char *name; { long winid; /* put the graphical process in the foreground so that we can interact with it */ foreground(); /* set the prefered size of the window */ prefsize(width,height); /* open the window, return 0 if failure */ winid = winopen(name); if (winid == 0) { fprintf(stderr,"Cannot open window\n"); return(0); } /* configure the window with double buffer */ doublebuffer(); RGBmode(); gconfig(); /* clear both of the double buffers */ clear(); swapbuffers(); clear(); /* specify clipping region */ ortho2(0,width,0,height); /* select devices to queue */ qdevice(REDRAW); return(winid); } /* close a window */ void close_window(winid) long winid; /* the window id */ { winclose(winid); } /* draw one line to a GL window */ draw_line(winid,p0,p1,rgb_value) long winid; /* the window id */ float p0[2],p1[2]; /* the start and end points of the line */ short *rgb_value; /* the desired line color */ { winset(winid); /* set the current window */ c3s(rgb_value); /* set the appropriate color */ bgnline(); /* GL command to begin a line */ v2f(p0); /* first vertex */ v2f(p1); /* second vertex */ endline(); /* end the line */ } /* create a rectangular frame with the desired color */ Frame *create_frame(xmin,ymin,xmax,ymax,RGBcolor) float xmin,ymin,xmax,ymax; short *RGBcolor; { Frame *frame; int i; /* allocate the structure */ frame = (Frame *)malloc(sizeof(Frame)); /* fill in the structure */ frame->xmax = xmax; frame->ymax = ymax; frame->xmin = xmin; frame->ymin = ymin; frame->aspect_ratio = (xmax - xmin)/(ymax - ymin); for(i=0;i<3;++i) frame->color[i] = RGBcolor[i]; return(frame); } /* free a frame structure */ void free_frame(frame) Frame *frame; { if(frame!=0) free(frame); } /* draw a frame to the window in the appropriate color */ draw_frame(winid,frame) long winid; /* the window id */ Frame *frame; /* frame data structure */ { int i; float p[2]; winset(winid); /* set the window */ c3s(frame->color); /* set appropriate color */ bgnline(); /* begin the Line */ /* first point */ p[0] = frame->xmin; p[1] = frame->ymin; v2f(p); /* second, etc... */ p[1] = frame->ymax; v2f(p); p[0] = frame->xmax; v2f(p); p[1] = frame->ymin; v2f(p); p[0] = frame->xmin; v2f(p); endline(); /* end the line */ } /* routine to transform, clip and then draw the polygon object */ redraw_stereo(winid,poly,M1,M2,frame,zmin,zmax,stereo,left_color,right_color) long winid; Polygon *poly; Frame *frame; float *M1,*M2; float zmin,zmax; int stereo; short *left_color,*right_color; { /* set the current window to be the draw window */ winset(winid); reshapeviewport(); /* clear the window */ c3s(RGBblack); clear(); if(stereo) { RGBwritemask(RGBred[0],RGBred[1],RGBred[2]); perspective_project_polygons(winid,M1,poly,frame->xmin,frame->xmax,frame->ymin,frame->ymax,zmin,zmax,left_color); RGBwritemask(RGBblue[0],RGBblue[1],RGBblue[2]); perspective_project_polygons(winid,M2,poly,frame->xmin,frame->xmax,frame->ymin,frame->ymax,zmin,zmax,right_color); RGBwritemask(RGBwhite[0],RGBwhite[1],RGBwhite[2]); } else perspective_project_polygons(winid,M1,poly,frame->xmin,frame->xmax,frame->ymin,frame->ymax,zmin,zmax,poly->color); /* draw the frame */ draw_frame(winid,frame); /* we're drawing in double-buffer mode, swap buffers */ swapbuffers(); }