Sign in to follow this  
daniel_i_l

Ray tracing question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this