opengl Tutorial => Basics of framebuffers
文章推薦指數: 80 %
Framebuffer is a type of buffer which stores color values, depth and stencil information of pixels in memory. When you draw something in OpenGL the output ... Downloadopengl(PDF) opengl Gettingstartedwithopengl 3dMath BasicLighting EncapsulatingOpenGLobjectswithC++RAII Framebuffers Basicsofframebuffers Instancing OGLviewandprojection OpenGLcontextcreation. ProgramIntrospection ShaderLoadingandCompilation Shaders Texturing UsingVAOs opengl Gettingstartedwithopengl 3dMath BasicLighting EncapsulatingOpenGLobjectswithC++RAII Framebuffers Basicsofframebuffers Instancing OGLviewandprojection OpenGLcontextcreation. ProgramIntrospection ShaderLoadingandCompilation Shaders Texturing UsingVAOs opengl Framebuffers Basicsofframebuffers Example Framebufferisatypeofbufferwhichstorescolorvalues,depthandstencilinformationofpixelsinmemory.WhenyoudrawsomethinginOpenGLtheoutputisstoredinthedefaultframebufferandthenyouactuallyseethecolorvaluesofthisbufferonscreen.Youcanalsomakeyourownframebufferwhichcanbeusedforalotofcoolpost-processingeffectssuchasgray-scale,blur,depthoffield,distortions,reflections... Tostartofyouneedtocreateaframebufferobject(FBO)andbinditlikeanyotherobjectinOpenGL: unsignedintFBO; glGenFramebuffers(1,&FBO); glBindFramebuffer(GL_FRAMEBUFFER,FBO); Nowyouhavetoaddatleastoneattachment(color,depthorstencil)totheframebuffer.Anattachmentisamemorylocationthatactsasabufferfortheframebuffer.Itcaneitherbeatexture,orarenderbufferobject.Theadvantageofusingatextureisthatyoucaneasilyusethistextureinapost-processingshaders.Creatingthetextureissimilarasanormaltexture: unsignedinttexture; glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); Thewidthandheightshouldbethesameasyourrenderingwindowsize.ThetexturedatapointerisNULLbecauseyouonlywanttoallocatethememoryandnotfillthetexturewithanydata.Thetextureisreadysoyoucanactuallyattachittotheframebuffer: glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,texture,0); Yourframebuffershouldbereadytousenowbutyoumaywanttoalsoadddepthattachmentorbothdepthandstencilattachments.Ifyouwanttoaddthoseastextureattachments(andusethemforsomeprocessing)youcancreateanothertextureslikeabove.Theonlydifferencewouldbeintheselines: glTexImage2D( GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,width,height,0, GL_DEPTH_COMPONENT,GL_FLOAT,NULL ); glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,texture,0); Ortheseifyouwanttousedepthandstencilattachmentinasingletexture: glTexImage2D( GL_TEXTURE_2D,0,GL_DEPTH24_STENCIL8,width,height,0, GL_DEPTH_STENCIL,GL_UNSIGNED_INT_24_8,NULL ); glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,GL_TEXTURE_2D,texture,0); Youcanalsousearenderbufferinsteadofatextureasanattachmentfordepthandstencilbuffersifdon'twanttoprocessthevalueslater.(Itwillbeexplainedinanotherexample...) Youcancheckiftheframebufferissuccessfullycreatedandcompletedwithoutanyerrors: if(glCheckFramebufferStatus(GL_FRAMEBUFFER)==GL_FRAMEBUFFER_COMPLETE) //dosomething... Andfinallydon'tforgettounbindtheframebuffersothatyoudon'taccidentallyrendertoit: glBindFramebuffer(GL_FRAMEBUFFER,0); Limits ThemaximumnumberofcolorbufferswhichcanbeattachedtoasingleframebuffercanbedeterminedbytheOGLfunctionglGetIntegerv,byusingtheparameterGL_MAX_COLOR_ATTACHMENTS: GLintmaxColAttchments=0; glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS,&maxColAttchments); Usingtheframebuffer Theusageisquitestraightforward.Firstlyyoubindyourframebufferandrenderyoursceneintoit.Butyouwon'tactuallyseeanythingyetbecauseyourrenderbufferisnotvisible.Sothesecondpartistorenderyourframebufferasatextureofafullscreenquadontothescreen.Youcanjustrenderitasitisordosomepost-processingeffects. Herearetheverticesforafullscreenquad: floatvertices[]={ //positionstexturecoordinates -1.0f,1.0f,0.0f,1.0f, -1.0f,-1.0f,0.0f,0.0f, 1.0f,-1.0f,1.0f,0.0f, -1.0f,1.0f,0.0f,1.0f, 1.0f,-1.0f,1.0f,0.0f, 1.0f,1.0f,1.0f,1.0f }; YouwillneedtostoretheminaVBOorrenderusingattributepointers.Youwillalsoneedsomebasicshaderprogramforrenderingthefullscreenquadwithtexture. Vertexshader: invec2position; invec2texCoords; outvec2TexCoords; voidmain() { gl_Position=vec4(position.x,position.y,0.0,1.0); TexCoords=texCoords; } Fragmentshader: invec2TexCoords; outvec4color; uniformsampler2DscreenTexture; voidmain() { color=texture(screenTexture,TexCoords); } Note:YoumayneedtoadjusttheshadersforyourversionofGLSL. Nowyoucandotheactualrendering.Asdescribedabove,thefirstthingistorenderthesceneintoyourFBO.TodothatyousimplybindyourFBO,clearitanddrawthescene: glBindFramebuffer(GL_FRAMEBUFFER,FBO); glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //drawyourscenehere... Note:InglClearfunctionyoushouldspecifyallframebufferattachmentsyouareusing(Inthisexamplecoloranddepthattachment). NowyoucanrenderyourFBOasafullscreenquadonthedefaultframebuffersothatyoucanseeit.TodothisyousimplyunbindyourFBOandrenderthequad: glBindFramebuffer(GL_FRAMEBUFFER,0);//unbindyourFBOtosetthedefaultframebuffer glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); shader.Use();//shaderprogramforrenderingthequad glBindTexture(GL_TEXTURE_2D,texture);//colorattachmenttexture glBindBuffer(GL_ARRAY_BUFFER,VBO);//VBOofthequad //YoucanalsouseVAOorattributepointersinsteadofonlyVBO... glDrawArrays(GL_TRIANGLES,0,6); glBindBuffer(GL_ARRAY_BUFFER,0); Andthat'sall!Ifyouhavedoneeverythingcorrectlyyoushouldseethesamesceneasbeforebutrenderedonafullscreenquad.Thevisualoutputisthesameasbeforebutnowyoucaneasilyaddpost-processingeffectsjustbyeditingthefragmentshader.(Iwilladdeffectsinanotherexample(s)andlinkithere) PDF-Downloadopenglforfree Previous Next ThismodifiedtextisanextractoftheoriginalStackOverflowDocumentationcreatedbyfollowingcontributorsandreleasedunderCCBY-SA3.0 ThiswebsiteisnotaffiliatedwithStackOverflow SUPPORT&PARTNERS Advertisewithus Contactus PrivacyPolicy STAYCONNECTED Getmonthlyupdatesaboutnewarticles,cheatsheets,andtricks. Subscribe
延伸文章資訊
- 1帧缓冲 - LearnOpenGL-CN
把这几种缓冲结合起来叫做帧缓冲(Framebuffer),它被储存于内存中。OpenGL给了我们自己定义帧缓冲的自由,我们可以选择性的定义自己的颜色缓冲、深度和模板缓冲。
- 2OpenGL學習腳印: 幀緩沖對象(Frame Buffer Object) - IT閱讀
FBO中包含兩種類型的附加圖像(framebuffer-attachable): 紋理圖像和RenderBuffer圖像(texture images and renderbuffer imag...
- 3opengl Tutorial => Basics of framebuffers
Framebuffer is a type of buffer which stores color values, depth and stencil information of pixel...
- 4OpenGL Frame Buffer Object (FBO) - Song Ho Ahn
Framebuffer is a collection of 2D arrays or storages utilized by OpenGL; colour buffers, depth bu...
- 5【OpenGL】OpenGL帧缓存对象(FBO:Frame Buffer Object)
OpenGL管线的最终渲染目的地被称作帧缓存(framebuffer)。帧缓冲是一些二维数组和OpenG所使用的存储区的集合:颜色缓存、深度缓存、模板缓存和累计缓存。