好吧,FBO 的使用范围很广,它有助于使复杂的效果更容易。我设置FBO并绘制它,然后渲染FBO纹理四边形,没有任何问题,甚至测试简单的屏幕处理颜色着色器。但是由于某种原因,我渲染给 FBO 的所有内容都颠倒了。如果我渲染到股票屏幕 FBO 一切正常,我什至不执行任何旋转精灵。
FBO创建
typedef struct tex
{
dim2i size; // int width, int height
GLuint id;
} tex;
typedef struct fbo
{
GLuint frame_buffer;
tex buffer_tex;
}fbo;
void set_fbo(fbo* res, int width, int height)
{
GLint tex_size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &tex_size);
GLint render_buffer_size;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &render_buffer_size);
LOGI("Max render buffer size: %d, max texture size: %d", render_buffer_size, tex_size); //max render buffer size: 3838, max texture size: 2048
glGenFramebuffers(1, &res->frame_buffer);
GLuint depth_buffer;
glGenRenderbuffers(1, &depth_buffer);
glGenTextures(1, &res->buffer_tex.id);
res->buffer_tex.size.w = width;
res->buffer_tex.size.h = height;
glBindTexture(GL_TEXTURE_2D, res->buffer_tex.id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, res->buffer_tex.size.w, res->buffer_tex.size.h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, res->buffer_tex.size.w, res->buffer_tex.size.h);
glBindFramebuffer(GL_FRAMEBUFFER, res->frame_buffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, res->buffer_tex.id, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
LOGE("BUFFER OK");
else
LOGE("BUFFER NOT OK");
//BUFFER OK
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
FBO 使用情况
static fbo FBO;
static decor DECOR; //shield quad
static decor SCREEN_DECOR;// FBO textured quad
void on_surface_changed(int width, int height)
{
set_fbo(&FBO, width, height);
...
}
void on_draw()
{
glBindFramebuffer(GL_FRAMEBUFFER, FBO.frame_buffer);
set_render_color(YELLOW); //glClearColor(...)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_decor(&DECOR, get_shader(SPRITE_SIMPLE_SHADER), *get_proj(ORTHO_PROJ));
glBindFramebuffer(GL_FRAMEBUFFER, 0);
set_render_color(BLACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_decor(&SCREEN_DECOR, get_shader(SPRITE_SIMPLE_SHADER), *get_proj(ORTHO_PROJ));
}
结果: 带有灰度着色器的结果:
不使用 FBO 渲染:
void on_draw_simple()
{
set_render_color(YELLOW); //glClearColor(...)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_decor(&DECOR, get_shader(SPRITE_SIMPLE_SHADER), *get_proj(ORTHO_PROJ));
}