trying out opengl recently, can carry out basic stuffs but I seemed like cannot link all together to get what i want here: it is rather simple, I want to draw a line, and it can react to mouse click, e.g click n drag mouse would make it pull upward/downwards, double click would make it broken at certain click. Maybe I am stupid… Anyone please help me, or direct me to some resources which might help me… thanks a lot
Your task involves a few topics.
if you want to click-select the line you’re going to want to use glRenderMode(GL_SELECT).
and do some name pushing, etc…
Check out NeHe’s tutorial on picking:
once you have the line selected, you could determine which end point is closer to the point at which the user clicked and set that as the active endpoint (the one that moves up/down, left/right as the user moves the mouse)
suppose the line is defined by the two endpoints P1 & P2, and the user clicked down at point Q.
distance from Q to P1 is:
likewise distance from Q to P2 is:
this is your basic cartesian distance.
set that closer point as the active endpoint and redraw the line on each MouseMove message with the active endpoint moved to the current mouse position.
as far as breaking the line, figure out how large the gap should be where the user double clicks on the line (this is handled in your OnLButtonDblClck() or whatever you want to call it) and draw 2 lines. one from P1 to Q-(gapsize) (Q is the position of the double click) and another from Q+(gapsize) to P2.
Edit: actually breaking the line is a bit more complicated than that. this would only move the x coordinate thus changing the slope of the line. what you need to really do to break the line in 2 is interpolate the new endpoints of the 2 lines from the original line.
I’m pressed for time, but if you need further help with that, set up another post and I’ll help you with it.
it seems I might have misunderstood and you want to move the entire line, not just an endpoint.
if that’s what you want to do you need to keep track of the change in mouse position from one MouseMove message to the next.
set up 2 CPoints, OldPoint, NewPoint.
when the user left clicks on the canvas (screen, whatever) set OldPoint to that point, then on each mouse move message the new position of the mouse is stored in NewPoint. NewPoint.x-OldPoint.x and NewPoint.y-OldPoint.y give you a
deltaX and deltaY respectively. Now, add these deltas to your endpoint P1 and P2 to move the line by the same amount as the mouse move. (clearing your color buffer and redrawing each time, of course).
after that you set OldPoint = NewPoint, and continue.
thanks a lot. would try it out, and any problems would seek help from you.