【问题标题】:Sharpdx D3D9 nothing drawSharpdx D3D9 什么都没有画
【发布时间】:2020-08-25 00:09:06
【问题描述】:

我尝试在 github 上遵循他们的示例,我只想画一条线。但是当窗口打开时,只有一个黑屏。我不知道我做错了什么。

这是我的源代码。 感谢您的帮助!

static void Main()
        {
            var form = new RenderForm("Test");
            int width = form.ClientSize.Width;
            int height = form.ClientSize.Height;
            var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(width, height) { PresentationInterval = PresentInterval.One });

            Line line = new Line(device);
            
            RawVector2[] vertices =
            {
                new RawVector2(10, 10),
                new RawVector2(10, 10)
            };
            

            RenderLoop.Run(form, () =>
            {
                
                device.Clear(ClearFlags.Target, new RawColorBGRA(0, 0, 0, 1), 1.0f, 0);
                device.BeginScene();

                line.Width = 10;
                line.GLLines = true;
                line.Antialias = false;
                line.Draw(vertices, new RawColorBGRA(254, 254, 254, 1));

                device.EndScene();
                device.Present();
            });
        }

【问题讨论】:

  • 您是否尝试将第二行顶点坐标更改为不同于 (10,10) 的坐标?
  • 是的,我试过了,但没有任何改变
  • 我没有使用 SharpDX,主要是使用 SlimDX,但我认为您需要在尝试显示任何内容之前为您的场景设置一个投影矩阵。正交投影可用于显示二维对象。
  • 我是电子游戏开发的新手,我不知道该怎么做!
  • 最后的参考资料还展示了如何使用 device.SetTransform 方法设置正交投影。

标签: c# sharpdx


【解决方案1】:

这可能不是您的错误,但这无济于事,因为即使没有错误,您也只会看到很少。我只在窗口上下文中使用过它,并且我关注了https://github.com/Dan6040/SharpDX-Rastertek-Tutorials,因为我来自 C++ Unreal、Raw DX9 和 C# Unity、XNA 框架。

您的顶点正在绘制一个 1 像素的点,因为您需要在不同位置的 2 个向量来绘制一条线

[
new RawVector2(10, 10),
new RawVector2(10, 10)
]

也就是说从 x:10 y:10 绘制到 x:10 y:10

尝试:

[
new RawVector2(10, 10),
new RawVector2(30, 30)
]

因此,为了更好地解释绘制三角形,您需要 3 个点和 3 条线,但要绘制每条线,您需要在排序中使用点

[
new RawVector2(10, 10),
new RawVector2(10, 30),
new RawVector2(30, 30),
new RawVector2(10, 10)
]

这样可以说从 10,10 到 10,30 然后到 30,30,最后回到 10,10

请注意,这就是为什么游戏引擎不通过点数组工作的原因,它们是指向点的指针数组,因此您在内存中只有一次点对象,并且您只使用指向它们的指针 E.G

static void Main()
        {
            var form = new RenderForm("Test");
            int width = form.ClientSize.Width;
            int height = form.ClientSize.Height;
            var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(width, height) { PresentationInterval = PresentInterval.One });

            Line line = new Line(device);
            
            RawVector2[] TrianglePoints=
            {
                new RawVector2(10, 10),
                new RawVector2(10, 30),
                new RawVector2(30, 30)
            };
            
            RawVector2[] vertices = {
                TrianglePoints[0],
                TrianglePoints[1],
                TrianglePoints[2],
                TrianglePoints[0]
            }

            RenderLoop.Run(form, () =>
            {
                
                device.Clear(ClearFlags.Target, new RawColorBGRA(0, 0, 0, 1), 1.0f, 0);
                device.BeginScene();

                line.Width = 10;
                line.GLLines = true;
                line.Antialias = false;
                line.Draw(vertices, new RawColorBGRA(254, 254, 254, 1));

                device.EndScene();
                device.Present();
            });
        }

【讨论】:

  • 感谢您的回复,但遗憾的是没有任何变化,黑屏仍然存在
  • 您是否已将 RawColorRGB 更改为类似 read 的内容,以查看它是否正在这样做,这可能是您的 DirectDraw Context 根本不起作用。
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
相关资源
最近更新 更多