5

I'm looking at SurfaceFlinger, i.e. the the code that is doing the composition in Android, and I have trouble understanding some of the OpenGL ES 1.0 calls as I've only programmed ES 2.0 code.

Here goes the piece of code that interests me:

glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

What it is supposed to be doing is blending a texture into the buffer.

Why does one need the glTextEnvx and glColor4x calls? Isn't glBlendFunc enough for achieving blending?

I know my question is naive, but I still don't understand what is glTexEnvx for even after reading the docs.

Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105

1 Answers1

7

glTexEnv() sets the texture environment mode. GL_REPLACE tells the renderer to skip the current color (for example, from glColor4()) and instead tells the renderer to use your texture's colors for every corresponding pixel. If you, instead of GL_REPLACE, use GL_MODULATE, then your glColor4() call will be included together with the texture's colors when the renderer sets a pixel's color.

Your glColor4() call shouldn't be doing anything (when using GL_REPLACE) that can be seen on your object.

About your glBlendFunc() arguments:

GL_ONE is using the current color that comes from your incoming primitive, which we call source. GL_ONE_MINUS_SRC_ALPHA is multiplying the destination (which is the currently stored pixel in the frame buffer) by (1 - source alpha value).

Normally, when you're not using textures, you achieve a transparent effect from color primitives when the glColor4() contains an alpha value where 1 equals fully opaque and 0 fully transparent.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • After you had explained it all I read the doc again and it made sense now. However, I think you're wrong that `GL_MODULATE` uses the color set by `glColor`. According to the doc `GL_MODULATE` uses only the fragment and texture colors, whereas `GL_BLEND` uses the env color. – Albus Dumbledore Jul 27 '11 at 07:00
  • 1
    @Albus: `GL_MODULATE` means that it will take the texture color and multiply it by the incoming color. Try to Google: *what does `GL_MODULATE`?* and you'll see other posts stating the same fact. – Wroclai Jul 27 '11 at 07:57
  • Well, dunno. According to the [doc](http://www.khronos.org/opengles/documentation/opengles1_0/html/glTexEnv.html), the color output for `GL_MODULATE` would be: `color_of_the_texture * color_of_the_fragment`, whereas for `GL_BLEND`: `(1 - color_of_the texture) * color_of_the_fragment + color_of_the_texture * env_color`. That is, the env color comes in only for `GL_BLEND`. Not trying to be obstinate, sry. Your answer did help me a lot, but according to the doc you seem to be wrong or I am misreading it somehow. – Albus Dumbledore Jul 27 '11 at 08:21
  • 2
    @Albus: The fragment, in this context, is actually the pixel color which already is in the frame buffer. – Wroclai Jul 27 '11 at 08:25
  • @Albus: Oh, no problems. It is always good to understand the whole concept then just *suspecting* to know. :) – Wroclai Jul 27 '11 at 08:25
  • Thanks, mate! So, `GL_MODULATE` would use the pixel color from the fb, not the color set by `glColor4x`, whereas `GL_BLEND` would *also* use the *environment color*, which actually is the color set by `glColor4x`. Am I getting it wrong? – Albus Dumbledore Jul 27 '11 at 08:37
  • @Pompedevelo let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1869/discussion-between-albus-dumbledore-and-pompe-de-velo) – Albus Dumbledore Jul 27 '11 at 08:38
  • 2
    Well, when you are calling `glColor4()`, you are setting a new pixel color for your primitive and that will be the *fragment* color (or whatever everyone says :)). `GL_BLEND` will take both texture's colors and a pixel's color when calculating the blending when we use `GL_MODULATE`. However, when we use `GL_REPLACE` it is essentially saying to remove the fragment color when blending (and, for example, only use a texture´s alpha layer to determine transparent parts), in this context. – Wroclai Jul 27 '11 at 08:49