opengl Tutorial => Instancing by Vertex Attribute Arrays

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

A single instance represents one object or group of vertices (one grass leaf etc). Attributes associated with instanced arrays only advance between instances; ... Downloadopengl(PDF) opengl Gettingstartedwithopengl 3dMath BasicLighting EncapsulatingOpenGLobjectswithC++RAII Framebuffers Instancing InstancingbyVertexAttributeArrays OGLviewandprojection OpenGLcontextcreation. ProgramIntrospection ShaderLoadingandCompilation Shaders Texturing UsingVAOs opengl Gettingstartedwithopengl 3dMath BasicLighting EncapsulatingOpenGLobjectswithC++RAII Framebuffers Instancing InstancingbyVertexAttributeArrays OGLviewandprojection OpenGLcontextcreation. ProgramIntrospection ShaderLoadingandCompilation Shaders Texturing UsingVAOs opengl Instancing InstancingbyVertexAttributeArrays Example 3.3 Instancingcanbedoneviamodificationstohowvertexattributesareprovidedtothevertexshader.Thisintroducesanewwayofaccessingattributearrays,allowingthemtoprovideper-instancedatathatlookslikearegularattribute. Asingleinstancerepresentsoneobjectorgroupofvertices(onegrassleafetc).Attributesassociatedwithinstancedarraysonlyadvancebetweeninstances;unlikeregularvertexattributes,theydonotgetanewvalueper-vertex. Tospecifythatanattributearrayisinstanced,usethiscall: glVertexAttribDivisor(attributeIndex,1); Thissetsvertexarrayobjectstate.The"1"meansthattheattributeisadvancedforeachinstance.Passinga0turnsoffinstancingfortheattribute. Intheshader,theinstancedattributelookslikeanyothervertexattribute: invec3your_instanced_attribute; Torendermultipleinstances,youcaninvokeoneoftheInstancedformsofthevalueglDraw*calls.Forexample,thiswilldraw1000instances,witheachinstanceconsistingof3vertices: glDrawArraysInstanced(GL_TRIANGLES,0,3,1000); InstancedArrayCode SettingupVAOs,VBOsandtheattributes: //Listof10trianglex-offsets(translations) GLfloattranslations[10]; GLintindex=0; for(GLintx=0;x<10;x++) { translations[index++]=(GLfloat)x/10.0f; } //vertices GLfloatvertices[]={ 0.0f,0.05f, 0.05f,-0.05f, -0.05f,-0.05f, 0.0f,-0.1f, }; //SettingVAOsandVBOs GLuintmeshVAO,vertexVBO,instanceVBO; glGenVertexArrays(1,&meshVAO); glGenBuffers(1,&instanceVBO); glGenBuffers(1,&vertexVBO); glBindVertexArray(meshVAO); glBindBuffer(GL_ARRAY_BUFFER,vertexVBO); glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(GLfloat),(GLvoid*)0); glBindBuffer(GL_ARRAY_BUFFER,instanceVBO); glBufferData(GL_ARRAY_BUFFER,sizeof(translations),translations,GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1,1,GL_FLOAT,GL_FALSE,sizeof(GLfloat),(GLvoid*)0); glVertexAttribDivisor(1,1);//Thissetsthevertexattributetoinstancedattribute. glBindBuffer(GL_ARRAY_BUFFER,0); glBindVertexArray(0); Drawcall: glBindVertexArray(meshVAO); glDrawArraysInstanced(GL_TRIANGLE_STRIP,0,4,10);//10diamonds,4verticesperinstance glBindVertexArray(0); Vertexshader: #version330core layout(location=0)invec2position; layout(location=1)infloatoffset; voidmain() { gl_Position=vec4(position.x+offset,position.y,0.0,1.0); } Fragmentshader: #version330core layout(location=0)outvec4color; voidmain() { color=vec4(1.0,1.0,1.0,1.0f); } PDF-Downloadopenglforfree Previous Next ThismodifiedtextisanextractoftheoriginalStackOverflowDocumentationcreatedbyfollowingcontributorsandreleasedunderCCBY-SA3.0 ThiswebsiteisnotaffiliatedwithStackOverflow SUPPORT&PARTNERS Advertisewithus Contactus PrivacyPolicy STAYCONNECTED Getmonthlyupdatesaboutnewarticles,cheatsheets,andtricks.  Subscribe



請為這篇文章評分?