Sign in to follow this  
wabbz111

mouse polygon drawing

Recommended Posts

hi i have a slight problem with this code.I is suppossed to draw a polygon with a mouse
#include <GL/glut.h>

GLdouble winHeight;


GLsizei winWidth =400, winheight=300;
GLint endPTCtr=0;

class scrPt{
      public:
      GLint x,y;
      };
      void init(void)
      {
           glClearColor (0.0, 0.0, 1.0,1.0);
           glMatrixMode(GL_PROJECTION);
           gluOrtho2D (0.0, 200.0, 0.0,150.0);
           }
           
           void displayFcn(void)
           {
                glClear(GL_COLOR_BUFFER_BIT);
                }
                
      void winReshapeFcn (GLint newWidth, GLint newHeight)
      {
           /*reset viewport parameters*/
           glViewport (0, 0, newWidth,newHeight);
           glMatrixMode(GL_PROJECTION);
           glLoadIdentity();
           gluOrtho2D (0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));
           
           /* reset dspaly window*/
           winWidth =  newWidth;
            winHeight = newHeight;
           }
           
      void drawLineSegment (scrPt endPt1,scrPt endPt2)
      {
           glBegin (GL_LINES);
           glVertex2i(endPt1.x, endPt1.y);
            glVertex2i(endPt2.x, endPt2.y);
          glEnd();
          }
          
          void polyline (GLint button, GLint action, GLint xMouse, GLint yMouse)
          {
               static scrPt endPt1, endPt2;
               int ptCtr;
               if(ptCtr == 0){
                if(button== GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
                  endPt1.x= xMouse;
                  endPt1.y= (GLint)(winHeight - yMouse);
                  ptCtr = 1;
                  }
                  else
                     if (button == GLUT_RIGHT_BUTTON)
                         exit(0);
                    }
                    else
                      if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN){
                               endPt2.x = xMouse;
                               endPt2.x = (GLint)(winHeight- yMouse);
                               drawLineSegment (endPt1, endPt2);
                               
                               endPt1 = endPt2;
                               
                      }
                      else
                        if (button == GLUT_RIGHT_BUTTON)
                          exit(0);
                                   
                               glFlush();
                          }
                          
         int main (int argc, char** argv)
          {
               glutInit (&argc,argv);
               glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
               glutInitWindowPosition (100,100);
               glutInitWindowSize (winWidth,winHeight);
               glutCreateWindow ("draw");
               init();
               glutDisplayFunc(displayFcn);
               glutReshapeFunc(winReshapeFcn);
               glutMouseFunc(polyline);
               
          glutMainLoop();
          }
                       
                                                                  

the problem is with int ptCtr; if it i remove it from the code i get an error ptCtr undeclared.If i declare it i get two messages In function `int main(int, char**)': warning: passing `GLdouble' for converting 2 of `void glutInitWindowSize(int, int)' and if run it i get only a top window with no display area please help outNB: using devC++

Share this post


Link to post
Share on other sites
My guess is that you never learned C and want to start direct with OpenGL.. oh well.

Look at the top of the file where you have global variable declaration:
GLdouble winHeight;
GLsizei winWidth =400, winheight=300;

Now if you notice you have a winHeight who is a GLdouble(double), and winheight who is a GLsizei(int). The glutInitWindowSize function need two integer, so just erase the first one who is declared for nothing and it should work.

ptCtr have nothing to do with this, and it should be declared.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this