Model won't render in my XNA game

Posted by Daniel Lopez on Game Development See other posts from Game Development or by Daniel Lopez
Published on 2011-11-25T22:50:20Z Indexed on 2011/11/26 2:09 UTC
Read the original article Hit count: 400

Filed under:
|
|
|

I am trying to create a simple 3D game but things aren't working out as they should. For instance, the mode will not display. I created a class that does the rendering so I think that is where the problem lies.

P.S I am using models from the MSDN website so I know the models are compatible with XNA.

Code:

    class ModelRenderer
    {
        private float aspectratio;
        private Model model;
        private Vector3 camerapos;
        private Vector3 modelpos;
        private Matrix rotationy;
        float radiansy = 0;
        public ModelRenderer(Model m, float AspectRatio, Vector3 initial_pos, Vector3 initialcamerapos)
        {
            model = m;
            if (model.Meshes.Count == 0)
            {
                throw new Exception("Invalid model because it contains zero meshes!");
            }
            modelpos = initial_pos;
            camerapos = initialcamerapos;
            aspectratio = AspectRatio;
            return;
        }
        public Vector3 CameraPosition
        {
            set
            {
                camerapos = value;
            }
            get
            {
                return camerapos;
            }

        }
        public Vector3 ModelPosition
        {
            set
            {
                modelpos = value;
            }
            get
            {
                return modelpos;
            }
        }
        public void RotateY(float radians)
        {
            radiansy += radians;
            rotationy = Matrix.CreateRotationY(radiansy);
        }

        public float AspectRatio
        {
            set
            {
                aspectratio = value;
            }
            get
            {
                return aspectratio;
            }
        }
        public void Draw()
        {


            Matrix world = Matrix.CreateTranslation(modelpos) * rotationy;
            Matrix view = Matrix.CreateLookAt(this.CameraPosition, this.ModelPosition, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), this.AspectRatio, 1.0f, 10000f);
            model.Draw(world, view, projection);
        }
    }

If you need more code just make a comment.

© Game Development or respective owner

Related posts about XNA

Related posts about c#