嗯,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));
}
答案 0 :(得分:4)
从评论中汇总:
这是因为OpenGL的左下角有原点,与大多数使用中的窗口系统不同,原点位于左上角。
只需确保纹理坐标从左上角的0,1到右下角的1,0以正确绘制FBO,或者在垂直翻转纹理的情况下绘制四边形。