Home > OS >  Which method inside GameWindow do we override to draw an object with OpenTk?
Which method inside GameWindow do we override to draw an object with OpenTk?

Time:12-12

Problem

I am doing a video game environment design research and I happened to start with the basics of OpenGL with OpenTk-The C# variant of OpenGL. I got code to draw a cube object in my GameWindow with OpenTk from this site. I override the method OnLoad for my GameWindow object and I call the method to draw the cube but nothing happens, the code draws the GameWindow and NativeWIndow without any Graphical Object.

Expectations

I expected the code to draw a cube inside the GameWindow Object.

Code Applied

//extend the GameWindow object to access methods from the super
class MyWindow : GameWindow
{
    public MyWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings)
    {
        WindowState = OpenTK.Windowing.Common.WindowState.Maximized;
    }

    //when the window is loaded, draw the cube
    protected override void onl oad()
    {
        this.DrawBox(10);

    }
    //method to draw the cube 
    private void DrawBox(float size)
    {
        float[,] n = new float[,]{
            {-1.0f, 0.0f, 0.0f},
            {0.0f, 1.0f, 0.0f},
            {1.0f, 0.0f, 0.0f},
            {0.0f, -1.0f, 0.0f},
            {0.0f, 0.0f, 1.0f},
            {0.0f, 0.0f, -1.0f}
        };
        int[,] faces = new int[,]{
            {0, 1, 2, 3},
            {3, 2, 6, 7},
            {7, 6, 5, 4},
            {4, 5, 1, 0},
            {5, 6, 2, 1},
            {7, 4, 0, 3}
        };
        float[,] v = new float[8, 3];
        int i;

        v[0, 0] = v[1, 0] = v[2, 0] = v[3, 0] = -size / 2;
        v[4, 0] = v[5, 0] = v[6, 0] = v[7, 0] = size / 2;
        v[0, 1] = v[1, 1] = v[4, 1] = v[5, 1] = -size / 2;
        v[2, 1] = v[3, 1] = v[6, 1] = v[7, 1] = size / 2;
        v[0, 2] = v[3, 2] = v[4, 2] = v[7, 2] = -size / 2;
        v[1, 2] = v[2, 2] = v[5, 2] = v[6, 2] = size / 2;

        GL.Begin(BeginMode.Quads);
        for (i = 5; i >= 0; i--)
        {
            GL.Normal3(ref n[i, 0]);
            GL.Vertex3(ref v[faces[i, 0], 0]);
            GL.Vertex3(ref v[faces[i, 1], 0]);
            GL.Vertex3(ref v[faces[i, 2], 0]);
            GL.Vertex3(ref v[faces[i, 3], 0]);
        }
        GL.End();
    }
}
//instantiation in the main method
 GameWindowSettings gameWindowSettings= new GameWindowSettings();
 NativeWindowSettings nativeWindowSettings = new NativeWindowSettings();
gameWindowSettings.IsMultiThreaded = false;

new MyWindow(gameWindowSettings, nativeWindowSettings).Run();

Please help me make this work.

CodePudding user response:

You must implement the OnUpdateFrame event callback. You also need to call Context.SwapBuffers(); to update the display:

class MyWindow : GameWindow
{
    // [...]

    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        this.DrawBox(0.5f);

        Context.SwapBuffers();
        base.OnUpdateFrame(e);
    }
}

If you are not using a projection matrix, the coordinates must be in the range [-1.0, 1.0]. The Normalized Device Space is a unique cube with the left, bottom, near corner (-1, -1, -1) and right, top, far corner (1, 1, 1). Without a projection matrix this cube is also the viewing volume (the space which is projected on the viewport).

Since you are using Legacy OpenGL (glBegin/glEnd) you need to create a Compatibility profile OpenGL Context:

nativeWindowSettings.API = ContextAPI.OpenGL;
nativeWindowSettings.Profile = ContextProfile.Compatability;

See c_sharp_opengl/OpenTK_hello_triangle for a very basic example using "modern" OpenGL.

  • Related