unproject.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef IGL_UNPROJECT_H
  2. #define IGL_UNPROJECT_H
  3. namespace igl
  4. {
  5. // Wrapper for gluProject that uses the current GL_MODELVIEW_MATRIX,
  6. // GL_PROJECTION_MATRIX, and GL_VIEWPORT
  7. // Inputs:
  8. // win* screen space x, y, and z coordinates respectively
  9. // Outputs:
  10. // obj* pointers to 3D objects' x, y, and z coordinates respectively
  11. // Returns return value of gluUnProject call
  12. inline int unproject(
  13. const double winX,
  14. const double winY,
  15. const double winZ,
  16. double* objX,
  17. double* objY,
  18. double* objZ);
  19. }
  20. // Implementation
  21. #ifdef __APPLE__
  22. # include <OpenGL/gl.h>
  23. # include <OpenGL/glu.h>
  24. #else
  25. # ifdef _WIN32
  26. # define NOMINMAX
  27. # include <Windows.h>
  28. # undef NOMINMAX
  29. # endif
  30. # include <GL/gl.h>
  31. # include <GL/glu.h>
  32. #endif
  33. inline int igl::unproject(
  34. const double winX,
  35. const double winY,
  36. const double winZ,
  37. double* objX,
  38. double* objY,
  39. double* objZ)
  40. {
  41. // Put model, projection, and viewport matrices into double arrays
  42. double MV[16];
  43. double P[16];
  44. int VP[4];
  45. glGetDoublev(GL_MODELVIEW_MATRIX, MV);
  46. glGetDoublev(GL_PROJECTION_MATRIX, P);
  47. glGetIntegerv(GL_VIEWPORT, VP);
  48. return gluUnProject(winX,winY,winZ,MV,P,VP,objX,objY,objZ);
  49. }
  50. #endif