Draw a 3d Model on HUD with XNA

Started by
5 comments, last by kelendil87 15 years, 12 months ago
Hi all. I'm writing a game and I have some problem on how i can Draw a 3d Models over my HUD. In the world that i have created, I can explore the land trought a camera:

         Vector2 cameraRotation = new Vector2(-4.023f,0.13f);

         Vector3 cameraPosition = new Vector3(172.0f, -98.0f,224.0f);
And the update function:

            KeyboardState keys = Keyboard.GetState();

            if (keys.IsKeyDown(Keys.Up))

                cameraRotation.Y -= elapsedTime;

            if (keys.IsKeyDown(Keys.Down))

                cameraRotation.Y += elapsedTime;

            if (keys.IsKeyDown(Keys.Left))

                cameraRotation.X -= elapsedTime;

            if (keys.IsKeyDown(Keys.Right))

                cameraRotation.X += elapsedTime;



            Matrix viewRotationMatrix = Matrix.Transpose(ViewRotation);

            Vector3 cameraMovement = Vector3.Zero;

            if (keys.IsKeyDown(Keys.S))

                cameraMovement += viewRotationMatrix.Forward;

            if (keys.IsKeyDown(Keys.W))

                cameraMovement += viewRotationMatrix.Backward;

            if (keys.IsKeyDown(Keys.A))

                cameraMovement += viewRotationMatrix.Right;

            if (keys.IsKeyDown(Keys.D))

                cameraMovement += viewRotationMatrix.Left;

            cameraPosition += cameraMovement * 40.0f * elapsedTime;
This is my Draw function:

void Draw(Model model, Matrix world)

        {

            foreach (ModelMesh mesh in model.Meshes)

            {

                foreach (Effect effect in mesh.Effects)

                {

                    effect.Parameters["World"].SetValue(world);

                    effect.Parameters["View"].SetValue(View);

                    effect.Parameters["Projection"].SetValue(Projection);

                }



                mesh.Draw(SaveStateMode.SaveState);

            }

        }
I need to Draw the model in the X,Y position and when I move around with the camera, the model must "follow" me. With Texture2d there is the possibility to draw in a X,Y position, how can i do with a 3d model? Thanks
Advertisement
Well with 3D models it's much tricker to simply specify a screen-space coordinate for rendering, since typically a perspective projection is used rather than an orthographic projection. With perspective projection the projected 2D coordinate of any vertex depends on the view-space depth (z-component) of that vertex, you cant just say "draw my model at this point on the screen".

What you can do, is render your model using view-space coordinates rather that world-space coordinates. Doing this is easy: simply set your view matrix to identity. When you do this, the position you use for the translation portion of your world matrix is now in view-space, which means a position of <10,20,-5> would be 10 units to the right of the camera, 20 units above, and 5 units in front. Now like I said you don't know exactly where this will end up on your screen without transforming by the projection matrix, but you can then tweak the position to a nice value and it will always stay in the same spot regardless of where your camera is positioned or where its looking.
You can use the GraphicsDevice.Viewport.Unproject function to convert to world space, then use those coordinates to render something that gets projected to exactly where you want.

or you could try using an Orthographic projection.
Thanks a lot, this work wonderfully for me.

Here is how I have implements what you say:

void DrawModelOnHud(Model model, Matrix world)        {            foreach (ModelMesh mesh in model.Meshes)            {                foreach (Effect effect in mesh.Effects)                {                    effect.Parameters["World"].SetValue(world);                    effect.Parameters["View"].SetValue(Matrix.Identity);                    effect.Parameters["Projection"].SetValue(Projection);                }                mesh.Draw(SaveStateMode.SaveState);            }        }


And I set the world matrix as:

     Matrix.CreateScale(0.2f) *      Matrix.CreateRotationY(applicationTime) *      Matrix.CreateTranslation(new Vector3(-2.9f, -1.7f, -5.0f))


Thanks for the help ;)
Another problem. If I use a perspective projection the model result deformer. So I decide to use for it a Orthographic projection. I try to study it a bit http://msdn2.microsoft.com/en-us/library/bb195662.aspx. But to get the model in the position that i want (the screen resolution is 1280 x 1024) I need to set the translation matrix to (-580.0f, -340.0f, 0.0f). But the model result too small, so I must Scale it to 35x... Is this normal? Will this influence the game performance? Thanks
It depends original size of the model. Scaling should have no performance detriment as far as the GPU is concerned, since the scale and translation are given as a single world matrix.
Ok, thanks for the help :D

This topic is closed to new replies.

Advertisement