MattLance
MattLance

Reputation: 43

Multiple Models with XNA

hi first i want you to know, that i'm new to XNA.

I created a XNA Application for a Surface. In my App i'm drawing a fbx Model. My Problem is, that i don't know how to draw more than one Model. Here is my code, so you can see how i am drawing:

protected override void Draw(GameTime gameTime)
    {
        if (!applicationLoadCompleteSignalled)
        {
            // Dismiss the loading screen now that we are starting to draw
            ApplicationServices.SignalApplicationLoadComplete();
            applicationLoadCompleteSignalled = true;
        }

        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        rot += 0.0005f;

        foreach(Model model in modelList)
        {
            DrawModel(model);
        }

        graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

        base.Draw(gameTime);
    }

    private void DrawModel(Model myModel)
    {
        foreach (ModelMesh mesh in myModel.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();  // Beleuchtung aktivieren
                effect.World = Matrix.CreateRotationZ(rot) * worldMatrix;
                effect.View = viewMatrix;

                effect.Projection = projectionMatrix;
            }
            mesh.Draw();
        }
    }

The model which is displayed first is only a plane. Now i want to add a cube, so i am calling this in my Update:

if(Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
        {
            LoadModel("cube");
        }

LoadModel(String) just loads the Model and adds it to the modelList. But the cube is not displayed.

Thanks for helping me out.

Upvotes: 0

Views: 1021

Answers (1)

user1722351
user1722351

Reputation:

Looks like in the above code snippet, there are no errors. It would be nice to look at the method LoadModel.

While only advice:

  1. Look in debug mode what is happening: the cube does not exist at all (not loaded) or whether it was loaded, but not drawn. Maybe it's the shader or object correctly positioned relative to the camera.

  2. Not the best option so just call a method on the condition that the button is pressed. In this case, it will run several times while you hold the button as the check occurs very often (typically 60 times per second). Would have to add a flag or check if they are certainly not in the method LoadModel.

EDIT

The code seems correct but there is some strange in this situation. If you call your LoadModel method you should have an exception if something wrong with model or null in your collection. I think that in some reason your method not execute at all. Try load your model it in LoadContent methot directly, then try call LoadModel from LoadContent.

Hint: separate code lines. For example use

Model model = Content.Load<Model>(name);
modelList.Add(model);

instead of

modelList.Add(Content.Load<Model>(name));

It makes much easy debug your code.

Upvotes: 1

Related Questions