0

I need to merge two bitmaps to use the resulting bitmap as a mask. Is there a way to do that in OpenGL-1.0 without affecting the previously drawn system.

Here is my algorithm:

1.- draw the background (opaque) 2.- draw the mask (this is a combination of two bitmaps) 3.- draw a bitmap (this need to be masked).

As a side note, I'm working with OpenGLES 1.0, so the shaders won't work. Any help would be very appreciated!

Here is my draw function:

  public void onDrawFrame(GL10 gl) {

  // clear the screen and depth buffer
  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

  // drawing
  gl.glLoadIdentity();

  // draw the background
     gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
     gl.glTranslatef(0.0f, 0.0f, -3.72f);
     background.draw(gl);
  gl.glPopMatrix();

  gl.glPushMatrix();
     gl.glEnable(GL10.GL_BLEND);
     gl.glDisable(GL10.GL_DEPTH_TEST);

     // mask1 + mask2   
     gl.glLoadIdentity();
     gl.glTranslatef(0.0f, 0.0f, -3.72f);
     gl.glBlendFunc(GL10.GL_DST_COLOR, GL10.GL_ZERO);
     mask1.draw(gl);

     gl.glLoadIdentity();
     gl.glTranslatef(0.0f, y, -3.72f);
     gl.glBlendFunc(GL10.GL_DST_COLOR, GL10.GL_ZERO);
     mask2.draw(gl);

     gl.glLoadIdentity();
     gl.glTranslatef(0.0f, 0.0f, -3.72f);
     gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
     picture.draw(gl);

     gl.glDisable(GL10.GL_BLEND);
     gl.glEnable(GL10.GL_DEPTH_TEST);
     gl.glFlush();
  gl.glPopMatrix();

     y -= 0.2f;

  }
ilbesculpi
  • 328
  • 3
  • 15
  • The masks are some screen-aligned quads? Could `mask1` and `mask2` be used as textures for `picture`? – Stefan Hanke Mar 26 '12 at 16:37
  • Yes. Both could be used as textures. The thing is, one will be on a fixed position, and the other one will be moving and/or rotated. As a result, I wish to display only the composition of both masks (some sort of AND between them). – ilbesculpi Mar 26 '12 at 19:55
  • `Background` spans the whole screen. What about `picture`? Does this span the complete screen, too? The masks surely are somewhat smaller. – Stefan Hanke Mar 27 '12 at 05:36
  • 1
    have you taken a look at this: http://stackoverflow.com/questions/5097145/opengl-mask-with-multiple-textures – Tark Mar 27 '12 at 21:58
  • Yes, I was looking for something just like that. The thing is that OpenGLES seems to not have this function glBlendFuncSeparate (at least not on Android). And it is taking so long for solve a simple task... I may have to discard OpenGL as a solution :-S – ilbesculpi Mar 28 '12 at 13:15

1 Answers1

4

Have you looked at something like the stencil buffer for this? This answer doesn't actually merge the bitmaps, but it does use their combination as a mask if that's the result you're looking for. You can use either the logical OR or logical AND of the masks (not sure what you want).

Pseudocode I believe would go something like:

glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 

draw_background();

glEnable(GL_STENCIL_TEST); //enable stencil buffer
glStencilFunc(GL_ALWAYS, 0 , 0); //always pass stencil test
glStencilOp(GL_KEEP,GL_KEEP,GL_INCR); //increment buffer on fragment draw

draw_mask1();
draw_mask2();

if(OR masks) 
    glStencilFunc(GL_LEQUAL, 1, 0xffffffff); //OR masks: pass fragment if  (1 <= buffer)
else if (AND masks)
    glStencilFunc(GL_LEQUAL, 2, 0xffffffff); //AND masks: pass fragment if (2 <= buffer)

glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP); //don't change buffer 

draw_bitmap_to_be_masked(); 
Tim
  • 35,413
  • 11
  • 95
  • 121