#include //need it for rand() function #include using namespace std; #include "GL/glut.h" #define random() ((float)rand())/RAND_MAX //Generates a random number in range [0,1] //Triangle structure struct TRIANGLE { float color[3]; // RGB colors of the triangle float vertex[3][2]; // Vertices of the triangle } Triangle; // Mock trianglution vertices float triVer[10][2]={ {2,3}, {11,2}, {6,6}, {15,5}, {4,10}, {11,9}, {7,13}, {15,11}, {14,16},{17,14} }; // Coordinates of the polygon we want to triangulate float poly[10][2]={ {2,3}, {6,6}, {4,10}, {7,13}, {14,16}, {17,14}, {15,11}, {11,9}, {15,5}, {11,2}}; int nTriangles=0; // Variable that specifies the number of triangles to be drawn on the screen(for animation) TRIANGLE tri[8]; // Array that holds the triagles after mock triangulation //initialize the OpenGL state void init(){ } //Resize the window (You may use this function as it is) void reshape(int width, int height){ GLfloat nRange = 20.0; //Increase or decrease this value to zoom in/out int w = width; // check new client width of the main window int h = height; // check new client height of the main window if(h == 0) // prevent a division by zero, by making sure that h = 1; // windows height is at least 1 glViewport(0, 0, w, h); // reset viewport glLoadIdentity(); // restore matrix to original state glTranslatef(-8.0f,-7.0f,0.0f); // Move the screen to the left(X) 8.0 Units and downwards(Y) 7.0 units glMatrixMode(GL_PROJECTION); // add perspective to scene glLoadIdentity(); // restore matrix to original state if (w <= h){ glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); } else{ glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); } glMatrixMode(GL_MODELVIEW); } //Mock triangulation function void triangulate(){ for(int i=0;i<8;i++){ //Assign a random color to current triangle tri[i].color[0]=random(); tri[i].color[1]=0.8; tri[i].color[2]=1; //Set vertex coordinates for current triangle tri[i].vertex[0][0]=triVer[i][0]; tri[i].vertex[0][1]=triVer[i][1]; tri[i].vertex[1][0]=triVer[i+1][0]; tri[i].vertex[1][1]=triVer[i+1][1]; tri[i].vertex[2][0]=triVer[i+2][0]; tri[i].vertex[2][1]=triVer[i+2][1]; } } //Draws a triangle for a given TRIANGLE structure void drawTriangle(TRIANGLE tri){ glBegin(GL_TRIANGLES); glColor3fv(tri.color); //Set the color of the triangle glVertex2fv(tri.vertex[0]); glVertex2fv(tri.vertex[1]); glVertex2fv(tri.vertex[2]); //Put the vertices on the frame glEnd(); glLineWidth(1.0); //Draw a border for the triangle glBegin(GL_LINE_LOOP); glColor3f(1.0,0.0,0.0); //Set the color to RED glVertex2fv(tri.vertex[0]); glColor3f(0.0,1.0,0.0); //Set the color to GREEN glVertex2fv(tri.vertex[1]); glColor3f(0.0,0.0,1.0); //Set the color to BLUE glVertex2fv(tri.vertex[2]); glEnd(); } //Draws a polygon for a given 2D vector void drawPolygon(){ glLineWidth(3); glBegin(GL_LINE_LOOP); glColor3f(0.7,0.7,0.7); for(int i=0;i<10;i++){ glVertex2fv(poly[i]); } glEnd(); } //Drawing function for GLUT void display(){ glClear(GL_COLOR_BUFFER_BIT); drawPolygon(); // This function uses global array "poly" to draw a polygon for(int i=0;i=8){ return; } nTriangles++; glutPostRedisplay(); //Redraw the frame glutTimerFunc(1000,timer,0); //Reset the timer to be triggered after 1000 msecs } // Keyboard events are handled here void keyboard(unsigned char key,int x,int y){ switch(key){ case 'q':case'Q': //Quit application exit(0); break; case 'f':case'F': //Go Forward (Draws the next state of triangulation) if(nTriangles<8){ nTriangles++; } glutPostRedisplay(); break; case 'b':case'B': //Go back (Draws the previous state of triangulation) if(nTriangles>0){ nTriangles--; } glutPostRedisplay(); break; case 'a':case'A': //Start animation glutTimerFunc(1000,timer,0); break; } } void main(int argc,char** argv){ int mode=GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode(mode); //Initialize glut display mode with rgb and double buffer mode glutInitWindowSize(600,600); // Initialize window size glutCreateWindow("Triangulation Example"); // Create window with specified title init(); //initialize the program triangulate(); // mock triangulation function that triangulates global array "poly" glutReshapeFunc(reshape); // Register Rehape function glutKeyboardFunc(keyboard); // Register Keyboard function glutDisplayFunc(display); // Register Display function glutTimerFunc(0,timer,0); // REgister Timer function glutMainLoop(); //Program gets into an infinite loop until it quits }