我尝试用fbo制作android ndk opengl es2。但是得到了黑屏。 以下是代码。
init fbo
glGenFramebuffers(1, &FBO);
glGenRenderbuffers(1, &RBO);
glGenTextures(1, &FTextureID);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glBindRenderbuffer(GL_RENDERBUFFER, RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, m_width, m_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, RBO);
glBindTexture(GL_TEXTURE_2D, FTextureID);
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, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glGenerateMipmap(FTextureID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FTextureID, 0);
glBindTexture(GL_TEXTURE_2D, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::POSTPROCESSOR: Failed to initialize FBO" << std::endl;
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
init_render_data();
m_post_processor_shader.set_int("scene", 0, GL_TRUE);
init_render_data
GLuint VBO;
GLfloat vertices[] = {
// Pos // Tex
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
m_vertex_handle = glGetAttribLocation(m_post_processor_shader.get_id(), "vertex");
__android_log_print(ANDROID_LOG_INFO, "[Breakout][post_processor]", "glGetAttribLocation(\"vertex\") = %d\n",
m_vertex_handle);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(m_vertex_handle);
glVertexAttribPointer(m_vertex_handle, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
类着色器。 set_int和使用功能
shader& shader::use()
{
glUseProgram(m_id);
return *this;
}
void shader::set_int(const GLchar* name, GLuint value, GLboolean use_shader)
{
if(use_shader)
use();
glUniform1f(glGetUniformLocation(m_id, name), value);
}
着色器
顶点
attribute vec4 vertex;
varying vec2 TexCoords;
void main()
{
gl_Position = vec4(vertex.xy, 0.0f, 1.0f);
TexCoords = vertex.zw;
}
片段
precision mediump float;
varying vec2 TexCoords;
uniform sampler2D scene;
void main()
{
gl_FragColor = texture2D(scene, TexCoords);
}
渲染
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//render scene
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
//render texture from fbo
m_post_processor_shader.use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, FTextureID);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glEnableVertexAttribArray(m_vertex_handle);
glVertexAttribPointer(m_vertex_handle, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
着色器编译和链接正确。 fbo创建正确的
glCheckFramebufferStatus(GL_FRAMEBUFFER)
返回GL_FRAMEBUFFER_COMPLETE
。
场景绘制正确(无fbo)。与fbo黑屏。 我能问什么问题吗?
答案 0 :(得分:0)
我发现了错误。顶点缓冲区对象的init_render_data变量名称中的值为VBO
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
在渲染函数中,我使用变量m_VBO
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
还有这整个错误