【OpenGL蓝宝书】Chapter 9. Fragment Processing and the Framebuffer

编程入门 行业动态 更新时间:2024-10-13 16:16:30

【OpenGL蓝<a href=https://www.elefans.com/category/jswz/34/1688550.html style=宝书】Chapter 9. Fragment Processing and the Framebuffer"/>

【OpenGL蓝宝书】Chapter 9. Fragment Processing and the Framebuffer

四、Off-Screen Rendering

1. 自定义framebuffer。

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color_texture, 0);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_texture, 0);static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, draw_buffers);

首先生成并绑定当前framebuffer为自定义的fbo。然后,将texture设置到FBO的color attachment或者depth attachment。最后,传一个数组给glDrawBuffers(),它表示当前fbo将会渲染到数组中的color attachment。

另外,在fragment shader中,指定out 变量的location就可以把该颜色输出至draw_buffers[]数组的相应位置。如下所示,color将会输出至draw_buffers[]的第三个attachment。

#version 410 core                                                            in VS_OUT                                                                    
{                                                                            vec4 color;                                                              vec2 texcoord;                                                           
} fs_in;                                                                     layout (location = 2) out vec4 color;                                                              void main(void)                                                              
{                                                                            color = sin(fs_in.color * vec4(40.0, 20.0, 30.0, 1.0)) * 0.5 + vec4(0.5);
}                                                                            

在render的时候,clearbuffer时要clear所有的colorbuffer,也即clear draw_buffers[]数组中的所有color attachment.

glClearBufferfv(GL_COLOR, 2, sb7::color::Green);

第二个参数与fragment shader中out变量的location对应。

更多推荐

【OpenGL蓝宝书】Chapter 9. Fragment Processing and the Framebuffer

本文发布于:2024-02-12 16:27:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宝书   Chapter   OpenGL   Framebuffer   Processing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!