How to load an image in WebGL - Jim Fisher

文章推薦指數: 80 %
投票人數:10人

How to load an image in WebGL. Above you should see a picture of me. It looks weird, not because I look weird but because the RGB channels ... LearnmoreaboutRussianwarcrimesinUkraine. HowtoloadanimageinWebGL Aboveyoushouldseeapictureofme. Itlooksweird,notbecauseIlookweirdbutbecausetheRGBchannelsareswapped. ThisisdonewithaWebGLfragmentshader,whichlookslike: uniformsampler2Dtex; voidmain(void){ mediumpvec2coord=vec2(gl_FragCoord.x/512.0,1.0-(gl_FragCoord.y/512.0)); mediumpvec4sample=texture2D(tex,coord); gl_FragColor=vec4(sample.b,sample.r,sample.g,1.0); } Toloadtheimageintotheuniformsampler2Dtex,Iused: constimg=newImage(); img.onload=function(){ gl.activeTexture(gl.TEXTURE0); consttex=gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D,tex); gl.texImage2D(gl.TEXTURE_2D,0,gl.RGB,gl.RGB,gl.UNSIGNED_BYTE,img); gl.generateMipmap(gl.TEXTURE_2D); consttexLoc=gl.getUniformLocation(prog,"tex"); gl.uniform1i(texLoc,0); gl.drawArrays(gl.TRIANGLE_FAN,0,4);//drawovertheentireviewport }; img.src='/assets/jim_512.jpg'; IjustreleasedTigYog: interactivetutorialsoncoding,math,crypto,science! Learnfromwizards,orwriteyourownquizzes! What’syourbag? Coding Math MorebyJim YoursyntaxhighlighteriswrongGranddaddiedtodayTheThreeTsofTime,ThoughtandTyping:measuringcostonthewebIhatetelephonesThesorrystateofOpenSSLusabilityThedotsdomatter:howtoscamaGmailuserMyparentsareFlat-EarthersHowHackerNewsstaysinterestingProjectC-43:thelostoriginsofasymmetriccryptoThehackerhypecycleTheinceptionbar:anewphishingmethodTimeisrunningouttocatchCOVID-19AprobabilisticpubquizfornerdsSmearphishing:anewAndroidvulnerability Tagged. AllcontentcopyrightJamesFisher2017. Thispostisnotassociatedwithmyemployer. Foundanerror?Editthispage. JimFisher TigYog Speaking CV Blogroll RSS



請為這篇文章評分?