How to use two Opengl viewports using QT?

Asked

Viewed 221 times

0

I’m trying to make two viewports in Qt but I’m not succeeding, below follows the code:

1. Method of drawing on canvas:

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 of doing 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 in two, but shows only in one place the drawings.

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.

Viewport 1

Taking a test by changing it case 2: for case 3:, which does not enter the second part, the drawings are painted only in the viewport 1, as shown below.

Viewport 2

Logically, he’s wiping the screen at every turn viewport, so just stay the second, but I don’t know how to fix it.

1 answer

2


What happens is that when the program window opens, the function is first called resizeGL, then the paintGL. In this case, the drawing will always be done on 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, one for each viewport. You can do the following:

  1. Place object drawing routines in a separate function;
  2. Move the code that is in resizeGL for paintGL;
  3. At the end of each marry, call the function created in 1.

Maybe this isn’t the most elegant solution, but I think you got the idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.