Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't get my 3D model to display, I think it might have to do with the draw order inside of my Draw method. The part I'm working on is the aircraft selection menu I'm making for a flight sim.
Here's my code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Thin_Air
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //------------------------------------------------------------------------------------------------------\
        GameState state;
        Model airCraft;
        float modelRotation, aspectRatio;
        Vector3 modelPosition, cameraPosition, pos;
        int iii, mouseX, mouseY;
        string[] info;
        SpriteFont font;
        KeyboardState kbState_Old, kbState_New;
        MouseState mouseState;
        SelectedAirCraft selectedObject;
        Texture2D airCraftSelectBG, selectionSquare, selectionSquare_Hover, displayPanel;
        Song introMusic;
        bool hit;
        public enum SelectedAirCraft
        {
            EuroFighter,
            Craft2,
            Craft3
        }
        public enum GameState
        {
            MainMenu,
            AreaSelect,
            AirCraftSelect,
            Flying,
            EndFlight,
            Pause
        }
        public void Draw_AirCraftSelection_Background()
        {
            spriteBatch.Draw(airCraftSelectBG, new Rectangle(0, 0, 800, 600), Color.White);
        }
        public void Draw_AirCraftSelection_DisplayPanel()
        {
            spriteBatch.Draw(displayPanel, new Rectangle(0, 400, 800, 200), Color.White);
        }
        public void Draw_AirCraftSelection_Models(Vector3 pos)
        {
            Matrix[] transforms = new Matrix[airCraft.Bones.Count];
            airCraft.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in airCraft.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] *
                        Matrix.CreateRotationY(modelRotation) *
                        Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition, pos, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),aspectRatio,1.0f,10000.0f);
                }
                mesh.Draw();
            }
        }
        //------------------------------------------------------------------------------------------------------/
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //--------------------------------------------------------------------------------------------------\
            this.IsMouseVisible = true;
            state = GameState.AirCraftSelect;
            selectedObject = SelectedAirCraft.Craft2;
            pos = new Vector3((float)iii, 0, 0);
            info = new string[5];
            iii = -100;
            hit = false;
            cameraPosition = new Vector3(0.0f, 150.0f, 180.0f);
            modelRotation = 0.0f;
            modelPosition = Vector3.Zero;
            //--------------------------------------------------------------------------------------------------/
        }
        protected override void Initialize()
        {
            //--------------------------------------------------------------------------------------------------\
            kbState_Old = Keyboard.GetState();
            for (int i = 0; i < 5; i++)
                info[i] = "...";
            //--------------------------------------------------------------------------------------------------/
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            //--------------------------------------------------------------------------------------------------\
            airCraft = Content.Load<Model>("Models\\eurofighter_fbx");
            airCraftSelectBG = Content.Load<Texture2D>("Backgrounds\\nebo004");
            selectionSquare = Content.Load<Texture2D>("AirCraftSelect\\SelectedAirCraft");
            selectionSquare_Hover = Content.Load<Texture2D>("AirCraftSelect\\SelectedAirCraft_Hover");
            displayPanel = Content.Load<Texture2D>("AirCraftSelect\\Panel");
            introMusic = Content.Load<Song>("Audio\\Intro\\Game_Intro_Music");
            font = Content.Load<SpriteFont>("Fonts\\SpriteFont1");
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
            //--------------------------------------------------------------------------------------------------/
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            //--------------------------------------------------------------------------------------------------\
            mouseState = Mouse.GetState();
            mouseX = mouseState.X;
            mouseY = mouseState.Y;
            switch (state)
            {
                case GameState.MainMenu:

                    break;
                case GameState.AreaSelect:

                    break;
                case GameState.AirCraftSelect:
                    kbState_New = Keyboard.GetState();
                    if (kbState_New.IsKeyDown(Keys.D))
                    {
                        if (!kbState_Old.IsKeyDown(Keys.D))
                        {

                        }
                    }
                    else if (kbState_Old.IsKeyDown(Keys.D))
                    {
                        if (iii <= -100)
                            iii = 100;
                        else
                            iii -= 100;
                        pos = new Vector3(iii, 0, 0);
                        switch (selectedObject)
                        {
                            case SelectedAirCraft.Craft2:
                                info[0] = "Name: Craft2";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                            case SelectedAirCraft.Craft3:
                                info[0] = "Name: Craft3";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                            case SelectedAirCraft.EuroFighter:
                                info[0] = "Name: EuroFighter";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                        }
                        hit = true;
                    }
                    if (kbState_New.IsKeyDown(Keys.A))
                    {
                        if (!kbState_Old.IsKeyDown(Keys.A))
                        {

                        }
                    }
                    else if (kbState_Old.IsKeyDown(Keys.A))
                    {
                        if (iii >= 100)
                            iii = -100;
                        else
                            iii += 100;
                        pos = new Vector3(iii, 0, 0);
                        switch (selectedObject)
                        {
                            case SelectedAirCraft.Craft2:
                                info[0] = "Name: Craft2";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                            case SelectedAirCraft.Craft3:
                                info[0] = "Name: Craft3";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                            case SelectedAirCraft.EuroFighter:
                                info[0] = "Name: EuroFighter";
                                info[1] = "Top Speed: 0";
                                info[2] = "Type: 0";
                                info[3] = "Weapon: 0";
                                info[4] = "Good For: 0";
                                break;
                        }
                        if (!hit)
                            hit = true;
                    }
                    switch (iii)
                    {
                        case 100:
                            selectedObject = SelectedAirCraft.Craft2;
                            break;
                        case 0:
                            selectedObject = SelectedAirCraft.EuroFighter;
                            break;
                        case -100:
                            selectedObject = SelectedAirCraft.Craft3;
                            break;
                    }
                    kbState_Old = kbState_New;//reset
                    modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.03f);
                    break;
                case GameState.Flying:

                    break;
                case GameState.Pause:

                    break;
                case GameState.EndFlight:

                    break;
            }
            //--------------------------------------------------------------------------------------------------/
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            //--------------------------------------------------------------------------------------------------\
            spriteBatch.Begin();
            switch (state)
            {
                case GameState.MainMenu:

                    break;
                case GameState.AreaSelect:

                    break;
                case GameState.AirCraftSelect:
                    Draw_AirCraftSelection_Background();
                    Draw_AirCraftSelection_Models(pos);
                    //Draw_AirCraftSelection_DisplayPanel();
                    
                        spriteBatch.DrawString(font, info[0], new Vector2(50, 620), Color.White);
                        spriteBatch.DrawString(font, info[1], new Vector2(50, 640), Color.White);
                        spriteBatch.DrawString(font, info[2], new Vector2(50, 660), Color.White);
                        spriteBatch.DrawString(font, info[3], new Vector2(50, 680), Color.White);
                        spriteBatch.DrawString(font, info[4], new Vector2(50, 700), Color.White);
                    
                    if(!hit)
                    {
                        spriteBatch.DrawString(font, "Welcome", new Vector2(50, 620), Color.White);
                    }
                    
                    if ((mouseX >= 300 && mouseX <= 500) && (mouseY >= 200 && mouseY <= 400))
                    {
                        spriteBatch.Draw(selectionSquare_Hover, new Rectangle(300, 200, 200, 200), Color.White);
                        if (mouseState.LeftButton == ButtonState.Pressed)
                            state = GameState.Flying;
                    }
                    else
                    {
                        spriteBatch.Draw(selectionSquare, new Rectangle(300, 200, 200, 200), Color.White);
                    }
                    break;
                case GameState.Flying:

                    break;
                case GameState.Pause:

                    break;
                case GameState.EndFlight:

                    break;
            }
            spriteBatch.End();
            //--------------------------------------------------------------------------------------------------/
            base.Draw(gameTime);
        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900