Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: C++  |  size: 3.68 KB  |  hits: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         static void Get_Selection_Ray(int x, int y, float *_p, float *_q)
  2.         {
  3.                 //printf("Mouse clicked at (%i, %i)", x, y);
  4.                 //Convert x, y from (0, width; 0, height) into a (-1, 1; -1, 1) range
  5.                 float x_p = (2.0f*x) / screen_width - 1;
  6.         float y_p = 1 - (2.0f*y) / screen_height;
  7.  
  8.                 printf("Mouse clicked at (%.2f, %.2f)\n", x_p, y_p);
  9.                
  10.                 //Calculate the inverse of GL_PROJECTION_MATRIX
  11.                 float M[16];
  12.                 glGetFloatv(GL_PROJECTION_MATRIX, M);
  13.                 //M is in column-major!
  14.                
  15.                 /*printf("[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n\n",
  16.                            M[0], M[4], M[8], M[12],
  17.                            M[1], M[5], M[9], M[13],
  18.                            M[2], M[6], M[10], M[14],
  19.                            M[3], M[7], M[11], M[15]);*/
  20.                
  21.                
  22.                 float inv_M[16];
  23.                 memset(inv_M, 0, sizeof(float)*16);
  24.                
  25.                 inv_M[0]=1/M[0];
  26.                 inv_M[5]=1/M[5];
  27.                 inv_M[14]=1/M[14];
  28.                 inv_M[11]=-1;
  29.                 inv_M[15]=M[10]/M[14];
  30.                 //inv_m is in row-major!
  31.                
  32.                 /*printf("[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n[%.2f %.2f %.2f %.2f]\n\n",
  33.                            inv_m[0], inv_m[1], inv_m[2], inv_m[3],
  34.                            inv_m[4], inv_m[5], inv_m[6], inv_m[7],
  35.                            inv_m[8], inv_m[9], inv_m[10], inv_m[11],
  36.                            inv_m[12], inv_m[13], inv_m[14], inv_m[15]);*/
  37.                
  38.                
  39.                 //Calculate rx, ry, rz
  40.                 float p1[4];
  41.                 float p2[4];
  42.                 p1[0] = inv_M[0] * x_p;
  43.                 p1[1] = inv_M[5] * y_p;
  44.                 p1[2] = inv_M[11] * 1;
  45.                 p1[3] = inv_M[14] * 1 + inv_M[15] * 1;
  46.                
  47.                 p2[0] = inv_M[0] * x_p;
  48.                 p2[1] = inv_M[5] * y_p;
  49.                 p2[2] = inv_M[11] * 1;
  50.                 p2[3] = inv_M[14] * -1 + inv_M[15] * 1;
  51.                
  52.                
  53.                
  54.                
  55.                 //Convert two vectors into two 3D points (homogeneous)
  56.                 for (int i = 0; i <= 3; i ++) {
  57.                         p1[i] = p1[i]/p1[3];
  58.                         p2[i] = p2[i]/p2[3];
  59.                 }
  60.  
  61.                 printf("p1 =\n[%.2f]\n[%.2f]\n[%.2f]\n[%.2f]\n\n", p1[0], p1[1], p1[2], p1[3]);
  62.                 printf("p2 =\n[%.2f]\n[%.2f]\n[%.2f]\n[%.2f]\n\n", p2[0], p2[1], p2[2], p2[3]);
  63.  
  64.  
  65.                 //Convert these 3D points (homogeneous) from the eye space into the world space.
  66.                
  67.                 glMatrixMode(GL_MODELVIEW);
  68.  
  69.                 glLoadIdentity();
  70.                 glTranslatef(target[0], target[1], target[2]);
  71.                 glRotatef(-swing_angle, 0, 1, 0);
  72.                 glRotatef(-elevate_angle, 1, 0, 0);
  73.                 glTranslatef(0, 0, zoom);
  74.                
  75.  
  76.                 //gluLookAt(0, 0, -zoom, 0, 0, 0, 0, -1, 0);
  77.  
  78.                
  79.                 memset(inv_M, 0, sizeof(float)*16);
  80.                 glGetFloatv(GL_MODELVIEW_MATRIX, inv_M);
  81.                 /*printf("inv_M = \n%.2f %.2f %.2f %.2f\n%.2f %.2f %.2f %.2f\n%.2f %.2f %.2f %.2f\n%.2f %.2f %.2f %.2f\n\n",
  82.                            inv_M[0], inv_M[4], inv_M[8], inv_M[12],
  83.                            inv_M[1], inv_M[5], inv_M[9], inv_M[13],
  84.                            inv_M[2], inv_M[6], inv_M[10], inv_M[14],
  85.                            inv_M[3], inv_M[7], inv_M[11], inv_M[15]);*/
  86.                
  87.                
  88.                
  89.                 p1[0] = inv_M[0] * p1[0] + inv_M[4] * p1[1] + inv_M[8] * p1[2] + inv_M[12] * p1[3];
  90.                 p1[1] = inv_M[1] * p1[0] + inv_M[5] * p1[1] + inv_M[9] * p1[2] + inv_M[13] * p1[3];
  91.                 p1[2] = inv_M[2] * p1[0] + inv_M[6] * p1[1] + inv_M[10] * p1[2] + inv_M[14] * p1[3];
  92.                 p1[3] = inv_M[3] * p1[0] + inv_M[7] * p1[1] + inv_M[11] * p1[2] + inv_M[15] * p1[3];
  93.                
  94.                 p2[0] = inv_M[0] * p2[0] + inv_M[4] * p2[1] + inv_M[8] * p2[2] + inv_M[12] * p2[3];
  95.                 p2[1] = inv_M[1] * p2[0] + inv_M[5] * p2[1] + inv_M[9] * p2[2] + inv_M[13] * p2[3];
  96.                 p2[2] = inv_M[2] * p2[0] + inv_M[6] * p2[1] + inv_M[10] * p2[2] + inv_M[14] * p2[3];
  97.                 p2[3] = inv_M[3] * p2[0] + inv_M[7] * p2[1] + inv_M[11] * p2[2] + inv_M[15] * p2[3];
  98.                
  99.                
  100.                
  101.                 //Convert them from the homogenous space into 3D space
  102.                 _p[0] = p1[0]/p1[3];
  103.                 _p[1] = p1[1]/p1[3];
  104.                 _p[2] = p1[2]/p1[3];
  105.                
  106.                 _q[0] = p2[0]/p2[3];
  107.                 _q[1] = p2[1]/p2[3];
  108.                 _q[2] = p2[2]/p2[3];
  109.                
  110.                 //printf("p =\n[%.2f]\n[%.2f]\n[%.2f]\n[%.2f]\n", _p[0], _p[1], _p[2], _p[3]);
  111.                 //printf("q =\n[%.2f]\n[%.2f]\n[%.2f]\n[%.2f]\n", _q[0], _q[1], _q[2], _q[3]);
  112.  
  113.  
  114.         }