首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SharpDx direct3d11如何开始呈现

SharpDx direct3d11如何开始呈现
EN

Stack Overflow用户
提问于 2017-02-20 11:02:23
回答 1查看 2.7K关注 0票数 0

我想在C#上使用directx,我使用的是SharpDX包装器。我有一本叫做Direct3D渲染食谱的书,我从中得到了基本代码。我想要创建一个三维世界视图。为此,我需要一个相机视图和一个网格,以帮助识别世界位置,就像在Autodesk Maya,但我不知道如何做。我的头脑是团结和我应该做什么开始?

在这里,我有一些代码已经准备好呈现一些我认为:

代码语言:javascript
运行
复制
using System;
using SharpDX.Windows;
using SharpDX.DXGI;
using SharpDX.Direct3D11;

using Device = SharpDX.Direct3D11.Device;
using Device1 = SharpDX.Direct3D11.Device1;

namespace CurrencyConverter
{
    static class Program
    {[STAThread]
            static void Main()
            {
                // Enable object tracking
                SharpDX.Configuration.EnableObjectTracking = true;
                SharpDX.Animation.Timer timer = new SharpDX.Animation.Timer();

                #region Direct3D Initialization
                // Create the window to render to
                Form1 form = new Form1();
                form.Text = "D3DRendering - EmptyProject";
                form.Width = 640;
                form.Height = 480;
                // Declare the device and swapChain vars
                Device device;
                SwapChain swapChain;
                // Create the device and swapchain
                // First create a regular D3D11 device
                using (var device11 = new Device(
                 SharpDX.Direct3D.DriverType.Hardware,
                 DeviceCreationFlags.None,
                 new[] {
     SharpDX.Direct3D.FeatureLevel.Level_11_1,
     SharpDX.Direct3D.FeatureLevel.Level_11_0,
                 }))
                {
                    // Query device for the Device1 interface (ID3D11Device1)
                    device = device11.QueryInterfaceOrNull<Device1>();
                    if (device == null)
                        throw new NotSupportedException(
                        "SharpDX.Direct3D11.Device1 is not supported");
                }// Rather than create a new DXGI Factory we reuse the
                 // one that has been used internally to create the device
                using (var dxgi = device.QueryInterface<SharpDX.DXGI.Device2>())
                using (var adapter = dxgi.Adapter)
                using (var factory = adapter.GetParent<Factory2>())
                {
                    var desc1 = new SwapChainDescription1()
                    {
                        Width = form.ClientSize.Width,
                        Height = form.ClientSize.Height,
                        Format = Format.R8G8B8A8_UNorm,
                        Stereo = false,
                        SampleDescription = new SampleDescription(1, 0),
                        Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
                        BufferCount = 1,
                        Scaling = Scaling.Stretch,
                        SwapEffect = SwapEffect.Discard,
                    };
                    swapChain = new SwapChain1(factory,
                    device,
                    form.Handle,
                    ref desc1,
                    new SwapChainFullScreenDescription()
                    {
                        RefreshRate = new Rational(60, 1),
                        Scaling = DisplayModeScaling.Centered,
                        Windowed = true
                    },
                    // Restrict output to specific Output (monitor)
                    adapter.Outputs[0]);
                }

                // Create references for backBuffer and renderTargetView
                var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain,
               0);
                var renderTargetView = new RenderTargetView(device,
               backBuffer);
                #endregion

                // Setup object debug names
                device.DebugName = "The Device";
                swapChain.DebugName = "The SwapChain";
                backBuffer.DebugName = "The Backbuffer";
                renderTargetView.DebugName = "The RenderTargetView";

                #region Render loop
                // Create and run the render loop
                RenderLoop.Run(form, () =>
                {
                    // Clear the render target with...
                    var lerpColor = SharpDX.Color.Lerp(SharpDX.Color.White,
     SharpDX.Color.DarkBlue,
     (float)((timer.Time) / 10.0 % 1.0));
                    device.ImmediateContext.ClearRenderTargetView(
                     renderTargetView,
                     lerpColor);

                    // Execute rendering commands here...
                    //...
                    //I DO NOT HAVE ANY IDEA
                    //...
                    // Present the frame
                    swapChain.Present(0, PresentFlags.RestrictToOutput); 
                });
                #endregion

                #region Direct3D Cleanup
                // Release the device and any other resources created
                renderTargetView.Dispose();
                backBuffer.Dispose();
                device.Dispose();
                swapChain.Dispose();
                #endregion
            }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-23 12:02:18

一般来说,在使用Direct3D之前,需要大量代码才能在屏幕上发生任何事情。

在SharpDX存储库中,您有一个MiniCube示例,它包含足够让您真正开始工作的内容,因为它拥有绘制3d场景所需的所有元素。

我建议特别注意:

  • 深度缓冲区创建(DepthStencilView)
  • fx文件,因为您需要着色器在屏幕上有任何东西(不再有固定功能)
  • 如何创建顶点缓冲区,您需要在三角形中拆分几何图形(在普通情况下,还有其他可能性)。
  • 不要忘记SetViewport (省略它是很常见的)
  • 引用输入汇编程序的调用分配要绘制的几何图形。
  • 常量缓冲区创建:这是为了传递矩阵和更改数据(如漫射)。
  • 还要确保DeviceCreationFlags.None具有Device.CreateWithSwapChain调用,并在visual调试选项中使用“启用本机代码调试”。如果没有正确设置某些内容,这将给您提供错误和警告,另外,如果资源创建失败(而不是“无效的Args",这是毫无意义的),这是一个有意义的原因。
  • 作为另一项建议,所有Direct3D11资源创建参数都非常容易出错,而且非常繁琐(许多选项彼此不兼容),因此将这些选项封装到一些更容易使用的辅助函数中非常重要(并进行少量单元测试来一次性验证它们)。旧的工具包有很多这样的例子
  • SharpDX包装器相对于c++包装器比较接近,因此c++文档中的任何内容也适用于它。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42342814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档