Mkay, I see where you're going with this.
Firstly, you don't need to use gluUnProject for this. What this function does is take a position in screen space and go all the way back to object space. This is the space in which you specify vertices in absence of a model (aka world) transform.
Under the assumption that you're getting the correct screen space mouse coordinates relative to the window the only thing you need to do is account for the viewport transform. Now, the code you have there is actually not going to render in screen space.
By setting a view matrix (the translate call after setting the modelview matrix as current) and a projection matrix (gluPerspective after setting the projection matrix as current) you are not rendering in screen space. You are then passing model space coordinates which will be subject to the aforementioned transformations. Now, the easiest way to render in screen space (if this is indeed what you're trying to do) is to simply render a quad that covers the entire buffer. What you'll do is render to what's called NDC (Normalized Device Coordinates) which is a uniform cube from -1 to 1 in all directions (in OpenGL). By setting up identity matrices for both the view and projection transforms you're effectively rendering to NDC space. This is why you say that OpenGL draws to the point (0, 0)...because (0, 0) is the center of the screen in NDC space.
Code: Select all
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST); // Turn zbuffer off if you want to use a 3 component vertex //
glBegin(GL_QUADS);
glVertex3f(-1.0F, -1.0F, 1.0F);
glVertex3f(-1.0F, 1.0F, 1.0F);
glVertex3f(1.0F, 1.0F, 1.0F);
glVertex3f(1.0F, -1.0F, 1.0F);
glEnd();
glEnable(GL_DEPTH_TEST); // Avoid doing this frequently //
// Restore state //
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
OK, so there's a quad that covers the entire damn buffer. Now....where to PUT that buffer in relation to the parent window. Easy.
Where's your mouse? At some point (x, y).
Well, the only thing to determine is how big of a surface you want to cover here. It's unclear in your post as to what EXACTLY you're drawing here...so we'll just assume its a square. Set up a viewport mapping before actually rendering.
So assuming this surface has a width and height (width, height). The other thing you have to consider here is that in screen space, your OS API or whatever API you're using to give you the mouse coordinates will MOST likely be giving them to you where (0, 0) is the upper left. In OpenGL, (0, 0) is the lower left....so Y is inverted.
Code: Select all
glViewport(x - width * 0.5, (height - y) - height * 0.5, width, height);
So you'd call something like so before you call the draw routine I posted earlier for any other square you seek to draw.