Sunday, June 1, 2014

Opengl,C++ : Shrinked Polygon

The following program Specify a point for the Polygon by the left mouse button and shrinked polygon (say, by half) with respect to the origin, when you press the key "Z".
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
 Point2D p1,p2,p3,p4;  
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
 }  
 Point2D translate(Point2D p){  
     p.x =p.x/2; //.....wite the equations for translation   
     p.y = p.y/2; //.....wite the equations for translation  
     return p;  
 }  
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
            glClear(GL_COLOR_BUFFER_BIT);  
                           p1.x = 0;  
                           p1.y =0;  
                           switch(first)  
                               {  
                               case 0:  
                                    p2.x = x;  
                                    p2.y = screenheight - y;  
                                    first = 1;  
                                    break;       
                               case 1:  
                                    p3.x = x;  
                                    p3.y = screenheight - y;  
                                    first = 2;  
                                    break;  
                               case 2:  
                                    p4.x = x;  
                                    p4.y = screenheight - y;  
                                    DrawPolygonSegment(p1,p2,p3,p4);  
                                    first = 0;  
                                    break;       
                               }  
        }  
     }  
 }  
 void Shrinked_Polygon()  
 {  
      Point2D translate_p1 = translate(p1);  
   Point2D translate_p2 = translate(p2);  
      Point2D translate_p3 = translate(p3);  
      Point2D translate_p4 = translate(p4);  
   DrawPolygonSegment(translate_p1,translate_p2,translate_p3,translate_p4);  
 }  
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='Z') glutIdleFunc(Shrinked_Polygon);  
 }  
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
        glutDisplayFunc( myDisplay );  
     glutMouseFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

No comments:

Post a Comment