ilbesculpi
ilbesculpi

Reputation: 328

OpenGLES: merging two masks (bitmaps)

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;

  }

Upvotes: 0

Views: 894

Answers (1)

Tim
Tim

Reputation: 35923

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

Upvotes: 4

Related Questions