示例#1
0
        public bool Render()
        {
            // Clear the buffers to begin the scene.
            D3D.BeginScene(0, 0, 0, 1f);

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

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

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

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

            return(true);
        }
        public bool Render()
        {
            // Clear the buffers to begin the scene.
            D3D.BeginScene(0, 0, 0, 1f);

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

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

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

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

            // Set the position of the billboard model.
            Vector3 modelPosition = new Vector3();

            modelPosition.X = 0.0f;
            modelPosition.Y = 1.5f;
            modelPosition.Z = 0.0f;

            // Calculate the rotation that needs to be applied to the billboard model to face the current camera position using the arc tangent function.
            double angle = Math.Atan2(modelPosition.X - cameraPosition.X, modelPosition.Z - cameraPosition.Z) * (180.0f / Math.PI);
            // Convert rotation into radians.
            float rotation = (float)angle * 0.0174532925f;

            // Setup the rotation the billboard at the origin using the world matrix.
            Matrix.RotationY(rotation, out worldMatrix);
            // Setup the translation matrix from the billboard model.
            Matrix translationMatrix = Matrix.Translation(modelPosition.X, modelPosition.Y, modelPosition.Z);

            // Finally combine the rotation and translation matrices to create the final world matrix for the billboard model.
            Matrix.Multiply(ref worldMatrix, ref translationMatrix, out worldMatrix);

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

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

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

            return(true);
        }
        private bool RenderScene()
        {
            // Clear the buffer to begin the scene as Black.
            D3D.BeginScene(0, 0, 0, 1f);

            // Generate the view matrix based on the camera 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;

            //// Rotate the world matrix by the rotation value so that the triangle 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 with the texture shader.
            if (!TextureShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).First()))
            {
                return(false);
            }

            // Get the world matrix again and translate down for the floor model to render underneath the cube.
            worldMatrix = D3D.WorldMatrix;
            Matrix.Translation(0, -1.5f, 0, out worldMatrix);

            // Get the camera reflection view matrix.
            var reflectionMatrix = Camera.ReflectionViewMatrix;

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

            // Render the floor model using the reflection shader, reflection texture, and reflection view matrix.
            if (!ReflectionShader.Render(D3D.DeviceContext, FloorModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, FloorModel.TextureCollection.Select(item => item.TextureResource).First(), RenderTexture.ShaderResourceView, reflectionMatrix))
            {
                return(false);
            }

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

            return(true);
        }