简体   繁体   中英

OpenGL ES 1.1 - combine opacity and diffuse maps

I have rather simple problem. There are two textures: the first one contains diffuse map and the second one contains transparency (created using GL_ALPHA). I just need to replace alpha value of diffuse component with the alpha component of the opacity component.

It is very easy to do using GL_BLEND and OpenGL ES 2.0:

gl_FragColor.w = texture2D(opacity_samp, tex_cord).w;

But I can't figure out how to do that using OpenGL 1.1 texture combiners.

Easiest way would be combine textures on CPU, and then upload new one back to GPU.

If you have OES_blend_func_separate and OES_framebuffer_object extensions available then you can do following steps:

  1. Create framebuffer.
  2. Attach new texture to it - this will contain both combined textures.
  3. Draw diffuse map by rendering quad covering whole framebuffer.
  4. Finally render second texture (transparency) as quad covering whole framebuffer. Do it with following blend mode enabled:

     glEnable(GL_BLEND); glBlendFuncSeparateOES(GL_ZERO, GL_ONE, GL_ONE, GL_ZERO); 

Texture combiners will help you. If your diffuse texture has 1.0 alpha value and opacity texture has white color, then use this approach:

//Multiply textureID0 with textureID1

glActiveTexture(GL_TEXTURE0);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, textureID0);
 //Simply sample the texture
 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 //------------------------
 glActiveTexture(GL_TEXTURE1);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, textureID1);
 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
 //Sample RGB, multiply by previous texunit result
 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);   //Modulate RGB with RGB
 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
 //Sample ALPHA, multiply by previous texunit result
 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);  //Modulate ALPHA with ALPHA
 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE);
 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);

Otherwise you can tune combine settings: set GL_REPLACE instead of GL_MODULATE. Additional info is here: http://www.opengl.org/wiki/Texture_Combiners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM