解除Open GL ES纹理解除绑定的正确方法?

时间:2015-12-28 19:06:00

标签: ios c opengl-es textures opengl-es-2.0

我的绘图代码首先绘制没有纹理的背景,然后将纹理覆盖在它上面。我目前正在使用DB::connection('pgsql')->statement('your statement here...'); ,以便在执行第二批绘图后取消绑定纹理。

但是,当我在Xcode中分析GPU性能时,这会产生以下警告:

enter image description here

当我删除代码以取消绑定纹理时,警告消失,但事情没有正确绘制。我绘制的纹理没有启用mipmapping,所以它不是来自那里。

绘图代码:

glBindTexture(GL_TEXTURE_2D, 0)

纹理加载代码:

glClear(GL_COLOR_BUFFER_BIT);

{ // Drawing

    glViewport(0, 0, frameWidth, frameHeight);

    GLuint vertexCount = animBuffer.vertexCounts[animIndex];

    glBindBuffer(GL_ARRAY_BUFFER, animBuffer.vertexBuffers[animIndex]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, animBuffer.indexBuffers[animIndex]);

    glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
    glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)(sizeof(float)*3));
    glVertexAttribPointer(textureCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)(sizeof(float)*7));

    glDrawElements(GL_TRIANGLE_STRIP, vertexCount, GL_UNSIGNED_SHORT, 0);


    // glEnable(GL_TEXTURE_2D) (tried this, gives another warning)

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glUniform1i(textureUniform, 0);

    glBindBuffer(GL_ARRAY_BUFFER, textureColorBuffer); // Transparent color buffer to allow texture to be overlayed.
    glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(sizeof(float)*3));

    glDrawElements(GL_TRIANGLE_STRIP, vertexCount, GL_UNSIGNED_SHORT, 0);

    // Unbind texture.... this line is causing the error.
    glBindTexture(GL_TEXTURE_2D, 0);

    // glDisable(GL_TEXTURE_2D) (tried this, gives another warning)    
}

[context presentRenderbuffer:GL_RENDERBUFFER];

我应该如何去除未绑定纹理以删除此警告?

修改

我尝试过使用CGImageRef textureImage = image.CGImage; size_t width = CGImageGetWidth(textureImage); size_t height = CGImageGetHeight(textureImage); GLubyte* spriteData = malloc(width*height*4); CGColorSpaceRef cs = CGImageGetColorSpace(textureImage); CGContextRef c = CGBitmapContextCreate(spriteData, width, height, 8, width*4, cs, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(cs); CGContextScaleCTM(c, 1, -1); CGContextTranslateCTM(c, 0, -CGContextGetClipBoundingBox(c).size.height); CGContextDrawImage(c, (CGRect){CGPointZero, {width, height}}, textureImage); CGContextRelease(c); GLuint glTex; glGenTextures(1, &glTex); glBindTexture(GL_TEXTURE_2D, glTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); glBindTexture(GL_TEXTURE_2D, 0); free(spriteData); return glTex; &纹理绘图之前和之后的glEnable(GL_TEXTURE_2D),虽然现在给出了以下警告:

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为避免此警告的最简单方法是保持纹理绑定,但只需向片段着色器发送uniform,指示是否要混合纹理。

所以,在绘图之前只需添加:

glUniform1i(drawTexInput, GL_FALSE); // Or GL_TRUE to draw.

在片段着色器中:

varying lowp vec4 destinationColor;

uniform bool drawTex;
varying lowp vec2 texCoordOut;
uniform sampler2D tex;

void main() {

    lowp vec4 result;
    if (drawTex) {
        lowp vec4 tex2D = texture2D(tex, texCoordOut);
        result = tex2D + vec4(1.0 - tex2D.a) * destinationColor; // Alpha blending
    } else {
        result = destinationColor;
    }

    gl_FragColor = result;
}