public bool Render()
        {
            // Clear the buffer to begin the scene.
            D3D.BeginScene(0f, 0f, 0f, 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;

            // 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 multitexture shader.
            if (!MultiTextureLightShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).ToArray()))
            {
                return(false);
            }

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

            return(true);
        }
Exemple #2
0
        public bool Render()
        {
            // Clear the buffer to begin the scene.
            D3D.BeginScene(0f, 0f, 0f, 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;
            var orthoMatrix      = D3D.OrthoMatrix;

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

            // Initialize the count of the models that have been rendered.
            var renderCount = 0;

            Vector3 position;
            Vector4 color;

            // Go through all models and render them only if they can seen by the camera view.
            for (int index = 0; index < ModelList.ModelCount; index++)
            {
                // Get the position and color of the sphere model at this index.
                ModelList.GetData(index, out position, out color);

                // Set the radius of the sphere to 1.0 since this is already known.
                var radius = 1.0f;

                // Check if the sphere model is in the view frustum.
                var renderModel = Frustum.CheckSphere(position, radius);

                // If it can be seen then render it, if not skip this model and check the next sphere.
                if (renderModel)
                {
                    // Move the model to the location it should be rendered at.
                    worldMatrix = Matrix.Translation(position);

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

                    // Render the model using the color shader.
                    if (!MultiTextureLightShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).ToArray(), Light.Direction, Light.AmbientColor, color, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower))
                    {
                        return(false);
                    }

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

                    // Since this model was rendered then increase the count for this frame.
                    renderCount++;
                }
            }

            // Set the number of the models that was actually rendered this frame.
            if (!Text.SetRenderCount(renderCount, 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);
        }