Camera target

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Camera target

      Hi,

      I'm trying to figure out if there is a problem with the camera class and the SetView method in particular. In order to render an object we need the position and the orientation of the camera but if the camera is following a target its position and orientation are determined by the target object. Moreover we need the position of the target in world space. If we set some child node as a camera target how can we even render the parent??? In order to render the parent we will need the position and orientation of the camera and those are determined by the target, which happens to be the child in this situation.

      How do we know the global position of the child before it has even been rendered? Don't we need the matrix stack to determine the child's global position and orientation?

      Thanks in advance.
    • RE: Camera target

      Figuring out the word space position and orientation is a simple matter of following the matrix stack - if there are no children or parents it is just the single matrix.

      No rendering is required....
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • RE: Camera target

      Yes, but imagine the following scenario - we have one parent node (position [100, 100, 100]), which has one child node (position [200, 200, 200]), which in turn has another child node with position [300, 300, 300] - as we know the position are relative to the parent. We set the last child node as the camera targer.
      In Scene::OnRender() we have a call to m_Camera->SetView(this). SetView will try to figure out the position of the camera, based on its target. But at that moment we just have the single matrix on the top of the matrix stack so we can't figure out the global position of the target node since we haven't reached the target node in the rendering process and its PreRender() routine hasn't been executed.

      I could be missing something but the SetView() does not seem right when we have a target set at a child node. Could you please explain if I'm not getting it right.

      Great book by the way... thanks

      The post was edited 1 time, last by milann ().

    • RE: Camera target

      Ok - I think I see where the confusion is coming from. Here's the bit of code we're talking about:

      Source Code

      1. HRESULT CameraNode::SetView(Scene *pScene)
      2. {
      3. //If there is a target, make sure the camera is
      4. //rigidly attached right behind the target
      5. if(m_pTarget.valid())
      6. {
      7. Mat4x4 mat = (*m_pTarget)->VGet()->ToWorld();
      8. Vec4 at = g_Forward4 * -10.0f;
      9. Vec4 atWorld = mat.Xform(at);
      10. Vec3 pos = mat.GetPosition() + Vec3(atWorld);
      11. mat.SetPosition(pos);
      12. VSetTransform(&mat);
      13. }
      14. DXUTGetD3DDevice()->SetTransform( D3DTS_VIEW, &VGet()->FromWorld() );
      15. return S_OK;
      16. }
      Display All


      First, the CameraNode::SetView method takes a Scene as a parameter, not a scene node - this is because the camera will render an entire scene. If there is a camera target, the camera will use world transforms of the target - which are set in the SceneNodeProperties::Transform method.

      Nothing in this chain assumes a possible chain of child nodes - so your observation is entirely correct for this code base.

      To make this work one would need to make some changes - primarilly you would need to change the ToWorld and FromWorld matricies to ToParent and FromParent - and you would need to write routines that would calculate ToWorld and FromWorld - so you could grab them when you needed them. You would also have to change the render pipeline to call Camera::SetView to take a SceneNode as a parameter instead of the Scene - so that you could minimize calls to DXUTGetD3DDevice()->SetTransform( D3DTS_VIEW, &VGet()->FromWorld() );
      .

      So, you've found a missing link in the source code - even though the SceneNode::VAddChild method clearly allows scene nodes to be added as children of other scene nodes - there is no attempt at the rendering pipeline to make the correct matrix concatenations to render them properly...

      That's a little embarrassing, actually! I seem to remember thinking to myself - "gee, I'm going to have to change all of that code to make a boned character work properly....I'll get on that tomorrow..." and, well, I guess we all know what happened....
      Mr.Mike
      Author, Programmer, Brewer, Patriot