Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

combining 2d and 3d objects in XNA 3.1

$
0
0
I was going through tutorial of xna 4 of combining 2d and 3d objects like making 2d background and 3d object, so i found a code
GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
i used this code in my program after 2d drawing and before 3d drawing to turn on depth buffer.
since my code was on xna 3.1 i got error as
Please also suggest some tips on moving a camera behind objects.

Error	1	'Microsoft.Xna.Framework.Graphics.GraphicsDevice' does not contain a definition for 'DepthStencilState' and no extension method 'DepthStencilState' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.GraphicsDevice' could be found (are you missing a using directive or an assembly reference?)	

Error	2	The type or namespace name 'DepthStencilState' could not be found (are you missing a using directive or an assembly reference?)	



I know DepthStencilState is only used in xna 4,so please suggest some alternate ways to combining 2d and 3d objects in XNA 3.1
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 WindowsGame3D1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Model helicopterModel;
        private float mainRotorAngle = 0;
        private float tailRotorAngle = 0;
        private Vector3 position = new Vector3(0, 0, 0);
        private float angle = 0f;
        private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
        private Matrix view = Matrix.CreateLookAt(new Vector3(10, 10, 10), new Vector3(0, 0, 0), Vector3.UnitY);
        private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
        private Texture2D background;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            background = Content.Load<Texture2D>("Build");
            helicopterModel = Content.Load<Model>("Helicopter");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            KeyboardState state = Keyboard.GetState();
            bool Space = state.IsKeyDown(Keys.Space);
            bool Up = state.IsKeyDown(Keys.Up);
            bool down = state.IsKeyDown(Keys.Down);
            // TODO: Add your update logic here
            if (state.IsKeyDown(Keys.Space))
            {
                tailRotorAngle -= 0.5f;
                mainRotorAngle -= 1.0f;

                angle += 0.0199f;
                position += Vector3.Transform(new Vector3(0.1f, 0, 0), Matrix.CreateRotationY(MathHelper.ToRadians(90) + angle));
            }
            else
            {
                tailRotorAngle -= 0.0f;
                mainRotorAngle -= 0.1f;

                angle += 0.02f;
                position += Vector3.Transform(new Vector3(0.0f,-0.09f, 0), Matrix.CreateRotationY(MathHelper.ToRadians(90) + angle));
            }
            // TODO: Add your update logic here
            if (state.IsKeyDown(Keys.Up) && state.IsKeyDown(Keys.Space))
            {
                tailRotorAngle -= 0.5f;
                mainRotorAngle -= 1.0f;

                angle += 0.02f;
                position += Vector3.Transform(new Vector3(0.1f, 0.1f, 0), Matrix.CreateRotationY(MathHelper.ToRadians(90) + angle));
            }
            if (state.IsKeyDown(Keys.Down) && state.IsKeyDown(Keys.Space))
            {
                tailRotorAngle -= 0.5f;
                mainRotorAngle -= 1.0f;

                angle += 0.02f;
                position += Vector3.Transform(new Vector3(0.1f, -0.1f, 0), Matrix.CreateRotationY(MathHelper.ToRadians(360) + angle));
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
            spriteBatch.End();
            GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
            window.AllowUserResizing = true;
            // TODO: Add your drawing code here
            Matrix[] meshWorldMatrices = new Matrix[3];
            meshWorldMatrices[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0));
            meshWorldMatrices[1] = Matrix.CreateRotationY(mainRotorAngle);
            meshWorldMatrices[2] = Matrix.CreateTranslation(new Vector3(0, -0.25f, -3.4f)) * 
                                            Matrix.CreateRotationX(tailRotorAngle) * 
                                            Matrix.CreateTranslation(new Vector3(0, 0.25f, 3.4f));
 
            world = Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);
            
            DrawModel(helicopterModel, world, meshWorldMatrices, view, projection);
            base.Draw(gameTime);
        }
        private void DrawModel(Model model, Matrix objectWorldMatrix, Matrix[] meshWorldMatrices, Matrix view, Matrix projection)
        {
            for (int index = 0; index < model.Meshes.Count; index++)
            {
                ModelMesh mesh = model.Meshes[index];
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.EnableDefaultLighting(); 
                    effect.PreferPerPixelLighting = true;
                    effect.World = mesh.ParentBone.Transform * meshWorldMatrices[index] * objectWorldMatrix;
                    effect.View = view;
                    effect.Projection = projection;
                }
                mesh.Draw();
            }

        }

    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>