Friday, July 18, 2014

Opengl,C++ : Line_clipping scratch code using midpoint subdivision method

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
   
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
   
 int x0 = 0,y0 =0 ,x1 =0 ,y1=0 ;  
 int xmin;int ymin;int xmax;int ymax;  
   
 void DrawRect(int x0, int y0, int x1, int y1)  
 {  
      glClear(GL_COLOR_BUFFER_BIT);  
      glRecti(x0,y0,x1,y1);  
      glFlush();  
 }  
   
 void DrawLineSegment(int x0, int y0, int x1, int y1){  
      glColor3d(1,0,0);  
      glBegin(GL_LINES);  
           glVertex2i(x0,y0);  
           glVertex2i(x1,y1);  
      glEnd();  
      glFlush();  
 }  
   
 typedef int OutCode;  
   
 const int INSIDE = 0; // 0000  
 const int BOTTOM = 1;  // 0001  
 const int RIGHT = 2; // 0010  
 const int TOP = 4; // 0100  
 const int LEFT = 8;  // 1000  
   
 OutCode ComputeOutCode(int x, int y)  
 {  
      OutCode code;  
   
      code = INSIDE;     // initialised as being inside of clip window  
   
      if (x < xmin)      // to the left of clip window  
           code |= LEFT;  
      else if (x > xmax)      // to the left of clip window  
           code |= RIGHT;  
      else if (y < ymin)      // to the left of clip window  
           code |= BOTTOM;// Complete the code segment here  
      else if (y > ymax)      // to the left of clip window  
           code |= TOP;  
 return(code);  
 }  
   
 void lineclip(int x0,int y0,int x1,int y1) {  
  OutCode code0,code1;  
  int midx,midy;  
   
  if(abs(x0-x1)==1 && abs(y0-y1)==1) /*Adjacent points */  
   return;  
    
  code0=ComputeOutCode(x0,y0);  
  code1=ComputeOutCode(x1,y1);  
  if( !(code0 | code1) ) { /*The two points are completely inside the window*/  
   DrawLineSegment(x0,y0,x1,y1);  
   return;  
  }  
  else if(code0 & code1) /*The two points are completely outside the window.*/  
           return ;  
   
  midx=(x0+x1)/2;  
  midy=(y0+y1)/2;  
  lineclip(midx,midy,x1,y1);  
  lineclip(x0,y0,midx,midy);  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
      glPointSize(5.0);  
      if(state == GLUT_DOWN)   
      {  
           if(button == GLUT_LEFT_BUTTON)   
           {  
                if (flag){  
                     x0 = x;  
                     y0 = screenheight - y;  
                     flag = false;  
                } else {  
                     x1 = x;  
                     y1 = screenheight - y;  
                     lineclip(x0,y0,x1,y1);  
                     flag = true;  
                }  
           }  
           else if (button == GLUT_RIGHT_BUTTON)   
           {  
                if (flag){  
                     x0 = x;  
                     y0 = screenheight - y;  
                     flag = false;  
                } else {  
                     x1 = x;  
                     y1 = screenheight - y;  
                     glColor3d(1,1,0);  
                     DrawRect(x0,y0,x1,y1);  
                     xmin=x0;  
                     xmax=x1;  
                     ymin=y0;                      
                     ymax=y1;  
                     flag = true;  
                }  
           }  
      }  
 }  
   
   
 void myDisplay()  
 {  
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //black  
      glClear(GL_COLOR_BUFFER_BIT);  
      glFlush();  
 }  
   
 int main( int argc, char ** argv ) {  
   
      glutInit( &argc, argv );  
      glutInitWindowPosition( 0, 0 );  
      glutInitWindowSize( 800, 600 );  
   
      // create window  
      glutCreateWindow( "midpoint subdivision method Clipping" );  
   
      // set the view frustum  
      glMatrixMode( GL_PROJECTION );   
      glLoadIdentity();  
      gluOrtho2D( 0, 800, 0, 600 );  
   
      // clear rendering surface  
      glViewport(0, 0, 800, 600);  
   
      glutMouseFunc( myMouse );  
      glutDisplayFunc( myDisplay );  
      glutMainLoop();  
   
      return( 0 );  
 }  

No comments:

Post a Comment