#include #include #include #include #include #ifdef __APPLE__ #include //apple include line #else #include //linux and windows include line #endif using namespace std; struct MainGraphics { int winheight, winwidth; float xs,ys, xe,ye; // for a line perhaps float theta; // rotations or angles perhaps bool lineStart; // do I have the first point xs,ys yet or not? }; MainGraphics graph; void mydisplay(); void init(); void keyboard(unsigned char key, int x, int y); void mouse(int button, int state, int x, int y); void reshape(int,int); void idle(); int main(int argc, char *argv[]) { glutInit(&argc, argv); init(); glutMainLoop(); } //the initialization function void init() { graph.lineStart = false; // glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); graph.winwidth = graph.winheight = 500; glutInitWindowSize(graph.winwidth, graph.winheight); glutInitWindowPosition(0,0); glutCreateWindow("Ramsey"); glutDisplayFunc(mydisplay); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutIdleFunc(idle); glutReshapeFunc(reshape); //clear to black and fill to "greenish" glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(0.1, 1.0, 0.1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //to setup standard orthogonal view with clipping box as cube of side 2 //with center at the origin. This is the default view (uncomment below) // gluOrtho2D(-2.0,2.0,-2.0,2.0); //the following sets up the screen as winwidth x winheight pixels gluOrtho2D(0, graph.winwidth, 0, graph.winheight); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void reshape(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); // if(w <= h) // gluOrtho2D(-2.0,2.0,-2.0*h/w, 2.0f* h/w); // else // gluOrtho2D(-2.0 * w / h, 2.0 * w / h, -2.0, 2.0); gluOrtho2D(0,w,0,h); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,w,h); graph.winheight = h; graph.winwidth = w; } //text? void text(string s, float x, float y) { glRasterPos2d(x,y); unsigned int i = 0; for (;i < s.length(); i++) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,s[i]); } } //this function will draw our line using the dda algorithm void DDADrawLine() { float dx = graph.xe - graph.xs; float dy = graph.ye - graph.ys; if ( fabs(dx) >= fabs(dy) ) { /// slope between -1 and 1 if(graph.xe < graph.xs) { swap(graph.xe, graph.xs); swap(graph.ye, graph.ys); } //at this point, xs < xe and |dx| >= |dy| float m = dy / dx; float xCurrent = graph.xs, yCurrent = graph.ys; glBegin(GL_POINTS); while(xCurrent < graph.xe) { glVertex2d( (int) xCurrent, (int)(yCurrent+0.5) ); xCurrent++; yCurrent += m; } glEnd(); } } void DrawLine() { glBegin(GL_LINES); glVertex2f(graph.xs, graph.ys); glVertex2f(graph.xe, graph.ye); glEnd(); } //the display function //whatever is in here is what will be displayed #define NUM_PTS 50.0 void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); if(! graph.lineStart) { /* --draw the open gl line in orange glColor3f(1.0f, 0.5f, 0.2f); DrawLine(); */ glColor3f(0.6f, 0.0f, 0.6f); //some kind of purple/pink - blah meh grr DDADrawLine(); } else { //draw a rubber band line glColor3f(0.6f, 0.6f, 1.0f); //light blue? DrawLine(); } glutSwapBuffers();//double buffer only } //the keyboard function void keyboard(unsigned char key, int x, int y) { y = graph.winheight - y; if(key == 'q' || key == 'Q' || key == 3) //q or escape exit(0); if(key =='p') printf("(%d,%d)\n", x, y); } //the mouse does stuff void mouse(int button, int state, int x, int y) { y = graph.winheight - y; if(state==GLUT_DOWN && button == GLUT_RIGHT_BUTTON) { exit(0); //close the program } else if(state == GLUT_UP && button == GLUT_LEFT_BUTTON) { graph.xs = graph.xe = x; graph.ys = graph.ye = y; graph.lineStart = true; //end a line } else if(state==GLUT_DOWN && button == GLUT_LEFT_BUTTON) { graph.xe = x; graph.ye = y; graph.lineStart = false; } //if the mouse is clicked - redraw glutPostRedisplay(); } //idle is what to do when you're not rendering //display only happens once until glutpostredisplay is called //between redisplays - idle happens void idle() { }