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); } }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)directControl.ClientSize.Width, (uint)directControl.ClientSize.Height, sd.BufferDescription.Format, sd.Options); SetViews(); // Update the projection matrix projectionMatrix = DXUtil.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.25f, ((float)directControl.ClientSize.Width / (float)directControl.ClientSize.Height), 0.5f, 100.0f); projectionVariable.Matrix = projectionMatrix; } Matrix4x4F worldMatrix; currentTime = (Environment.TickCount - dwTimeStart) / 1000.0f; //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial //360 degrees == 2 * Math.PI //world matrix rotates the first cube by t degrees RotateTransform3D rt1 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), currentTime * 30)); worldMatrix = rt1.Value.ToMatrix4x4F(); // Modify the color meshColor.X = ((float)Math.Sin(currentTime * 1.0f) + 1.0f) * 0.5f; meshColor.Y = ((float)Math.Cos(currentTime * 3.0f) + 1.0f) * 0.5f; meshColor.Z = ((float)Math.Sin(currentTime * 5.0f) + 1.0f) * 0.5f; // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix; meshColorVariable.FloatVector = meshColor; // // Render the cube // TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } swapChain.Present(0, PresentOptions.None); }
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); } }