예제 #1
0
        /// <summary>
        /// This sets up the vertices for all of the triangles in this quad-tree section
        /// passes them to the main terrain component.
        /// </summary>
        private void SetupTerrainVertices()
        {
            int offset = this.terrain.VertexList.Count;

            // Texture the level
            for (int x = 0; x < width; ++x)
            {
                for (int z = 0; z < width; ++z)
                {
                    VertexTerrain tempVert     = new VertexTerrain();
                    int           offsetXtotal = offsetX + x;
                    int           offsetZtotal = offsetZ + z;
                    tempVert.Position = new Vector3(offsetXtotal * terrain.ScaleFactor,
                                                    terrain.heightData[offsetXtotal, offsetZtotal],
                                                    offsetZtotal * terrain.ScaleFactor);

                    tempVert.Normal = terrain.normals[offsetXtotal, offsetZtotal];

                    this.terrain.VertexList.Add(tempVert);
                }
            }

            this.vertexBufferOffset    = offset;
            this.vertexBufferOffsetEnd = terrain.VertexList.Count;
        }
예제 #2
0
        /// <summary>
        /// Setup vertex buffer for entire terrain section.
        /// </summary>
        private void SetupTerrainVertexBuffer()
        {
            VertexTerrain[] verticesArray = new VertexTerrain[vertexList.Count];

            this.vertexList.CopyTo(verticesArray);

            int SizePlusSizeDivWidth = (this.size + (this.size / QSConstants.DefaultQuadTreeWidth));

            this.vertexBuffer = new VertexBuffer(this.parentEntity.Game.GraphicsDevice, verticesArray[0].GetType(), SizePlusSizeDivWidth * SizePlusSizeDivWidth, BufferUsage.WriteOnly);
            this.vertexBuffer.SetData(verticesArray);
        }
예제 #3
0
        /// <summary>
        /// Setup <see cref="Terrain"/> normals. Normals are used for lighting, normal mapping, and physics with terrain.
        /// </summary>
        private void SetupTerrainNormals()
        {
            VertexTerrain[] terrainVertices = new VertexTerrain[this.size * this.size];
            this.normals = new Vector3[this.size, this.size];

            // Determine vertex positions so we can figure out normals in section below.
            for (int x = 0; x < this.size; ++x)
            {
                for (int z = 0; z < this.size; ++z)
                {
                    terrainVertices[x + z * this.size].Position = new Vector3(x * this.scaleFactor, this.heightData[x, z], z * this.scaleFactor);
                }
            }

            // Setup normals for lighting and physics (Credit: Riemer's method)
            int sizeMinusOne = this.size - 1;

            for (int x = 1; x < sizeMinusOne; ++x)
            {
                for (int z = 1; z < sizeMinusOne; ++z)
                {
                    int     ZTimesSize = (z * this.size);
                    Vector3 normX      = new Vector3((terrainVertices[x - 1 + ZTimesSize].Position.Y - terrainVertices[x + 1 + ZTimesSize].Position.Y) / 2, 1, 0);
                    Vector3 normZ      = new Vector3(0, 1, (terrainVertices[x + (z - 1) * this.size].Position.Y - terrainVertices[x + (z + 1) * this.size].Position.Y) / 2);

                    // We inline the normalize method here since it is used alot, this is faster than calling Vector3.Normalize()
                    Vector3 normal = normX + normZ;
                    float   length = (float)Math.Sqrt((float)((normal.X * normal.X) + (normal.Y * normal.Y) + (normal.Z * normal.Z)));
                    float   num    = 1f / length;
                    normal.X *= num;
                    normal.Y *= num;
                    normal.Z *= num;

                    this.normals[x, z] = terrainVertices[x + ZTimesSize].Normal = normal;    // Stored for use in physics and for the
                                                                                             // quad-tree component to reference.
                }
            }
        }