简体   繁体   中英

iPhone OpenGL texture not completely transparent

I tried to paint a transparent texture over a sphere, but the transparent areas are not completely transparent. A vivid shade of gray remains. I tried to load a Photoshop generated PNG then paint it on sphere using the code below:

My code to load textures:

- (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);
}

Drawing clouds:

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();

This first thing that stands out in your code is the line:

glBlendFunc(GL_SRC_ALPHA, GL_COPY);

The second argument ( GL_COPY ), is not a valid argument for glBlendFunc . You might want to change that to something along the lines of

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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