Sign in to follow this  

(XNA) Rendering My Heightmap Using BasicEffect

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I created a heightmap which gets loaded from a bitmap, and then sets up its vertices and indices, and when I debug it the values seem to be loaded and correct, but I can't get BasicEffect to render it, I don't quite understand how XNA renders stuff! I got some house cleaning to do on my code yet and have to add some things but heres what I got so far, any comments welcome. Help me please, thanks. Also, would it be more efficient to load it through the content pipeline instead of reading the information from the header, I dont see much differnce either then this you don't need GSE to load the file, and it's more work. :)
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace WindowsGame1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        private int[,] heightData;
        private VertexBuffer vertexBuffer;
        private IndexBuffer indexBuffer;
        private BasicEffect basicEffect;
        private int bfOffbits;
        private int biWidth;
        private int biHeight;
        private int bitmapData;

        private VertexPositionColor[] verticles;
        private int[] indices;

        System.IO.FileStream fileStream;
        System.IO.BinaryReader binaryReader;



        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }


        private void LoadHeightData(string bitmap)
        {
            this.fileStream = new System.IO.FileStream(bitmap, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            this.binaryReader = new System.IO.BinaryReader(this.fileStream);

            this.binaryReader.BaseStream.Seek(10, System.IO.SeekOrigin.Current);
            this.bfOffbits = (int)binaryReader.ReadUInt32();

            this.binaryReader.BaseStream.Seek(4, System.IO.SeekOrigin.Current);
            this.biWidth = (int)binaryReader.ReadUInt32();
            this.biHeight = (int)binaryReader.ReadUInt32();

            this.heightData = new int[this.biWidth, this.biHeight];

            this.binaryReader.BaseStream.Seek(this.bfOffbits - 26, System.IO.SeekOrigin.Current);

            for (int x = 0; x < this.biWidth; x++)
            {
                for (int y = 0; y < this.biHeight; y++)
                {
                    this.bitmapData = (int)binaryReader.ReadByte();
                    this.bitmapData += (int)binaryReader.ReadByte();
                    this.bitmapData += (int)binaryReader.ReadByte();
                    this.bitmapData /= 8;

                    this.heightData[this.biWidth - 1 - x, this.biHeight - 1 - y] = this.bitmapData;
                }
            }

            this.verticles = new VertexPositionColor[this.biWidth * this.biHeight];

            for (int x = 0; x < this.biWidth; x++)
            {
                for (int y = 0; y < this.biHeight; y++)
                {
                    this.verticles[x + y * this.biWidth].Position = new Vector3(x, y, this.heightData[x, y]);
                    this.verticles[x + y * this.biWidth].Color = Color.White;
                }
            }

            this.vertexBuffer = new VertexBuffer(this.graphics.GraphicsDevice, sizeof(float) * 4 * this.biWidth * this.biHeight, ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);
            this.vertexBuffer.SetData(this.verticles);

            this.indices = new int[(this.biWidth - 1) * (this.biHeight - 1) * 6];

            for (int x = 0; x < this.biWidth - 1; x++)
            {
                for (int y = 0; y < this.biHeight - 1; y++)
                {
                    this.indices[(x + y * (this.biWidth - 1)) * 3] = (x + 1) + (y + 1) * this.biWidth;
                    this.indices[(x + y * (this.biWidth - 1)) * 3 + 1] = (x + 1) + (y + 1) * this.biWidth;
                    this.indices[(x + y * (this.biWidth - 1)) * 3 + 2] = (x + 1) + (y + 1) * this.biWidth;
                }
            }

            this.indexBuffer = new IndexBuffer(this.graphics.GraphicsDevice, typeof(int), (this.biWidth - 1) * (this.biHeight - 1) * 6, ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);
            this.indexBuffer.SetData(this.indices);
        }


        protected override void Initialize()
        {
            this.basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            LoadHeightData("heightmap.bmp");

            base.Initialize();
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                content.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            this.graphics.GraphicsDevice.Clear(Color.Black);
            this.graphics.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
            this.graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;;

            this.basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), (float)graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height, 1.0f, 100.0f);
            this.basicEffect.View = Matrix.CreateLookAt(new Vector3(0, -50, 100), new Vector3(0, -5, 0), Vector3.Up);
            this.basicEffect.LightingEnabled = true;

            this.basicEffect.Begin();
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();

                this.graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);
                this.graphics.GraphicsDevice.Indices = this.indexBuffer;
                this.graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
                this.graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, this.biWidth * this.biHeight, 0, (this.biWidth - 1) * (this.biHeight - 1) * 2);

                pass.End();
            }

            this.basicEffect.End();


            base.Draw(gameTime);
        }
    }
}

Share this post


Link to post
Share on other sites

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this