Home > C#, Game Programming, Programming, XNA > XNA – TriangleStrip

XNA – TriangleStrip

xna_logo

The idea behind TriangleStrips is that you should define each vertex only once. So for the first triangle, you have to define vertices 0,1 and 2. Now, for the next triangle, you only have to add vertex 3, XNA will always use the last 3 vertices to draw the triangle, so for the second triangle it uses vertices 1, 2 and 3, which is correct.

Untitled


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 TriangleStripExample
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        BasicEffect basicEffect;
        QuakeCamera fpsCam;

        VertexPositionColor[] vertices;
        VertexDeclaration myVertexDeclaration;

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

        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 750;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            fpsCam = new QuakeCamera(GraphicsDevice.Viewport);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            basicEffect = new BasicEffect(device, null);
            InitVertices();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

            if (gamePadState.Buttons.Back == ButtonState.Pressed || keyState.IsKeyDown(Keys.Escape))
                this.Exit();

            fpsCam.Update(mouseState, keyState, gamePadState);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);

            //draw triangles
            device.RenderState.CullMode = CullMode.None;
            basicEffect.World = Matrix.Identity;
            basicEffect.View = fpsCam.ViewMatrix;
            basicEffect.Projection = fpsCam.ProjectionMatrix;
            basicEffect.VertexColorEnabled = true;

            basicEffect.Begin();
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                device.VertexDeclaration = myVertexDeclaration;
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 10);
                pass.End();
            }
            basicEffect.End();

            base.Draw(gameTime);
        }

        private void InitVertices()
        {
            myVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            vertices = new VertexPositionColor[12];

            vertices[0] = new VertexPositionColor(new Vector3(-5, 1, 1), Color.Red);
            vertices[1] = new VertexPositionColor(new Vector3(-5, 5, 1), Color.Green);
            vertices[2] = new VertexPositionColor(new Vector3(-3, 1, 1), Color.Blue);

            vertices[3] = new VertexPositionColor(new Vector3(-3, 5, 1), Color.Gray);
            vertices[4] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Purple);
            vertices[5] = new VertexPositionColor(new Vector3(-1, 5, 1), Color.Orange);

            vertices[6] = new VertexPositionColor(new Vector3(1, 1, 1), Color.BurlyWood);
            vertices[7] = new VertexPositionColor(new Vector3(1, 5, 1), Color.Gray);
            vertices[8] = new VertexPositionColor(new Vector3(3, 1, 1), Color.Green);

            vertices[9] = new VertexPositionColor(new Vector3(3, 5, 1), Color.Yellow);
            vertices[10] = new VertexPositionColor(new Vector3(5, 1, 1), Color.Blue);
            vertices[11] = new VertexPositionColor(new Vector3(5, 5, 1), Color.Red);
        }
    }
}



Source :
XNA 3.0 Game Programming Recipes

  1. No comments yet.
  1. No trackbacks yet.