Hello I dont know why but I seem to be

Hello I dont know why but I seem to be unable to use gl.DrawElements and ebo I'm surelly missing something. Il make a thread to post my code and render doc result
27 Replies
Jemy191
Jemy191OP3w ago
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using System.Drawing;

GL gl = null!;

var options = WindowOptions.Default;
options.Size = new Silk.NET.Maths.Vector2D<int>(800, 600);
options.Title = "Silk.NET OpenGL Triangle";

var window = Window.Create(options);

window.Load += OnLoad;
window.Render += OnRender;
window.Run();
return;

unsafe void OnLoad()
{
gl = GL.GetApi(window);
gl.ClearColor(Color.CornflowerBlue);

Span<uint> indices = [0, 1, 2];
Span<float> vertices =
[
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
];

var vertexBuffer = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexBuffer);

fixed (void* d = vertices) { gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), d, BufferUsageARB.StaticDraw); }

var indexBuffer = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, indexBuffer);

fixed (void* d = indices) { gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), d, BufferUsageARB.StaticDraw); }

var vertexArray = gl.GenVertexArray();
gl.BindVertexArray(vertexArray);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), IntPtr.Zero);
gl.EnableVertexAttribArray(0);
}

void OnRender(double delta)
{
gl.Clear((uint)ClearBufferMask.ColorBufferBit);
gl.DrawElements(PrimitiveType.Triangles, 3, DrawElementsType.UnsignedInt, UIntPtr.Zero);
}
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using System.Drawing;

GL gl = null!;

var options = WindowOptions.Default;
options.Size = new Silk.NET.Maths.Vector2D<int>(800, 600);
options.Title = "Silk.NET OpenGL Triangle";

var window = Window.Create(options);

window.Load += OnLoad;
window.Render += OnRender;
window.Run();
return;

unsafe void OnLoad()
{
gl = GL.GetApi(window);
gl.ClearColor(Color.CornflowerBlue);

Span<uint> indices = [0, 1, 2];
Span<float> vertices =
[
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
];

var vertexBuffer = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vertexBuffer);

fixed (void* d = vertices) { gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), d, BufferUsageARB.StaticDraw); }

var indexBuffer = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, indexBuffer);

fixed (void* d = indices) { gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), d, BufferUsageARB.StaticDraw); }

var vertexArray = gl.GenVertexArray();
gl.BindVertexArray(vertexArray);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), IntPtr.Zero);
gl.EnableVertexAttribArray(0);
}

void OnRender(double delta)
{
gl.Clear((uint)ClearBufferMask.ColorBufferBit);
gl.DrawElements(PrimitiveType.Triangles, 3, DrawElementsType.UnsignedInt, UIntPtr.Zero);
}
Also when I use (void*)0 instead of UIntPtr.Zero in DrawElement it throw this: System.ExecutionEngineException
Jemy191
Jemy191OP3w ago
When using DrawArray
No description
Jemy191
Jemy191OP3w ago
When using DrawElement
No description
Jemy191
Jemy191OP3w ago
Really weird that it won't work. I must be missing some little simple thing.
Aqua
Aqua3w ago
You are creating your vertex array after you create the buffers. This means that your BindBuffer calls are not being bound to the vertex array and are instead being bound to the “null” vertex array. Create and bind your vertex array before you create the buffers and see how it goes I’m slightly surprised that DrawArray works at all either
Jemy191
Jemy191OP3w ago
Me too that was thowing me off XD
Aqua
Aqua3w ago
You should also be using (void*) 0 instead of uintptr, as I believe silk will try and interpret that as a value and it won’t work I may be misremembering but I believe that is the case
Jemy191
Jemy191OP3w ago
Yeah if I use (void*) 0 with DrawElement it throw but with UIntPtr.Zero it just do not draw🙃
Aqua
Aqua3w ago
Yeah, the throwing behaviour is normal when somethings incorrect Seems better than it used to be tho, when I was first implementing silk on Linux it crashed my entire driver several times lol
Jemy191
Jemy191OP3w ago
So you said: Gen and bind the vertexArray, Gend, bind and send vertBuffer and gen, bind and send indexBuffer. Right? it wont work using the (void*) 0 too
Aqua
Aqua3w ago
Another issue I can see is that you are enabling the vertex attrib array after setting its pointer, I don’t know for sure but I’ve never seen it done in that order so I wouldn’t be surprised if it’s not working
Jemy191
Jemy191OP3w ago
So gen bind enable and Set pointer? It wont work either
Aqua
Aqua3w ago
Is this your entire code?
Jemy191
Jemy191OP3w ago
yes
Aqua
Aqua3w ago
Ah. You haven’t created a shader of any kind, you’ll need that to draw anything
Jemy191
Jemy191OP3w ago
But if I use DrawArrays it work and I have a black triangle🙃 Il add a shader to see if it help in any way
Aqua
Aqua3w ago
Yeah that’s very… odd… behaviour, definitely not spec compliant that’s for sure
Aqua
Aqua3w ago
I can recommend you follow the silk tutorials if you aren’t already, they go through everything in detail! https://dotnet.github.io/Silk.NET/docs/opengl/c1/2-hello-quad.html
Silk.NET - High-Speed & Advanced .NET Graphics & Compute
1.2 - Hello Quad
Jemy191
Jemy191OP3w ago
Omg yes I follow it a while ago. I want thinking there was no use of Ebo. I'll do that real quick
Aqua
Aqua3w ago
Don’t worry there’s full use of everything to draw a quad including element buffers and shaders!
Jemy191
Jemy191OP3w ago
Thanks for pointing me the obvious 😅 The tutorial code work. I started my project so long ago that I was thinking I did the basic stuff right🙃
Aqua
Aqua3w ago
Haha no worries it happens
Jemy191
Jemy191OP3w ago
Sometime it so simple and obvious that you don't think of it 🤣
Aqua
Aqua3w ago
Don’t worry about it, graphics programming is just like that sometimes lol
Jemy191
Jemy191OP3w ago
Yeah it pretty hard sometime XD. It kinda work in my engine now 🙂 My abstraction was making thing out of order😅 Little question should I not be able to create the vbo and vbo prior to creating the vao and just rebinding the vbo and vbo after the vao? Ex:
handle = this.gl.GenVertexArray();
Bind();
vbo.Bind();
ebo.Bind();
handle = this.gl.GenVertexArray();
Bind();
vbo.Bind();
ebo.Bind();
Aqua
Aqua3w ago
Yep you can do that as well. As long as you bind them after the vertex array has been created, it doesn’t matter when you create the buffers. Just be aware that if you create buffers before binding them to a VAO, you should always call BindVertexArray(0) before creating them as otherwise you risk overwriting a previously bound vertex array.
Jemy191
Jemy191OP2w ago
Thanks

Did you find this page helpful?