Desperate problem with ray tracing / line shooting


I am somehow stuck, and I can’t get any further searching the news

The problem:

I just want to shoot a straight line through an object under the current cursor position.

The set up:

I use

gluPerspective(45,(w / h),0.1,1000);
and the camera is with gluLookAt
0,0,70 facing the 0,0,0 (orgin);

The window is 300 * 300 pixel (w, h);

I tried using the gluUnProject() function as described in previous posts put somehow does this not work. I am as well not sure how to convert the actual mouse coords into screen coord at my current position. (I tried to find out the z-heigth at my camera position with tan and mapped it to the realtive mouse coords):

float zCoord = tan(PI/8) * pCamera->vActualCamPosition.z; vMouse.x = (float)zCoord/(iWindowWidth/2) * (float)( x -
vMouse.y = (float)zCoord/(iWindowHeight/2) * (float)((iWindowHeight/2)

  • y);
    vMouse.z = pCamera->vActualCamPosition.z;

or is this complete nonsense?

I would very appreciate if someone could help me.

Thanks a lot in advance


Here is a routine that I use convert the mouse to x/y pixels to openGL world coords.

Works best in ortho mode, the number 18 is the width and hight based on the ortho settings.
// My ortho for the example
glOrtho(-9.0, 9.0, -9.0, 9.0, 0.0, 30.0); // Setup an Ortho view

Now perspective is even harder since the window is not a cube like in ortho.

void mouse(int button, int state, int x, int y)
// We convert windows mouse coords to out openGL coords
mouse_x = (18 * (float) ((float)x/(float)Win_x));
mouse_y = (18 * (float) ((float)y/(float)Win_y));

Note Win_x and Win_y are current window hight and width.

Hope this helps.

[This message has been edited by nexusone (edited 04-30-2003).]