250 likes | 442 Views
CH8 Frame Buffer Object. Course Map. We are here!. Vertex pipeline. Transformation & Lighting. Viewport culling & clipping. Primitive assembly. Rasterizer setup. Pixel pipeline. Texture blending. Per Fragment operations. Buffer operations. Framebuffer. Introduction. Name
E N D
Course Map We are here! Vertex pipeline Transformation & Lighting Viewport culling & clipping Primitive assembly Rasterizer setup Pixel pipeline Texture blending Per Fragment operations Buffer operations Framebuffer
Introduction • Name • OpenGL : Frame Buffer Object • DirectX: Render Target • Usage • Render to Texture
Replace the Screen (Default) Screen 0 Pixels Color Color Attachment 0 User FBO 1 …. Depth Attachment Depth gl_FragData[0] User FBO n Bind Color Attachment 0 gl_FragData[1] Color Attachment 1
Functions • Create and Destroy FBO • void glGenFramebuffersEXT(GLsizei n, GLuint* ids) • void glDeleteFramebuffersEXT(GLsizei n, const GLuint* ids) • Bind FBO • void glBindFramebufferEXT(GLenum target, GLuint id)
Attachment • Texture • Render Buffer • You cant send it to shader • It supports some format that texture doesn’t.
Render Buffer • Create • void glGenRenderbuffersEXT(GLsizei n, GLuint* ids); • Destroy • void glDeleteRenderbuffersEXT(GLsizei n, const Gluint* ids); • Bind • void glBindRenderbufferEXT(GLenum target, GLuint id); • Storage • void glRenderbufferStorageEXT(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height)
Render Buffer Parameters • Target • GL_RENDERBUFFER_EXT • Internal Format • GL_RGB, GL_RGBA • GL_DEPTH_COMPONENT16, 24, 32, 32F • GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8. • GL_STENCIL_INDEX, GL_STENCIL_INDEX8_EXT(Does not recommend!)
Attachment Points Framebuffer Object Pixels GL_COLOR_ATTACHMENT0 Texture GL_COLOR_ATTACHMENT1 ......... GL_DEPTH_ATTACHMENT Render Buffer GL_STENCIL_ATTACHMENT
Attach Texture • glFramebufferTexture2DEXT(GLenum target, GLenum attachmentPoint, GLenum textureTarget, GLuint textureId, GLint level) • Target • GL_FRAMEBUFFER_EXT • Attachment Point • GL_COLOR_ATTACHMENT0_EXT ~ GL_COLOR_ATTACHMENTn_EXT • GL_DEPTH_ATTACHMENT_EXT • GL_STENCIL_ATTACHMENT_EXT • Texture Target • GL_TEXTURE_2D or GL_TEXTURE_2D_ARRAY • Texture Id • Set 0 to detach texture • Level • The mipmap level of texture
Attach Render Buffer • void glFramebufferRenderbufferEXT(GLenum target, GLenum attachmentPoint, GLenum renderbufferTarget, GLuint renderbufferId) • Target • GL_FRAMEBUFFER_EXT • Attachment Point • As same as texture • Render Buffer Target • GL_RENDERBUFFER_EXT • Render Buffer Id • Set 0 to detach render buffer
Multiple Render Target • void glDrawBuffers( GLsizei n, const GLenum *bufs ); • Call this function after binding anther FBO • n • The number for render targets • Buffers • The connection of gl_FragData[] to color attachment
The example of buffer array GLenum iDrawBuffer[16] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT, . . . . GL_COLOR_ATTACHMENT15_EXT };
If you want … gl_FragData[0] Color Attachment 0 Pixel Shader Color Attachment 1 gl_FragData[1] GLenum iDrawBuffer[2] = { GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT0_EXT, }; glDrawBuffers(2, iDrawBuffer); gl_FragData[0] Color Attachment 0 Pixel Shader Color Attachment 1 gl_FragData[1] Not Recommend!
Check FBO • GLenum glCheckFramebufferStatusEXT(GLenum target) • Return Value Case • GL_FRAMEBUFFER_COMPLETE_EXT : OK • GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT • GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT • GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT • GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT • GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT • GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT • GL_FRAMEBUFFER_UNSUPPORTED_EXT
Additional Information • Build Mipmap • void glGenerateMipmap(GLenum target); • Get Texture Content • void glGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, GLvoid * img);
Code #include <stdio.h> #include "glew.h" #include "glut.h“ int g_iViewport[4]; GLuint g_iFBO; GLuint g_iRB; GLuint g_iTexture; void init(); void display(); void reshape(GLsizei , GLsizei ); void init();
void reshape(GLsizei w, GLsizei h){ g_iViewport[0] = 0; g_iViewport[1] = 0; g_iViewport[2] = w; g_iViewport[3] = h; } int main(int argc, char** argv){ glutInit(&argc, argv); glutInitWindowSize(512, 512); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("FBO"); glewInit(); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }
void init() { //generate texture glEnable(GL_TEXTURE_2D); glGenTextures(1, &g_iTexture); glBindTexture(GL_TEXTURE_2D, g_iTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, 512, 512, 0, GL_RGBA, GL_FLOAT, NULL); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//generate render buffer glGenRenderbuffers(1, &g_iRB); glBindRenderbuffer(GL_RENDERBUFFER, g_iRB); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, 512, 512); //generate FBO glGenFramebuffers(1, &g_iFBO); glBindFramebuffer(GL_FRAMEBUFFER, g_iFBO); //attach glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_iRB); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_iTexture, 0); }
void display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); //Pass1 { //render to texture glBindFramebuffer(GL_FRAMEBUFFER, g_iFBO); //clear texture and render buffer in FBO glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); glViewport(g_iViewport[0], g_iViewport[1], g_iViewport[2], g_iViewport[3]);
glBindTexture(GL_TEXTURE_2D, 0); glBegin(GL_QUADS); glColor3d(1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glColor3d(1.0f, 0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); glColor3d(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glColor3d(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); glColor3f(1.0, 1.0, 1.0); glutSolidTeapot(0.5); }
//Pass2 { //render to screen glBindFramebuffer(GL_FRAMEBUFFER, 0); //clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, 0.5, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.5, 0.0, 50.0, 0.0, 50.0, 0.0, 1.0, 0.0);
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, g_iTexture); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, 0.0, 0.0); glTexCoord2f(0.0, 100.0); glVertex3f(-50.0, 0.0, 100.0); glTexCoord2f(100.0, 100.0); glVertex3f(50.0, 0.0, 100.0); glTexCoord2f(100.0, 0.0); glVertex3f(50.0, 0.0, 0.0); glEnd(); } glutSwapBuffers(); }