Why is my primitive xna square not drawn/shown?

Posted by Mech0z on Game Development See other posts from Game Development or by Mech0z
Published on 2012-04-05T18:23:35Z Indexed on 2012/04/05 23:43 UTC
Read the original article Hit count: 233

Filed under:
|
|

I have made this class to draw a rectangle, but I cant get it to be drawn, I have no issues displaying a 3d model created in 3dmax, but shown these primitives seems much harder

I use this to create it

board = new Board(Vector3.Zero, 1000, 1000, Color.Yellow);

And here is the implementation

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Shapes;
using Quadro.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Quadro
{
    public class Board : IGraphicObject
    {
        //Private Fields
        private Vector3 modelPosition;
        private BasicEffect effect;
        private VertexPositionColor[] vertices;
        private Matrix rotationMatrix;
        private GraphicsDevice graphicsDevice;
        private Matrix cameraProjection;

        //Constructor
        public Board(Vector3 position, float length, float width, Color color)
        {
            var _color = color;

            vertices = new VertexPositionColor[6];
            vertices[0].Position = new Vector3(position.X, position.Y, position.Z);
            vertices[1].Position = new Vector3(position.X, position.Y + width, position.Z);
            vertices[2].Position = new Vector3(position.X + length, position.Y, position.Z);
            vertices[3].Position = new Vector3(position.X + length, position.Y, position.Z);
            vertices[4].Position = new Vector3(position.X, position.Y + width, position.Z);
            vertices[5].Position = new Vector3(position.X + length, position.Y + width, position.Z);

            for(int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Color = color;
            }

            initFields();
        }

        private void initFields()
        {
            graphicsDevice = SharedGraphicsDeviceManager.Current.GraphicsDevice;
            effect = new BasicEffect(graphicsDevice);
            modelPosition = Vector3.Zero; 

            float screenWidth = (float)graphicsDevice.Viewport.Width;
            float screenHeight = (float)graphicsDevice.Viewport.Height;
            float aspectRatio = screenWidth / screenHeight;
            this.cameraProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
            this.rotationMatrix = Matrix.Identity;
        }

        //Public Methods
        public void Update(GameTimerEventArgs e)
        {

        }

        public void Draw(Vector3 cameraPosition, GameTimerEventArgs e)
        {
            Matrix cameraView = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                effect.World = rotationMatrix * Matrix.CreateTranslation(modelPosition);
                effect.View = cameraView;
                effect.Projection = cameraProjection;
                graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2, VertexPositionColor.VertexDeclaration);
            }
        }

        public void Rotate(Matrix rotationMatrix)
        {
            this.rotationMatrix = rotationMatrix;
        }

        public void Move(Vector3 moveVector)
        {
            this.modelPosition += moveVector;
        }
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about 3d