Beispiel #1
0
        public bool Render(float rotation)
        {
            Matrix worldMatrix, viewMatrix, projectionMatrix;
            float  refractionScale;

            // First set the refraction scale to modify how much perturbation occurs in the glass.
            // Set the refraction scale for the glass shader.
            refractionScale = 0.01f;

            // Clear the buffers to begin the scene.
            D3D.BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

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

            // Then render the 3D spinning cube scene as normal.
            // Multiply the world matrix by the rotation.
            Matrix.RotationY(rotation, out worldMatrix);

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

            // Render the cube model using the texture shader.
            if (!TextureShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).First()))
            {
                return(false);
            }

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

            // Now render the window model using the glass shader with the color texture, normal map, refraction render to texture, and refraction scale as input.
            // Translate to back where the window model will be rendered.
            Matrix.Translation(0.0f, 0.0f, -1.5f, out worldMatrix);

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

            // Render the window model using the glass shader.
            if (!GlassShader.Render(D3D.DeviceContext, WindowModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, WindowModel.TextureCollection.Select(item => item.TextureResource).First(), WindowModel.TextureCollection.Select(item => item.TextureResource).ToArray()[1], RenderTexture.ShaderResourceView, refractionScale))
            {
                return(false);
            }

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

            return(true);
        }
    public static void Main(string[] args)
    {
        // Create shaders:
        Shader solidRed  = new UnlitColorShader("red");
        Shader solidBlue = new UnlitColorShader("blue");
        Shader glass     = new GlassShader();

        // Create the models:
        Model cube   = new CubeModel(solidRed);
        Model monkey = new Suzanne(glass);

        // Draw the two models:
        cube.Draw();   // "Rendering Cube with a red color."
        monkey.Draw(); // "Rendering Suzanne to look like glass."

        // Switch out the shader on suzanne:
        monkey.SetShader(solidBlue);

        monkey.Draw(); // "Rendering Suzanne with a blue color."
    }
Beispiel #3
0
        public void Shutdown()
        {
            // Release the camera object.
            Camera = null;

            // Release the GlassShader object.
            GlassShader?.ShutDown();
            GlassShader = null;
            // Release the texture shader object.
            TextureShader?.ShutDown();
            TextureShader = null;
            // Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the model object.
            Model?.Shutdown();
            Model = null;
            // Release the model object.
            WindowModel?.Shutdown();
            WindowModel = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }