2015-12-28 86 views
2

我的繪製代碼首先繪製沒有紋理的背景,然後在其上覆蓋紋理。我正在使用glBindTexture(GL_TEXTURE_2D, 0)以在執行第二批繪圖之後解除綁定紋理。解開Open GL ES紋理的正確方法?

然而,當我分析在Xcode的GPU性能,這是產生以下警告:

enter image description here

當我刪除代碼未綁定的質感,警告消失,但事情不是活得正確繪製。我正在繪製的紋理沒有啓用mipmapping,所以它不是從那裏來的。

繪圖代碼:

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) & glDisable(GL_TEXTURE_2D)之前與紋理繪畫後嘗試過,但現在,這給了我以下警告:

enter image description here

+0

如果你明確地定義設置'每個非mipmapped質感GL_TEXTURE_MAX_LOD' 0這個警告可能會消失通過'glTexParameter'。 – derhass

+0

無法找到該參數'GL_TEXTURE_MAX_LOD' ...在ES 2.0中已棄用? – Hamish

+0

對不起,我的壞。 ES中不提供「GL_TEXTURE_MAX_LOD」。 – derhass

回答

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