PDA

View Full Version : superimpose two different viewport and projection



Arnaud
03-01-2011, 09:33 AM
Hi,

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.

carsten neumann
03-01-2011, 04:01 PM
You probably need to clear the depth buffer between drawing the two scenes - unless you want objects from the first scene to occlude objects from the second one?

Scott M
03-01-2011, 04:21 PM
I something like this as well - render some thing in ortho and some things in a frustrum, to the same window. It works ok for me. The previous poster's suggestion about clearing the depth buffer is probably the key.

Here's my painting function. I do 2D first and then 3D. Feel free to steal whatever may apply:

GL::FlipOnExit flipper(GL::hDC);
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );

RECT r;
GetClientRect(Windex::hWnd_, &r);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, r.right, 0, r.bottom, -1, 1);
glViewport(0, 0, r.right, r.bottom);

//2d objects here
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Sort by depth; we need to draw back to front

//Make sure deletes elsewhere do no harm
Collection<Widget>::Iterator stablizer(Windex::widgets);

//copy pointers to the valid drawables
std::vector<Widget*> worklist;
for (Collection<Widget>::Iterator i(Windex::widgets); i.next(); )
{
Windex::Widget* c = *i;
if (c &amp;&amp; c->isVisible())
worklist.push_back(c);
}
//sort by z depth
std::sort(worklist.begin(), worklist.end(), Widget::depthSort);
//draw
for (std::vector<Widget*>::iterator w = worklist.begin(); w != worklist.end(); ++w)
{
Windex::Widget* c = *w;

if (!c->is3D())
c->draw();
else
c->drawBorder(); //only chance to put borders around 3d canvases
}

//do 3d objects here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0,
30000000000.0);

glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_BLEND);
glEnable(GL_DEPTH);
glEnable(GL_CULL_FACE);
for (Collection<Widget>::Iterator i(Windex::widgets); i.next(); )
{
Windex::Widget* c = *i;
if (c &amp;&amp; c->is3D())
c->draw();
}
//flip happens here

Arnaud
03-02-2011, 03:11 AM
Thanks ! clearing only depth_buffer makes my viewports match !

But I now have a problem of alpha compositing :

Here is the background video stream (paintGL()):
http://www.kirikoo.net/images/14Anonyme-20110302-112144.jpg

And here is the image I wish to superimpose on it (with the drawScene())
http://www.kirikoo.net/images/14Anonyme-20110302-111711.jpg

But here is what I obtain :
http://www.kirikoo.net/images/14Anonyme-20110302-112109.jpg

Why just calling paintGL and then drawScene() does not superimpose correctly the two images ?

In the bSetupPixelFormat(HDC hdc), these flags are called :
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;

Cheers, Arnaud.

Arnaud
03-02-2011, 04:06 AM
youhRRaah !

I found my answer in this thread, just next to mine :

http://www.opengl.org/discussion_boards/...;gonew=1#UNREAD (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=292655&amp;gonew=1# UNREAD)

=> I added this :
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);

just after the glBlend and glBlendFuncSeparate calls.

And it's perfect ... still remains the question why gl_modulate.

mobeen
03-02-2011, 05:06 AM
And it's perfect ... still remains the question why gl_modulate.
Check ZBuffer's reply to a thread 2 threads below this thread
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=292689#Post2926 89