Beispiel #1
0
        private void makeIndicesSetData()
        {
            // set indices clockwise from point of view ... surfaces really left handed
            int i = 0;

            for (int z = 0; z < height - 1; z++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    indices[i++] = z * width + x;
                    indices[i++] = z * width + x + 1;
                    indices[i++] = (z + 1) * width + x;
                    indices[i++] = (z + 1) * width + x;
                    indices[i++] = z * width + x + 1;
                    indices[i++] = (z + 1) * width + x + 1;
                }
            }

            // create VertexBuffer and store on GPU
            vb = new VertexBuffer(display, typeof(VertexPositionColor), vertex.Length, BufferUsage.WriteOnly);
            vb.SetData <VertexPositionColor>(vertex); // , 0, vertex.Length);
                                                      // create IndexBuffer and store on GPU
            ib = new IndexBuffer(display, typeof(int), indices.Length, BufferUsage.WriteOnly);
            IB.SetData <int>(indices);
        }
Beispiel #2
0
        public Terrain(Scene aScene, string label, Vector3 position, Vector3 orientAxis, float radians, string heightFile, string colorFile)
            : base(aScene, label, position, orientAxis, radians)
        {
            int i;

            range         = scene.Range;
            width         = height = range;
            nVertices     = width * height;
            terrainHeight = new int[width, height];
            vertex        = new VertexPositionColor[nVertices];
            nIndices      = (width - 1) * (height - 1) * 6;
            indices       = new int[nIndices]; // there are 6 indices 2 faces / 4 vertices
            spacing       = scene.Spacing;
            // set display information
            display           = scene.Display;
            effect            = scene.SceneEffect;
            vertexDeclaration = new VertexDeclaration(display, VertexPositionColor.VertexElements);
            // create heightMap values from heightTexture
            heightTexture = scene.Content.Load <Texture2D>(heightFile);
            heightMap     = new Microsoft.Xna.Framework.Graphics.Color[width * height];
            heightTexture.GetData <Microsoft.Xna.Framework.Graphics.Color>(heightMap);
            // create colorMap values from colorTexture
            colorTexture = scene.Content.Load <Texture2D>(colorFile);
            colorMap     = new Microsoft.Xna.Framework.Graphics.Color[width * height];
            colorTexture.GetData <Microsoft.Xna.Framework.Graphics.Color>(colorMap);
            // create  vertices for terrain
            Vector4 vector4;
            int     vertexHeight;

            i = 0;
            for (int z = 0; z < height; z++)
            {
                for (int x = 0; x < width; x++)
                {
                    vector4             = heightMap[i].ToVector4(); // convert packed Rgba32 values to floats
                    vertexHeight        = (int)(vector4.X * 255);   // scale vertexHeight 0..255
                    vertexHeight       *= multiplier;               // multiply height
                    terrainHeight[x, z] = vertexHeight;             // save height for navigation
                    vertex[i]           = new VertexPositionColor(
                        new Vector3(x * spacing, vertexHeight, z * spacing),
                        new Color(colorMap[i].ToVector4())
                        );
                    i++;
                }
            }
            // free up unneeded maps
            colorMap  = null;
            heightMap = null;
            // set indices clockwise from point of view
            i = 0;
            for (int z = 0; z < height - 1; z++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    indices[i++] = z * width + x;
                    indices[i++] = z * width + x + 1;
                    indices[i++] = (z + 1) * width + x;
                    indices[i++] = (z + 1) * width + x;
                    indices[i++] = z * width + x + 1;
                    indices[i++] = (z + 1) * width + x + 1;
                }
            }

            // create VertexBuffer and store on GPU
            vb = new VertexBuffer(display, VertexPositionColor.SizeInBytes * vertex.Length, BufferUsage.WriteOnly);
            vb.SetData <VertexPositionColor>(vertex, 0, vertex.Length);
            // create IndexBuffer and store on GPU
            ib = new IndexBuffer(display, typeof(int), indices.Length, BufferUsage.WriteOnly);
            IB.SetData <int>(indices);
        }