コード例 #1
0
        private bool RenderRefractionToTexture()
        {
            // Setup a clipping plane based on the height of the water to clip everything above it to create a refraction.
            Vector4 clipPlane = new Vector4(0.0f, -1.0f, 0.0f, WaterModel.WaterHeight + 0.1f);

            // Set the render target to be the refraction render to texture.
            RefractionTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the refraction render to texture.
            RefractionTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

            // Get the matrices from the camera and d3d objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewMatrix       = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Render the terrain using the reflection shader and the refraction clip plane to produce the refraction effect.
            TerrainModel.Render(D3D.DeviceContext);
            if (!ReflectionShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, TerrainModel.ColorTexture.TextureResource, TerrainModel.NormalMapTexture.TextureResource, Light.DiffuseColour, Light.Direction, 2.0f, clipPlane))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #2
0
        private bool RenderSceneToTexture()
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Get the world matrix from the d3d object.
            Matrix worldMatrix = D3D.WorldMatrix;

            // Generate the light view matrix based on the light's position.
            Light.GenerateViewMatrix();

            // Get the view and orthographic matrices from the light object.
            Matrix lightViewMatrix  = Light.ViewMatrix;
            Matrix lightOrthoMatrix = Light.OrthoMatrix;

            // Translate to the position of the tree.
            Vector3 treePosition = TreeModel.GetPosition();

            Matrix.Translation(treePosition.X, treePosition.Y, treePosition.Z, out worldMatrix);

            // Render the tree trunk with the depth shader.
            TreeModel.RenderTrunk(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, TreeModel.TrunkIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Render the tree leaves using the depth transparency shader.
            TreeModel.RenderLeaves(D3D.DeviceContext);
            if (!TransparentDepthShader.Render(D3D.DeviceContext, TreeModel.LeafIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix, TreeModel.LeafTexture.TextureResource))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Translate to the position of the ground model.
            Vector3 groundPosition = GroundModel.GetPosition();

            Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix);

            // Render the ground model with the depth shader.
            GroundModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #3
0
        private bool RenderSceneToTexture()
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

            // Get the world, view, projection, ortho, and base view matrices from the camera and Direct3D objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewMatrix       = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Render the terrain using the depth shader.
            TerrainModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #4
0
        private bool RenderHorizontalBlurToTexture()
        {
            // Store the screen width in a float that will be used in the horizontal blur shader.
            float screenSizeX = 1024.0f / 2;

            // Set the render target to be the render to texture.
            HorizontalBlurTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            HorizontalBlurTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

            // Get the world and view matrices from the camera and d3d objects.
            Matrix cameraViewMatrix = Camera.BaseViewMatrix;
            Matrix worldMatrix      = D3D.WorldMatrix;

            // Get the ortho matrix from the render to texture since texture has different dimensions.
            Matrix orthoMatrix = HorizontalBlurTexture.OrthoMatrix;

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

            // Put the small ortho window vertex and index buffers on the graphics pipeline to prepare them for drawing.
            if (!SmallWindow.Render(D3D.DeviceContext))
            {
                return(false);
            }

            // Render the small ortho window using the horizontal blur shader and the down sampled render to texture resource.
            if (!HorizontalBlurShader.Render(D3D.DeviceContext, SmallWindow.IndexCount, worldMatrix, cameraViewMatrix, orthoMatrix, DownSampleTexure.ShaderResourceView, screenSizeX))
            {
                return(false);
            }

            // Turn the Z buffer back on now that all 2D rendering has completed.
            D3D.TurnZBufferOn();

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #5
0
        private bool DownSampleTextureMethod()
        {
            // Set the render target to be the render to texture.
            DownSampleTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            DownSampleTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 1.0f, 0.0f, 1.0f);

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

            // Get the world and view matrices from the camera and d3d objects.
            Matrix worldMatrix = D3D.WorldMatrix;
            Matrix viewMatrix  = Camera.ViewMatrix;

            // Get the ortho matrix from the render to texture since texture has different dimensions being that it is smaller.
            Matrix orthoMatrix = DownSampleTexture.OrthoMatrix;

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

            // Put the small ortho window vertex and index buffers on the graphics pipeline to prepare them for drawing.
            if (!SmallWindow.Render(D3D.DeviceContext))
            {
                return(false);
            }

            // Render the small ortho window using the texture shader and the render to texture of the scene as the texture resource.
            if (!TextureShader.Render(D3D.DeviceContext, SmallWindow.IndexCount, worldMatrix, viewMatrix, orthoMatrix, RenderTexture.ShaderResourceView))
            {
                return(false);
            }

            // Turn the Z buffer back on now that all 2D rendering has completed.
            D3D.TurnZBufferOn();

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #6
0
        private bool RenderUIElementsToTexture()
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

            // Get the world, view, and ortho matrices from the camera and d3d objects.
            Matrix worldMatrix = D3D.WorldMatrix;
            Matrix viewMatrix  = Camera.ViewMatrix;
            Matrix orthoMatrix = D3D.OrthoMatrix;

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

            // Put the bitmap vertex and index buffers on the graphics pipeline to prepare them for drawing.
            if (!BitMap.Render(D3D.DeviceContext, 100, 100))
            {
                return(false);
            }


            // Render the bitmap using the texture shader.
            if (!TextureShader.Render(D3D.DeviceContext, BitMap.IndexCount, worldMatrix, viewMatrix, orthoMatrix, BitMap.Texture.TextureResource))
            {
                return(false);
            }

            // Turn the Z buffer back on now that all 2D rendering has completed.
            D3D.TurnZBufferOn();

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #7
0
        private bool RenderSceneToTexture()
        {
            // Set the render buffers to be the render target.
            DeferredBuffers.SetRenderTargets(D3D.DeviceContext);

            // Clear the render buffers.
            DeferredBuffers.ClearRenderTargets(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Get the matrices from the camera and d3d objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix cameraViewMatrix = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Update the rotation variable each frame.
            Rotate();

            // Rotate the world matrix by the rotation value so that the cube will spin.
            Matrix.RotationY(Rotation, out worldMatrix);

            // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
            Model.Render(D3D.DeviceContext);

            // Render the model using the deferred shader.
            if (!DeferredShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, Model.Texture.TextureResource))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render buffers anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #8
0
        private bool RenderSceneToTexture(float rotation)
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

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

            // Rotate the world matrix by the rotation value so that the cube will spin.
            worldMatrix = Matrix.RotationY(rotation);

            // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
            Model.Render(D3D.DeviceContext);

            // Render the model using the texture shader.
            if (!TextureShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.GetTexture()))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #9
0
        private bool RenderReflectionToTexture()
        {
            // Setup a clipping plane based on the height of the water to clip everything below it.
            Vector4 clipPlane = new Vector4(0.0f, 1.0f, 0.0f, -WaterModel.WaterHeight);

            // Set the render target to be the reflection render to texture.
            ReflectionTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the reflection render to texture.
            ReflectionTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Use the camera to render the reflection and create a reflection view matrix.
            Camera.RenderReflection(WaterModel.WaterHeight);

            // Get the camera reflection view matrix instead of the normal view matrix.
            Matrix reflectionViewMatrix = Camera.ReflectionViewMatrix;

            // Get the world and projection matrices from the d3d object.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Get the position of the camera.
            Vector3 cameraPosition = Camera.GetPosition();

            // Invert the Y coordinate of the camera around the water plane height for the reflected camera position.
            cameraPosition.Y = -cameraPosition.Y + (WaterModel.WaterHeight * 2.0f);

            // Translate the sky dome and sky plane to be centered around the reflected camera position.
            Matrix.Translation(cameraPosition.X, cameraPosition.Y, cameraPosition.Z, out worldMatrix);

            // Turn off back face culling and the Z buffer.
            D3D.TurnOffCulling();
            D3D.TurnZBufferOff();

            // Render the sky dome using the reflection view matrix.
            SkyDome.Render(D3D.DeviceContext);
            if (!SkyDomeShader.Render(D3D.DeviceContext, SkyDome.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, SkyDome.ApexColour, SkyDome.CenterColour))
            {
                return(false);
            }

            // Enable back face culling.
            D3D.TurnOnCulling();

            // Enable additive blending so the clouds blend with the sky dome color.
            D3D.EnableSecondBlendState();

            // Render the sky plane using the sky plane shader.
            SkyPlane.Render(D3D.DeviceContext);
            if (!SkyPlaneShader.Render(D3D.DeviceContext, SkyPlane.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, SkyPlane.CloudTexture.TextureResource, SkyPlane.PerturbTexture.TextureResource, SkyPlane.m_Translation, SkyPlane.m_Scale, SkyPlane.m_Brightness))
            {
                return(false);
            }

            // Turn off blending and enable the Z buffer again.
            D3D.TurnOffAlphaBlending();
            D3D.TurnZBufferOn();

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Render the terrain using the reflection view matrix and reflection clip plane.
            TerrainModel.Render(D3D.DeviceContext);
            if (!ReflectionShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, reflectionViewMatrix, projectionMatrix, TerrainModel.ColorTexture.TextureResource, TerrainModel.NormalMapTexture.TextureResource, Light.DiffuseColour, Light.Direction, 2.0f, clipPlane))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #10
0
        private bool RenderSceneToTexture2()
        {
            // Set the render target to be the render to texture.
            RenderTexture2.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture2.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Generate the light view matrix based on the light's position.
            Light2.GenerateViewMatrix();

            // Get the world matrix from the d3d object.
            Matrix worldMareix = D3D.WorldMatrix;

            // Get the view and orthographic matrices from the light object.
            Matrix lightViewMatrix  = Light2.ViewMatrix;
            Matrix lightOrthoMatrix = Light2.ProjectionMatrix;

            // Setup the translation matrix for the cube model.
            Vector3 cubePosition = CubeModel.GetPosition();

            Matrix.Translation(cubePosition.X, cubePosition.Y, cubePosition.Z, out worldMareix);

            // Render the cube model with the depth shader.
            CubeModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, CubeModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMareix = D3D.WorldMatrix;

            // Setup the translation matrix for the sphere model.
            Vector3 spherePosition = SphereModel.GetPosition();

            Matrix.Translation(spherePosition.X, spherePosition.Y, spherePosition.Z, out worldMareix);

            // Render the sphere model with the depth shader.
            SphereModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMareix = D3D.WorldMatrix;

            // Setup the translation matrix for the ground model.
            Vector3 groundPosition = GroundModel.GetPosition();

            Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMareix);

            // Render the ground model with the depth shader.
            GroundModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #11
0
        private bool RenderBlackAndWhiteShadows()
        {
            // Set the render target to be the render to texture.
            BlackWhiteRenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            BlackWhiteRenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

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

            // Generate the light view matrix based on the light's position.
            Light.GenerateViewMatrix();

            // Get the world, view, and projection matrices from the camera and d3d objects.
            Matrix cameraViewMatrix = Camera.ViewMatrix;
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Get the light's view and projection matrices from the light object.
            Matrix lightViewMatrix       = Light.ViewMatrix;
            Matrix lightProjectionMatrix = Light.ProjectionMatrix;

            // Setup the translation matrix for the cube model.
            Vector3 cubePosition = CubeModel.GetPosition();

            Matrix.Translation(cubePosition.X, cubePosition.Y, cubePosition.Z, out worldMatrix);

            // Render the cube model using the shadow shader.
            CubeModel.Render(D3D.DeviceContext);
            if (!ShadowShader.Render(D3D.DeviceContext, CubeModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Setup the translation matrix for the sphere model.
            Vector3 spherePosition = SphereModel.GetPosition();

            Matrix.Translation(spherePosition.X, spherePosition.Y, spherePosition.Z, out worldMatrix);

            // Render the sphere model using the shadow shader.
            SphereModel.Render(D3D.DeviceContext);
            if (!ShadowShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Setup the translation matrix for the ground model.
            Vector3 groundPosition = GroundModel.GetPosition();

            Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix);

            // Render the ground model using the shadow shader.
            GroundModel.Render(D3D.DeviceContext);
            if (!ShadowShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }