cklee75's picture

glutPostRedisplay for GLControl

Hi all,

I am new to OpenTK. What is the equivalent method to glutPostRedisplay() (or Qt updateGL()) for GLControl?

Thanks in advance.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Hi and welcome. :)

The equivalent method in WinForms is called Invalidate().

However, if you are looking for continuous animation, it will be faster to do something like this:

Application.Idle += (sender, e) =>
{
    while (glControl.IsIdle)
    {
        Render();
    }
};

Where Render() is you rendering method (the WinForms designer calls this glControl_Paint by default) and glControl is the designer name of the GLControl (glControl1 by default, rename as necessary!)

cklee75's picture

Thanks for your prompt reply.

I have an old code in glut, which load a vrml file with animation.
I am able to port to Qt, and is able to see the animation, by using void GLWidget::timerEvent(QTimerEvent*), and update method.

I have tried your suggestion, but it still only load the vrml file without animation. I believe is due to the animation is not drawn by Opengl but is embedded in the vrml file.

Do you have any suggestion?

Thanks.

the Fiddler's picture

Sorry, I need more information to give any meaningful advice. Have you written the vrml loader yourself or are you using a third-party library? What do you need for animation to work? (For example, do you need timing information?)

You can create a timer using System.Diagnostics.Stopwatch:

Application.Idle += (sender, e) =>
{
    Stopwatch watch = new Stopwatch();
    watch.Start();
    while (glControl.IsIdle)
    {
        Render(watch.ElapsedSeconds);
        watch.Reset();
        watch.Start();
    }
};

Where Render now looks like this:

void Render(long elapsed)
{
    if (elapsed <= 0)
        return; // Nothing to render
 
    glControl.Clear(ClearBufferMask.ColorBufferBit |
        ClearBufferMask.DepthBufferBit);
    ...
    glControl.SwapBuffers();
}
cklee75's picture

Hi,

I am using an C++ internal library that wraps OpenVRML library, and I further create wrapper as managed dll and try to rewrite the example with OpenTK.
I do not need timer.

Qt works without issue.

void GLWidget::timerEvent(QTimerEvent*)
{
    updateGL();
}

Thanks again.

the Fiddler's picture

I'd suggest using the visual debugger to inspect the program flow. Set a breakpoint at the updateGL() call (or the equivalent name in your C# project) and use F5 to check whether it is called continuously or just once. If it is called just once, you'll have to find out why - single stepping through the code (F10/F11) can give a clue.

In general, you'll only need to place the code from my first post to your Form constructor (where Render() is the equivalent of updateGL()) and things will work correctly. Consider posting your game loop if you need more help.