【问题标题】:Transparent textures in OpenGL ES 1.1 for iOSOpenGL ES 1.1 for iOS 中的透明纹理
【发布时间】:2014-11-06 05:56:58
【问题描述】:

我已经阅读了有关类似问题的其他帖子,并且似乎已经尝试了所有存在的方法,但仍然无法达到预期的效果。

我想要的是在屏幕上显示箭头,让用户可以通过按下它们来旋转选定的对象。我的想法是创建几个具有透明背景的 png 文件并将它们显示为纹理。

相应的代码分散在几个源文件中,但我认为相关部分如下:

typedef struct {
    unsigned int width;
    unsigned int height;
    void *image;
} TextureData;


NSString *path = [[NSBundle mainBundle] pathForResource:arrowFile ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

TextureData *texData;
texData->width  = CGImageGetWidth(image.CGImage);
texData->height = CGImageGetHeight(image.CGImage);
texData->image = malloc(4*texData->height*texData->width);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(texData->image, texData->width,
    texData->height, 8, 4*texData->width,colorSpace,
    kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextClearRect(context, CGRectMake(0, 0, texData->width, texData->height));
CGContextTranslateCTM(context, 0, texData->height - texData->height);
CGContextDrawImage(context, CGRectMake(0, 0, texData->width, texData->height),
    image.CGImage);
CGContextRelease(context);

[image release];

使用获得的TextureData结构生成纹理

glBindTexture(GL_TEXTURE_2D, arrowTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texData->width, texData->height,
    0, GL_RGBA, GL_UNSIGNED_BYTE, texData->image);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

在渲染阶段,我进行如下操作:

vec2 texVertices[6];
vec3 vertices[6];

texVertices[0] = vec2(1.0f, -1.0f);
texVertices[1] = vec2(0.0f, -1.0f);
texVertices[2] = vec2(0.0f,  0.0f);
texVertices[3] = vec2(1.0f, -1.0f);
texVertices[4] = vec2(0.0f,  0.0f);
texVertices[5] = vec2(1.0f,  0.0f);

vertices[0] = vec3( 0.5f,  0.5f, 0.0f);
vertices[1] = vec3(-0.5f,  0.5f, 0.0f);
vertices[2] = vec3(-0.5f, -0.5f, 0.0f);
vertices[3] = vec3( 0.5f,  0.5f, 0.0f);
vertices[4] = vec3(-0.5f, -0.5f, 0.0f);
vertices[5] = vec3( 0.5f, -0.5f, 0.0f);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexPointer(3, GL_FLOAT, sizeof(vec3), &vertices[0]);
glTexCoordPointer(2, GL_FLOAT, sizeof(vec2), &texVertices[0]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBindTexture(GL_TEXTURE_2D, arrowTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);

glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

我看到的结果是我在黑色背景上的箭头,我对此感到很困惑。感谢您对此的任何帮助/想法/建议,在此先感谢您。

【问题讨论】:

    标签: textures transparency blending opengl-es-1.1


    【解决方案1】:

    我设法找出了为什么我得到黑色背景而不是正确混合的根本原因。把它贴在这里,以防有人遇到同样的问题: 我忘记将对象渲染到它们的距离,最远的第一个,结果,OpenGL 将我的纹理与黑色的 glClearColor 混合。显然,当我现在看到它时,它几乎让我绝望。另一件事; glDisable(GL_LIGHTING) 并不是那么重要,尽管它确实让我的箭头看起来更好。缺少的部分是使用 glDepthFunc(GL_ALWAYS) 关闭深度测试。

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多