How to select a line point to drag it ? (newbie question)

Hi,

first of all i have to say that i´m an extremly newbie in ogl (work with it since 5 days).
I have to work with ogl in 2d only for the first time.

My problem´s are:

  1. How to select a point of a line?
  2. How to drag this point?
    I will do it like this:
    OnLButtonDown -> mouseDown=true;
    pointname = 1;
    OnMouseMove -> if(mouseDown==true) ->
    dragPoint();
    OnLButtonUp -> mouseDown=false;
  3. How to add a new point in an existing line?

My startup draw function looks like this:
glBegin(GL_LINE_LOOP);
glVertex2f(0.0,0.0);
glVertex2f(0.0,1.0);
glVertex2f(1.0,0.0);
glEnd();

A newbie friendly tutorial link would be nice.

Also a hint for a good ogl book in german would be great.

Thanks
daSickboy

Can not help you with German, but can give a few tip’s.

You should learn about how variables work in C/C++.

For you line you should store the varibles in a array.

example:
Line[i].x1 // Start of first line
Line[i].y1
Line[i].x2 // End of first line
Line[i].y2

Here is how you would store and draw many lines.

for(i = 0; i < number_lines_to_draw; i++)
{
glBegin(GL_LINES);
glVertex2f(Line[i].x1,Line[i].y1);
glVertex2f(Line[i].x2,Line[i].y2);
glEnd()
}

Then to select or add to a line, just compare the Line[i] to the current mouse position.

Hope this helps

That wasn´t very helpfull sorry.

I know that i could simply compare the stored points to my mouse, but that´s a bad way,cause i´ll have too much points in the Project

I thought more about something like this: http://fly.cc.fer.hr/~unreal/theredbook/chapter12.html

Which i don´t know how to use for my problems

I am not sure that will gain anything by using the selection buffer, since it if more for selecting object’s vs. lines or points.
You maybe better off even with a large number of points, since they can be processed very quickly on today’s fast CPU’s.

nehe.gamedev.net has a tutor on using selection buffer.

Originally posted by Sickboy:
[b]That wasn´t very helpfull sorry.

I know that i could simply compare the stored points to my mouse, but that´s a bad way,cause i´ll have too much points in the Project

I thought more about something like this: http://fly.cc.fer.hr/~unreal/theredbook/chapter12.html

Which i don´t know how to use for my problems [/b]

Ok i understand… maybe your right

But what´s the best way for my 2nd and 3rd problem ?
2nd: Do i need to redraw the whole gfx everytime i change the position of 1 point ?

3rd: is there an easy method to determine if the mouse is on a line between two points ?

  1. Yes, you have to redraw the screen each time the dot is moved, remember openGL renders the information to the screen. So any change in the scene will have to be re-rendered.
    If the change did not effect the whole scene, you maybe could use layers. Where the active layer would be the current points of change and the lower level would be a static scene, only after a change has been complete do you update the static scene.

  2. I have a couple of ideas:
    a. You select the line that is nearest to your mouse, and use the aproximate location of the mouse relative to the line. Look up 2D/3D math, and finding points on a line.

b. You click on a point on ether end of the line and a new point is added in the center of that line that you can drag the point up and down the line. Again you can use standard line math to get this information.

Originally posted by Sickboy:
[b]Ok i understand… maybe your right

But what´s the best way for my 2nd and 3rd problem ?
2nd: Do i need to redraw the whole gfx everytime i change the position of 1 point ?

3rd: is there an easy method to determine if the mouse is on a line between two points ?[/b]