1 / 25

CH8 Frame Buffer Object

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

kevlyn
Download Presentation

CH8 Frame Buffer Object

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CH8 Frame Buffer Object

  2. 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

  3. Introduction • Name • OpenGL : Frame Buffer Object • DirectX: Render Target • Usage • Render to Texture

  4. 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

  5. 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)

  6. Attachment • Texture • Render Buffer • You cant send it to shader • It supports some format that texture doesn’t.

  7. 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)

  8. 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!)

  9. Attachment Points Framebuffer Object Pixels GL_COLOR_ATTACHMENT0 Texture GL_COLOR_ATTACHMENT1 ......... GL_DEPTH_ATTACHMENT Render Buffer GL_STENCIL_ATTACHMENT

  10. 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

  11. 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

  12. 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

  13. 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 };

  14. 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!

  15. 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

  16. Additional Information • Build Mipmap • void glGenerateMipmap(GLenum target); • Get Texture Content • void glGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, GLvoid * img);

  17. Example : Render To Texture

  18. 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();

  19. 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; }

  20. 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);

  21. //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); }

  22. 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]);

  23. 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); }

  24. //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);

  25. 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(); }

More Related