Thread: XNA 3d - 32 bit indices

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    XNA 3d - 32 bit indices

    Having become fairly adept with 2d programming in xna, I am testing the waters of 3d which seems a fair leap. I am working from Riemers tutorials and copied this code

    Code:
    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;
    
    namespace Terrain
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            GraphicsDevice device;
    
            Effect effect;
            VertexPositionColor[] vertices;
            Matrix viewMatrix;
            Matrix projectionMatrix;
            int[] indices;
    
            private float angle = 0f;
            private int terrainWidth = 4;
            private int terrainHeight = 3;
            private float[,] heightData;
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
    
            protected override void Initialize()
            {
                graphics.PreferredBackBufferWidth = 500;
                graphics.PreferredBackBufferHeight = 500;
                graphics.IsFullScreen = false;
                graphics.ApplyChanges();
                Window.Title = "Riemer's XNA Tutorials -- 3D Series 1";
    
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                spriteBatch = new SpriteBatch(GraphicsDevice);
    
                device = graphics.GraphicsDevice;
    
                effect = Content.Load<Effect>("effects"); SetUpCamera();
    
    
    
                Texture2D heightMap = Content.Load<Texture2D>("heightmap"); LoadHeightData(heightMap);
    
                SetUpVertices();
                SetUpIndices();
            }
    
            protected override void UnloadContent()
            {
            }
    
            private void SetUpVertices()
            {
                vertices = new VertexPositionColor[terrainWidth * terrainHeight];
                for (int x = 0; x < terrainWidth; x++)
                {
                    for (int y = 0; y < terrainHeight; y++)
                    {
                        vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
                        vertices[x + y * terrainWidth].Color = Color.White;
                    }
                }
            }
    
            private void SetUpIndices()
            {
                indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
                int counter = 0;
                for (int y = 0; y < terrainHeight - 1; y++)
                {
                    for (int x = 0; x < terrainWidth - 1; x++)
                    {
                        int lowerLeft = x + y * terrainWidth;
                        int lowerRight = (x + 1) + y * terrainWidth;
                        int topLeft = x + (y + 1) * terrainWidth;
                        int topRight = (x + 1) + (y + 1) * terrainWidth;
    
                        indices[counter++] = topLeft;
                        indices[counter++] = lowerRight;
                        indices[counter++] = lowerLeft;
    
                        indices[counter++] = topLeft;
                        indices[counter++] = topRight;
                        indices[counter++] = lowerRight;
                    }
                }
            }
    
            private void LoadHeightData(Texture2D heightMap)
            {
                terrainWidth = heightMap.Width;
                terrainHeight = heightMap.Height;
    
                Color[] heightMapColors = new Color[terrainWidth * terrainHeight];
                heightMap.GetData(heightMapColors);
    
                heightData = new float[terrainWidth, terrainHeight];
                for (int x = 0; x < terrainWidth; x++)
                    for (int y = 0; y < terrainHeight; y++)
                        heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;
            }
    
            private void SetUpCamera()
            {
                viewMatrix = Matrix.CreateLookAt(new Vector3(60, 80, -80), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
            }
    
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                angle += 0.005f;
    
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                device.Clear(Color.Black);
    
                RasterizerState rs = new RasterizerState();
                rs.CullMode = CullMode.None;
                rs.FillMode = FillMode.WireFrame;
                device.RasterizerState = rs;
    
    
                Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f);
                effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
                effect.Parameters["xView"].SetValue(viewMatrix);
                effect.Parameters["xProjection"].SetValue(projectionMatrix);
                effect.Parameters["xWorld"].SetValue(worldMatrix);
    
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
    
                    device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColor.VertexDeclaration);
                }
    
                base.Draw(gameTime);
            }
        }
    }
    This is for xna 4.0 (Directx10) and this is the crux of the problem. I have C# 2010 and xna 4 installed, but my graphics card only supports Reach, not HiDef. I tried changing some ints to short as a 16 bit variable, but that seems not to work. I was even googling about castings in the SetUpIndices function, but couldn't figure this out.

    So until I find a NVIDIA card that supports Directx 10 or 11, what do I do?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    32 or 16-bit indices usually has nothing to do with your card. This is simply the format of the indices you wish to use and 16/32 bit indices should be supported by every single card made within the last decade. I've used 32-bit indices on cards as old as a GeForce 3 64 MB with no problem. DirectX 10 and 11 do offer new features but unfortunately 16/32 bit indices is not one of those since those have been available for about a decade or more already. You do not need DX10 or DX11 for 32 bit indices.

    Even though this is a game programming forum it is on a C/C++ forum. We normally answer questions related to game programming as it pertains to C/C++ and not XNA. If you want help with XNA you can go to the forums at www.gamedev.net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding indices efficiently
    By KBriggs in forum C Programming
    Replies: 29
    Last Post: 05-21-2010, 12:16 PM
  2. Best way to have an list of indices which changes order
    By laertius in forum C Programming
    Replies: 5
    Last Post: 09-29-2008, 12:45 AM
  3. Vertices and Indices
    By beene in forum Game Programming
    Replies: 15
    Last Post: 05-07-2007, 03:08 AM
  4. Array of indices
    By ronenk in forum C Programming
    Replies: 4
    Last Post: 06-15-2004, 05:36 PM
  5. What does 'indices' mean?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2002, 07:54 AM