1
votes

I try to follow their examples on github and i just want to draw a line. But when the window opens, there only a blackscreen. I dont know what i did wrong.

Here is my source code. Thank you for helping !

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();
            });
        }
1
Did you try to change second line vertex coordinates to ones that are different from (10,10)?Mykola
Yes i tried but nothing changeAdfop
I did not work with SharpDX, mostly with SlimDX, but I think that you need to setup a projection matrix for your scene before trying to display anything. Orthographic projection can be used to display 2d objects.Mykola
I'm a newbie in video games dev I dont know how to do this !Adfop
The last reference also shows how to setup Orthographic projection using device.SetTransform method.Mykola

1 Answers

-1
votes

This might not be your error but it won't help matters because even if there are no errors you will see very little. I have used it but only in a window context and I followed https://github.com/Dan6040/SharpDX-Rastertek-Tutorials because I was coming from C++ Unreal, Raw DX9, and C# Unity, XNA framework.

Your vertices are drawing a 1-pixel dot because you need 2 vectors at different locations to draw a line

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

That is saying draw from x:10 y:10 to x:10 y:10

try:

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

So to better explain to draw a triangle you need 3 points and 3 lines, but to draw each line you need points in your ordering

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

So that works by saying draw from 10,10 to 10,30 then to 30,30 and finally back to 10,10

Just to note this is why Games engines don't work through arrays of points they are arrays of pointers to points so you only have the point objects in memory once and you just using pointers to them 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();
            });
        }