create a window with glfw and render a triangle using opengl in csharp

main.cs
using System;
using OpenGL;
using GLFW;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize GLFW
            GLFW.Init();

            // Create a window
            var window = GLFW.CreateWindow(800, 600, "Triangle", Monitor.None, Window.None);

            // Set the OpenGL context
            GLFW.MakeContextCurrent(window);

            // Initialize OpenGL
            Gl.Initialize();

            // Set the clear color
            Gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);

            // Create Vertex Array, Vertex Buffer and Element Buffer Objects
            uint vao = Gl.GenVertexArray();
            uint vbo = Gl.GenBuffer();
            uint ebo = Gl.GenBuffer();

            // Define vertices and indices of a triangle
            float[] vertices = {
                -0.5f, -0.5f, 0.0f, // bottom left
                0.5f, -0.5f, 0.0f, // bottom right
                0.0f, 0.5f, 0.0f // top
            };
            uint[] indices = {
                0, 1, 2 // triangle
            };

            // Bind VAO
            Gl.BindVertexArray(vao);

            // Bind VBO and copy vertices to it
            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            Gl.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * vertices.Length, vertices, BufferUsage.StaticDraw);

            // Bind EBO and copy indices to it
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, sizeof(uint) * indices.Length, indices, BufferUsage.StaticDraw);

            // Set the vertex attribute pointers
            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, 3 * sizeof(float), IntPtr.Zero);
            Gl.EnableVertexAttribArray(0);

            // Unbind VAO
            Gl.BindVertexArray(0);

            // Render loop
            while (!GLFW.WindowShouldClose(window))
            {
                // Clear the color buffer
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                // Bind VAO and draw the triangle
                Gl.BindVertexArray(vao);
                Gl.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);

                // Swap the front buffer with the back buffer
                GLFW.SwapBuffers(window);

                // Poll events
                GLFW.PollEvents();
            }

            // Delete VAO, VBO and EBO
            Gl.DeleteVertexArrays(1, ref vao);
            Gl.DeleteBuffers(1, ref vbo);
            Gl.DeleteBuffers(1, ref ebo);

            // Terminate GLFW
            GLFW.Terminate();
        }
    }
}
2705 chars
86 lines

gistlibby LogSnag