How to use two OpenGL viewports using QT?

0

I'm trying to do two viewports in qt but I'm not getting success, here's the code below:

1. Method that makes the drawings on the screen:

void GLWidget::paintGL()
{
    //Limpa buffer
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //Funcao que indica que vou usar vertices e que quero deslocar objetos
    //Permite fazer rotação, translação e escala
    glMatrixMode(GL_MODELVIEW);

    //Le matriz identidade
    glLoadIdentity();

    //parametro face: Especifica os poligonos para o qual 'mode' se aplica
    //parametro mode: especifica como os poligonos serão rasterizados
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //GL_FILL, GL_LINE, GL_POINT

    /* GL_POINTS */

    //faz transformação linear do tipo translação (move objeto)
    //Configuração um novo sistema de coordenadas onde vai ser desenhado
    glTranslatef(-7.0f, 5.0f, -8.0f);

    //glPointSize(6.0f);

    glColor3f(1.0f, 0.0f, 0.0f);

    glBegin(GL_LINE_LOOP);
    glVertex2f(0.0f, 0.0f);
    glVertex2f(1.0f, 0.0f);
    glVertex2f(1.0f, 1.0f);
    glVertex2f(0.0f, 1.0f);
    glEnd();

    glLoadIdentity();
    glTranslatef(-5.0f, 3.0f, -7.0f);

    glBegin(GL_QUADS);
    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex2f(0.0f, 0.0f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(1.0f, 1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2f(0.0f, 1.0f);
    glEnd();

    glLoadIdentity();
    glTranslatef(-5.0f, 3.0f, -8.0f);

    /* GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN */

    glLoadIdentity();
    glTranslatef(-3.0f, 1.0f, -8.0f);

    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex2f( 1.0f, 1.5f);
    glVertex2f( 0.0f, 0.0f);
    glVertex2f( 2.0f, 0.0f);
    glEnd();

    glLoadIdentity();
    glTranslatef(-3.0f, 1.0f, -8.0f);

    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_TRIANGLES);
    glVertex2f(0.0f, 1.0f);
    glVertex2f(1.0f, -1.0f);
    glVertex2f(2.0f, 1.0f);
    glEnd();

    glLoadIdentity();
    glTranslatef(4.5f, -5.0f, -8.0f);

    glColor3f(0.0f, 1.0f, 0.0f);

    float radius = 0.9f; //radiano é o tamanho entre o centro e o uma borda. O tamanho do circulo
    float Pi = 3.141592653589793238462643383279502884f;

    glBegin(GL_TRIANGLE_FAN);

    //glVertex2f(origemX, origemY);

    for(int i = 0; i <= 360; i++) {
        glVertex2f(radius * cos(i * Pi/180), //cosseno refere-se a X
                   radius * sin(i * Pi/180)); // seno refere-se a Y
    }

    glEnd();

    glFlush();
    //glFinish();
}

2. Method that makes the viewports:

void GLWidget::resizeGL( int w, int h)//w e h são as dimensões novas da janela
{    

    glClear (GL_COLOR_BUFFER_BIT);                              // Clear Screen

    for(int viewportIs=1;viewportIs<=2;viewportIs++){
        switch (viewportIs)
        {
        case 1:

            glViewport     ( 0, 0, w/2, h); //primeiros dois parametros são o inicio da viewport
            //para este método, x e y (0,0) especificam o ponto |||inferior esquerdo|||

            //diz que o modo é matrix de projeto. Ela configura o que vai ser enxergado
            glMatrixMode   (GL_PROJECTION);
            glLoadIdentity ();

            if ( h==0 )  // Calcula Aspect Ratio da janela
                gluPerspective ( 80, ( float ) w, 1.0, 5000.0 ); // define altura, largura, profundidade da janela
            else
                gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );


            //muda para projeção ortogonal (2D)
            //gluOrtho2D

            //gluPerspective e gluOrtho2D são de uma biblioteca util que chamam OpenGL por trás

            //esse metodo abaixo foi chamado aqui apenas por garantia que o padrao é GL_MODELVIEW
            //glMatrixMode(GL_MODELVIEW);
            //glLoadIdentity();

            break;

            /* Segundo viewport */

        case 2:

            glViewport(w/2, 0, w/2, h);

            glMatrixMode   (GL_PROJECTION );

            glLoadIdentity ();

            if ( h==0 )  // Calcula Aspect Ratio da janela
                gluPerspective ( 80, ( float ) w, 1.0, 500 ); // define altura, largura, profundidade da janela
            else
                gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 500 );

            break;

        }
    }

    glClear (GL_DEPTH_BUFFER_BIT);                          // Clear Depth Buffer

}

What happens is that it divides the screen into two, but only shows the drawings in one place.

In my code I have a switch case , and if I leave the same the way it is appears only in the second viewport , as shown below.

Bydoingatestbychangingcase2:tocase3:,whichdoesnotenterthesecondpart,thedrawingsareonlypaintedin%asshownbelow.

By logic, it is cleaning the screen every viewport , so it's only the second, but I do not know how to solve it.

    
asked by anonymous 03.08.2015 / 18:26

1 answer

2

What happens is that when the program window opens, the resizeGL function is called first, then the paintGL . In this case, the drawing will always be done over the last specified viewport, which is confirmed by your two examples.

If you want the drawing to be done in both viewports, you will need to call the drawing function twice, once for each viewport. You can do the following:

  • Put the drawing routines of objects in a separate function;
  • Move the code in resizeGL to paintGL ;
  • At the end of each case , call the function created at 1.
  • Maybe this is not the most elegant solution, but I think it gave you the idea.

        
    21.10.2015 / 05:16