/* Compiling C++/OpenGL programs in OS X 1. Open up a text file called "gcomp" and cut and paste the following code into it. g++ -o $1 $1.cpp -L/Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries -L/usr/local/lib -lGL -lGLU -framework GLUT 2. At the terminal prompt type: chmod u+x gcomp This makes the file gcomp into an executable. 3. If your program is called myProg.cpp, just type "gcomp myProg". and run the program by typing "myProg". */ #include #include #include #include #include using namespace std; void initWindow(int xres, int yres) { } void setPixel(int x, int y, float color[]) { glRasterPos2i(x,y); glDrawPixels(1,1,GL_RGB,GL_FLOAT,color); } void line(int x1, int y1,int x2,int y2,float color[]) { // Implement Bresenham's algorithm here // modified so that it works for all slopes // and both positive and negative dx and dy. // When a pixel (x,y) is found on the line // call setPixel(x,y,color) // Test code that you will remove: for (int i=x1;i>x1) != 0) { fin >> y1 >> x2 >> y2; line(x1,y1,x2,y2,color); glFlush(); } glEndList(); } void initGraphics(int xres,int yres,char *argv) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); // This puts origin at center of the window gluOrtho2D((double) -xres/2,(double) xres/2,(double) -yres/2,(double) yres/2); glTranslatef(0.375, 0.375, 0.0); glMatrixMode(GL_MODELVIEW); makeList(argv); } void display(void) { glClearColor(1.0,1.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT); // Clear window glCallList(1); glutSwapBuffers(); glCallList(1); } void button_event(int button, int state, int x, int y) { } void key_event(unsigned char key, int x, int y) { switch (key) { case 'q' : exit(0); } glutPostRedisplay(); } void mouse_motion(int x, int y) { } int main(int argc, char *argv[]) { // Initialize glut parameters glutInit(&argc, argv); /* initialize glut */ glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); // enter xres, yres, xpos, ypos of window and // open a window int xres= 801, yres = 801, xpos = 0, ypos = 0; glutInitWindowSize(xres,yres); glutInitWindowPosition(xpos,ypos); glutCreateWindow(argv[0]); // Your function to set initial graphics to be displayed in the window initGraphics(xres,yres,argv[1]); // argv[1] is the name of the input file // Your function Display is called whenever glutPostRedisplay() is invoked; glutDisplayFunc(display); // Your function that is called when interrupt is generated glutMouseFunc(button_event); glutKeyboardFunc(key_event); glutMotionFunc(mouse_motion); // Call your Display function and turn over control to window manager display(); glutMainLoop(); }