예제 #1
0
        public void GenerateStructures()
        {
            /*vertices = new VertexPositionNormalTexture[(m_Dimension) * (m_Dimension)];*/
            vertices  = new CityVertex[(m_Dimension) * (m_Dimension)];
            m_Indices = new int[m_Dimension * m_Dimension * 6];
            for (int i = 0; i < m_Dimension; i++)
            {
                for (int j = 0; j < m_Dimension; j++)
                {
                    //VertexPositionNormalTexture vert = new VertexPositionNormalTexture();
                    CityVertex vert = new CityVertex();
                    vert.Position     = new Vector3((i - m_Dimension / 2.0f) * m_CellSize, 0, (j - m_Dimension / 2.0f) * m_CellSize);
                    vert.Normal       = Vector3.Up;
                    vert.TextureCoord = new Vector2((float)i / m_Dimension, (float)j / m_Dimension);
                    vertices[i * (m_Dimension) + j] = vert;
                }
            }

            for (int i = 0; i < m_Dimension; i++)
            {
                for (int j = 0; j < m_Dimension; j++)
                {
                    m_Indices[6 * (i * m_Dimension + j)]     = (i * (m_Dimension) + j);
                    m_Indices[6 * (i * m_Dimension + j) + 1] = (i * (m_Dimension) + j + 1);
                    m_Indices[6 * (i * m_Dimension + j) + 2] = ((i + 1) * (m_Dimension) + j + 1);

                    m_Indices[6 * (i * m_Dimension + j) + 3] = (i * (m_Dimension) + j);
                    m_Indices[6 * (i * m_Dimension + j) + 4] = ((i + 1) * (m_Dimension) + j + 1);
                    m_Indices[6 * (i * m_Dimension + j) + 5] = ((i + 1) * (m_Dimension) + j);
                }
            }
        }
예제 #2
0
        public QuadtreeLeaf(float CellSize, BoundingBox boundingBox, Color[] heightMapColors,
                            GraphicsDevice graphicsDevice) : base(boundingBox)
        {
            float maxHeight = 100.0f;
            //int heightMapWidth = 2048;
            int heightMapWidth  = 255;
            int heightmapHeight = 512;

            int sx     = (int)(boundingBox.Min.X / CellSize);
            int sy     = (int)(boundingBox.Min.Z / CellSize);
            int width  = (int)((boundingBox.Max.X - boundingBox.Min.X) / CellSize) + (sx < 2016 ? 1 : 0);
            int height = (int)((boundingBox.Max.Z - boundingBox.Min.Z) / CellSize) + (sy < 2016 ? 1 : 0);

            // vertex buffer generation
            CityVertex[] vertices = new CityVertex[width * height];

            this.boundingBox.Min.Y = maxHeight;
            this.boundingBox.Max.Y = 0;
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    float depth = heightMapColors[(sy + y) * heightMapWidth + sx + x].R / 255.0f;
                    vertices[y * width + x]              = new CityVertex();
                    vertices[y * width + x].Position     = new Vector3(sx + x, depth, sy + y);
                    vertices[y * width + x].Normal       = Vector3.Up;
                    vertices[y * width + x].TextureCoord = new Vector2((float)x / (heightMapWidth), (float)y / (heightmapHeight));
                    depth *= maxHeight;
                    this.boundingBox.Min.Y = MathHelper.Min(this.boundingBox.Min.Y, depth);
                    this.boundingBox.Max.Y = MathHelper.Max(this.boundingBox.Max.Y, depth);
                }
            }

            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(CityVertex), vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData <CityVertex>(vertices);

            // index buffer generation
            int indicesPerRow = width * 2 + 2;

            short[] indices = new short[indicesPerRow * (height - 1)];

            for (int i = 0; i < height - 1; ++i)
            {
                int index;
                for (int ii = 0; ii < width; ++ii)
                {
                    index              = i * indicesPerRow + ii * 2;
                    indices[index]     = (short)((i) * width + ii);
                    indices[index + 1] = (short)((i + 1) * width + ii);
                }

                index              = i * indicesPerRow + width * 2;
                indices[index]     = indices[index - 1];
                indices[index + 1] = (short)((i + 1) * width);
            }

            indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);
        }