When should I enable/disable vertex position attributes in ...
文章推薦指數: 80 %
I'm working on some WebGL code that has multiple shader programs that run sequentially. Previously, I was using gl.enableVertexAttribArray(...) ...
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
WhenshouldIenable/disablevertexpositionattributesinWebGL/OpenGL?
AskQuestion
Asked
6years,3monthsago
Modified
1year,2monthsago
Viewed
6ktimes
6
2
I'mworkingonsomeWebGLcodethathasmultipleshaderprogramsthatrunsequentially.
Previously,Iwasusinggl.enableVertexAttribArray(...)asrequiredduringinitializationformyglcontextandshaders.Iassumed,perhapsincorrectly,thatcallingthisfunctionwassettingstatespecifictotheprogramselectedbygl.useProgram(...)
Now,myfirstshaderprogramhastwoenabledattributearrays,andmysecondonehasoneenabled.Whenthesecondprogramruns,Igetanerror:
Error:WebGL:drawArrays:noVBOboundtoenabledvertexattribindex1!
SothatleadsmetothinkthatmaybeIneedtobedisablingvertexattribute1afterusingitinthefirstprogram,butIwantedtoverifythatthisishowI'msupposedtobedoingit,andhopefullygetanexplanationofwhythisisorisn'tcorrect.
IsthebestpracticetoenableVertexAttribArray(...)anddisableVertexAttribArrayforeveryarraylocationbeforeandaftereachuse?
javascriptwebgl
Share
Follow
editedMar29,2016at15:33
NicolBolas
416k6262goldbadges720720silverbadges912912bronzebadges
askedMar29,2016at15:17
JoshuaBreedenJoshuaBreeden
1,69311goldbadge1515silverbadges2727bronzebadges
2
soundslikeitexpectsitafteryousetthevertexAttribPointer
– ratchetfreak
Mar29,2016at15:24
Whoops...Idescribeditincorrectlybefore.Editednow,butit'smyfirstprogramwithtwoattributes,andmysecondwithone.Anditdoesn'tlikegoingtooneafterstartingwithtwo.
– JoshuaBreeden
Mar29,2016at15:26
Addacomment
|
3Answers
3
Sortedby:
Resettodefault
Highestscore(default)
Trending(recentvotescountmore)
Datemodified(newestfirst)
Datecreated(oldestfirst)
7
I'venevercalleddisableVertexAttribArrayinmylifeandI'vewritten100sofWebGLprograms.Theremayormaynotbeanyperfbenefitstocallingitbutthere'snocompatibilityissuestonotcallingit.
Thespecsaysyou'llonlygetanerroriftheattributeisconsumedbythecurrentprogramandaccesswouldbeoutofrangeorifthere'snobufferboundtoanenabledattribute.
Wecantestthatandseethatitworksjustfine.
varvsThatUses2Attributes=`
attributevec4position;
attributevec2texcoord;
varyingvec2v_texcoord;
voidmain(){
v_texcoord=texcoord;
gl_Position=position;
}
`;
varvsThatUses1Attribute=`
attributevec4position;
varyingvec2v_texcoord;
voidmain(){
v_texcoord=position.xy*0.5+0.5;
gl_Position=position+vec4(1,0,0,0);
}
`;
varfs=`
precisionmediumpfloat;
varyingvec2v_texcoord;
voidmain(){
gl_FragColor=vec4(v_texcoord,v_texcoord.x*v_texcoord.y,1);
}
`;
vargl=document.querySelector("canvas").getContext("webgl");
document.body.appendChild(gl.canvas);
varprogramThatUses2Attributes=twgl.createProgramFromSources(gl,[vsThatUses2Attributes,fs]);
varprogramThatUses1Attribute=twgl.createProgramFromSources(gl,[vsThatUses1Attribute,fs]);
varpositionLocation2AttribProg=gl.getAttribLocation(programThatUses2Attributes,"position");
vartexcoordLocation2AttribProg=gl.getAttribLocation(programThatUses2Attributes,"texcoord");
varpositionLocation1AttribProg=gl.getAttribLocation(programThatUses1Attribute,"position");
varpositionBufferFor2AttribPrg=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,positionBufferFor2AttribPrg);
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array([
-1,-1,
-.5,1,
0,-1,
]),gl.STATIC_DRAW);
vartexcoordBufferFor2AttribPrg=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBufferFor2AttribPrg);
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array([
0,0,
0.5,1,
1,0,
]),gl.STATIC_DRAW);
varpositionBufferFor1AttribPrg=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,positionBufferFor1AttribPrg);
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array([
-1,-1,
0,-1,
-1,1,
-1,1,
0,-1,
0,1,
]),gl.STATIC_DRAW);
//turnon2attributes
gl.enableVertexAttribArray(positionLocation2AttribProg);
gl.enableVertexAttribArray(texcoordLocation2AttribProg);
gl.bindBuffer(gl.ARRAY_BUFFER,positionBufferFor2AttribPrg);
gl.vertexAttribPointer(positionLocation2AttribProg,2,gl.FLOAT,false,0,0);
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBufferFor2AttribPrg);
gl.vertexAttribPointer(texcoordLocation2AttribProg,2,gl.FLOAT,false,0,0);
//drawwith2attributesenabled
gl.useProgram(programThatUses2Attributes);
gl.drawArrays(gl.TRIANGLES,0,3);
//setupforsecondprogramthatusesonly1attribute
gl.bindBuffer(gl.ARRAY_BUFFER,positionBufferFor1AttribPrg);
gl.vertexAttribPointer(positionLocation1AttribProg,2,gl.FLOAT,false,0,0);
//NOTICEWEHAVE!NOTturnedoffotherattribute
gl.useProgram(programThatUses1Attribute);
gl.drawArrays(gl.TRIANGLES,0,6);
log("glError:",gl.getError());
functionlog(){
varpre=document.createElement("pre");
pre.appendChild(document.createTextNode(Array.prototype.join.call(arguments,"")));
document.body.appendChild(pre);
}
canvas{border:1pxsolidblack;}
Thisexampledrawsatrianglewith3verticesusing2attributes.
Itthendrawsaquadusing6verticesand1attribute
WITHOUTTURNINGOFFTHENOW2ndUNUSEDATTRIBUTE.
Thatmeansnotonlyisthatattributeleftonbutitonlyhas3verticeseven
thoughthedrawwilluse6vertices.Becausethatattributeisnot'comsumed'by
thecurrentprogramit'sokaccordingtothespec.
延伸文章資訊
- 1dom WebGLRenderingContext.enableVertexAttribArray()
The WebGLRenderingContext.enableVertexAttribArray() method of the WebGL API turns the generic ver...
- 2enableVertexAttribArray method - dart:web_gl library - Dart
API docs for the enableVertexAttribArray method from the RenderingContext2 class, for the Dart pr...
- 3When should I enable/disable vertex position attributes in ...
I'm working on some WebGL code that has multiple shader programs that run sequentially. Previousl...
- 4glEnableVertexAttribArray
- 5[Day5] WebGL 修羅道(2) - 資料傳遞 - iT 邦幫忙
enableVertexAttribArray(points); gl.vertexAttrib1f(size, 100.0); var color = gl.getUniformLocatio...