convert window coordinates to 3D world coordinates with glut function glutMouseFunc()


I am trying to get the 3D coordinates of a mouse click C++/OpengGL with the glut function glutMouseFunc(). So I created a function like this:

void mouse(int button, int state, int x, int y){ if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { mouse_x=x; mouse_y=y; } }

The function gets the window coordinates of the click of the mouse and i use it with the glut function glutMouseFunc() like this:


My question is how would I modify the coordinates given by the mouse function so I could use them in a 3D world. My exact purpose would be the following: to be able to see if I have clicked on a 3D shape drawn in the world.

[EDIT] Would it be easier to transform the coordinates of the 3D object to 2D window coordinates and then compare it to the coordinates of the mouse click?


Mouse click does not correspond to a point in 3d space, but to a ray.

In any case, you use <a href="http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml" rel="nofollow">gluUnProject</a>.

If you know scene "depth" under cursor, then you can get 3d position of a click - by passing depth via winZ parameter.

If you don't know depth, pass 0.0 in winZ parameter, to get start of the ray, and 1.0 to get the "end". You'll have to calculate yourself if this ray hits anything.


  • Node Socket.io object trouble
  • How to pause a python script running in terminal
  • How to create L lists of n non-zero random decimals where each list sums to 1.0?
  • Detaching entity along with referenced entities
  • how to get the drawing graphic on picture box in c#
  • Update XML node (in an XML column) in SQL Server 2005 with another column value in the same row
  • How to clear out the contents of a map when clear() method call throws UnsupportedOperationException
  • Regexp, capture between parentheses, javascript
  • Writing to Ontotext GraphDB using Jena
  • How to reshape a 3D numpy array?
  • How to eliminate warning for passing multidimensional array as const multidimensional array?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Save image as is in photo album using swift
  • Reading space separated values file in c++ error
  • Changing references to deprecated methods C++
  • How to pass EC named curve list used by SSL_CTX?
  • How to copy styled text in JTextPane
  • How to autopopulate a field in SugarCRM form
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • Detect when Facebook like button is clicked
  • OOP Javascript - Is “get property” method necessary?
  • Alert pop up with LWUIT
  • Ajax Loaded meta Tags
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Xamarin Forms - UWP Fonts
  • Master page gives error
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How can I use Kendo UI with Razor?
  • Redux, normalised entities and lodash merge
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Arrays break string types in Julia
  • Linker errors when using intrinsic function via function pointer
  • AngularJs get employee from factory
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)