为什么我的精灵在纹理周围有暗影/线条/框架?

时间:2011-12-16 09:07:27

标签: ios opengl-es transparency textures glkit

我正在使用Apple的GLKit手开始OpenGL我遇到了一些麻烦让我的精灵正确显示。问题在于它们都被细细的黑线包围着。下面的屏幕截图显示了两个带有png图像纹理的矩形,包含透明度(显然)。

enter image description here

围绕它们的黑色阴影绝对不是pngS的一部分。绿色png在没有抗锯齿的情况下完成,蓝色png具有抗锯齿边框。如果我只绘制一个精灵,黑色边框也很明显。

代码的相关部分(希望如此......)是:

//render the scene
-(void)render
{
    glClearColor(69./255., 115./255., 213./255., 1.);
    glClear(GL_COLOR_BUFFER_BIT);
    [shapes enumerateObjectsUsingBlock:^(AAAShape *shape, NSUInteger idx, BOOL *stop)   
     {
         [shape renderInScene:self];
     }];
}

//creating and storing the effect inside shape class
-(GLKBaseEffect *)effect
{
    if(!effect)
    {
        effect = [[GLKBaseEffect alloc] init];
    } 
    return effect;
}

//rendering the shape (including effect configuration)   
-(void)renderInScene:(AAAScene *)scene 
{
    //TODO: Storing vertices in Buffer
    self.effect.transform.projectionMatrix = scene.projectionMatrix; 
    self.effect.transform.modelviewMatrix = self.objectMatrix; 
    if(texture)
    {
        self.effect.texture2d0.enabled = GL_TRUE;
        self.effect.texture2d0.envMode = GLKTextureEnvModeReplace;
        self.effect.texture2d0.target = GLKTextureTarget2D;
        self.effect.texture2d0.name = texture.name;
    }
    [self.effect prepareToDraw];

    if(texture)
    {
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
    glDrawArrays(GL_TRIANGLE_FAN, 0, self.vertexCount);
    glDisableVertexAttribArray(GLKVertexAttribPosition);

    if(texture)
    {
        glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
        glDisable(GL_BLEND);
    }
}

任何想法?谢谢。

2 个答案:

答案 0 :(得分:1)

这对我有用:

glEnable( GLES20.GL_BLEND );
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

答案 1 :(得分:-3)

来吧stackoverflowers,14个小时,没有答案;-)。在gamedev上花了大约14分钟给this great answer。投票给他!