superimpose two different viewport and projection

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) :

[b]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();[/b]

(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 :

[b]// 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[/b]
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.

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?

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 && 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 && c->is3D())
        c->draw();
  }

//flip happens here

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()):

And here is the image I wish to superimpose on it (with the drawScene())

But here is what I obtain :

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.

youhRRaah !

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

http://www.opengl.org/discussion_boards/…;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.

Check ZBuffer’s reply to a thread 2 threads below this thread
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=292689#Post292689