XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Drawing a model in front of the camera.

Last post 11/2/2009 8:01 PM by Eckish. 6 replies.
  • 11/2/2009 5:53 PM

    Drawing a model in front of the camera.

    Hi,

    I would like to draw a little widget for my camera (like the one in the top right corner in the Unity editor, a 3D model of a square with cones sticking out) but I am not sure about how to go about implementing it. I would like it to always be in front of all of the models, and to always stay in the top right corner of the screen. Should I do this by using some sort of special view matrix? or maybe by using a special World matrix. I have tried using Matrix.CreateLookAt(cameraPosition, widgetPosition, Up) instead of just my camera.ViewMatrix, but this give a weird result where the model spins uncontrollably whenever the camera moves, and the model is also drawn in the wrong place. Has anyone got any suggestions?
  • 11/2/2009 6:28 PM In reply to

    Re: Drawing a model in front of the camera.

    You would want both a special world and special view matrix. 

    The view matrix is just going to be the identity matrix (so, technically, you can omit it if your shader will allow it).  This will ensure that "camera" never moves and the object will always draw in the same place assuming the same world matrix everytime. 

    The world matrix will be the normal scale, rotation and translation.  However, translation and scale will remain static.  Once you have it positioned how you want it, you just need to do the rotations according to the camera position.

    Finally, you want to draw it last and with the depth buffer turned off.  This will ensure that it gets drawn on top of everything and will look like a piece of the UI rather than a free floating cube in your scene.
  • 11/2/2009 7:02 PM In reply to

    Re: Drawing a model in front of the camera.

    To make it render on top of everything else, I'd try clearing the z-buffer at the end, and then rendering your widget after everything else.
  • 11/2/2009 7:12 PM In reply to

    Re: Drawing a model in front of the camera.

    Thanks for the quick reply. I have tried setting the view matrix to Matrix.Identity and then I have tried altering the position and scale through the world matrix but I am not sure that this is the way to go. When I move my cube to the right it seems to rotate because of the perspective. And it is hard getting the widget positioned properly. Also I'm not sure I understand how to do the rotation according to the camera. Do I need to set the rotation so the cube is facing the camera? Is there possibly a different approach, maybe involving a view matrix that handles the rotation of the cube? Or is the way stated above the best method to go with?
  • 11/2/2009 7:23 PM In reply to

    Re: Drawing a model in front of the camera.

    Instead of clearing the Z buffer, you can use the ZMin and ZMax values of the Viewport.

    When rendering the scene, set ZMin to 0.01 and ZMax to 0.99.
    Then, when rendering the skybox, set ZMin to 0.99 and ZMax to 1.00. That way, the skybox will be "behind" everything else.
    Then, when rendering your UI widgets, set ZMin to 0 and ZMax to 0.01. That way, the UI widgets will be "in front of" everything else. (Note that the Z resolution will be poor, so you probably want to set the "near" value to 10 meters, and render all UI widgets 11 meters out from the camera)

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 11/2/2009 7:53 PM In reply to

    Re: Drawing a model in front of the camera.

    Ok, I think I have found a solution to the positioning and rotating the widget. I can't take credit for this solution however as I am just modifying some code I found on the SunBurn forums. The code I am using is as follows:

    Matrix camera_aligned_world; 
    Matrix viewToWorld = Matrix.Invert(camera.ViewMatrix); 
     
    camera_aligned_world = bones[mesh.ParentBone.Index] * World; 
    camera_aligned_world.Translation = camera.position + camera.forward; 
     
    Vector3 offset = new Vector3(0.9f, 0.6f, -1f); 
    camera_aligned_world.Translation = camera_aligned_world.Translation + Vector3.TransformNormal(offset, viewToWorld); 
     
    effect.World = camera_aligned_world; 
    effect.View = camera.ViewMatrix; 
    effect.Projection = camera.ProjectionMatrix; 

    I will look into modifying the Z Buffer now. Thanks for all your reply's!
  • 11/2/2009 8:01 PM In reply to

    Re: Drawing a model in front of the camera.

    bleh, I just typed out an explanation of a solution for you and the forums ate it =(

    So, here is the quick and dirty if your previous post doesn't work out for you.

    The camera's transformation can be done in world space by doing the exact opposite transformation.  Since we only need the rotation of the camera (the translation and scale are fixed based on where you want the box to appear), you just need to get a copy of the camera's view matrix, invert it and set its translation to zero.
Page 1 of 1 (7 items) Previous Next