Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
        }
    }
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

I've tried your code and it appears to be working on an empty project for the most part. But there are a few things that could make it fail:

  1. You need to enable VertexColorEnabled on your effect otherwise it will be rendered white:

    effect.VertexColorEnabled = true;
    
  2. If you don't see the board being rendered you're probably looking at it from behind. By default, backfacing polygons are culled so they won't be drawn. Either position your camera so that you're seeing the board from the correct angle, or disable backface culling before rendering with:

    graphicsDevice.RasterizerState = RasterizerState.CullNone; 
    
  3. You should be setting your effect properties before applying the pass, not after! That could also be the source of the problem if you're rendering other objects before and after the board. And move all the effect.Property assignments that you're currently doing outside the loop. There's no meaning in them being inside the loop if the values are the same in every pass.

  4. This one is optional. Since the BasicEffect only uses one pass you can also simplify your rendering code by replacing the loop with a simple:

    effect.CurrentTechnique.Passes[0].Apply();
    
share|improve this answer
    
I dont get anything even when I add those things :/ I see a white gimplse when I run the game, so it might be rendered a single time, but not sure if thats even this object –  Mech0z Apr 5 '12 at 19:21
    
@Mech0z Can you place a copy of the current state of your class in a Pastie? You might have missed something, because it did work here. And if possible also the place where you're calling Draw too. –  David Gouveia Apr 5 '12 at 19:23
    
You can download the project here gupl.dk/677248 –  Mech0z Apr 5 '12 at 19:47
    
@Mech0z I couldn't run the project but I managed to see the code, and you didn't implement my changes correctly... What I meant for the Draw method was something like this instead: http://pastie.org/3734886. In particular you're missing step 4 of my list, which is quite important. Step 3 is optional but I added it anyway since it's more simple. –  David Gouveia Apr 5 '12 at 19:53
    
@Mech0z Also, at least in the version I downloaded I don't see where you're using the Board class. Your board object is a Model3D. –  David Gouveia Apr 5 '12 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.