Exemple #1
0
        private void GenerateTerrainNormals(VertexPositionNormalTangentBinormalTexture[] vertices, int[] indices)
        {
            for (int i = 0; i < indices.Length; i += 3)
            {
                Vector3 v1 = vertices[indices[i]].Position;
                Vector3 v2 = vertices[indices[i + 1]].Position;
                Vector3 v3 = vertices[indices[i + 2]].Position;

                Vector3 vu = v3 - v1;
                Vector3 vt = v2 - v1;
                Vector3 normal = Vector3.Cross(vu, vt);
                normal.Normalize();

                vertices[indices[i]].Normal += normal;
                vertices[indices[i + 1]].Normal += normal;
                vertices[indices[i + 2]].Normal += normal;
            }

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Normal.Normalize();
            }
        }
Exemple #2
0
        /**
         * Generate terrain mesh vertices and indices
         * The vertex is currently VertexPositionNormalTangentBinormalTexture
         */
        private VertexPositionNormalTangentBinormalTexture[] GenerateTerrainVertices(int[] terrainIndices)
        {
            float halfTerrainWidth = (vertexCountX - 1) * blockScale * 0.5f;
            float halfTerrainDepth = (vertexCountZ - 1) * blockScale * 0.5f;

            // Texture coordinates
            float tu = 0;
            float tv = 0;
            float tuDerivative = 1.0f / (vertexCountX - 1);
            float tvDerivative = 1.0f / (vertexCountZ - 1);

            int vertexCount = 0;
            VertexPositionNormalTangentBinormalTexture[] vertices =
                new VertexPositionNormalTangentBinormalTexture[vertexCountX * vertexCountZ];

            // Set vertices position and texture coordinate
            for (float i = -halfTerrainDepth; i <= halfTerrainDepth; i += blockScale)
            {
                tu = 0.0f;
                for (float j = -halfTerrainWidth; j <= halfTerrainWidth; j += blockScale)
                {
                    vertices[vertexCount].Position = new Vector3(j, heightMap[vertexCount].R * heightScale, i);
                    vertices[vertexCount].TextureCoordinate = new Vector2(tu, tv);

                    tu += tuDerivative;
                    vertexCount++;
                }

                tv += tvDerivative;
            }

            // Generate vertice's normal, tangent and binormal
            GenerateTerrainNormals(vertices, terrainIndices);
            GenerateTerrainTangentBinormal(vertices, terrainIndices);

            return vertices;
        }
Exemple #3
0
        public void GenerateTerrainTangentBinormal(VertexPositionNormalTangentBinormalTexture[] vertices, int[] indices)
        {
            for (int i = 0; i < vertexCountZ; i++)
            {
                for (int j = 0; j < vertexCountX; j++)
                {
                    int vertexIndex = j + i * vertexCountX;
                    Vector3 v1 = vertices[vertexIndex].Position;

                    if (j < vertexCountX - 1)
                    {
                        Vector3 v2 = vertices[vertexIndex + 1].Position;
                        vertices[vertexIndex].Tanget = (v2 - v1);
                    }
                    else
                    {
                        Vector3 v2 = vertices[vertexIndex - 1].Position;
                        vertices[vertexIndex].Tanget = (v1 - v2);
                    }

                    vertices[vertexIndex].Tanget.Normalize();
                    vertices[vertexIndex].Binormal = Vector3.Cross(vertices[vertexIndex].Tanget, vertices[vertexIndex].Normal);
                }
            }
        }