Search code examples
c#windows-phone-7xnavertex-buffer

My XNA app doesn't render correctly


I'm trying to render a triangle using custom vertex data in XNA, but the output is totally messed up:

enter image description here

My GPU (Radeon HD7610M) supports DX11.

Either I am doing something wrong or it's my GPU drivers.

Here's my code:

public class MyGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    VertexBuffer vertexBuffer;
    VertexPositionColor[] vertices;
    BasicEffect effect;

    public MyGame()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        TargetElapsedTime = TimeSpan.FromTicks(333333);
        InactiveSleepTime = TimeSpan.FromSeconds(1);
    }
    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        effect = new BasicEffect(GraphicsDevice);
        effect.VertexColorEnabled = true;

        vertices = new VertexPositionColor[]
        {
            new VertexPositionColor(new Vector3(-0.8F, -0.8F, 0), Color.Black),
            new VertexPositionColor(new Vector3(-0.8F,  0.8F, 0), Color.Black),
            new VertexPositionColor(new Vector3( 0.8F, -0.8F, 0), Color.Black),
            //new VertexPositionColor(new Vector3( 0.8F,  0.8F, 0), Color.Black),
        };

        vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData<VertexPositionColor>(vertices);
    }

    protected override void UnloadContent() {}

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        GraphicsDevice.SetVertexBuffer(vertexBuffer);

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
        }

        base.Draw(gameTime);
    }
}

I'm fairly new to XNA. Am I doing something wrong?


Solution

  • Fixed it.

    I had to either set the GDM to fullscreen:

    graphics.IsFullScreen = true;
    

    Or set the dimensions of the back buffer explicitly:

    graphics.PreferredBackBufferWidth = width;
    graphics.PreferredBackBufferWidth = height;