OpenGL ES 2.0 Framebuffer,渲染到纹理iOS:没有显示

时间:2015-06-09 19:47:09

标签: c++ ios opengl-es opengl-es-2.0 framebuffer

我正在使用Framebuffer进行后期处理。 创建和绘制缓冲区(通过glReadPixelsglCheckFramebufferStatus测试)工作正常。 但是现在我想用四边形渲染这个纹理到屏幕上。但屏幕仍为黑色。

相同的代码(用于绘图)在其他平台上运行良好,如android,windows,OSX。

使用模板和纹理的初始帧缓冲::

//framebuffer
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);

//texture
glGenTextures(1, &color);
glBindTexture(GL_TEXTURE_2D, color);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//width and height: screen size
glTexImage2D(GL_TEXTURE_2D ,0, GL_RGBA, width, height,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);

//depth and stencil
glGenRenderbuffers(1, &depthStencil);
glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencil);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencil);

将内容绘制到帧缓冲区:

glBindFramebuffer(GL_FRAMEBUFFER, fb);

glViewport(0, 0, width, height);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f,0.0f,0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// ....

glBindFramebuffer(GL_FRAMEBUFFER, 0);

绘制帧缓冲内容:

//unbind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);

std::vector<GLfloat> vertices = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
std::vector<GLfloat> texCoords = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
std::vector<GLushort> indices = {0,1,2,0,2,3};

glm::mat4 projection = glm::ortho(0.0f, 1.0f, 0.0f, 1.0f, -10.0f, 10.0f);

//clear
glEnable(GL_DEPTH_TEST, true);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//shader
glUseProgram(shader);
auto aPosition = glGetAttribLocation(shader,"aPosition");
auto aTexCoord = glGetAttribLocation(shader,"aTexCoord");
auto uTex = glGetUniformLocation(shader,"uTex");
auto uMVP = glGetUniformLocation(shader,"uMVP");

//blending
glEnable(GL_BLEND, true);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

//binding texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, color);
glUniform1i(uTex, 0);

//set attributes
glUniformMatrix4fv(uMVP, 1, GL_FALSE, glm::value_ptr(projection));

glVertexAttribPointer(aPosition, 3, GL_FLOAT, GL_FALSE, 0, &vertices[0]);
glEnableVertexAttribArray(aPosition);
glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, &texCoords[0]);
glEnableVertexAttribArray(aTexCoord);

//draw
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, &indices[0]);

顶点着色器:

attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
varying vec3 vPosition;
uniform mat4 uMVP;

void main()
{
    gl_Position = uMVP * aPosition;

    vTexCoord = aTexCoord;
    vPosition = gl_Position.xyz;
}

Fragment Shader:

varying vec2 vTexCoord;
varying vec3 vPosition;
uniform sampler2D uTex;

void main()
{
    vec4 texCol = texture2D(uTex, vTexCoord.xy);
    gl_FragColor = texCol;
}

1 个答案:

答案 0 :(得分:3)

0不是iOS上的默认帧缓冲区ID。

获取默认的帧缓冲区ID:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    [((GLKView *) self.view) bindDrawable];

    GLint defaultFBO;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);

    //draw ...
}