• Advertisement


This topic is now archived and is closed to further replies.

Creating a line in perspective mode that points into the screen?

This topic is 5514 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

This one is a bit hard to explain so I''ll do the best I can. I want to select triangles in perspective mode. In order to do that I want to project a ray using the camera matrix, so that when drawn in perspective mode, you will only see a pixel drawn. This means the the starting and ending point of the ray is perpendicular to the screen. Usually the ray would move towards the center of the screen the further the point was away due to the perspective, but I don''t want it to do that. If I could somehow reverse the effect of the projection matrix so that instead of the point moving towards the center of the screen it would move away from the center of the screen. This would give me a ray that when I picked a point on the screen I would get a resulting ray that would interesct a triangle . I know that gluUnProject works when you only want to do it with a single point tangent to your view, but I can''t get it to do a second point that is suppose to go into the screen a certain distance. I hope that made sense to someone .

Share this post

Link to post
Share on other sites
Just to add a little bit of detail, I would do it like this if I had the right ProjectionMatrix

Step 1:
Multiply the Start and End point of my ray in world space with my Cameras inverse matrix.

Step 2:
Now that the ray is relative to the Camera, I can multiply the Start and End points with a Projection matrix that makes vertices move away from the center of the screen instead of towards when the vertex is moving away.

Step 3:
Multiply the newly projected Start and End points of the ray by the Camera matrix.

When you draw the Start and End points they should only be a pixel on the screen.

Is the near plane the width and height of the viewport?

Share this post

Link to post
Share on other sites
Here is the source code I had written a while back and just gave up on because it wouldn't work. I don't know how to make the projection matrix have the reverse effect I mention above in my other post.

This function should create a point in world space that will render right on top
of your mouse cursor

This function, when it's working, is good for selecting objects in world space in
perspective mode.

S3Dvoid s3d_camera::ScreenToWorld(S3Dfloat xin, S3Dfloat yin, S3Dfloat zin, S3Dfloat* xout, S3Dfloat* yout, S3Dfloat* zout)
S3Dmat16f CameraMat;
S3Dmat16f CameraInverseMat;
S3Dmat16f ProjectionInversMat;
S3Dvec3f VecUp;
S3Dvec3f VecForward;
S3Dvec3f VecRight;
S3Dvec4f Pos;
S3Dint ViewPort[4];

glGetIntegerv(GL_VIEWPORT, ViewPort);


//flip y

//orientate point relative to the center of the viewport

//retrieve camera information

//set up vectors that will eventually be added to Pos
s3dVecMultiplyScalar3f(VecRight, xin);
s3dVecMultiplyScalar3f(VecUp, yin);
s3dVecMultiplyScalar3f(VecForward, zin);

//orientate Pos
s3dVecAdd3f(Pos, VecRight);
s3dVecAdd3f(Pos, VecUp);
s3dVecAdd3f(Pos, VecForward);

I probably need to add the near plane distance to Pos too...I dunno yet

glGetFloatv(GL_PROJECTION_MATRIX, ProjectionInversMat);


//invert the camera matrix

s3dMatCopy16f(CameraInverseMat, CameraMatrix);

//move the Pos into camera space

s3dMatMultiply4x16f(Pos, CameraInverseMat);

//do a reverse projection

s3dMatMultiply4x16f(Pos, ProjectionInversMat);

//move the Pos back into world space

s3dMatMultiply4x16f(Pos, CameraMatrix);


Entering 0.0f in zin should result in a point lying right on the near plane. Entering the far plane distance into zin should result in a point perpendicular to the view while also lying directly on the far plane. xin and yin are the x and y position of the mouse cursor in window coordinates.

[edited by - WhatEver on January 19, 2003 9:29:17 PM]

Share this post

Link to post
Share on other sites

  • Advertisement