What is the purpose of vertexAttribPointer? - Stack Overflow

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

it tells WebgL how to interpret the data: gl.vertexAttribPointer(attrib, index, gl.FLOAT, false, stride, offset);. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore WhatisthepurposeofvertexAttribPointer? AskQuestion Asked 7years,8monthsago Modified 1yearago Viewed 8ktimes 10 2 Iamwritingagameengineusingjavascriptandwebgl.TotestitoutIhavewrittenaprogramthatdrawsacube.ForthisprogramtoworkvertexAttribPointermustbecalledafterIhavecalledbindbuffersbutbeforeIcalldrawtriangles.IwaswonderingwhatexactlythismethoddidandwhyIhavetocallthesemethodsinthisorder? MybestguessisthatitinitializestheattributebutIdon'tunderstandwhyitmustbecalledontheclientsideifthatisthecase. Ihaveincludesomeofthesourcebelow.Everythingiswrittenintypescript.Forthefullsourceseegithub.com/dkellycollins/nemesis Settinguptheshader: varcubeShader=newshaderProgram(); cubeShader.addShader(shaders.colorVertexShader); cubeShader.addShader(shaders.colorFragmentShader); cubeShader.init(); cubeShader.enableAttrib("position",3,4*(3+3),0); cubeShader.enableAttrib("color",3,4*(3+3),3*4); ShaderProgram: classshaderProgram{ constructor(){ this.Id=gl.createProgram(); } publicId; publicaddShader(shader:WebGLShader[]):void{ if(shaderinstanceofArray){ shader.forEach(s=>{ gl.attachShader(this.Id,s); }); }else{ gl.attachShader(this.Id,shader); } } publicinit():void{ gl.linkProgram(this.Id); } publicsetActive(){ gl.useProgram(this.Id); } publicenableAttrib(attribName:string,index:number,stride:number,offset:number){ varattrib=gl.getAttribLocation(this.Id,attribName); gl.enableVertexAttribArray(attrib); gl.vertexAttribPointer(attrib,index,gl.FLOAT,false,stride,offset); } publicsetFloatAttrib(attribName:string,value:number){ varattrib=gl.getAttribLocation(this.Id,attribName); gl.vertexAttrib1f(attrib,value); } publicsetMatrix(uniName:string,value:number[]){ varuniform=gl.getUniformLocation(this.Id,uniName); gl.uniformMatrix4fv(uniform,false,value); } } Renderingthecube: publicrender():void{ gl.drawElements(gl.TRIANGLES,this._triangles,gl.UNSIGNED_SHORT,0); } Vertexshadersource: attributevec3position;//thepositionofthepoint uniformmat4Pmatrix; uniformmat4Vmatrix; uniformmat4Mmatrix; attributevec3color;//thecolorofthepoint varyingvec3vColor; voidmain(void){//pre-builtfunction gl_Position=Pmatrix*Vmatrix*Mmatrix*vec4(position,1.);//0.isthez,and1isw vColor=color; } javascriptopengl-estypescriptwebgl Share Improvethisquestion Follow editedOct31,2014at15:39 genpfault 49.5k1010goldbadges8080silverbadges129129bronzebadges askedOct31,2014at15:02 dkellycollinsdkellycollins 49766silverbadges1616bronzebadges Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 14 ittellsWebgLhowtointerpretthedata: gl.vertexAttribPointer(attrib,index,gl.FLOAT,false,stride,offset); thismeans:forattributeattribthereareindexcomponentsoftypegl.FLOATthatarenotnormalizedstartingatoffsetandstrideapartinthegl.ARRAY_BUFFERboundatthetimeofthiscall. Theclientisfreetosetitsdataanywayitwishesaslongastheycanbedescribedasabove. Share Improvethisanswer Follow editedJun11,2021at15:26 answeredOct31,2014at15:37 ratchetfreakratchetfreak 46.1k55goldbadges6464silverbadges104104bronzebadges 4 Sowhymustthisbedoneforattributes?Uniformsdon'tneedtodothis.Shouldn'twebglbeabletodeterminetheindex,offset,andstridejustfromthefacttheattributeisavec3oramImissingsomething? – dkellycollins Oct31,2014at15:48 1 Itwouldn'tbeabletofigureoutstride(thatdependsentirelyonhowtheprogrammerinterleavedthedata,thereisadefault0valuefortightlypackedthough),andsomepeopleusehalffloatsorintegersorshortsforthedata(andthebufferstoresjustasequenceofbyteswithouttypeinformation) – ratchetfreak Oct31,2014at15:57 IthinkIunderstandnow.Iwasn'tputtingtogetherthatallattributesgettheirdatafromthearraybuffer.Thankyouforthehelp! – dkellycollins Oct31,2014at20:19 5 justtobeclear,therecanbemorethanoneARRAY_BUFFERandeveryattributecanreferenceadifferentbuffer.WhichbufferthatattributeisgettingitsdatafromisdeterminedbywhichbufferwasboundtotheARRAY_BUFFERbindpointatthetimevertexAttribPointerascalledforaparticularattribute. – gman Oct31,2014at20:51 Addacomment  |  1 AccordingtothisWebGl2Fundamentalstutorialthisisnecessarytospecifyhowtopullthedataout.Also: Ahiddenpartofgl.vertexAttribPointeristhatitbindsthecurrentARRAY_BUFFERtotheattribute.InotherwordsnowthisattributeisboundtopositionBuffer.Thatmeanswe'refreetobindsomethingelsetotheARRAY_BUFFERbindpoint.TheattributewillcontinuetousepositionBuffer. Share Improvethisanswer Follow answeredMar22,2021at10:38 ArthurVisserArthurVisser 3966bronzebadges Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedjavascriptopengl-estypescriptwebgloraskyourownquestion. TheOverflowBlog HowStackOverflowislevelingupitsunittestinggame Developersvsthedifficultybomb(Ep.459) FeaturedonMeta Testingnewtrafficmanagementtool Duplicatedvotesarebeingcleanedup Trending:Anewanswersortingoption Updatedbuttonstylingforvotearrows:currentlyinA/Btesting Related 5172 WhatisthemostefficientwaytodeepcloneanobjectinJavaScript? 4154 HowdoIcopytotheclipboardinJavaScript? 5805 What'sthedifferencebetweenusing"let"and"var"? 3867 Whatisthe!!(notnot)operatorinJavaScript? 3242 WhatistheJavaScriptversionofsleep()? 8209 Whatdoes"usestrict"doinJavaScript,andwhatisthereasoningbehindit? 1638 WhatisthepurposeofthevarkeywordandwhenshouldIuseit(oromitit)? 3344 Whatisthedifferencebetweencallandapply? 1529 WhatisthepurposeofNode.jsmodule.exportsandhowdoyouuseit? 6386 HowdoIreturntheresponsefromanasynchronouscall? HotNetworkQuestions TheEndoftheRainbow Can"RighttoRepair"laws(orotherlaws)requireacompanytoopensourcetheirproprietaryfirmware? Problemmodellingasimpleshapeandthenapplyingsubdivisionsurfacemodifier Howistimemeasuredinparticleexperiments? HowdoIknowwhichweaponstokeepinBorderlands2? Whyisitcalled"slewrate"? Ifnuclearweaponswereneverexistedorinvented,whatcouldreplaceit HowdoIleaveagameaftercharacterdeathwithouthurtingotherplayers'feelings? Whatisthedifferencebetween\dotsmand\cdots? Whatpumpsbloodthroughalistener'sbody? Bashfunctioninsidefunction:Howtointerpolatecertainvariablesfromouterfunction Cgenericdynamicarray(usingpreprocessor) ExampleillustratingnecessityofconsideringbirationalequivalenceandnotbiholomorphicequivalenceinMMP HowdoInotatealternatingquicklybetweentwonotesanoctaveawayfromeachother? Bookinvolvingametaverseofmanyparallelworlds,asectionofwhichisknownas"TheBlight" UnusualroutesfromUKtoDenmark Howtoinsertpilcrow¶atthestartofeachparagraphautomatically "if"statementsyntaxdifferencesbetweenCandC++ Cananelectricfieldbeconvertedintoamagneticfieldandhow?Ifnot,thenwhy? Short-termcapitalgainstaxoncryptocurrency Isthereanultimatebreak-evenpointwithrocketnozzlelengthorexpansionratioinavacuum? Ijustrealizedthatmypublishedpapersarebasedonabuggyversionofmydataset(duetoaSQLerror).Howtoproceed?Howtotellmyadvisor? Howtowritecorrectly$|.|$ Arebriftersmeanttospin/slideonimpact,bythechoiceofalowattachment-bolttorque? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-js Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?