Exhibit #0: Creating and Using a Framebuffer Object
文章推薦指數: 80 %
A framebuffer object (FBO) allows WebGL programmer to render images to other memory location other than the default framebuffer. Exhibit0CreatingandUsingaFramebufferObject Post-processingfilter: CopyPixels BlurinXdirection BlurinYdirection ConverttosRGB WhatAreWeDoing? Aframebufferobject(FBO)allowsWebGLprogrammertorenderimagestoothermemory locationotherthanthedefaultframebuffer. Rememberthejargonsfromthefirstlecture: Buffer="aregionofphysicalmemoryusedtotemporarilystoredata" (accordingWikipedia). Inourcontext,abuffermostlyresidesintheGPU. Framebuffer=abufferthatholdstheimagethatisdisplyaedonthemonitor. WithanFBO,youcanaskWebGLtorendertoreceiversotherthanthemonitor: Textures:Wearealreadyfamiliarwiththis. Renderbuffers:Sameastexturesbutsupportfeweroperations. YoucannotuseWebGLcommandstoreadanythingfromarenderbuffer. Youcannotaccessarenderbufferinashader. TheyonlyserveastargetsforrenderedimagesandmustbeusewithanFBO. However,theyarelessofahassletomaintainthanatexture. AnFBOallowsforpost-processingofrenderedimages. Rendera3Dscenetoatexture. Operateontherenderedimagewithanothershader. TocreateanFBO,usethegl.createFramebufferObject()command. YouonlyneedoneFBOmostofthetime,sothiscommandisonlycalledonceinanapplication. varfbo=gl.createFramebuffer(); Here'showtorendertoatextureratherthanthemonitor: //Step1:BindtheFBO. gl.bindFramebuffer(gl.FRAMEBUFFER,fbo); //Step2:AttachthetexturetotheFBO. gl.framebufferTexture2D( //Firstargumentisalwaysgl.FRAMEBUFFER gl.FRAMEBUFFER, //Thesecondargumentindicatesthe"attachmentslot"oftheFBO. //Basically,itindicatesthefunctionofthetexturethatyouareattaching. //gl.COLOR_ATTACHMENT0meansthatthetexturewillserveasthezerothcolorbuffer. //Bydefault,thisistheonlycolorbufferattachmentslotofthatyoucanuseinvaniallyWebGL. //Touseothercolorbufferattachmentslot,youneedanextension.(Wewillcoverthislater.) gl.COLOR_ATTACHMENT0, //Thethirdargumentindicatesthekindoftextureweareattaching. //SinceweareattachingaTEXTURE_2D,wegiveitgl.TEXTURE_2D gl.TEXTURE_2D, //Thefourthargumentisthetexturethatyouwanttoattach. //Ofcourse,thismustbecreatedbeforehand. renderTexture, //Thefifthargumentisthemipmaplevelofthetexture. //Thisisalways0. 0 ); //Step3:Doyourrenderingasusual. { gl.clearColor(0.75,0.75,0.75,1.0); gl.clear(gl.COLOR_BUFFER_BIT); // //Codeomittedforbrevity. // //Step4:Flushthebufferjusttobesureeverythingisrenderedtothetexture. gl.flush(); } //Step4:Detachthetexture. //UsethesameargumentsasthecommandinStep2,butnowthetextureisnull. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, null,//nulltexturemeanswearedetaching. 0); //Step5:UnbindtheFBO. gl.bindFramebuffer(gl.FRAMEBUFFER,null); InLecture7,welearnedhowtocreateatexturefromanimage. However,therenderTextureweusedaboveisnotcreatedfromanimage. Itisoriginallyablanktexture. Tocreateablanktextureisaslightlydifferentfromcreatingatexturefromanimage. EverythingisthesameexceptfortheuseofthetexImage2Dcommand. functioncreateFloatTexture(gl,width,height){ vartexture=gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D,texture); gl.texImage2D( //Alwaysgl.TEXTURE_2Dfora2Dtexture. gl.TEXTURE_2D, //Mipmaplevel.Always0. 0, //Internalformatofeachpixel.HerewewantanRGBAtexture. gl.RGBA, //Widthofthetexture. width, //Heightofthetexture. height, //Widthoftheborderofthetexture.Always0. 0, //ThepixelformatofthedatathatisgoingtobeuploadedtotheGPU. //Wehavenodatahere,sousesomethingthatmatchestheinternalformat. gl.RGBA, //Thetypeofeachcomponentofthepixelthatisgoingtobeuploaded. //Herewewantafloatingpointtexture. gl.FLOAT, //Thedatathatisgoingtobeuploaded. //Wedon'thaveanydata,sowegivenull. //WebGLwilljustallocatethetextureandleaveitblank. null ); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE); gl.bindTexture(gl.TEXTURE_2D,null); returntexture; } varrenderTexture=createFloatTexture(gl,512,512); Withthecommandabove,wecreateatexturewhosepixelcomponentsarestoredasfloatingpoint numbers. ThisisnotafeaturesupportedbyvanillaWebGL. WeneedWebGLextensionstouseit,andhereishowtoenabletheextensions: vargl=initializeWebGL($("#webglCanvas")); gl.getExtension("OES_texture_float"); gl.getExtension("OES_texture_float_linear"); Therearetwoextensionsthatweenabled. OES_texture_floatallowsfloatingpointtextures. OES_texture_float_linearallowsgl.LINEARtobespecified asTEXTURE_MAG_FILTERandTEXTURE_MIN_FILTERparameters offloatingpointtextures.
延伸文章資訊
- 1WebGL中图片多级处理(FrameBuffer) - 纸异兽- 博客园
FBO(Frame Buffer Object)是被推荐用于将数据渲染到纹理对象的扩展。 FrameBuffer就像是一个webgl显示容器一样,平时我们使用gl.drawArrays或者gl.
- 2Introduction to Computer Graphics, Section 7.4 -- Framebuffers
In WebGL, a framebuffer is a data structure that organizes the memory resources that are needed t...
- 3JavaScript webgl framebuffer object | Develop Paper
By default, the final drawing result of webgl is stored in the color buffer, and the frame buffer...
- 4一起幫忙解決難題,拯救IT 人的一天
Framebuffer. 如何在網頁中繪製3D 場景?從WebGL 的基礎開始說起系列第22 篇. PastLeo. 7 個月前‧ 283 瀏覽. 0. 大家好,我是西瓜,你現在看到的是2021...
- 5How to work with framebuffers in webgl? - Stack Overflow
A fragment shader outputs to either the canvas or the attachments in a framebuffer. To be more te...