let me try to implement your guide. Thanks.
Main Topics
Browse All Topicsplease 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
i just wonder
glBegin(GL_LINE_STRIP);
for(int i = 0; i < points.size(); ++i)
{
glVertex2i(points[i].x, points[i].y);
}
glEnd();
--> so each time when user clicks a new point, do i have to start to render from beginning? can u just render a line between last click point and new one? I think of this issue because just in case we have one object with such as 500000points, and whenever it move in a game, we have to render again and again some many points?
>> so each time when user clicks a new point, do i have to start to render from beginning?
>> and whenever it move in a game, we have to render again and again some many points?
Yes, that is how it is done in games. Actually you try to render as often as you can, to get a high frame-rate. Usually the frame rate in games goes from 20 frames per second (fps) up to 60fps.
But you dont need such a high frame rate in your app, because the data in your case can only change, when the user presses a button.
However, you should render the scene again, whenever the window changes, the position or the size.
You can pass a callback-function to glut for that case. Such a callback function looks like this:
void resize(int width, int height)
{
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode( GL_PROJECTION);
gluOrtho2D(0.0, width,0.0, height);
glMatrixMode(GL_MODELVIEW)
}
In main you have to pass the callback to glut like this:
glutReshapeFunc(resize);
When ever the user clicks on the window and adds a new point, you must call glutPostRedisplay() to render the scene again.
glutPostRedisplay();
glutPostRedisplay() calls the function that you passed to glutDisplayFunc, which is lineSegment. So just call glutPostRedisplay, then line_segment will be called from glut.
I changed your code below. I removed glutPostRedisplay from line_segment and added it in processMouse, when a new point was added:
Ah ok, I see what it is, you loop behind the array, you have:
for(int i =0; i <= points.size();i++)
but it must be "<" not "<="
Change the line:
for(int i =0; i < points.size();i++)
Thats it. By the way, that was an assertation, if you press "Retry" in that window, then you can see which line caused the error.
Right, I had the same result here. You need to clear the Depth-Buffer too in line_segment:
Replace this:
glClear (GL_COLOR_BUFFER_BIT);
with:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Then you will find another issue. The windows y- coordinates in process-mouse start at window top with 0 and in window bottom is max y. You told opengl the opposite in glOrtho2D
So either replace:
gluOrtho2D(0.0, width, 0.0, height);
with:
gluOrtho2D(0.0, width, height, 0.0);
or calculate the proper y from the process_mouse parameter
Btw. Why did you remove th "glLoadIdentity()" in line_segement as shown in your previous q:
http://www.experts-exchang
You should put it back, it is important to load the identity-matrix into current matrix in matrix-stack before rendering.
Yes, that works too, actually you can remove everything from init, but the glClearColor, since resize is called on startup, it is all done there.
So you just need:
void init (void)
{
glClearColor (1.0,1.0,1.0,0.0);
}
About the Depth-Buffer. Do you plan to render more things behind and before the lines? If yes, you should clear the Depth-Buffer each time in line_segment, otherwise that wont work correctly.
I really like your help. It help me to understand a lot for more advanced topic later on. Let me change it the Depth-buffer for future reference.
I plan to update this program to make it flexible a little bit more in next question :)
Before i close this, i just want to clarify one thing. The way how openGL works in program:
Mouse click -> glutMOuseFun -> glutPostRedisplay-> glutDisplayFunc
Am I right?
Business Accounts
Answer for Membership
by: ikeworkPosted on 2009-08-28 at 10:19:57ID: 25209723
Hey valleytech,
What you will need is a container for the line points. I suggest you make a structure for the points, that stores the x- and y-component.
struct point
{
int x;
int y;
};
Then you need an array, or better a vector (it has a dynamic size), which stores the points that the user clicked at:
#include <vector>
// global vector of all the points, that the user clicked
std::vector<point> points;
When the user clicks a point, just add it to your vector:
point p;
p.x = ...
p.y = ...
points.push_back(p);
Then in your render-func you iterate through the vector and render the lines
glBegin(GL_LINE_STRIP);
for(int i = 0; i < points.size(); ++i)
{
glVertex2i(points[i].x, points[i].y);
}
glEnd();
You have to setup the viewport with glOrtho to the same size as the window is, then your coordinates directly map to a point on the screen.
Put that into your code and if you encounter problems feel free to ask :)
ike