convert 3D position to 2D position

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario
Contact:

convert 3D position to 2D position

Post by Vel » Tue Mar 01, 2005 7:29 pm

can anyone tell me how i could get the position of a scenenode in 2D coordinates?

i need to determine the viewport UV position, window pixel position and screen pixel position of a mesh.

any guidance is greatly appreciated.
0 x

Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario
Contact:

Post by Vel » Wed Mar 02, 2005 3:42 am

just an update...i'm close to solving my problem...here's my function:

Code: Select all

Vector2 WorldSpaceToUV(float x, float y, float z)
{
    Matrix4 V = g_pCamera->getViewMatrix();
    Vector4 res = V * Vector4( x, y, z, 1 );

    return Vector2( out.x + 0.5, (-out.y) + 0.5 );
}
this function seems to yield the results i'm looking for, however it does not appear to take into account the camera's FOV. can anyone point me in the right direction to properly take that into account?
0 x

User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 1
Contact:

Post by Kencho » Wed Mar 02, 2005 11:04 am

I think you first need to obtain the projection matrix. If I recall correctly, the projection matrix is the responsible for close/far clipping, fov, and aspect. Then, apply the view matrix to convert 3D to 2D :)
0 x

Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario
Contact:

Post by Vel » Wed Mar 02, 2005 4:48 pm

yes, this is my finished version of the function:

Code: Select all

Vector2 WorldSpaceToUV(float x, float y, float z)
{
	Vector3 pos = cam->getProjectionMatrix() * (cam->getViewMatrix() * Vector3(x,y,z));
	return Vector2((-pos.x+1)/2,(pos.y+1)/2);
}
this function returns the screen UVs of the 3D position in space.
0 x

User avatar
Cyberdigitus
Halfling
Posts: 55
Joined: Thu Mar 04, 2004 7:08 pm
Location: Belgium
Contact:

Post by Cyberdigitus » Thu Mar 03, 2005 5:22 pm

Are you sure this works? I've been trying this too but haven't found the correct way yet, your method doesn't seem to give correct results either.
0 x
. . .

User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 1
Contact:

Post by Kencho » Thu Mar 03, 2005 8:36 pm

I think you should reverse the order of matrix multiplication. First use the projection matrix, and then de view matrix. That's the correct order (I think)
0 x

Post Reply