Horde3D
http://www.horde3d.org/forums/

Calculating window coordinates from world coordinates
http://www.horde3d.org/forums/viewtopic.php?f=8&t=1652
Page 1 of 1

Author:  nuschii [ 18.03.2012, 15:20 ]
Post subject:  Calculating window coordinates from world coordinates

Hey,

wanted to create a simple GameEngine component which could be used to show some sort of "gamer tag". Technically spoken, it should be able to display some overlay text exactly at the point where a GameEntity is currently rendered.

Image

For this purpose, I looked for a function within Horde3D to calculate window coordinates from given world coordinates, but couldn't find any. In Horde3DUtils, there are functions to pick a node at given window coordinates (PickRay, PickNode). So, I simply turned the PickRay function around:

Code:
DLLEXP void h3dutWorldToWindowCoordinates( H3DNode cameraNode, float x, float y, float z, float *nwx, float *nwy )
{
   // Get projection matrix
   Matrix4f projMat;
   h3dGetCameraProjMat( cameraNode, projMat.x );
   
   // Get camera view matrix
   const float *camTrans;
   h3dGetNodeTransMats( cameraNode, 0x0, &camTrans );      
   Matrix4f viewMat( camTrans );
   viewMat = viewMat.inverted();

   // Project
   Vec4f p = (projMat * viewMat) * Vec4f(x, y, z, 1);

   if (p.z == 0) {
      *nwx = Math::NaN;
      *nwy = Math::NaN;
   } else {
      p.x /= p.z;
      p.y /= p.z;

      // Transform from normalized device [-1, 1] to normalized window [0, 1] coordinates
      *nwx = 0.5f * p.x + 0.5f;
      *nwy = 0.5f * p.y + 0.5f;
   }
}


Using this, the text label can be displayed, for example, like this in the application's render function:
Code:
// get entity's world transformation
const float* trans = GameEngine::getEntityTransformation(GameEngine::entityWorldID("Player1"));

float wx, wy;

// calculate current window coordinates
h3dutWorldToWindowCoordinates(GameEngine::entitySceneGraphID(m_camID), trans[12], trans[13] + 2.5f, trans[14], &wx, &wy);

// show text, taking window aspect ratio into account
h3dutShowText( "Player1", wx * 4.0f/3.0f, 1.0f - wy, 0.04f, 0, 1.0f, 0.4f, _fontMatRes );


Now, is there some other way to achieve this (some way I missed in the Horde3D API)?
Particularly, is there some way to retrieve window coordinates from vertex shaders' output? This would be much more efficient than projecting the world coordinates on the CPU.

If the answer to these questions is "no" and "no", then I would propose to add the h3dutWorldToWindowCoordinates to Horde3DUtils. I think some users could make good use of it.

Best regards,

Christoph

Author:  marciano [ 20.03.2012, 00:33 ]
Post subject:  Re: Calculating window coordinates from world coordinates

This projection is best done on the CPU, in general reading back data from the GPU is always problematic, as CPU and GPU run asynchronously for performance reasons.

It makes definitely sense to add a projection function to the Utils. In case you missed it, there is also a wiki entry which does basically the same (but divides by w which is more correct).

Author:  nuschii [ 26.03.2012, 15:13 ]
Post subject:  Re: Calculating window coordinates from world coordinates

Thanks, I wasn't aware of this wiki entry. You're right, dividing by w is a better solution. Also, checking for back projection is definitely not a bad idea. :roll:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/