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.
Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario

convert 3D position to 2D position

Post by Vel »

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.
Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario

Post by Vel »

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?
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2

Post by Kencho »

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 :)
Vel
Gnoblar
Posts: 5
Joined: Wed Sep 17, 2003 10:23 pm
Location: Toronto, Ontario

Post by Vel »

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.
User avatar
Cyberdigitus
Halfling
Posts: 55
Joined: Thu Mar 04, 2004 7:08 pm
Location: Belgium

Post by Cyberdigitus »

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.
. . .
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2

Post by Kencho »

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)