Search code examples
c#openglopengl-3opengl-4

Downgrading OpenGL 4.5 code to OpenGL 3.0


I found a repo to solve a problem I was having with OpenGL but it was all written in GL 4.5 and the application I'm writing needs to be GL 3.0 code for OSX support. I've been able to translate most of the code by referencing it on docs.GL and converting it back to 3.0 code that I'm familiar with.

I've struggled a bit with understanding the code below, so I was just wondering if anyone would be able to point me in the right direction.

vertexArray vertexBuffer indexBuffer are all int

GL.VertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, IntPtr.Zero, Unsafe.SizeOf<ImDrawVert>());
GL.VertexArrayElementBuffer(vertexArray, indexBuffer);

GL.EnableVertexArrayAttrib(vertexArray, 0);
GL.VertexArrayAttribBinding(vertexArray, 0, 0);
GL.VertexArrayAttribFormat(vertexArray, 0, 2, VertexAttribType.Float, false, 0);

GL.EnableVertexArrayAttrib(vertexArray, 1);
GL.VertexArrayAttribBinding(vertexArray, 1, 0);
GL.VertexArrayAttribFormat(vertexArray, 1, 2, VertexAttribType.Float, false, 8);

GL.EnableVertexArrayAttrib(vertexArray, 2);
GL.VertexArrayAttribBinding(vertexArray, 2, 0);
GL.VertexArrayAttribFormat(vertexArray, 2, 4, VertexAttribType.UnsignedByte, true, 16);

Edit: Im using OpenTK with C# if anyone was wondering.


Solution

  • For anyone who stumbles across this particular problem in future, I had to read the description of the GL 4.5 code and match it up to GL 3.0 code.

    For this particular code snippet, it becomes:

    GL.BindVertexArray(vertexArray);
    
    GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
    
    GL.EnableVertexAttribArray(0);
    GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, Unsafe.SizeOf<ImDrawVert>(), 0);
    
    GL.EnableVertexAttribArray(1);
    GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, Unsafe.SizeOf<ImDrawVert>(), 8);
    
    GL.EnableVertexAttribArray(2);
    GL.VertexAttribPointer(2, 4, VertexAttribPointerType.UnsignedByte, true, Unsafe.SizeOf<ImDrawVert>(), 16);