Exemplo n.º 1
0
        public void Render(DeviceContext deviceContext, Frustum frustum, HeightMapTerrainShader shader)
        {
            // Reset the number of the triangles that are drawn for this frame.
            DrawCount = 0;

            // Render each node that is visible at the parent node and moving down the tree.
            RenderNode(deviceContext, ParentNode, frustum, shader);
        }
Exemplo n.º 2
0
        private bool RenderGraphics()
        {
            // Clear the scene.
            D3D.BeginScene(0, 0, 0, 1);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, and projection matrices from camera and d3d objects.
            var viewMatrix       = Camera.ViewMatrix;
            var worldMatrix      = D3D.WorldMatrix;
            var projectionMatrix = D3D.ProjectionMatrix;
            var orthoMatrix      = D3D.OrthoMatrix;

            // Construct the frustrum
            Frustum.ConstructFrustum(SystemConfiguration.ScreenDepth, projectionMatrix, viewMatrix);

            // Set the terrain shader parameters that it will use for rendering.
            if (!HeightMapTerrainShader.SetShaderParameters(D3D.DeviceContext,
                                                            worldMatrix, viewMatrix, projectionMatrix,
                                                            Light.Direction, Light.AmbientColor, Light.DiffuseColor,
                                                            Terrain.Texture.TextureResource))
            {
                return(false);
            }

            // Render the terrain using the quad tree and terrain shader
            QuadTree.Render(D3D.DeviceContext, Frustum, HeightMapTerrainShader);

            // Set the number of rendered terrain triangles since some were culled.
            if (!Text.SetRenderCount(QuadTree.DrawCount, D3D.DeviceContext))
            {
                return(false);
            }

            // Turn off the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOff();

            // Turn on the alpha blending before rendering the text.
            D3D.TurnOnAlphaBlending();

            // Render the text string.
            if (!Text.Render(D3D.DeviceContext, worldMatrix, orthoMatrix))
            {
                return(false);
            }

            // Turn off the alpha blending before rendering the text.
            D3D.TurnOffAlphaBlending();

            // Turn on the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOn();

            // Present the rendered scene to the screen.
            D3D.EndScene();

            return(true);
        }
Exemplo n.º 3
0
        private bool RenderGraphics()
        {
            // Clear the scene.
            D3D.BeginScene(0, 0, 0, 1);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, and projection matrices from camera and d3d objects.
            var viewMatrix       = Camera.ViewMatrix;
            var worldMatrix      = D3D.WorldMatrix;
            var projectionMatrix = D3D.ProjectionMatrix;
            var orthoMatrix      = D3D.OrthoMatrix;

            // Render the terrain buffers.
            Terrain.Render(D3D.DeviceContext);

            // Render the model using the color shader.
            if (!HeightMapTerrainShader.Render(D3D.DeviceContext, Terrain.IndexCount,
                                               worldMatrix, viewMatrix, projectionMatrix,
                                               Light.Direction, Light.AmbientColor, Light.DiffuseColor,
                                               Terrain.Texture.TextureResource))
            {
                return(false);
            }

            // Turn off the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOff();

            // Turn on the alpha blending before rendering the text.
            D3D.TurnOnAlphaBlending();

            // Render the text string.
            if (!Text.Render(D3D.DeviceContext, worldMatrix, orthoMatrix))
            {
                return(false);
            }

            // Turn off the alpha blending before rendering the text.
            D3D.TurnOffAlphaBlending();

            // Turn on the Z buffer to begin all 2D rendering.
            D3D.TurnZBufferOn();

            // Present the rendered scene to the screen.
            D3D.EndScene();

            return(true);
        }
Exemplo n.º 4
0
        private void RenderNode(DeviceContext deviceContext, Node?node, Frustum frustum, HeightMapTerrainShader shader)
        {
            if (node == null)
            {
                return;
            }

            var theNode = node.Value;

            // 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(theNode.PositionX, 0, theNode.PositionZ, theNode.Width / 2))
            {
                return;
            }

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

            for (int i = 0; i < 4; i++)
            {
                if (theNode.Nodes[i] != null)
                {
                    count++;
                    RenderNode(deviceContext, theNode.Nodes[i], frustum, shader);
                }
            }

            // 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;
            }

            // Otherwise if this node can be seen and has triangles in it then render these triangles.

            // Set the vertex buffer to active in the input assembler so it can be rendered.
            deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(theNode.VertexBuffer, Utilities.SizeOf <HeightMapTerrainShader.Vertex>(), 0));
            // Set the index buffer to active in the input assembler so it can be rendered.
            deviceContext.InputAssembler.SetIndexBuffer(theNode.IndexBuffer, Format.R32_UInt, 0);
            // Set the type of the primitive that should be rendered from this vertex buffer, in this case triangles.
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

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

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

            // Increase the count of the number of polygons that have been rendered during this frame.
            DrawCount += theNode.TriangleCount;
        }
Exemplo n.º 5
0
        public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle)
        {
            if (Input == null)
            {
                Input = new InputClass();
                if (!Input.Initialize(configuration, windowHandle))
                {
                    MessageBox.Show("Could not initialize input object", "Error", MessageBoxButtons.OK);
                    return(false);
                }
            }

            // Create the Direct3D object.
            D3D = new DX11();
            // Initialize the Direct3D object.
            if (!D3D.Initialize(configuration, windowHandle))
            {
                MessageBox.Show("Could not initialize Direct3D", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create the camera object
            Camera = new Camera();

            // Initialize a base view matrix the camera for 2D user interface rendering.
            Camera.SetPosition(0, 0, -1);
            Camera.Render();
            var baseViewMatrix = Camera.ViewMatrix;

            // Set the initial position of the camera.
            var cameraX = 50f;
            var cameraY = 18f;
            var cameraZ = -7f;

            Camera.SetPosition(cameraX, cameraY, cameraZ);

            // Create the terrain object.
            Terrain = new HeightMapTerrain();

            // Initialize the terrain object.
            if (!(Terrain as HeightMapTerrain).Initialize(D3D.Device, "heightMap01.bmp"))
            {
                MessageBox.Show("Could not initialize the terrain object", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create and initialize Timer.
            Timer = new Timer();
            if (!Timer.Initialize())
            {
                MessageBox.Show("Could not initialize Timer object", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create the position object.
            Position = new Position();

            // Set the initial position of the viewer to the same as the initial camera position.
            Position.SetPosition(new Vector3(cameraX, cameraY, cameraZ));

            // Create and initialize the FPS object.
            FPS = new FPS();
            FPS.Initialize();

            // Create and initialize the CPU.
            CPU = new CPU();
            CPU.Initialize();

            // Create the font shader object.
            FontShader = new FontShader();

            // Initialize the font shader object.
            if (!FontShader.Initialize(D3D.Device, windowHandle))
            {
                MessageBox.Show("Could not initialize font shader object", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create the text object.
            Text = new Text();
            if (!Text.Initialize(D3D.Device, D3D.DeviceContext, windowHandle, configuration.Width, configuration.Height, baseViewMatrix))
            {
                MessageBox.Show("Could not initialize the text object", "Error", MessageBoxButtons.OK);
                return(false);
            }

            if (!Text.SetVideoCard(D3D.VideoCardDescription, D3D.VideoCardMemory, D3D.DeviceContext))
            {
                MessageBox.Show("Could not set video card into the text object", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create the height map terrain shader object.
            HeightMapTerrainShader = new HeightMapTerrainShader();

            // Initialize the height map terrain shader object.
            if (!HeightMapTerrainShader.Initialize(D3D.Device, windowHandle))
            {
                MessageBox.Show("Could not initialize the height map terrain shader", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Create the light object.
            Light = new Light();

            // Initialize the light object
            Light.SetAmbientColor(0.5f, 0.5f, 0.5f, 1f);
            Light.SetDiffuseColor(1f, 1f, 1f, 1f);
            Light.SetDirection(0f, 0f, 0.75f);

            return(true);
        }