0

I've been playing with OpenGL. I'm confused because most commands seem to call a static method instead of the window object I create - by what arcane methods does the compiler divine my target window, I can't fathom.

I assume I've misunderstood something along the way, since I can't get my code to work. The snippet below produces a window with a transparent background, as opposed to a black background; trying a few other commands for drawing also gave me nothing. What am I doing wrong?

public static void Main()
    {
        using (OpenTK.GameWindow a = new OpenTK.GameWindow(800, 600, GraphicsMode.Default, "Sandboxy")) 
        {
            a.Run(30);

            OpenTK.Graphics.OpenGL.GL.ClearColor(0, 0, 0, 0);
            OpenTK.Graphics.OpenGL.GL.ClearDepth(1.0);
            OpenTK.Graphics.OpenGL.GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        }
    }
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Fadeway
  • 549
  • 5
  • 18

2 Answers2

2

Yes, you're definitely doing something wrong, but firstly, the static method thing.

The way it knows where to draw the stuff to, even though you're using static methods, is because OpenTK tells OpenGL (OpenTK.Graphics.OpenGL.GL) where to draw to when it creates the GameWindow, it binds the window to the static OpenGL context (on windows it does this through the Win32 functions wglCreateContext(HDC) and wglMakeCurrent(HDC, HGLRC), look those up if you want more information).

The reason it doesn't work, is because you try to clear everything after you start the render loop:

a.Run(30);

Doesn't just open the window, it also enters the render loop, meaning it will return only when the windows closes. This is quite obviously not what you want. You want to render in the loop, not afterwards.

The preferred way to draw it (for OpenTK) is create a class deriving from GameWindow, and overriding the functions to do with the loop:

class MyGameWindow : GameWindow
{
    public MyGameWindow() : base(800, 600, GraphicsMode.Default, "Sandboxy")
    {
    }

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.ClearColor(0, 0, 0, 0);
        GL.ClearDepth(1.0);
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        this.SwapBuffers();
    }

    public static void Main(string[] args)
    {
        using (GameWindow a = new MyGameWindow())
        {
            a.Run(30);
        }
    }
}
antonijn
  • 5,702
  • 2
  • 26
  • 33
  • Thanks a lot! Guess I should've looked up exactly what Run() does before using it. – Fadeway Jan 31 '13 at 19:47
  • @Fadeway If you want to learn more OpenTK stuff, you should check out their website and have a look at the "Tutorials and examples". – antonijn Jan 31 '13 at 19:50
1

OpenGL commands operate on the current GL context. You acquire GL contexts from the operating system and make them current in a given thread via an OS-specific (wgl, glx, etc.) *MakeCurrent() call.

genpfault
  • 51,148
  • 11
  • 85
  • 139