views:

333

answers:

2

What glBlendFunc should I use to ensure that the opacity of my drawing is always the same? When I use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and multiple images are drawn on top of each other, the result is more and more opaque until it's completely opaque after a certain number of imgaes.

The closest I have come is to use glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA) which maintains a constant opacity no matter how many images are on top of each other, although there is a slight variation in opacity if the images overlap each other.

Any other render states I should consider trying? Any other ideas? I am making a drawing app for my kid and I don't want the images (brush) they draw to cover up the background.

Heres the closest I've got:
ImageShask

I want to have it so that the overlap part of the circles is the same color and opacity as the center part of the circle.

I am using cocos2d iphone v. 0.99

+2  A: 

Draw (without transparency) into a renderbuffer, then use the resulting image as a texture on a partially transparent quad in your main scene.

Here's the method for pre-RBO OpenGL, but not sure if your device will support it, you may have to convert it to use RBOs.

  • make sure you pick a display format having at least one AUX buffer
  • glGenTextures to allocate a texture id
  • glPushAttrib(GL_COLOR_BUFFER_BIT)
  • glDrawBuffer(AUX0) to direct rendering to an offscreen bitmap
  • glReadBuffer(AUX0)
  • glCopyTexSubImage2D to turn the image into a texture
  • glPopAttrib(GL_COLOR_BUFFER_BIT) to restore the draw buffer setting
  • glEnable(Texture_2D)
  • glBindTexture
  • glBegin(QUADS)
  • glColor(100% RGB, partial alpha)
  • glTexCoord (four times)
  • glVertex (four times)
  • glEnd

note that you may be able to reuse the same texture for multiple frames without having to re-render it every time, you can also do some rotation or stretching by creative use of texture coordinate to quad vertex mapping.

Ben Voigt
Any chance of some code? I don't quite understand what you meaan.
John JJ Curtis
Unfortunately I don't have permission to publish the source code I have that does this (it uses an older version of OpenGL without renderbuffer objects, but the idea is the same) but it's almost the same as double buffering. I'll add the steps to my answer.
Ben Voigt
A: 

I ended up doing this:

glAlphaFunc(GL_NOTEQUAL, 0);
[sprite1.texture setAliasTexParameters];
[sprite1 setBlendFunc: (ccBlendFunc) { GL_SRC_ALPHA, GL_ZERO }];

then, when I draw to my CCRenderTexture, I enable alpha test, visit my brush sprite and then disable alpha test.

John JJ Curtis