2
votes

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 http://img43.imageshack.us/img43/5347/img0085f.png

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

2 Answers

2
votes

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.

0
votes

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.