【问题标题】:SharpDx direct3d11 how to start renderingSharpDx direct3d11如何开始渲染
【发布时间】:2017-02-20 11:02:23
【问题描述】:

我想在 C# 上使用 directx,我正在使用 SharpDX 包装器。我得到了一本名为 Direct3D 渲染食谱的书,并从中获得了基本代码。我想创建一个 3d 世界观。为此,我将需要一个摄像机视图和一个网格,以帮助识别世界位置,就像在 Autodesk Maya 中一样,但我不知道该怎么做。我的心情很复杂,我该怎么做才能开始?

这里我有代码可以渲染我认为的东西:

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
            }
}
}

【问题讨论】:

    标签: c# direct3d sharpdx direct3d11


    【解决方案1】:

    一般来说,使用 Direct3D,您需要大量代码才能在屏幕上显示任何内容。

    在 SharpDX 存储库中,您有 MiniCube 示例,其中包含的内容足以让您真正入门,因为它包含绘制 3d 场景所需的所有元素。

    我建议特别寻找:

    • 深度缓冲区创建 (DepthStencilView)
    • fx 文件,因为您需要着色器才能在屏幕上显示任何内容(没有更多固定功能)
    • 如何创建顶点缓冲区,您需要将几何图形分割成三角形(在常见情况下,还有其他可能性)。
    • 不要忘记 SetViewport(省略它真的很常见)
    • 引用 Input Assembler 的调用正在分配要绘制的几何图形
    • 恒定缓冲区创建:这是为了传递矩阵和变化的数据(如漫反射)
    • 还要确保 DeviceCreationFlags.None 与 Device.CreateWithSwapChain 调用一起使用,并在 Visual Studio 调试选项中使用“启用本机代码调试”。如果设置不正确,这将为您提供错误和警告,加上一个有意义的原因以防资源创建失败(而不是“无效的参数”,这是毫无意义的)。
    • 作为另一项建议,所有 Direct3D11 资源创建参数都极易出错且繁琐(许多选项彼此不兼容),因此将它们包装成一些更易于使用的辅助函数非常重要(并制作少量单元测试以一劳永逸地验证它们)。旧的Toolkit 有很多这样的例子
    • SharpDX 包装器相对接近于 c++ 对应物,因此 c++ 文档中的任何内容也适用于它。

    【讨论】:

    • 感谢您的回复,我从不太复杂的一个开始。刚刚画了一个三角形,我移动它。现在我遇到了新问题,我应该怎么做才能在我已经渲染的场景上用 direct2d 绘制一些东西?
    • 一般情况下(所以不要混淆所有内容并在 cmets 中丢失有用的回复),最好提出一个新问题,因为这样更容易获得具体答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多