I have been struggling to find a solution for a simple masking using Stencil Buffer in OPEN GL 1.0 on the iOs.
I draw the background and then I draw on top of it using glPushMatrix a frame which is drawn using drawFrame.
My frame is made of a VideoFrameTexture which I try to mask using maskTexture using the stencil buffer. My objective is to draw only part of the videoFrameTexture (which is defined by the white area of my mask and which corresponds to a circle shape) onto the background.
I use the following code but the mask image is not taken into account. It's a Black an white png.
-(void) drawFrame{
GLfloat vertexes[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};
GLshort texes[] = {
1, 1,
0, 1,
0, 0,
1, 0,
};
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, vertexes);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_SHORT, 0, texes);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
/// Mask Drawing
glBindTexture(GL_TEXTURE_2D, maskTexture);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_EQUAL, 1, 0xffffffff); /* draw if ==1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
///Frame Drawing
glBindTexture(GL_TEXTURE_2D, videoFrameTexture);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisable(GL_STENCIL_TEST);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDeleteTextures(1, &videoFrameTexture);
}
I draw my view using the following code:
- (void) drawView
{
[EAGLContext setCurrentContext:context];
//glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -10.0f, 10.0f);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(1, 0.75, 1);
glTranslatef(0, -1, 0);
glRotatef(90, 0.0f, 0.0f, 1.0f);
[self drawBackground];
glPopMatrix();
glPushMatrix();
glScalef(1, 1, 1);
glTranslatef(0, 1, 0);
glRotatef(90, 0.0f, 0.0f, 1.0f);
[self drawFrame];
glPopMatrix();
//present frame buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}