Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Ray Casting Issues

This topic is 2758 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys so I have been having some issues with my game recently regarding ray casting. I have been working on an rts game and now I have been trying to be able to use my mouse inorder to select units and then tell them where to go. So after researching this I cam to this code.
// This function will find 2 points in world space that are on the line into the screen defined by screen-space( ie. window-space ) point (x,y)
double mvmatrix[16];
double projmatrix[16];
int viewport[4];
double dX, dY, dZ, dClickY; // glUnProject uses doubles, but I'm using floats for these 3D vectors

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
dClickY = double (stateInfo.windowHeight - y); // OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top

gluUnProject ((double) x, dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
Vector3f ClickRayP1 = Vector3f ( (float) dX, (float) dY, (float) dZ );
gluUnProject ((double) x, dClickY, 1.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
Vector3f ClickRayP2 = Vector3f ( (float) dX, (float) dY, (float) dZ );
std::cout<<"Ray 1 ("<<ClickRayP1.x<<","<<ClickRayP1.y<<","<<ClickRayP1.z<<")"<<std::endl;
std::cout<<"Ray 2 ("<<ClickRayP2.x<<","<<ClickRayP2.y<<","<<ClickRayP2.z<<")"<<std::endl;

So I understand what is going on here but I am having some issues. To start, the matrixes remain the same even after I move the camera around, so the points I get back are the same when I move the camera, and I cant figure out why. Further more, everything in my game is drawn on the XZ plane, with Y = 0, so how do I get the Z value information for this plane from clicking. I dont know if its useful but I will include the display function too cause it shows the matrix stuff.

void DisplayCallbackFunction(void)
/*clear the screen*/
/*draw the game*/
GLuint texture;
//draw in 2d
glOrtho(0.0, WINDOW_SCREEN_WIDTH,0.0, WINDOW_SCREEN_HEIGHT, -1.0, 10.0);

/*display buffer*/

any help will be greatly appreciated as this project is approaching its due date.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!