Search code examples
c#opengltao-framework

Tao OpenGL C# Tab Not Rotate


Why is my OpenGL not rotating when I placed it at tab bar?

    private void simpleOpenGlControl1_Paint_1(object sender, PaintEventArgs e)
    {
        Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

        Gl.glMatrixMode(Gl.GL_MODELVIEW);
        Gl.glLoadIdentity();

        Gl.glTranslated(0, 0, -5);

        Gl.glRotated(yrot += 1, 1, 1, 0);


        Gl.glPointSize(3);
        Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_LINES);
        Gl.glPolygonMode(Gl.GL_BACK, Gl.GL_LINES);
        Gl.glBegin(Gl.GL_QUADS);
        {
            ////Vista posterior
            Gl.glColor3ub(255, 0, 255);
            Gl.glVertex3d(0, 1, -1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(0, 1, -1);

            ////DEBAJO
            Gl.glColor3ub(0, 255, 255);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(1, -1, 1);
            Gl.glVertex3d(-1, -1, 1);

            ////POR LA IZQUIERDA
            Gl.glColor3ub(255, 255, 0);
            Gl.glVertex3d(0, 1, -1);
            Gl.glVertex3d(-1, -1, -1);
            Gl.glVertex3d(-1, -1, 1);
            Gl.glVertex3d(0, 1, 1);

            ////POR LA DERECHA
            Gl.glColor3ub(0, 0, 255);
            Gl.glVertex3d(0, 1, 1);
            Gl.glVertex3d(1, -1, 1);
            Gl.glVertex3d(1, -1, -1);
            Gl.glVertex3d(0, 1, -1);



            Gl.glColor3ub(255, 0, 0);
            Gl.glVertex3d(0, 1, 1);
            Gl.glVertex3d(-1, -1, 1);
            Gl.glVertex3d(1, -1, 1);
            Gl.glVertex3d(0, 1, 1);

            Gl.glEnd();
        }

I put on tab, but when I click another tab and back to OpenGL tab the 3D object changes but it does not rotate.


Solution

  • Unless you use a timer to force repaint any N times per second, the paint event only occurs when needed.

    When you switch between tabs, a redraw is needed and a paint event occurs. That why you see a little change at this time.

    You possibly will see the same thing by reducing/maximizing your window, etc...

    You should:

    • Use a timer who periodically call Invalidate or equivalent function.
    • Don't update yrot on paint event. Because you didn't know when it's called you have no control on the rotation speed. Use the timer, or a time-based interpolation of the value.