Question

Line adjusting in real time

Asked by: valleytech

hi,
 I have a line connect those points like ABCDEF after rendering. When user click on B and drag it to a new coordinate and release mouse. Then user release mouse. I want line ABCDEF will update the new shape. I want to see real-time changing when user drag point B. Please adivse. Thanks a lot.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
#include <GL/glut.h>
#include <vector>
 
struct point
{
        int x;
        int y;
};
 
        std::vector <point> points;
        point OnePoint;  
 
void init (void)
{
      glClearColor (1.0,1.0,1.0,0.0);
 
}
 
void lineSegment (void)
{
     
	  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
      glColor3f(1.0,0.0,0.0);           
 
      glBegin(GL_LINE_STRIP);
                for(int i =0; i < points.size();i++)
                {
                        glVertex2i(points[i].x, points[i].y);
                }
           
      glEnd();
      
      glFlush();
}
 
void processMouse(int button, int state, int x, int y) {
 
      if (state == GLUT_DOWN)
      {      
                  OnePoint.x= x;
                  OnePoint.y= y; 
                  points.push_back(OnePoint);
 
                  for (int i =0; i <= points.size() -1; i++)
                  {
                          printf("OnePoint is %d  %d\n",points[i].x, points[i].y);
                  }
                  printf("*****\n");
 
				  
				  glutPostRedisplay();  // << has to be here to render the scene with the new data
			
	  }
}
 
void resize(int width, int height)
{
    glViewport( 0, 0, (GLint)width, (GLint)height );
 
    glMatrixMode( GL_PROJECTION);
    
	//gluOrtho2D(0.0, width,0.0, height);
 
	gluOrtho2D(0.0, width, height, 0.0);
 
    glMatrixMode(GL_MODELVIEW);
           
}
 
void main(int argc, char ** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(50,100);
      glutInitWindowSize(512,512);
      glutCreateWindow("Drawing polyline with mouse interactive");
 
      init();
     
 
      //adding here the mouse processing callbacks
      glutMouseFunc(processMouse);
 
      glutDisplayFunc(lineSegment);
 
      glutReshapeFunc(resize);
          
      
      //glutMotionFunc(processMouseActiveMotion);
      //glutPassiveMotionFunc(processMousePassiveMotion);
      //glutEntryFunc(processMouseEntry);
 
      glutMainLoop();
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-30 at 10:07:45ID24693292
Tags

opengl glut mouse interaction

Topic

OpenGL Graphics & Game Programming

Participating Experts
2
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. draw a shape
    hi could someone please tell me how to 1. let a user drag the mouse making the shape of a triangle then 2. using the points of the triangle, make a straight lined one and display it. i'm new to java so any help is appreciated... thanks in advance,
  2. how to convert a real coordinate to screen coordinate
    how to convert a real coordinate to screen coordinate
  3. Drag and drop, delayed rendering, OnRenderGlobalData …
    I am having a bit of trouble with delayed rendering when dragging a file from my app (onto for example a Windows explorer window). I am getting multiple calls to delay render, most of which I would like to ignore, I only actually want to "render" my data when the ...
  4. Invalid shape coordinates fire events
    Hi guys, I have an MdiContainer Form that has MenuStrip and Toolstrip controls as well as an overview diagram represented by about 15 shape controls from Visual Basic PowerPacks V2.0. I want to raise an event when user double click some of those shapes and then perform some...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SyntacticsPosted on 2009-08-30 at 10:28:05ID: 25218432

You need to process the Mouse Move event in your mouse handler.  Then update the points[i].x, points[i].y value for the vertice you grabbing in the down event.  Then redraw the whole scene.  It doesn't look like OpenGL gives you a GLUT_MOVE event, so you may have to write a WM_MOUSE_MOVE handler in the main Windows pump.  Been a while since I did any openGL.

http://www.gamedev.net/reference/articles/article539.asp
http://lukerymarz.blogspot.com/2008/04/playing-with-opengl-mouse-interaction.html

 

by: valleytechPosted on 2009-08-30 at 23:14:58ID: 25220762

any other suggestions for real-time? thanks a lot.

 

by: ikeworkPosted on 2009-08-31 at 01:34:56ID: 25221252

>> It doesn't look like OpenGL gives you a GLUT_MOVE event

OpenGL does not give any mouse or GUI-events, but glut does. The asker already uses glut in his/her code. There is a mouse-motion function callback too in glut, that gives you the mouse-motion events.

>> any other suggestion

But more important in your case than a mouse-move-event is button-down and button-up. You have to check in the button-down function, if the mouse is over one of your points. You should add a little radius for that check-range, so it will not be that hard for your user to click the line-end-point.

I would not use the same mouse-button for that action, that you use for adding a point, you can use the middle-button or the right mouse button.

When the user releases this button replace the point with the new point.

When that works you can add some convinience-stuff, i.e. when the mouse is over one of your points, you could render a circle around that point, meaning that the user can select that point now. But first make the basic selection work.

ike

 

by: valleytechPosted on 2009-08-31 at 08:28:35ID: 25223554

Let me code the the little radius for check-range because it is hard for user to click on same point. So I will give a vicinity around it.
 Also, i Will use right mouse button as u suggest Ike. I will get back when i finish those basic steps.

 

by: valleytechPosted on 2009-08-31 at 23:22:37ID: 25228935

hi ike,
 FIrst of all, i update my code a little bit to make sure no duplicate point in vector. Done.
 now i try to use the glutMotionFunc  with active motion in order to get new point. However, glutMotionFunc give a lot of points as long as  drag the mouse. Am i on the right track?
 
PS: do u i know left mouse or right mouse? in processMouse, we didn't indicate left or  right mouse!!!

 

by: ikeworkPosted on 2009-08-31 at 23:59:57ID: 25229076

>> now i try to use the glutMotionFunc  with active motion in order to get new point. However, glutMotionFunc give a lot of points as long as  drag the mouse. Am i on the right track?
 
Start with moust button up and down. You dont need glutMotionFunc for that. Just pick the point where the mouse was down and see if there is one of your points nearby. If there is a point catch the mouse up and its x & y for the selected point.

The mouse-button identifier is the first param of your mouse callback function processMouse, called button. The Ids are defined in glut.h you can check it like this:

void processMouse(int button, int state, int x, int y) {
 
    switch(button)
    {
        case GLUT_LEFT_BUTTON:
            // put code for left button here
            break;

        case GLUT_MIDDLE_BUTTON:
            // put code for middle button here
            break;

        case GLUT_RIGHT_BUTTON:
            // put code for right button here
            break;
    }

 

by: valleytechPosted on 2009-09-01 at 00:00:25ID: 25229079

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
#include <GL/glut.h>
#include <vector>
 
struct point
{
        int x;
        int y;
};
 
        std::vector <point> points;
        point OnePoint;  
 
void init (void)
{
      glClearColor (1.0,1.0,1.0,0.0);
 
}
 
void lineSegment (void)
{
     
	  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
      glColor3f(1.0,0.0,0.0);           
 
      glBegin(GL_LINE_STRIP);
                for(int i =0; i < points.size();i++)
                {
                        glVertex2i(points[i].x, points[i].y);
                }
           
      glEnd();
      
      glFlush();
}
 
void processMouse(int button, int state, int x, int y) {
 
	printf("processMouse\n");
 
      if (state == GLUT_DOWN)
      {
		  if(button == GLUT_LEFT_BUTTON)
		  {
 
				  // check to avoid duplicate elements in vector
 
				  bool _bfound = false;
 
				  for (int ii = 0; ii < points.size(); ii++)
				  {	 
					  if( points[ii].x == x && points[ii].y == y)
					  {
						  _bfound = true;
						  printf("Duplicate element!!!\n");
						  break;
					  }
				  }
 
				  if( _bfound == false)
				  {
					  OnePoint.x= x;
					  OnePoint.y= y; 
					  points.push_back(OnePoint);
				  }
 
				 
                  for (int i =0; i <= points.size() -1; i++)
                  {
                          printf("OnePoint is %d  %d\n",points[i].x, points[i].y);
                  }
                  printf("*****\n");
				  glutPostRedisplay();  // << has to be here to render the scene with the new data
		  }	
	  }
}
 
void processMouseActiveMotion (int x,int y)
{
	int buttonDown;
 
	printf("activeMouseMotion\n");
	printf("x is %d\n",x);
	printf("y is %d\n",y);
		
	
 
}
 
 
void resize(int width, int height)
{
    glViewport( 0, 0, (GLint)width, (GLint)height );
 
    glMatrixMode( GL_PROJECTION);
    
	//gluOrtho2D(0.0, width,0.0, height);
 
	gluOrtho2D(0.0, width, height, 0.0);
 
    glMatrixMode(GL_MODELVIEW);
           
}
 
void main(int argc, char ** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(50,100);
      glutInitWindowSize(512,512);
      glutCreateWindow("Drawing polyline with mouse interactive");
 
      init();
     
 
      //adding here the mouse processing callbacks
      glutMouseFunc(processMouse);
      glutDisplayFunc(lineSegment);
 
	  glutMotionFunc(processMouseActiveMotion); 
 
 
 
      glutReshapeFunc(resize);
          
      
      //glutMotionFunc(processMouseActiveMotion);
      //glutPassiveMotionFunc(processMousePassiveMotion);
      //glutEntryFunc(processMouseEntry);
 
      glutMainLoop();
}
 

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:

Select allOpen in new window

 

by: ikeworkPosted on 2009-09-01 at 00:14:10ID: 25229131

I cant check your code now, I'm about to go to work ;)
Can you tell me what the problem is and what works so far?

 

by: valleytechPosted on 2009-09-01 at 00:16:54ID: 25229145

i am upgrading my code. i am working on your guide " start with mouse down and mouse up.."

 

by: valleytechPosted on 2009-09-01 at 00:17:31ID: 25229148

have a good day.

 

by: ikeworkPosted on 2009-09-01 at 01:30:22ID: 25229488

Ok, similar to the check for duplicate elements in vector for GLUT_LEFT_BUTTON, add a block for the right button and check there if user clicked a point (if x & y is already in the vector), if yes, store the index of the point in a global variable and in mouse button up replace the point at that index with the new x & y

add a global variable like:

int selectedPointIndex = -1;

Use -1 to indicate if a point is selected, if selectedPointIndex == -1, it means no point is selected, otherwise it is the index of the currently selected point

 

by: valleytechPosted on 2009-09-01 at 08:55:44ID: 25232880

please take look at my code :)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
#include <GL/glut.h>
#include <vector>
 
struct point
{
        int x;
        int y;
};
 
        std::vector <point> points;
        point OnePoint;  
		point TempPoint;
		point FinalPoint;
		int selectedPointIndex = -1;
 
void init (void)
{
      glClearColor (1.0,1.0,1.0,0.0);
 
}
 
void lineSegment (void)
{
     
	  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
      glColor3f(1.0,0.0,0.0);           
 
      glBegin(GL_LINE_STRIP);
                for(int i =0; i < points.size();i++)
                {
                        glVertex2i(points[i].x, points[i].y);
                }
           
      glEnd();
      
      glFlush();
}
 
void processMouse(int button, int state, int x, int y) {
 
      if (state == GLUT_DOWN)
      {
		  if(button == GLUT_LEFT_BUTTON)
		  {
				  // check to avoid duplicate elements in vector
				  bool _bfound = false;
 
				  for (int ii = 0; ii < points.size(); ii++)
				  {	 
					  if( points[ii].x == x && points[ii].y == y)
					  {
						  _bfound = true;
						  printf("Duplicate element!!!\n");
						  break;
					  }
				  }
 
				  if( _bfound == false)
				  {
					  OnePoint.x= x;
					  OnePoint.y= y; 
					  points.push_back(OnePoint);
				  }  
		  }
		  
		  if(button == GLUT_RIGHT_BUTTON)
		  {
			  printf("Right click on mouse\n");
 
			  printf("PRESSMOUSE x is %d\n",x);
			  printf("PRESSMOUSE y is %d\n",y);
			  
			  TempPoint.x= x;
			  TempPoint.y= y;
 
		  }
	  }
 
	  if( state == GLUT_UP)
	  {
		  if(button == GLUT_RIGHT_BUTTON)
		  {
			  printf("RELEASEMOUSE x is %d\n",x);
			  printf("RELEASEMOUSE y is %d\n",y);
 
			  FinalPoint.x = x;
			  FinalPoint.y  = y;
 
		  }
	  }
 
	  //check to see it TempPoint is in vector or not
		for (int iii = 0; iii < points.size(); iii++)
		{
			// use approximately difference is 0.01!!!!
			if( abs(points[iii].x- TempPoint.x) <= 0.01 && abs(points[iii].y - TempPoint.y) <=0.01 )
			{
				selectedPointIndex = iii;
				printf("Found point in vector!!!\n");
				break;
			}
		}
 
 
		// upgrade vector at selectedPointIndex with FinalPoint when right mouse Release
		if ( selectedPointIndex > -1)
		{
			points[selectedPointIndex].x = FinalPoint.x;
			points[selectedPointIndex].y = FinalPoint.y;
 
		}
 
	  //
 
	  for (int i =0; i <= points.size() -1; i++)
      {
		  printf("OnePoint is %d  %d\n",points[i].x, points[i].y);
	  }
      printf("*****\n");
	  glutPostRedisplay();  // << has to be here to render the scene with the new data
}
 
 
 
 
void resize(int width, int height)
{
    glViewport( 0, 0, (GLint)width, (GLint)height );
 
    glMatrixMode( GL_PROJECTION);
    
	//gluOrtho2D(0.0, width,0.0, height);
 
	gluOrtho2D(0.0, width, height, 0.0);
 
    glMatrixMode(GL_MODELVIEW);
           
}
 
void main(int argc, char ** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(50,100);
      glutInitWindowSize(512,512);
      glutCreateWindow("Drawing polyline with mouse interactive");
 
      init();
     
 
      //adding here the mouse processing callbacks
      glutMouseFunc(processMouse);
      glutDisplayFunc(lineSegment);
 
	  //glutMotionFunc(processMouseActiveMotion); 
 
 
 
      glutReshapeFunc(resize);
          
      
      //glutMotionFunc(processMouseActiveMotion);
      //glutPassiveMotionFunc(processMousePassiveMotion);
      //glutEntryFunc(processMouseEntry);
 
      glutMainLoop();
}
 

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:

Select allOpen in new window

 

by: ikeworkPosted on 2009-09-01 at 09:13:39ID: 25233080

Please do us a favor and correct the indention. Its so heard to read. At least in processMouse.
Then I'm gonna take a look later, when you posted the new version, cause I'm still at work

 

by: valleytechPosted on 2009-09-01 at 09:20:46ID: 25233139

let me try to paste my code in here,not using attach code snippet. I hope indention is not damaged.

==================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
#include <GL/glut.h>
#include <vector>
 
struct point
{
        int x;
        int y;
};
      std::vector <point> points;
    point OnePoint;  
      point TempPoint;
      point FinalPoint;
      int selectedPointIndex = -1;
 
void init (void)
{
    glClearColor (1.0,1.0,1.0,0.0);
 
}
 
void lineSegment (void)
{
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor3f(1.0,0.0,0.0);          
      glBegin(GL_LINE_STRIP);
            for(int i =0; i < points.size();i++)
        {
                  glVertex2i(points[i].x, points[i].y);
        }
           
    glEnd();
    glFlush();
}
 
void processMouse(int button, int state, int x, int y)
{
      if (state == GLUT_DOWN)
      {
              if(button == GLUT_LEFT_BUTTON)
              {
                          // check to avoid duplicate elements in vector
                          bool _bfound = false;

                          for (int ii = 0; ii < points.size(); ii++)
                          {       
                                if( points[ii].x == x && points[ii].y == y)
                                {
                                      _bfound = true;
                                      printf("Duplicate element!!!\n");
                                      break;
                                }
                          }

                          if( _bfound == false)
                          {
                                OnePoint.x= x;
                                OnePoint.y= y;
                                points.push_back(OnePoint);
                          }  
              }
             
              if(button == GLUT_RIGHT_BUTTON)
              {
                    printf("Right click on mouse\n");

                    printf("PRESSMOUSE x is %d\n",x);
                    printf("PRESSMOUSE y is %d\n",y);
                    
                    TempPoint.x= x;
                    TempPoint.y= y;

              }
        }

        if( state == GLUT_UP)
        {
              if(button == GLUT_RIGHT_BUTTON)
              {
                    printf("RELEASEMOUSE x is %d\n",x);
                    printf("RELEASEMOUSE y is %d\n",y);

                    FinalPoint.x = x;
                    FinalPoint.y  = y;

              }
        }

        //check to see it TempPoint is in vector or not
            for (int iii = 0; iii < points.size(); iii++)
            {
                  // use approximately difference is 0.005. Need advise on difference value !!!
                  if( abs(points[iii].x- TempPoint.x) <= 0.005 && abs(points[iii].y - TempPoint.y) <=0.005 )
                  {
                        selectedPointIndex = iii;
                        printf("Found point in vector!!!\n");
                        break;
                  }
            }

            // upgrade vector at selectedPointIndex with FinalPoint when right mouse Release
            if ( selectedPointIndex > -1)
            {
                  points[selectedPointIndex].x = FinalPoint.x;
                  points[selectedPointIndex].y = FinalPoint.y;

            }

        //

        for (int i =0; i <= points.size() -1; i++)
      {
              printf("OnePoint is %d  %d\n",points[i].x, points[i].y);
        }
      printf("*****\n");
        glutPostRedisplay();  // << has to be here to render the scene with the new data
}

void resize(int width, int height)
{
    glViewport( 0, 0, (GLint)width, (GLint)height );
 
    glMatrixMode( GL_PROJECTION);
   
      //gluOrtho2D(0.0, width,0.0, height);
 
      gluOrtho2D(0.0, width, height, 0.0);

    glMatrixMode(GL_MODELVIEW);
           
}
 
void main(int argc, char ** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
      glutInitWindowPosition(50,100);
      glutInitWindowSize(512,512);
      glutCreateWindow("Drawing polyline with mouse interactive");
 
      init();
     
 
      //adding here the mouse processing callbacks
      glutMouseFunc(processMouse);
      glutDisplayFunc(lineSegment);

        //glutMotionFunc(processMouseActiveMotion);

      glutReshapeFunc(resize);
               
      //glutMotionFunc(processMouseActiveMotion);
      //glutPassiveMotionFunc(processMousePassiveMotion);
      //glutEntryFunc(processMouseEntry);
 
      glutMainLoop();
}
 

 

by: ikeworkPosted on 2009-09-01 at 12:05:17ID: 25234836

you have to ckeck if any point was selected here

    if (state == GLUT_DOWN)
    {
        if(button == GLUT_RIGHT_BUTTON)
        {

                                              
1:
2:
3:
4:

Select allOpen in new window

 

by: ikeworkPosted on 2009-09-01 at 12:13:43ID: 25234905

right now you do it, whenever processMouse is called, there is no "if" around it, you dont check the button or the state for this block:

        //check to see it TempPoint is in vector or not
            for (int iii = 0; iii < points.size(); iii++)
            {
                  // use approximately difference is 0.005. Need advise on difference value !!!
                  if( abs(points[iii].x- TempPoint.x) <= 0.005 && abs(points[iii].y - TempPoint.y) <=0.005 )
                  {
                        selectedPointIndex = iii;
                        printf("Found point in vector!!!\n");
                        break;
                  }
            }

 

by: valleytechPosted on 2009-09-01 at 14:10:31ID: 25236202

i don't get your idea this time. Please elaborate for. Thanks.

 

by: valleytechPosted on 2009-09-01 at 14:12:17ID: 25236221

and also
 // use approximately difference is 0.005. Need advise on difference value !!!

--> should I calculate percentage error and set it less than 5%?

 

by: ikeworkPosted on 2009-09-01 at 14:23:32ID: 25236323

   if (state == GLUT_DOWN)
    {
        if(button == GLUT_RIGHT_BUTTON)
        {
             for (int iii = 0; iii < points.size(); iii++)
            {
                  if( abs(points[iii].x- x) <= 3 && abs(points[iii].y - y) <= 3 )
                  {
                        selectedPointIndex = iii;
                        printf("Found point in vector!!!\n");
                        break;
                  }
            }
        }
    }

>> // use approximately difference is 0.005. Need advise on difference value !!!

where do you have that code from, you better take a bigger value like e points

Sorry, got no time to elabporate, gonna go to sleep now. Just give it a try, I'm sure you gonna make it ;)

 

by: valleytechPosted on 2009-09-01 at 14:27:48ID: 25236357

good night. let me figure out what u mean. Thanks.

 

by: ikeworkPosted on 2009-09-01 at 23:04:44ID: 25238291

Hey vt, any luck so far?

 

by: valleytechPosted on 2009-09-02 at 09:06:49ID: 25242498

hi ike,
 right now i am thinking about your advise
 " where do you have that code from, you better take a bigger value like e points"

 I think of using 5% as error percentage. For example let say x = 100. So when user click around between  95 and 105 , i can consider that like 100. How do u think?

 

by: ikeworkPosted on 2009-09-02 at 10:20:30ID: 25243267

>> right now i am thinking about your advise

Where excactly are you stuck?

 

by: valleytechPosted on 2009-09-02 at 12:39:47ID: 25244794

not stuck. but try to find a good approach.

 

by: valleytechPosted on 2009-09-02 at 23:36:44ID: 25248277

hi ike,
 I have last two questions for this program before i close it.  I want to make vertex is 3 pixel and line width is 2 pixel. by that way, user can click on vertex easier. Thanks a lot.

 

by: ikeworkPosted on 2009-09-02 at 23:49:47ID: 25248319

You can set it with:

glPointSize(3);
glLineWidth(2);

But then you have to render the points too in order to see the 3-pixel-points. Add the code below to line_segment:

    glBegin(GL_POINTS);
    for(int i =0; i < points.size();i++)
    {
        glVertex2i(points[i].x, points[i].y);
    }       
    glEnd();

                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: valleytechPosted on 2009-09-03 at 00:17:04ID: 25248422

i declare
      glPointSize(3);
     glLineWidth(2);
 as global variables and this code

            glBegin(GL_POINTS);
    for(int ii =0; ii < points.size();ii++)
    {
        glVertex2i(points[ii].x, points[ii].y);
    }      
    glEnd();


 right now i have errors:

      C2373: 'glPointSize' : redefinition; different type modifiers
        c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1351) : see declaration of 'glPointSize'
D:\OPENGL\Cpp2.cpp(21) : error C2501: 'glLineWidth' : missing storage-class or type specifiers
D:\OPENGL\Cpp2.cpp(21) : error C2373: 'glLineWidth' : redefinition; different type modifiers
        c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1307) : see declaration of 'glLineWidth'
Error executing cl.exe.

 

by: ikeworkPosted on 2009-09-03 at 01:06:31ID: 25248643

You can put it into init:

void init (void)
{
    glClearColor (1.0,1.0,1.0,0.0);
    glPointSize(3);
    glLineWidth(2);
}
 

 

by: ikeworkPosted on 2009-09-03 at 01:08:34ID: 25248654

the rendering point-code has to be in line_segement:

void lineSegment (void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0,0.0,0.0);           
 
    // rendering the lines
    glBegin(GL_LINE_STRIP);
    for(int i =0; i < points.size();i++)
    {
        glVertex2i(points[i].x, points[i].y);
    }       
    glEnd();
 
    // rendering the points
    glBegin(GL_POINTS);
    for(int i =0; i < points.size();i++)
    {
        glVertex2i(points[i].x, points[i].y);
    }       
    glEnd();
 
    glFlush();
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

Select allOpen in new window

 

by: valleytechPosted on 2009-09-03 at 09:44:46ID: 25252468

i got it ide. thank you a lot.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...