简体   繁体   English

具有透明度的OpenGL纹理(alpha)

[英]OpenGL texture with transparency (alpha)

I'm trying to render a texture with part opaque color and other part with transparency. 我正在尝试使用部分不透明的颜色和其他部分的透明性渲染纹理。

This is my draw function for the object: 这是我对该对象的绘制函数:

void drawHighGrass(){
glDisable(GL_LIGHTING);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);

glDisable(GL_DEPTH_TEST);

glDepthMask(GL_FALSE);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texturas[HIGH_GRASS]);

glPushMatrix();
//glTranslatef(1000, 0, 1000);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(100, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();

glPopMatrix();

glDisable(GL_TEXTURE_2D);

glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);

}

The problem is that in the transparent part it's showing solid white. 问题是在透明部分显示纯白色。 I can make the texture transparent by using glColor4f(1.0, 1.0, 1.0, 0.5) but that's not what I want because it makes the entire texture transparent and not only the transparent part. 我可以使用glColor4f(1.0,1.0,1.0,0.5)使纹理透明,但这不是我想要的,因为它使整个纹理透明,而不仅仅是透明部分。

I've checked, my texture files is a PNG with transparency. 我已经检查过,我的纹理文件是具有透明性的PNG。

Restating the solution here so others can find it easily. 在这里重述解决方案,以便其他人可以轻松找到它。

Your rendering code seems to be correct, so what seems to have been the problem was the texture loading code. 您的渲染代码似乎是正确的,因此问题似乎出在纹理加载代码上。 When loading a texture, you must be sure that you are passing in the correct flags for the internal texture pixel format (GL_RGBA8, GL_RGBA16, etc.) as well as the source image pixel format (GL_RGBA or GL_BGRA, etc.). 加载纹理时,必须确保为内部纹理像素格式(GL_RGBA8,GL_RGBA16等)以及源图像像素格式(GL_RGBA或GL_BGRA等)传递正确的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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