Ejemplo n.º 1
0
        private void RenderNode(DeviceContext deviceContext, DNodeType node, DFrustum frustum, DTerrainShader terrainShader)
        {
            // Check to see if the node can be viewed, height doesn't matter in a quad tree.
            // If it can't be seen then none of its children can either, so don't continue down the tree, this is where the speed is gained.
            if (!frustum.CheckCube(new SharpDX.Vector3(node.positionX, 0.0f, node.positionZ), (node.width / 2.0f)))  //   63.7506409
            {
                return;
            }

            // If it can be seen then check all four child nodes to see if they can also be seen.
            int count = 0;

            for (int i = 0; i < 4; i++)
            {                                   // parentNode.width > 0.0f && parentNode.Nodes[i].VertexBuffer != null
                if (node.Nodes[i].width > 0.0f) //node.Nodes != null/ parentNode.Nodes[i].width > 0.0f
                {
                    count++;
                    RenderNode(deviceContext, node.Nodes[i], frustum, terrainShader);
                }
            }

            // If there were any children nodes then there is no need to continue as parent nodes won't contain any triangles to render.
            if (count != 0)
            {
                return;
            }

            int vertexCount = node.TriangleCount * 3;

            // Otherwise if this node can be seen and has triangles in it then render these triangles.
            // Set vertex buffer stride and offset.
            // Set the vertex buffer to active in the input assembler so it can be rendered.
            deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(node.VertexBuffer, Utilities.SizeOf <DTerrain.DVertexType>(), 0));

            // Set the index buffer to active in the input assembler so it can be rendered.
            deviceContext.InputAssembler.SetIndexBuffer(node.IndexBuffer, Format.R32_UInt, 0);

            // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            // Determine the number of indices in this node.
            int indexCount = node.TriangleCount * 3;

            // Call the terrain shader to render the polygons in this node.
            terrainShader.RenderShader(deviceContext, indexCount);

            // Increase the count of the number of polygons that have been rendered during this frame.
            DrawCount += node.TriangleCount;
        }
Ejemplo n.º 2
0
        public bool Render(DeviceContext deviceContext, DTerrainShader shader, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Vector4 ambiantColour, Vector4 diffuseColour, Vector3 lightDirection)
        {
            // Set vertex buffer stride and offset.
            // Set the shader parameters that it will use for rendering.
            if (!shader.SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, ambiantColour, diffuseColour, lightDirection))
            {
                return(false);
            }

            // Set the type of primitive that should be rendered from the vertex buffers, in this case triangles.
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            // Render each material group.
            for (int i = 0; i < m_MaterialCount; i++)
            {
                // Set the vertex buffer to active in the input assembler so it can be rendered.
                deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(Materials[i].vertexBuffer, Utilities.SizeOf <DVertexType>(), 0));

                // Set the index buffer to active in the input assembler so it can be rendered.
                deviceContext.InputAssembler.SetIndexBuffer(Materials[i].indexBuffer, Format.R32_UInt, 0);

                // If the material group has a valid second texture index then this is a blended terrain polygon.
                bool result;
                if (Materials[i].textureIndex2 != -1)
                {
                    result = shader.SetShaderTextures(deviceContext, Textures[Materials[i].textureIndex1].TextureResource, Textures[Materials[i].textureIndex2].TextureResource, Textures[Materials[i].alphaIndex].TextureResource, false);
                }
                else  // If not then it is just a single textured polygon.
                {
                    result = shader.SetShaderTextures(deviceContext, Textures[Materials[i].textureIndex1].TextureResource, null, null, false);
                }

                // Check if the textures were set or not.
                if (!result)
                {
                    return(false);
                }

                // Now render the prepared buffers with the shader.
                shader.RenderShader(deviceContext, Materials[i].indexCount);
            }

            return(true);
        }