A translucent OpenGL texture on top of another texture

I have 2 png images that are used as textures in a Qt / C ++ 2D OpenGL application. The first is used as some "middle" ground, and the second is used as an "object" that displays "ontop" (Note: they all have the same value of the atm z-value, I get the desired behavior, a certain order). The “object” texture is partially translucent. The "medium" texture is mostly solid. The problem is that the translucent part of the texture of the “object” has a solid background color, and not the “middle” texture.

Any tips on how to achieve this?

The following OpenGL figs are used for rendering textures.

glEnable (GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

Thanks for any help.

Edit:

More code:

 glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.1f); glEnable(GL_TEXTURE_2D); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, c->_texture); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glBegin(GL_QUADS); { glTexCoord2i(0,0); glVertex2i(-128,-128); glTexCoord2i(0,1); glVertex2i(-128,128); glTexCoord2i(1,1); glVertex2i(128,128); glTexCoord2i(1,0); glVertex2i(128,-128); } glEnd(); glDisable(GL_TEXTURE_2D); glDisable(GL_ALPHA_TEST); glDisable(GL_DEPTH_TEST); 

Edit: how do I upload my texture, and as far as I can tell, it loads it with alpha

 QImage img("./images/dummycar.png","PNG"); QImage t(QGLWidget::convertToGLFormat(img)); glGenTextures(1, &_texture); glBindTexture(GL_TEXTURE_2D, _texture); glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

Screenshot: http://img824.imageshack.us/img824/1912/blackbox.png

Skyimage is a "medium" background, solid black.

+7
transparent opengl textures
source share
3 answers
 glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() ); 

You specify 3 channels for internalFormat , so RGB, not RGBA. Instead, use GL_RGBA at this position.

+7
source share

Have you tried messing with glAlphaFunc ? You can use it to remove the drawn translucent parts of the texture of an object based on the alpha value.

How about

glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND)

or

glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ?

I hope this helps.

+4
source share

If you create one texture for each object, you need to use glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE).

If the texture still has a “margin” around it even after that, it was loaded without an alpha channel.

This works fine for me:

  glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, texture2->tex); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glColor4f(0.5f, 1.0f, 0.5f, 0.5f); glBegin(GL_TRIANGLE_FAN); glTexCoord2f(0, 0); glVertex2f(x, y); glTexCoord2f(1, 0); glVertex2f(x + texture2->width, y); glTexCoord2f(1, 1); glVertex2f(x + texture2->width, y+ texture2->height); glTexCoord2f(0, 1); glVertex2f(x, y + texture2->height); glEnd(); glDisable(GL_BLEND); 

Translucent texture, painted in the last color.

If “having a frame around the texture” does not describe your problem, you need to post screenshots.

- EDIT -

Updated data based on your screenshot .

You need to FIRST visualize the background, and then visualize the car. You see the background color, because even if the surface is transparent, it fills the z-buffer with values, and since the background is lower than the car, it becomes invisible.

When you create translucent objects, you need to follow these rules.

  • All opaque objects should appear in front of translucent objects. Always.

  • All transparent objects should be displayed in the opposite direction in front of the object. The most distant objects must be selected first, the closest objects must be made second.

  • If rendering in reverse order is not possible (unsorted particles with a GL_ADD mixture), writing to the depth buffer should be disabled using glDepthMask (GL_FALSE). If you do not, you will get ugly artifacts. (as in the screenshot).

+3
source share

All Articles