3D to 2D coordinates


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. static void __gluMultMatrixVecd(const GLdouble matrix[16], const GLdouble in[4],
  2. GLdouble out[4])
  3. {
  4. int i;
  5.  
  6. for (i=0; i<4; i++) {
  7. out[i] =
  8. in[0] * matrix[0*4+i] +
  9. in[1] * matrix[1*4+i] +
  10. in[2] * matrix[2*4+i] +
  11. in[3] * matrix[3*4+i];
  12. }
  13. }
  14.  
  15. GLint GLAPIENTRY
  16. gluProject(GLdouble objx, GLdouble objy, GLdouble objz,
  17. const GLdouble modelMatrix[16],
  18. const GLdouble projMatrix[16],
  19. const GLint viewport[4],
  20. GLdouble *winx, GLdouble *winy, GLdouble *winz)
  21. {
  22. double in[4];
  23. double out[4];
  24.  
  25. in[0]=objx;
  26. in[1]=objy;
  27. in[2]=objz;
  28. in[3]=1.0;
  29. __gluMultMatrixVecd(modelMatrix, in, out);
  30. __gluMultMatrixVecd(projMatrix, out, in);
  31. if (in[3] == 0.0) return(GL_FALSE);
  32. in[0] /= in[3];
  33. in[1] /= in[3];
  34. in[2] /= in[3];
  35. /* Map x, y and z to range 0-1 */
  36. in[0] = in[0] * 0.5 + 0.5;
  37. in[1] = in[1] * 0.5 + 0.5;
  38. in[2] = in[2] * 0.5 + 0.5;
  39.  
  40. /* Map x,y to viewport */
  41. in[0] = in[0] * viewport[2] + viewport[0];
  42. in[1] = in[1] * viewport[3] + viewport[1];
  43.  
  44. *winx=in[0];
  45. *winy=in[1];
  46. *winz=in[2];
  47. return(GL_TRUE);
  48. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.