scewps
scewps

Reputation: 91

C# OpenTK - simple example not rendering

I'm using OpenTK to create a graphics engine, but it doesn't want to draw anything. Clearing the screen works, also swapping the buffers, but when I try to draw a rectangle using a vertex array or legacy OpenGL it won't work.

I've reproduced the problem in this small test program:

private static GameWindow window;
private static int vao;
private static int vbo;

static void Main(string[] args)
{
    window = new GameWindow();
    window.Load += Window_Load;
    window.RenderFrame += Window_RenderFrame;
    window.Run();
}

private static void Window_Load(object sender, EventArgs e)
{
    vao = GL.GenVertexArray();
    GL.BindVertexArray(vao);
    vbo = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
    GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(3), new float[] {
        -0.5f, -0.5f, 0f,
        0.5f, -0.5f, 0f,
        0.5f, 0.5f, 0f,
        0.5f, 0.5f, 0f,
        -0.5f, 0.5f, 0f,
        -0.5f, -0.5f, 0f
    }, BufferUsageHint.StaticDraw);
    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
}

private static void Window_RenderFrame(object sender, FrameEventArgs e)
{
    GL.ClearColor(1, 0, 0, 1);
    GL.Clear(ClearBufferMask.ColorBufferBit);
    GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
    window.SwapBuffers();
}

Can anyone give me a hint on what I'm doing wrong?

Upvotes: 0

Views: 1579

Answers (2)

scewps
scewps

Reputation: 91

The solution was to make the context forward compatible.

Upvotes: 2

Brian A. Henning
Brian A. Henning

Reputation: 1511

A simple rendering pipeline needs some basic information to draw vertices:

  • It needs to know where they are in space
  • It needs to know their color or material
  • It needs to know the matrices for mapping 3D space onto a 2D viewport

Your code doesn't specify any of that except for the vertex locations. There are a number of reasons why your triangles might not be visible:

  • The vertices are in the wrong order and backface culling is turned on
  • The vertex color is the same as the background color
  • The triangles are outside the view volume

Upvotes: 0

Related Questions