Firstly: Remember the hack method I mentioned in class. Forget it. It does not work well and I know you all can do better.
Plus the hack method totally doesn't work if your window is resizable.
So here's how we do this properly.
Goals:
- we want to select a control point by clicking the mouse on the screen.
Our Assets:
- we can easily find the coordinates on the gl widget that our mouse has clicked (e->x(); and e->y();)
- we know where in 3D space our control points are.
Problem:
- how do we convert between these two coordinate systems?
Answer:
- By doing a projection from 3D space to viewspace.
What do we need to do this projection?
- get the viewport matrix
- get the projection matrix
- get the model view matrix
How do you get these?
- look in the blue book documention under glGet.
So how do we do the actual projection?
- from lectures you should know how to multiply all these matrices with your points to get your projected point (on the 2D window).
- if you're lazy (aren't we all) and want OpenGL to do this for you, take a look in the blue book documentation at gluProject.
So this will give you an x, y, and z coordinate that refer to the x and y pixel values on the screeni (it also gives you a z
value that indicates the "depth" of this screen coordinate, you can ignore this).
Is that it?
- not quite, the last thing you have to do is remember that your mouse coordinates for the gl windows have 0,0 in the top left corner.
- the coordinates you receive from the projection have 0,0 in the bottom left hand corner
- adjust for this difference by changing the y value.