We help IT Professionals succeed at work.
Get Started

draw polyline with mouse interactive

valleytech
valleytech asked
on
1,839 Views
Last Modified: 2013-12-06
please help me on drawing polyline with mouse click interactive on window. whenever a new click, a new line will be drawn from previous click to new one. Thank a lot.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
#include <GL/glut.h>
 
 
  int ViewportXFirst  , ViewportYFirst ;
  int ViewportXSecond , ViewportYSecond;
 
  int flag =0; // count 2 clicks at a time.
               // flag = 0 : first time. flag =1 : second time
 
 
 
void init (void)
{
      glClearColor (1.0,1.0,1.0,0.0);
 
      glMatrixMode( GL_PROJECTION);
      gluOrtho2D(0.0,200.0,0.0,150.0);
 
	   glMatrixMode(GL_MODELVIEW);
}
 
void lineSegment (void)
{
      glClear (GL_COLOR_BUFFER_BIT);
 
      glColor3f(1.0,0.0,0.0);
      glBegin(GL_LINE_STRIP);
            glVertex2i(ViewportXFirst,ViewportYFirst);
            glVertex2i(ViewportXSecond,ViewportYSecond);
      glEnd();
      
      glFlush();
}
 
void processMouse(int button, int state, int x, int y) {
 
      if (state == GLUT_DOWN)
      {      
            if(flag ==0)
            {
                  printf("flag is %d\n",flag);
                  ViewportXFirst = x;
                  ViewportYFirst = 512- y;
 
                  flag ++;
            }
            else
            {
				if(flag ==1)
				{
                  printf("flag is %d\n",flag);
 
                  ViewportXSecond = x;
                  ViewportYSecond = 512- y;
				}
				else
				{
					ViewportXFirst = ViewportXSecond;  // swap second point to first point 
					ViewportYFirst = ViewportYSecond;  //before obtain new point to second point
 
					ViewportXSecond = x;
					ViewportYSecond = 512- y;
 
				}
				flag++;
                  
            }
 
		
				printf("ViewportXFirst is %d   ",ViewportXFirst);
				printf("ViewportYFirst is %d",ViewportYFirst);
				printf("\n");
 
				printf("ViewportXSecond is %d   ",ViewportXSecond);
				printf("ViewportYSecond is %d",ViewportYSecond);
				printf("\n"); 
      }
	
	  if(flag >=2)
	  {
		glutDisplayFunc(lineSegment);
 
	  }
 
  
}
 
void main(int argc, char ** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(50,100);
      glutInitWindowSize(512,512);
      glutCreateWindow("An example openGL sample");
 
      init();
     
 
 
      //adding here the mouse processing callbacks
      glutMouseFunc(processMouse);
 
	  glutDisplayFunc(lineSegment);
	  
      
      //glutMotionFunc(processMouseActiveMotion);
      //glutPassiveMotionFunc(processMousePassiveMotion);
      //glutEntryFunc(processMouseEntry);
 
      glutMainLoop();
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 23 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE