iPhone OpenGL 纹理不完全透明

iPhone OpenGL texture not completely transparent

我尝试在球体上绘制透明纹理,但透明区域并非完全透明。一个生动的灰色阴影仍然存在。我尝试加载 Photoshop 生成的 PNG,然后使用以下代码将其绘制在球体上:

我加载纹理的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (void) loadPNGTexture: (int)index Name: (NSString*) name{
    CGImageRef imageRef = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",name]].CGImage;

    GLsizei width = CGImageGetWidth(imageRef);
    GLsizei height = CGImageGetHeight(imageRef);
    GLubyte * data = malloc(width * 4 * height);
    if (!data)
        NSLog(@"error allocating memory for texture loading!");
    else {
        NSLog(@"Memory allocated for %@", name);
    }

    NSLog(@"Width : %d, Height :%d",width,height);
    CGContextRef cg_context = CGBitmapContextCreate(data, width, height, 8, 4 * width, CGImageGetColorSpace(imageRef), kCGImageAlphaPremultipliedLast);//kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(cg_context, 0, height);
    CGContextScaleCTM(cg_context, 1, -1);
    CGContextDrawImage(cg_context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(cg_context);
    CGContextSetBlendMode(cg_context, kCGBlendModeCopy); //kCGBlendModeCopy);

    glGenTextures(2, m_texture[index]);
    glBindTexture(GL_TEXTURE_2D, m_texture[index][0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    free(data);
}

画云:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
glPushMatrix();
glTranslatef(0, 0, 3   );
glScalef(3.1, 3.1, 3.1);
glRotatef(-1, 0, 0, 1);
glRotatef(90, -1, 0, 0);
glDisable(GL_LIGHTING);

//Load Texture for left side of globe
glBindTexture(GL_TEXTURE_2D, m_texture[CLOUD_TEXTURE][0]);
glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertexData3D), &VertexData[0].vertex);
glNormalPointer(GL_FLOAT, sizeof(TexturedVertexData3D), &VertexData[0].normal);
glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertexData3D), &VertexData[0].texCoord);
// draw the sphere
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_COPY);
glDrawArrays(GL_TRIANGLES, 0, 11520);


glEnable(GL_LIGHTING);

glPopMatrix();


在您的代码中突出的第一件事是:

1
glBlendFunc(GL_SRC_ALPHA, GL_COPY);

第二个参数 (GL_COPY) 不是 glBlendFunc 的有效参数。
您可能希望将其更改为类似于

的内容

1
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);