# Getting 2D to 3D

**URL:** <https://community.khronos.org/t/getting-2d-to-3d/20050>\
**Category:** OpenGL: Basic Coding\
**Created:** [March 10, 2000, 3:24am UTC](https://community.khronos.org/t/getting-2d-to-3d/20050 "2000-03-10T03:24:00Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![McWare](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@McWare](https://community.khronos.org/u/McWare)\
**Post date:** [January 30, 2004, 4:42am UTC](https://community.khronos.org/t/getting-2d-to-3d/20050/6 "2004-01-30T04:42:00Z")

</div>

Something like this:

OnClick(int x, int y) {

GLint viewport[4];  
GLdouble mvmatrix[16], projmatrix[16];  
GLint realY //OpenGL coordinate, not window coordinate.  
GLdouble wx, wy, wz //world space.

//test for which button, etc…

glGetIntegerv(GL\_VIEWPORT, viewport);  
glGetDoublev(GL\_MODELVIEW\_MATRIX, mvmatrix);  
glGetDoublev(GL\_PROJECTION\_MATRIX, projmatrix);  
//viewport[3] is height of window in pixels  
realY = viewport[3]-(GLint)y;

//print out mouse point if you wish.

gluUnProject((GLdouble)x, (GLdouble)realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=0.0f

gluUnProject(GLdouble)x, (GLdouble)realY, 1.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=1.0f;

}

You may have to adjust your realY depending on what platform you’re on. Windows vs. Unix have different measures of coordinate systems. Windows is different from OpenGL in that windows measures positive Y as down, and Opengl measures positive Y as up (I THINK, I COULD BE WRONG ON THAT)

---

_[View the full topic](https://community.khronos.org/t/getting-2d-to-3d/20050)._
