简体   繁体   English

OpenGL ES 2.0纹理是透明的,但后面没有显示OpenGL内容

[英]OpenGL ES 2.0 texture is transparent, but does not show OpenGL content behind

Using OpenGL ES 2.0 on iOS 4 . iOS 4上使用OpenGL ES 2.0

I have an OpenGL view that inherits from UIView . 我有一个继承自UIViewOpenGL视图。 It is in front of a UIImageView that provides a background. 它位于提供背景的UIImageView的前面。

I have a . 我有一个 。 png image with transparency that I am using for an OpenGL texture. 我用于OpenGL纹理的具有透明性的png图像。

The problem I am having is the transparency of the image in the OpenGL view shows through to the image in the UIImageView background. 我遇到的问题是OpenGL视图中的图像的透明性一直显示到UIImageView背景中的图像。 I would like to show content in the OpenGL view that is behind the elements with the transparent texture. 我想在具有透明纹理的元素后面的OpenGL视图中显示内容。

The underneath OpenGL texture is being rendered, but any portion that has the transparent texture on top does not show the underneath OpenGL texture. 正在渲染下面的OpenGL纹理,但是顶部具有透明纹理的任何部分都不会显示下面的OpenGL纹理。 It shows the UIImageView background. 它显示了UIImageView背景。

What do I need to do so that the transparency will show content in the OpenGL view that is behind the transparent content? 我需要怎么做才能使透明显示在透明内容后面的OpenGL视图中?

EDIT : 编辑

I restructured the index array so all elements with transparent textures come at the end and should be rendered after all opaque elements when I call glDrawElements . 我重组了索引数组,以便所有具有透明纹理的元素都出现在末尾,当我调用glDrawElements时应在所有不透明元素之后呈现它们

Still having the problem with the transparency. 透明度仍然存在问题。

I set up some data so that I have 4 vertices and 6 indices for 2 triangles to draw a square with the opaque texture. 我设置了一些数据,以使我拥有2个三角形的4个顶点和6个索引,以绘制具有不透明纹理的正方形。 There are also 4 vertices and 6 indices for 2 triangles to draw a square with the transparent texture which come after the other entries in the vertex/index arrays. 还有2个三角形的4个顶点和6个索引来绘制具有透明纹理的正方形,该正方形位于顶点/索引数组中的其他条目之后。 The transparent element coordinates render it in front of the opaque element. 透明元素坐标将其呈现在不透明元​​素前面。

I can see the transparent element, and I can also see the edges of the opaque element sticking out from behind. 我可以看到透明元素,也可以看到不透明元素的边缘从后面伸出。 However, instead of showing the opaque element which lies immediately behind the transparent element, the transparency goes right through and shows the background of the view behind the OpenGL view. 但是,与其显示紧邻在透明元素后面的不透明元素,不如说它一直贯穿并显示OpenGL视图后面的视图背景。

Opaque yellow square: 不透明的黄色方块:

不透明的黄色正方形

Transparent test image: 透明测试图像:

透明测试图像

The transparent image covers the opaque image but the transparency shows the background not the opaque image behind: 透明图像覆盖不透明图像,但是透明显示背景而不是后面的不透明图像:

在此输入图像描述在此输入图像描述

Did you try using some blending? 您尝试使用一些混合吗? Something like this: 像这样的东西:

//draw your background scene here
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//draw your foreground scene here
glDisable(GL_BLEND);

-----------------EDIT on comment---------------- -----------------编辑评论----------------

So you want to do the blending in fragment shader: 因此,您想在片段着色器中进行混合:

vec4 color1 = texture2D(backgroundTexture, ...);
vec4 color2 = colorYouGetFromSecondaryTextureOrSomeOtherElement;

vec4 outputColor = color1*(1.0-color2.a) + color2*(color2.a);
outputColor.a = 1.0; //or some function of color1.a and color2.a

gl_FragColor = outputColor;

-----------------EDIT 2------------------------- -----------------编辑2 -------------------------

Also considering your results I would say that you forgot something in your pipeline. 考虑到您的结果,我会说您忘记了管道中的某些内容。 You probably use 2 or more textures but only your second is binded resulting in the fragment snippet I posted to be all black but your foreground.. Are you using "active texture"? 您可能使用了2个或更多纹理,但只有第二个纹理被绑定,导致我发布的片段片段全都是黑色,但前景却是黑色。您是否使用“活动纹理”? Something like this: 像这样的东西:

- (void)bindFirstTexture {
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_1D, firstTextureID);
}
- (void)bindSecondTexture {
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, secondTextureID);
}

You need such binding in both, before creating and before drawing multiple textures. 在创建和绘制多个纹理之前,都需要这样的绑定。

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

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