Ray tracing question

Started by
4 comments, last by daniel_i_l 18 years ago
I got the normalized direction of my ray like this:

VERTEX GetRay()
{
  VERTEX P1,P2,direction;
  double length=0;
   GLdouble modelMatrix[16];
   GLdouble projMatrix[16];
   GLint viewport[4];
   GLdouble objx,objy,objz;
  
  glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
  glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
  glGetIntegerv(GL_VIEWPORT,viewport);
  
  gluUnProject(mouse_x,
               viewport[3] - mouse_y,
               0,
               modelMatrix,
               projMatrix,
               viewport,
               &objx,
               &objy,
               &objz);

  
  P1.x = objx;  P1.y = objy;  P1.z = objz;
  
  gluUnProject(mouse_x,
               viewport[3] - mouse_y,
               1,
               modelMatrix,
               projMatrix,
               viewport,
               &objx,
               &objy,
               &objz);
  
  P2.x = objx;  P2.y = objy;  P2.z = objz;
  direction.x = P2.x - P1.x;
  direction.y = P2.y - P1.y;
  direction.z = P2.z - P1.z;
  
  length = sqrt(direction.x*direction.x + direction.y*direction.y + direction.z*direction.z);
  
  direction.x /= length;
  direction.y /= length;
  direction.z /= length;
  
  return(direction);
}

I wanted to see the ray so I did this:

glLoadIdentity();
 glDisable(GL_TEXTURE_2D);
 Camera.Look();
 glLineWidth(5.0f);
 glColor3f(1,1,1);
 glBegin(GL_LINES);
  glVertex3f(Camera.PositionX(), Camera.PositionY(), Camera.PositionZ() );
  glVertex3f(Camera.PositionX() +  1000*GetRay().x,
             Camera.PositionY() +  1000*GetRay().y, 
             Camera.PositionZ() +  1000*GetRay().z); 
 glEnd();
 glLineWidth(1.0f);

But I didn't see any line? How do I get the line and did I calculate the ray correctly(camera + direction)? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
I didn't look at the code at all, but keep in mind that if you try to render a picking ray from the point of view of the camera, it will only show up as a point (if it shows up at all). So this probably isn't the best way to test your picking function.
Then how should I test it? (could someone check the code maybe?)
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Quote:Original post by daniel_i_l
Then how should I test it?
One way is to set up a 'dummy' camera in addition to your 'actual' camera, and compute and draw the frustum for this camera. Then take the mouse position, use it to construct a pick ray given the parameters of the 'dummy' camera, and draw the ray (this is how I tested my picking code).
Thanks, I have another question, after getting the direction (which I think is right) do I add it to the camera position to get the ray, the camera view position, or something else? - Shouldn't the view direction have something to do with it?
Thanks.

[Edited by - daniel_i_l on April 3, 2006 3:48:34 AM]
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
I think I solved the problem! I was using gluLookAt and forgot to call it before getting the matrixes.
Thanks for the help!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement