Пример #1
0
        void RenderScene()
        {
            lock (syncObject)
            {
                if (device == null)
                {
                    CreateDeviceResources();
                }

                if (!pause)
                {
                    if (lastSavedDelta != 0)
                    {
                        startTime      = Environment.TickCount - lastSavedDelta;
                        lastSavedDelta = 0;
                    }
                    currentTimeVariation = (Environment.TickCount - startTime) / 6000.0f;
                    worldMatrix          = MatrixMath.MatrixTranslate(0, 0, currentTimeVariation);
                    textBrush.Transform  = Matrix3x2F.Translation(0, (4096f / 16f) * currentTimeVariation);
                }

                device.ClearDepthStencilView(
                    depthStencilView,
                    ClearOptions.Depth,
                    1,
                    0
                    );

                // Clear the back buffer
                device.ClearRenderTargetView(renderTargetView, backColor);

                diffuseVariable.Resource = null;

                technique.GetPassByIndex(0).Apply();

                // Draw the D2D content into our D3D surface
                RenderD2DContentIntoSurface();

                diffuseVariable.Resource = textureResourceView;

                // Update variables
                worldMatrixVariable.Matrix = worldMatrix;

                // Set index buffer
                device.IA.IndexBuffer = new IndexBuffer(facesIndexBuffer, Format.R16UInt, 0);

                // Draw the scene
                technique.GetPassByIndex(0).Apply();

                device.DrawIndexed((uint)Marshal.SizeOf(VertexArray.VerticesInstance), 0, 0);

                swapChain.Present(0, Microsoft.WindowsAPICodePack.DirectX.Graphics.PresentOptions.None);
            }
        }
Пример #2
0
        void RenderScene()
        {
            lock (syncObject)
            {
                //initialize D3D device and D2D render targets the first time we get here
                if (device == null)
                {
                    CreateDeviceResources();
                }

                //tick count is used to control animation and calculate FPS
                int currentTime = Environment.TickCount;
                if (!startTime.HasValue)
                {
                    startTime = currentTime;
                }

                currentTicks = currentTime - startTime.GetValueOrDefault();

                float a = (currentTicks * 360.0f) * ((float)Math.PI / 180.0f) * 0.0001f;
                worldMatrix = MatrixMath.MatrixRotationY(a);

                // Swap chain will tell us how big the back buffer is
                SwapChainDescription swapDesc = swapChain.Description;
                uint nWidth  = swapDesc.BufferDescription.Width;
                uint nHeight = swapDesc.BufferDescription.Height;

                device.ClearDepthStencilView(
                    depthStencilView, ClearOptions.Depth,
                    1, 0
                    );

                // Draw a gradient background before we draw the cube
                if (backBufferRenderTarget != null)
                {
                    backBufferRenderTarget.BeginDraw();

                    backBufferGradientBrush.Transform =
                        Matrix3x2F.Scale(
                            backBufferRenderTarget.Size,
                            new Point2F(0.0f, 0.0f));

                    RectF rect = new RectF(
                        0.0f, 0.0f,
                        nWidth,
                        nHeight);

                    backBufferRenderTarget.FillRectangle(rect, backBufferGradientBrush);
                    backBufferRenderTarget.EndDraw();
                }

                diffuseVariable.Resource = null;
                technique.GetPassByIndex(0).Apply();

                // Draw the D2D content into a D3D surface.
                RenderD2DContentIntoTexture();

                // Pass the updated texture to the pixel shader
                diffuseVariable.Resource = textureResourceView;

                // Update variables that change once per frame.
                worldVariable.Matrix = worldMatrix;

                // Set the index buffer.
                device.IA.IndexBuffer = new IndexBuffer(facesIndexBuffer, Format.R16UInt, 0);

                // Render the scene
                technique.GetPassByIndex(0).Apply();

                device.DrawIndexed(vertexArray.s_FacesIndexArray.Length, 0, 0);

                // Update fps
                currentTime  = Environment.TickCount; // Get the ticks again
                currentTicks = currentTime - startTime.GetValueOrDefault();
                if ((currentTime - lastTicks) > 250)
                {
                    fps       = (swapChain.LastPresentCount) / (currentTicks / 1000f);
                    lastTicks = currentTime;
                }

                backBufferRenderTarget.BeginDraw();

                // Draw fps
                backBufferRenderTarget.DrawText(
                    String.Format("Average FPS: {0:F1}", fps),
                    textFormatFps,
                    new RectF(
                        10f,
                        nHeight - 32f,
                        nWidth,
                        nHeight
                        ),
                    backBufferTextBrush
                    );

                backBufferRenderTarget.EndDraw();

                swapChain.Present(0, Microsoft.WindowsAPICodePack.DirectX.Graphics.PresentOptions.None);
            }
        }