Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.

Similar presentations


Presentation on theme: "CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2."— Presentation transcript:

1 CH8 Frame Buffer Object 1

2 Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2

3 Replace the Screen Pixels Screen 0 User FBO 1 User FBO n …. (Default) Bind Color Color Attachment 0 Depth Attachment Depth Color Attachment 0 Color Attachment 1 gl_FragData[0] gl_FragData[1] 3

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

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

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

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

8 Attachment Points 8

9 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 9

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

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

12 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 }; 12

13 Pixel Shader Color Attachment 0 Color Attachment 1 gl_FragData[0] gl_FragData[1] Pixel Shader Color Attachment 0 Color Attachment 1 gl_FragData[0] gl_FragData[1] If you want … Does Not Recommend! GLenum iDrawBuffer[2] = { GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT0_EXT, }; glDrawBuffers(2, iDrawBuffer); 13

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

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

16 Example : Render To Texture 16

17 Code #include #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(); 17

18 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; } 18

19 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); 19

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

21 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]); 21

22 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); } 22

23 //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); 23

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


Download ppt "CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2."

Similar presentations


Ads by Google