Search code examples
opengladapterscaletranslateviewport

OpenGL: how to superimpose in the same GL window two gl drawn in different viewport and glortho


in the Draw() function of a GLForm (openGL with borland builder), I first draw an image with a SDK function called capture_card::paintGL(). And this function compells to have this projection before being called :

glViewport(0, 0, (GLsizei)newSize.width, (GLsizei)newSize.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

And on foreground of this painted image, i have to draw an other layer which has been allready coded for another viewport and another glortho projection :

(in the MainResizeGL() called on "onResize" events) :

glViewport(-width, -height, width * 2, height * 2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(double(-width)*viewport_ratio, double(width)*viewport_ratio, double(-height), double(height), 1000.0, 100000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

(and in the MainDraw() called by a "Ontimer") :

glLoadIdentity();
glTranslatef(0.0, 0.0, -50000.0);
mpGLDrawScene->DrawScene(); //(this calls a doDrawScene() I don't understand exactly how it draws : with calling this->parent, etc.)
glFlush();
SwapBuffers(ghDC);

So I transformed the MainDraw() to the following :

// viewport and projection needed for the paintGL
glViewport(0, 0, (GLsizei)newSize.width, (GLsizei)newSize.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);

// call of the paintGL
if(capture_button_clicked) capture_card::paintGL();

// content of the ResizeGL in order to get back to the projection desired and to matrixmode modelview
glViewport(-width, -height, width * 2, height * 2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(double(-width)*viewport_ratio, double(width)*viewport_ratio, double(-height), double(height), 1000.0, 100000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// original drawscene call
glLoadIdentity();
glTranslatef(0.0, 0.0, -50000.0);
mpGLDrawScene->DrawScene(); //(this calls a doDrawScene() I don't understand exactly how it draws : with calling this->parent, etc.)
glFlush();
SwapBuffers(ghDC);

The result is that I see the ancient project's "drawscene" items but when I click on the "capture_button" the paintGL remains invisible and the items drawn turn into something like an alpha-channel canva.

I tried to add glScalef(width, height, 1) after the paintGL, changed the glTranslatef(0,0,50000) with the result that I saw a small amount of pixel with the colors of the paintGL, but the overlay items then disappear.

How could I get these two different viewport to superimpose (i.e. the drawscene above the paintGL) ?

Thanks in advance, cheers, Arnaud.


Solution

  • Use scissor test together with the viewport

    glEnable(GL_SCISSOR_TEST);
    glScissor(viewport.x, viewport.y, viewport.width, viewport.height)
    glViewport(viewport.x, viewport.y, viewport.width, viewport.height)
    

    and clear the depth buffer (only the depth buffer) between the different rendering stages.