Ejemplo n.º 1
0
        /// <summary>
        /// This callback function will be called at the end of every frame to perform all the
        /// rendering calls for the scene, and it will also be called if the window needs to be
        /// repainted. After this function has returned, the sample framework will call
        /// Device.Present to display the contents of the next buffer in the swap chain
        /// </summary>
        public void OnFrameRender(Device device, double appTime, float elapsedTime)
        {
            bool beginSceneCalled = false;

            // Clear the render target and the zbuffer
            device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
            try
            {
                device.BeginScene();
                beginSceneCalled = true;

                // Get the world matrix
                Matrix worldMatrix = worldCenter * camera.WorldMatrix;

                if ((meshes != null) && (meshes.Length > 0))
                {
                    // Update the effect's variables.  Instead of using strings, it would
                    // be more efficient to cache a handle to the parameter by calling
                    // Effect.GetParameter
                    effect.SetValue("g_mWorldViewProjection", worldMatrix * camera.ViewMatrix * camera.ProjectionMatrix);
                    effect.SetValue("g_mWorld", worldMatrix);

                    // Set and draw each of the materials in the mesh
                    for (int i = 0; i < meshMaterials.Length; i++)
                    {
                        effect.SetValue("g_vDiffuse", meshMaterials[i].DiffuseColor);
                        effect.SetValue("g_txScene", meshTextures[i]);
                        int passes = effect.Begin(0);
                        for (int pass = 0; pass < passes; pass++)
                        {
                            effect.BeginPass(pass);
                            if (isShowingOptimized)
                            {
                                meshes[currentMeshIndex].DrawSubset(i);
                            }
                            else
                            {
                                fullMesh.DrawSubset(i);
                            }
                            effect.EndPass();
                        }
                        effect.End();
                    }
                }
                // Show frame rate
                RenderText();

                // Show UI
                hud.OnRender(elapsedTime);
                sampleUi.OnRender(elapsedTime);
            }
            finally
            {
                if (beginSceneCalled)
                {
                    device.EndScene();
                }
            }
        }
Ejemplo n.º 2
0
 internal void DoRender(Device device, int i)
 {
     // set the device material and texture
     device.Material = meshMaterials[i];
     device.SetTexture(0, meshTextures[i]);
     if (useProgessive >= 0 && useProgessive < 10)
     {
         pmesh.DrawSubset(i);
     }
     else
     {
         mesh.DrawSubset(i);
     }
 }
Ejemplo n.º 3
0
 public void Render()            //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架
 {
     if (device == null)
     {
         return;
     }
     device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.WhiteSmoke, 1.0f, 0);
     device.BeginScene();
     SetupMatrices();
     for (int i = 0; i < meshMaterials1.Length; i++) //Mesh中可能有多个3D图形,逐一显示
     {
         device.Material = meshMaterials1[i];        //设定3D图形的材质
         device.SetTexture(0, meshTextures[i]);      //设定3D图形的纹理
         mesh2.DrawSubset(i);                        //显示该3D图形
     }
     device.EndScene();
     device.Present();
 }
Ejemplo n.º 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()
        {
            if (!initDone)
            {
                return;
            }

            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);

            device.BeginScene();

            if (pmeshes != null)
            {
                // Set and draw each of the materials in the mesh
                for (int i = 0; i < meshMaterials.Length; i++)
                {
                    device.Material = meshMaterials[i];
                    device.SetTexture(0, meshTextures[i]);
                    if (showOptimized)
                    {
                        pmeshes[currentPmesh].DrawSubset(i);
                    }
                    else
                    {
                        fullPmesh.DrawSubset(i);
                    }
                }
            }

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

            if (showOptimized)
            {
                if (pmeshes == null)
                {
                    font.DrawText(2, 60, System.Drawing.Color.Yellow, "Unoptimized");
                    font.DrawText(2, 40, System.Drawing.Color.Yellow, string.Format("Num Vertices = {0}, Optimized {1}", 0, 0.0f));
                }
                else
                {
                    font.DrawText(2, 60, System.Drawing.Color.Yellow, string.Format("PMesh {0} out of {1}", currentPmesh + 1, pmeshes.Length));

                    font.DrawText(2, 40, System.Drawing.Color.Yellow, string.Format("Num Vertices = {0}, Min = {1}, Max = {2}",
                                                                                    ((BaseMesh)pmeshes[currentPmesh]).NumberVertices,
                                                                                    pmeshes[currentPmesh].MinVertices,
                                                                                    pmeshes[currentPmesh].MaxVertices));
                }
            }
            else
            {
                font.DrawText(2, 60, System.Drawing.Color.Yellow, "Unoptimized");
                font.DrawText(2, 40, System.Drawing.Color.Yellow, string.Format("Num Vertices = {0}, Min = {1}, Max = {2}",
                                                                                ((BaseMesh)fullPmesh).NumberVertices,
                                                                                fullPmesh.MinVertices,
                                                                                fullPmesh.MaxVertices));
            }

            // Output text
            if (displayHelp)
            {
                font.DrawText(2, 80, System.Drawing.Color.White, "<F1>\n\n<Up>\n<Down>\n\n<PgUp>\n<PgDn>\n\n<Home>\n<End>\n<o>", 0);
                font.DrawText(70, 80, System.Drawing.Color.White, "Toggle help\n\nAdd one vertex\nSubtract one vertex\n\nAdd 100 vertices\nSubtract 100 vertices\n\nMax vertices\nMin vertices\nShow optimized pmeshes");
            }
            else
            {
                font.DrawText(2, 80, System.Drawing.Color.White, "<F1>\n");
                font.DrawText(70, 80, System.Drawing.Color.White, "Toggle help\n");
            }


            device.EndScene();
        }