Message Board


Message Board > C/C++ > from screen-coordinates to world-coordinates

April 21, 2006, 15:32
Gobliz
一年
75 posts

My question is, how do I convert screen coordinates to 3D world coordinates. eg. When you click on the screen it must return the 3D world coordinates. This must be implemented with OpenGL. Does anyone know how to do this?

Thanks in advance :)
____________
Cap'n Jimbo Catfish worklog
#
April 21, 2006, 15:50
Eckolin
Quite Whiskered
388 posts

Use GLint gluUnProject.
Parameters are:

GLdouble winX; // Enter x-position of your mouse.
GLdouble winY; // Enter y-position of your mouse. Note that GL works from the lower left corner rather than the upper left one. You can subtract the y-value of your mouse from the height of your viewport to get the corrected one.

GLdouble winZ // Read the z-buffer at x, y for this one.

glReadPixels(x,y, 1, 1 (dimensions), GL_DEPTH_COMPONENT (z buffer), GL_FLOAT, &z);

const GLdouble *model // Modelview matrix, use glGetDoublev(GL_MODELVIEW_MATRIX, <varname>); to retrieve it.

const GLdouble *proj // Projection matrix, use glGetDoublev(GL_PROJECTION_MATRIX, <varname>); to retrieve it (doh).

const GLint *view // Viewport information, can be retrieved using glGetIntegerv(GL_VIEWPORT, <varname>);

GLdouble* objX // Return variables.
GLdouble* objY // Return variables.
GLdouble* objZ // Return variables.

[Edited on April 21, 2006 by Eckolin]
____________
Maker of Games...
Wisdom is supreme; therefore get wisdom.
Need help with coding? I probably wrote something similar.
#
April 22, 2006, 15:33
Rhovanion
Exterminated
666 posts
erm, how does it work. How can the program know "how far" you click "in" the 3D environment. surely you can convert X and Y but how does it know you click on an object that's 30 pixels Z or 900 pixels Z ?
____________
#
May 15, 2006, 05:19
Stuckie
Code Munkee
21 posts
Quoting Rhovanion:
erm, how does it work. How can the program know "how far" you click "in" the 3D environment. surely you can convert X and Y but how does it know you click on an object that's 30 pixels Z or 900 pixels Z ?


If you have a list of things to draw (which you should so you can get the order right -- something ESPECIALLY important for transparancies), then you could check against that.
____________
#

Message Board > C/C++ > from screen-coordinates to world-coordinates

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!