Пример #1
0
        /// <summary>
        /// Renders all visual elements in the scene. This is called by the main
        /// Render() function, and also by the RenderIntoCubeMap() function.
        /// </summary>
        void RenderScene()
        {
            // Render the skybox

            // Save current state
            Matrix matViewSave, matProjSave;

            matViewSave = device.Transform.View;
            matProjSave = device.Transform.Projection;

            // Disable zbuffer, center view matrix, and set FOV to 90 degrees
            Matrix matView = matViewSave;
            Matrix matProj = matViewSave;

            matView.M41 = matView.M42 = matView.M43 = 0.0f;
            matProj     = Matrix.PerspectiveFovLH((float)Math.PI / 2, 1.0f, 0.5f, 10000.0f);
            device.Transform.Projection      = matProj;
            device.Transform.View            = matView;
            device.RenderState.ZBufferEnable = false;

            // Render the skybox
            skyBox.Render(device);

            // Restore the render states
            device.Transform.Projection      = matProjSave;
            device.Transform.View            = matViewSave;
            device.RenderState.ZBufferEnable = true;

            // Render any other elements of the scene here. In this sample, only a
            // skybox is render, but a much more interesting scene could be rendered
            // instead.
        }
Пример #2
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d rendering. This
        /// function sets up render states, clears the viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0);

            device.BeginScene();


            // Stage 0 is the base texture, with the height map in the alpha channel
            device.SetTexture(0, embossTexture);
            device.TextureState[0].TextureCoordinateIndex = 0;
            device.TextureState[0].ColorOperation         = TextureOperation.Modulate;
            device.TextureState[0].ColorArgument1         = TextureArgument.TextureColor;
            device.TextureState[0].ColorArgument2         = TextureArgument.Diffuse;
            device.TextureState[0].AlphaOperation         = TextureOperation.SelectArg1;
            device.TextureState[0].AlphaArgument1         = TextureArgument.TextureColor;

            if (isShowingEmbossMethod)
            {
                // Stage 1 passes through the RGB channels (SelectArg2 = Current), and
                // does a signed add with the inverted alpha channel. The texture coords
                // associated with Stage 1 are the shifted ones, so the result is:
                //    (height - shifted_height) * tex.RGB * diffuse.RGB
                device.SetTexture(1, embossTexture);
                device.TextureState[1].TextureCoordinateIndex = 1;
                device.TextureState[1].ColorOperation         = TextureOperation.SelectArg2;
                device.TextureState[1].ColorArgument1         = TextureArgument.TextureColor;
                device.TextureState[1].ColorArgument2         = TextureArgument.Current;
                device.TextureState[1].AlphaOperation         = TextureOperation.AddSigned;
                device.TextureState[1].AlphaArgument1         = TextureArgument.TextureColor | TextureArgument.Complement;
                device.TextureState[1].AlphaArgument2         = TextureArgument.Current;

                // Set up the alpha blender to multiply the alpha channel (monochrome emboss)
                // with the src color (lighted texture)
                device.RenderState.AlphaBlendEnable = true;
                device.RenderState.SourceBlend      = Blend.SourceAlpha;
                device.RenderState.DestinationBlend = Blend.Zero;
            }

            // Render the object
            renderObject.Render(device);

            // Restore render states
            device.TextureState[1].ColorOperation = TextureOperation.Disable;
            device.TextureState[1].AlphaOperation = TextureOperation.Disable;
            device.RenderState.AlphaBlendEnable   = false;

            // Output statistics
            drawingFont.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            drawingFont.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats);
            drawingFont.DrawText(2, 40, System.Drawing.Color.Yellow, "Move the light with the mouse");
            drawingFont.DrawText(2, 60, System.Drawing.Color.Yellow, "Emboss-mode:");
            drawingFont.DrawText(130, 60, System.Drawing.Color.White, isShowingEmbossMethod ? "ON" : "OFF");

            device.EndScene();
        }
Пример #3
0
        /// <summary>
        /// Renders all objects in the scene.
        /// </summary>
        public void RenderScene()
        {
            Matrix matLocal, matWorldSaved;

            matWorldSaved = device.Transform.World;

            // Build the local matrix
            matLocal = Matrix.Multiply(teapotMatrix, matWorldSaved);
            device.Transform.World = matLocal;

            // Render the object
            teapotMesh.Render(device);

            device.Transform.World = matWorldSaved;

            // Output statistics
            drawingFont.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            drawingFont.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats);
        }
Пример #4
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d rendering. This
        /// function sets up render states, clears the viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);

            device.BeginScene();

            if (useShader)
            {
                device.VertexFormat = BlendVertex.Format;
                device.VertexShader = shader;
                device.SetStreamSource(0, vertexBuffer, 0, DXHelp.GetTypeSize(typeof(BlendVertex)));
                device.Indices = indexBuffer;
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numFaces);
            }
            else
            {
                device.VertexShader = null;
                // Enable vertex blending using API
                device.Transform.World  = upperArm;
                device.Transform.World1 = lowerArm;
                renderState.VertexBlend = VertexBlend.OneWeights;

                // Display the object
                blendObject.Render(device);
            }


            // Output statistics
            drawingFont.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            drawingFont.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats);

            if (useShader)
            {
                drawingFont.DrawText(2, 40, System.Drawing.Color.White, "Using vertex shader");
            }
            else
            {
                drawingFont.DrawText(2, 40, System.Drawing.Color.White, "Using RenderState.VertexBlend");
            }

            device.EndScene();
        }
Пример #5
0
        /// <summary>
        /// Renders the scene
        /// </summary>
        private void RenderScene()
        {
            // Render terrain
            device.Transform.World = terrainMatrix;
            terrainMesh.Render(device);

            // Draw the mirror
            device.SetTexture(0, null);
            device.Material        = mirrorMaterial;
            device.Transform.World = mirrorMatrix;
            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.SourceBlend      = Blend.DestinationColor;
            device.RenderState.DestinationBlend = Blend.Zero;
            device.VertexFormat = MirrorVertex.Format;
            device.SetStreamSource(0, mirrorVertexBuffer, 0);
            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            device.RenderState.AlphaBlendEnable = false;

            // Draw the object. Note: do this last, in case the object has alpha
            device.Transform.World = helicopterMatrix;
            helicopterMesh.Render(device);
        }
Пример #6
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d rendering. This
        /// function sets up render states, clears the viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0);

            device.BeginScene();
            // Center view matrix for skybox and disable zbuffer
            Matrix matView, matViewSave;

            matViewSave                      = device.Transform.View;
            matView                          = matViewSave;
            matView.M41                      = 0.0f; matView.M42 = -3.0f; matView.M43 = 0.0f;
            device.Transform.View            = matView;
            device.RenderState.ZBufferEnable = false;
            // Some cards do not disable writing to Z when
            // D3DRS_ZENABLE is FALSE. So do it explicitly
            device.RenderState.ZBufferWriteEnable = false;

            // Render the skybox
            skyBoxMesh.Render(device);

            // Restore the render states
            device.Transform.View                 = matViewSave;
            device.RenderState.ZBufferEnable      = true;
            device.RenderState.ZBufferWriteEnable = true;

            // Draw the terrain
            terrainMesh.Render(device);

            // Draw the trees
            DrawTrees();

            // Output statistics
            drawingFont.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            drawingFont.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats);

            device.EndScene();
        }
Пример #7
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d rendering. This
        /// function sets up render states, clears the viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, fogColor, 1.0f, 0);

            device.BeginScene();

            device.Transform.World = terrainMatrix;
            terrainObject.Render(device);

            device.Transform.World = objectMatrix;
            airplane.Render(device, true, false);

            // Render the shadow volume into the stencil buffer, then add it into
            // the scene
            RenderShadow();
            DrawShadow();

            // Output statistics
            font.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            font.DrawText(2, 20, System.Drawing.Color.Yellow, deviceStats);

            device.EndScene();
        }
Пример #8
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d
        /// rendering. This function sets up render states, clears the
        /// viewport, and renders the scene.
        /// </summary>
        protected override void Render()
        {
            // Begin the scene
            device.BeginScene();
            // Render the Skybox
            {
                Matrix world = Matrix.Scaling(10.0f, 10.0f, 10.0f);

                Matrix view = matView;
                view.M41 = view.M42 = view.M43 = 0.0f;

                device.Transform.World      = world;
                device.Transform.View       = view;
                device.Transform.Projection = matProject;

                device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
                device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
                device.SamplerState[0].MinFilter      = TextureFilter.Linear;
                device.SamplerState[0].MagFilter      = TextureFilter.Linear;
                device.SamplerState[0].AddressU       = TextureAddress.Wrap;
                device.SamplerState[0].AddressV       = TextureAddress.Wrap;

                // Always pass Z-test, so we can avoid clearing color and depth buffers
                device.RenderState.ZBufferFunction = Compare.Always;
                skyBox.Render(device);
                device.RenderState.ZBufferFunction = Compare.LessEqual;
            }

            // Render the environment-mapped ShinyTeapot
            {
                // Set transform state
                Matrix viewProject = Matrix.Multiply(matView, matProject);

                Matrix  matViewInv  = Matrix.Invert(matView);
                Vector4 vecPosition = new Vector4(matViewInv.M41, matViewInv.M42, matViewInv.M43, 1.0f);

                effect.SetValue("matWorld", matWorld);
                effect.SetValue("matViewProject", viewProject);
                effect.SetValue("vecPosition", vecPosition);


                // Draw teapot
                VertexBuffer vb = shinyTeapot.LocalVertexBuffer;
                IndexBuffer  ib = shinyTeapot.LocalIndexBuffer;

                device.VertexFormat = shinyTeapot.LocalMesh.VertexFormat;

                device.SetStreamSource(0, vb, 0, DXHelp.GetTypeSize(typeof(EnvMappedVertex)));
                device.Indices = ib;

                int passes = effect.Begin(0);

                for (int iPass = 0; iPass < passes; iPass++)
                {
                    effect.Pass(iPass);

                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0,
                                                 0, shinyTeapot.LocalMesh.NumberVertices,
                                                 0, shinyTeapot.LocalMesh.NumberFaces);
                }

                effect.End();
            }



            // Output statistics
            font.DrawText(2, 1, System.Drawing.Color.Yellow, frameStats);
            font.DrawText(2, 21, System.Drawing.Color.Yellow, deviceStats);

            // End the scene.
            device.EndScene();
        }
Пример #9
0
        /// <summary>
        /// Renders all visual elements in the scene. This is called by the main
        /// Render() function, and also by the RenderIntoCubeMap() function.
        /// </summary>
        private void RenderScene(Matrix View, Matrix Project, bool canRenderTeapot)
        {
            // Render the Skybox
            {
                Matrix matWorld = Matrix.Scaling(10.0f, 10.0f, 10.0f);

                Matrix matView = View;
                matView.M41 = matView.M42 = matView.M43 = 0.0f;

                device.Transform.World      = matWorld;
                device.Transform.View       = matView;
                device.Transform.Projection = Project;

                device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
                device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
                device.SamplerState[0].MinFilter      = TextureFilter.Linear;
                device.SamplerState[0].MagFilter      = TextureFilter.Linear;
                device.SamplerState[0].AddressU       = TextureAddress.Mirror;
                device.SamplerState[0].AddressV       = TextureAddress.Mirror;

                // Always pass Z-test, so we can avoid clearing color and depth buffers
                device.RenderState.ZBufferFunction = Compare.Always;
                skyBoxMesh.Render(device);
                device.RenderState.ZBufferFunction = Compare.LessEqual;
            }


            // Render the Airplane
            {
                device.Transform.World      = airplaneMatrix;
                device.Transform.View       = View;
                device.Transform.Projection = Project;

                device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
                device.TextureState[0].ColorOperation = TextureOperation.SelectArg1;
                device.SamplerState[0].MinFilter      = TextureFilter.Linear;
                device.SamplerState[0].MagFilter      = TextureFilter.Linear;
                device.SamplerState[0].AddressU       = TextureAddress.Wrap;
                device.SamplerState[0].AddressV       = TextureAddress.Wrap;

                airplaneMesh.Render(device, true, false);

                device.Transform.World = worldMatrix;
            }

            // Render the environment-mapped ShinyTeapot
            if (canRenderTeapot)
            {
                // Set transform state
                if (cubeMap != null)
                {
                    effect.SetValue(worldMatrixHandle, worldMatrix);
                    effect.SetValue(viewMatrixHandle, View);
                }
                else
                {
                    Matrix matWorldView = Matrix.Multiply(worldMatrix, View);
                    effect.SetValue(worldViewMatrixHandle, matWorldView);
                }

                effect.SetValue(projectionMatrixHandle, Project);


                // Draw teapot
                VertexBuffer tempVertexBuffer = shinyTeapotMesh.LocalVertexBuffer;
                IndexBuffer  tempIndexBuffer  = shinyTeapotMesh.LocalIndexBuffer;
                int          numVert          = shinyTeapotMesh.LocalMesh.NumberVertices;
                int          numFace          = shinyTeapotMesh.LocalMesh.NumberFaces;

                device.SetStreamSource(0, tempVertexBuffer, 0, DXHelp.GetTypeSize(typeof(EnvMappedVertex)));
                device.VertexFormat = EnvMappedVertex.Format;
                device.Indices      = tempIndexBuffer;

                int Passes = effect.Begin(0);

                for (int iPass = 0; iPass < Passes; iPass++)
                {
                    effect.Pass(iPass);

                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                 0, 0, numVert, 0, numFace);
                }

                effect.End();
            }
        }