Question

I'm trying to learn how to create 3D shapes using primitives in XNA for a project at uni. I have found several tutorials on the subject and I think i have the basics down, but as I've begun to make a start on my own code for the project I've run into an issue. To begin with I was trying to create just a single triangle, code below:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    VertexPositionColor[] vertices;
    VertexBuffer vertexBuffer;
    BasicEffect effect;
    Camera camera;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    public void setUpVertices()
    {
        vertices=new VertexPositionColor[3];
        vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Blue);
        vertices[1] = new VertexPositionColor(new Vector3(1, -1, 0), Color.Red);
        vertices[2]= new VertexPositionColor(new Vector3(-1,-1,0),Color.Green);
    }
    protected override void Initialize()
    {
        camera = new Camera(this, new Vector3(0, 0, 5),Vector3.Zero, Vector3.Up);
        Components.Add(camera);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        setUpVertices();
        vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.None);
        vertexBuffer.SetData(vertices);
        effect = new BasicEffect(GraphicsDevice);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        GraphicsDevice.SetVertexBuffer(vertexBuffer);
        effect.World = Matrix.Identity;
        effect.View = camera.view;
        effect.Projection = camera.projection;
        effect.VertexColorEnabled = true;
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);
        }

        base.Draw(gameTime);
    }
}

The code itself compiles but no triangle is rendered on screen and I can't figure out why.

I have followed the tutorial, which says the triangle should now be appearing on screen, correctly (I've checked several times) and now I'm at a loss with what to do.

Any help or advice would be greatly appreciated as I'm really struggling with using triangle strips.

NOTE: the camera class makes a simple 3D static camera pointing at the origin 7

EDIT
Code for the camera's constructor

public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
: base(game)
{
    // TODO: Construct any child components here
    projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,(float)Game.Window.ClientBounds.Width /(float)Game.Window.ClientBounds.Height,1, 100);
}
Was it helpful?

Solution

I can't see camera.view in your Camera constructor but you use it: effect.View = camera.view;. Maybe you haven't set that variable?

OTHER TIPS

Maybe it's a culling problem, try adding:

GraphicsDevice.RasterizerState = RasterizerState.CullNone;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top