I'm using a Framebuffer to do post processing.
Creating and drawing to the buffer (tested via glReadPixels
and glCheckFramebufferStatus
) is working fine.
But now I want to render the this texture with a quad to the screen. But the screen remains black.
The same code (for drawing) is working fine on other platforms like android, windows, OSX.
Init Framebuffer with stencil and texture::
//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);
Drawing content to framebuffer:
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);
Drawing framebuffer content:
//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]);
Vertex-Shader:
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;
}
0
is not the default framebuffer id on iOS.to get the default framebuffer id: