#include #include #include #include #include #include #include "String.h" //for Ramsey String class. #include #include #ifdef __APPLE__ #include //apple include line #else #include //linux and windows include line #endif using namespace std; struct Line { float xs, ys, xe, ye; }; struct Point3D { float v[3]; }; struct Triangle { int vertexIndex[3]; }; struct MainGraphics { int winheight, winwidth; float xs,ys, rx, ry; // for a line perhaps float theta; // rotations or angles perhaps float thetaIncrement; bool lineStart; // do I have the first point xs,ys yet or not? vector lines; bool enableBlend; // enable or disable blend vector mv; //my vector list of integers from file vector vertices; vector triangles; }; 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(); void motionFunc(int x, int y); int main(int argc, char *argv[]) { glutInit(&argc, argv); init(); glutMainLoop(); } //the initialization function void init() { graph.lineStart = false; graph.enableBlend = true; // 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); glutMotionFunc(motionFunc); //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(); //anti aliasing glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); glLineWidth(1.5); } 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(Ramsey::String s, float x, float y) { glRasterPos2d(x,y); unsigned int i = 0; for (;i < s.str().length(); i++) { glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,s[i]); } } void DrawLine(int i) { glPushMatrix(); glTranslatef( (graph.lines[i].xs + graph.lines[i].xe)*0.5, (graph.lines[i].ys + graph.lines[i].ye)*0.5, 0); glRotatef(graph.theta, 0,0,1); glTranslatef( -(graph.lines[i].xs + graph.lines[i].xe)*0.5, -(graph.lines[i].ys + graph.lines[i].ye)*0.5, 0); glBegin(GL_LINES); glVertex2f(graph.lines[i].xs, graph.lines[i].ys); glVertex2f(graph.lines[i].xe, graph.lines[i].ye); glEnd(); glPopMatrix(); } void DrawRubberband() { glBegin(GL_LINES); glVertex2f(graph.xs, graph.ys); glVertex2f(graph.rx, graph.ry); 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); for(unsigned int i = 0; i < graph.lines.size(); i++) { // --draw the open gl line in orange glColor3f(1.0f, 0.8f, 0.5f); DrawLine(i); } if(graph.lineStart) { //draw a rubber band line glColor3f(0.6f, 0.6f, 1.0f); //light blue? DrawRubberband(); } glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glColor3f( 0.8f, 0.0f, 0.0f ); glBegin(GL_TRIANGLES); glVertex2d(0, 0); glVertex2d(100,0 ); glVertex2d(0, 100); glVertex2d(100,0 ); glVertex2d(0, 100); glVertex2d(150,150); glEnd(); glColor3f( 1.0f, 0.5f, 0.25f ); text( (Ramsey::String)"Number of Lines = " + graph.lines.size(), 10, 10 ); 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 || key == 27) { //q or escape 27 is escape on my keyboard exit(0); } else if(key == 'f') { ifstream fin("testread"); if(!fin) { printf("File unable to be read! booo Try again some other time."); } string s1, s2; fin >> s1 >> s2; cout << "Read in " << s1 << " " << s2 << endl; // printf(" Read in %s %s\n", s1.c_str(), s2.c_str()); graph.mv.resize(0); int n; fin >> n; for( int i = 0; i < n ; i++) { // fin >> graph.nv[i]; int f; fin >> f; graph.mv.push_back(f); } fin.close(); //done with reading in this file } else if(key == 'g') { cout << "You have " << graph.mv.size() << " values." << endl; cout << "Your values are: " ; for(unsigned int i = 0; i < graph.mv.size() ; i++) { cout << graph.mv[i] << " "; } cout << endl; } else if (key == 'b') {//enable or disable blend graph.enableBlend = ! graph.enableBlend; if(graph.enableBlend) { glEnable(GL_BLEND); } else { glDisable(GL_BLEND); } glutPostRedisplay(); } else if(key == 's') { if( graph.thetaIncrement > 0) { graph.thetaIncrement = 0; graph.theta = 0; } else { graph.thetaIncrement = 0.01; } } else if(key =='p') { printf("(%d,%d)\n", x, y); } else if(key == 127) { // backspace on my keyboard if(graph.lines.size() > 0) { graph.lines.resize( graph.lines.size() - 1 ); glutPostRedisplay(); } } else { printf("keypress = %d at (%d,%d)\n", key, x, y); } } void pushBackNewLine(int x, int y){ Line newline; newline.xs = graph.xs; newline.xe = x; newline.ys = graph.ys; newline.ye = y; graph.lines.push_back(newline); } //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_DOWN && button == GLUT_LEFT_BUTTON) { graph.xs = graph.rx = x; graph.ys = graph.ry = y; graph.lineStart = true; //end a line } else if(state==GLUT_UP && button == GLUT_LEFT_BUTTON) { pushBackNewLine(x,y); graph.lineStart = false; } //if the mouse is clicked - redraw glutPostRedisplay(); } void motionFunc(int x, int y) { if(graph.lineStart) { y = graph.winheight - y; graph.rx = x; graph.ry = y; 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() { graph.theta += graph.thetaIncrement; glutPostRedisplay(); }