Пример #1
0
        private bool InitialiseBuffers(Device device)
        {
            try
            {
                var vertices = new PositionTextureNormalVertex[_vertexCount];
                var indices  = new int[IndexCount];

                for (var i = 0; i < _vertexCount; ++i)
                {
                    vertices[i] = new PositionTextureNormalVertex()
                    {
                        position = new Vector3(ModelObject[i].x, ModelObject[i].y, ModelObject[i].z),
                        texture  = new Vector2(ModelObject[i].tu, ModelObject[i].tv),
                        //normal = new Vector3(ModelObject[i].nx, ModelObject[i].ny, ModelObject[i].nz)
                    };

                    indices[i] = i;
                }

                _vertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertices);
                _indexBuffer  = Buffer.Create(device, BindFlags.IndexBuffer, indices);

                return(true);
            }
            catch (Exception ex)
            {
                //Log.WriteToFile(ErrorLevel.Error, "Model.InitialiseBuffer", ex, true);

                return(false);
            }
        }
Пример #2
0
        // Methods
        public bool Initialise(Terrain terrain, Device device)
        {
            int vertexCount = terrain.VertexCount;

            TriangleCount = vertexCount / 3;

            VertexList = new PositionTextureNormalVertex[vertexCount];

            VertexList = terrain.Vertices;

            CalculateMeshDimensions(vertexCount, out float centerX, out float centerZ, out float width);

            ParentNode = new QuadTreeNodeType();

            CreateTreeNode2(device, ParentNode, centerX, centerZ, width, out QuadTreeNodeType createdNode);

            ParentNode = createdNode;

            VertexList = null;

            return(true);
        }
Пример #3
0
        private void CreateTreeNode2(SharpDX.Direct3D11.Device device, QuadTreeNodeType node, float positionX, float positionZ, float width, out QuadTreeNodeType createdNode)
        {
            // Store the node position and size.
            node.positionX = positionX;
            node.positionZ = positionZ;
            node.width     = width;

            // Initialize the triangle count to zero for the node.
            node.TriangleCount = 0;

            // Initialize the vertex and index buffer to null.
            node.VertexBuffer = null;
            node.IndexBuffer  = null;

            // Initialize the Nodes vertex array to null.
            node.vertexArray = null;

            // Count the number of triangles that are inside this node.
            int numTriangles = CountTriangles(positionX, positionZ, width);

            // Case 1: If there are no triangles in this node then return as it is empty and requires no processing.
            if (numTriangles == 0)
            {
                createdNode = node;
                return;
            }

            // Initialize the children nodes of this node to null.
            node.Nodes = new QuadTreeNodeType[4];

            // Case 2: If there are too many triangles in this node then split it into four equal sized smaller tree nodes.
            if (numTriangles > MAX_TRIANGLES)
            {
                for (int i = 0; i < 4; i++)
                {
                    // Calculate the position offsets for the new child node.
                    float offsetX = (((i % 2) < 1) ? -1.0f : 1.0f) * (width / 4.0f);
                    float offsetZ = (((i % 4) < 2) ? -1.0f : 1.0f) * (width / 4.0f);

                    // See if there are any triangles in the new node.
                    int count = CountTriangles((positionX + offsetX), (positionZ + offsetZ), (width / 2.0));

                    if (count > 0)
                    {
                        // If there are triangles inside where this new node would be then create the child node.
                        node.Nodes[i] = new QuadTreeNodeType();
                        QuadTreeNodeType aNewNode;

                        // Extend the tree starting from this new child node now.
                        CreateTreeNode2(device, node.Nodes[i], (positionX + offsetX), (positionZ + offsetZ), (width / 2.0f), out aNewNode);

                        node.Nodes[i] = aNewNode;
                        createdNode   = aNewNode;
                    }
                }

                createdNode = node;
                return;
            }

            // Case 3: If this node is not empty and the triangle count for it is less than the max then this node is at the bottom of the tree so create the list of triangles to store in it.
            node.TriangleCount = numTriangles;

            // Calculate the number of vertices.
            int vertexCount = numTriangles * 3;

            // Create the vertex array.
            PositionTextureNormalVertex[] vertices = new PositionTextureNormalVertex[vertexCount];
            // Create the index array
            int[] indices = new int[vertexCount];

            // Create the vertex array.
            node.vertexArray = new XYZType[vertexCount];

            // Initialize the index for this new vertex and index array.
            int index = 0;

            // Go through all the triangles in the vertex list.
            for (int i = 0; i < TriangleCount; i++)
            {
                // If the triangle is inside this node then add it to the vertex array.
                if (IsTriangleContained(i, positionX, positionZ, width))
                {
                    // Calculate the index into the terrain vertex list.
                    int vertexIndex = i * 3;

                    // Get the three vertices of this triangle from the vertex list.
                    vertices[index] = VertexList[vertexIndex];
                    indices[index]  = index;
                    // Also store the vertex position information in the node vertex array.
                    node.vertexArray[index].x = VertexList[vertexIndex].position.X;
                    node.vertexArray[index].y = VertexList[vertexIndex].position.Y;
                    node.vertexArray[index].z = VertexList[vertexIndex].position.Z;

                    // Increment the indexes.
                    index++;
                    vertexIndex++;

                    vertices[index]           = VertexList[vertexIndex];
                    indices[index]            = index;
                    node.vertexArray[index].x = VertexList[vertexIndex].position.X;
                    node.vertexArray[index].y = VertexList[vertexIndex].position.Y;
                    node.vertexArray[index].z = VertexList[vertexIndex].position.Z;

                    // Increment the indexes.
                    index++;
                    vertexIndex++;
                    vertices[index]           = VertexList[vertexIndex];
                    indices[index]            = index;
                    node.vertexArray[index].x = VertexList[vertexIndex].position.X;
                    node.vertexArray[index].y = VertexList[vertexIndex].position.Y;
                    node.vertexArray[index].z = VertexList[vertexIndex].position.Z;

                    index++;
                }
            }

            // Set up the description of the vertex buffer. no Need its static
            // Create the vertex buffer.
            node.VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);

            // Create the index buffer.
            node.IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);

            // Release the vertex and index arrays now that the data is stored in the buffers in the node.
            vertices = null;
            indices  = null;

            createdNode = node;

            return;
        }
Пример #4
0
        private bool InitialiseBuffers(Device device)
        {
            try
            {
                /// Calculate the number of vertices in the terrain mesh.
                VertexCount = (_size.Width - 1) * (_size.Height - 1) * 6;

                Vertices = new PositionTextureNormalVertex[VertexCount];

                // Set the index count to the same as the vertex count.
                IndexCount = VertexCount;

                // Create the index array.
                int[] indices = new int[IndexCount];

                // Initialize the index to the vertex array.
                int index = 0;

                // Load the vertex and index arrays with the terrain data.
                for (int j = 0; j < (_size.Height - 1); j++)
                {
                    for (int i = 0; i < (_size.Width - 1); i++)
                    {
                        int indexBottomLeft1  = (_size.Height * j) + i;                                 // Bottom left.
                        int indexBottomRight2 = (_size.Height * j) + (i + 1);                           // Bottom right.
                        int indexUpperLeft3   = (_size.Height * (j + 1)) + i;                           // Upper left.
                        int indexUpperRight4  = (_size.Height * (j + 1)) + (i + 1);                     // Upper right.

                        #region First Triangle

                        // Upper left.
                        float tv = HeightMap[indexUpperLeft3].tv;
                        // Modify the texture coordinates to cover the top edge.
                        if (tv == 1.0f)
                        {
                            tv = 0.0f;
                        }

                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexUpperLeft3].x, HeightMap[indexUpperLeft3].y, HeightMap[indexUpperLeft3].z),
                            texture  = new Vector2(HeightMap[indexUpperLeft3].tu, tv),
                            normal   = new Vector3(HeightMap[indexUpperLeft3].nx, HeightMap[indexUpperLeft3].ny, HeightMap[indexUpperLeft3].nz)
                        };
                        indices[index] = index++;

                        // Upper right.
                        float tu = HeightMap[indexUpperRight4].tu;
                        tv = HeightMap[indexUpperRight4].tv;

                        // Modify the texture coordinates to cover the top and right edge.
                        if (tu == 0.0f)
                        {
                            tu = 1.0f;
                        }
                        if (tv == 1.0f)
                        {
                            tv = 0.0f;
                        }

                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z),
                            texture  = new Vector2(tu, tv),
                            normal   = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz)
                        };
                        indices[index] = index++;

                        // Bottom left.
                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z),
                            texture  = new Vector2(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv),
                            normal   = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz)
                        };
                        indices[index] = index++;
                        #endregion

                        #region Second Triangle
                        // Bottom left.
                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexBottomLeft1].x, HeightMap[indexBottomLeft1].y, HeightMap[indexBottomLeft1].z),
                            texture  = new Vector2(HeightMap[indexBottomLeft1].tu, HeightMap[indexBottomLeft1].tv),
                            normal   = new Vector3(HeightMap[indexBottomLeft1].nx, HeightMap[indexBottomLeft1].ny, HeightMap[indexBottomLeft1].nz)
                        };
                        indices[index] = index++;

                        // Upper right.
                        tu = HeightMap[indexUpperRight4].tu;
                        tv = HeightMap[indexUpperRight4].tv;

                        // Modify the texture coordinates to cover the top and right edge.
                        if (tu == 0.0f)
                        {
                            tu = 1.0f;
                        }
                        if (tv == 1.0f)
                        {
                            tv = 0.0f;
                        }

                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexUpperRight4].x, HeightMap[indexUpperRight4].y, HeightMap[indexUpperRight4].z),
                            texture  = new Vector2(tu, tv),
                            normal   = new Vector3(HeightMap[indexUpperRight4].nx, HeightMap[indexUpperRight4].ny, HeightMap[indexUpperRight4].nz)
                        };
                        indices[index] = index++;

                        // Bottom right.
                        tu = HeightMap[indexBottomRight2].tu;

                        // Modify the texture coordinates to cover the right edge.
                        if (tu == 0.0f)
                        {
                            tu = 1.0f;
                        }

                        Vertices[index] = new PositionTextureNormalVertex()
                        {
                            position = new Vector3(HeightMap[indexBottomRight2].x, HeightMap[indexBottomRight2].y, HeightMap[indexBottomRight2].z),
                            texture  = new Vector2(tu, HeightMap[indexBottomRight2].tv),
                            normal   = new Vector3(HeightMap[indexBottomRight2].nx, HeightMap[indexBottomRight2].ny, HeightMap[indexBottomRight2].nz)
                        };
                        indices[index] = index++;
                        #endregion
                    }
                }

                // Create the vertex buffer.
                _vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, Vertices);

                // Create the index buffer.
                _indexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);

                // Release the arrays now that the buffers have been created and loaded.
                indices = null;

                return(true);
            }
            catch
            {
                return(false);
            }
        }