opengl Tutorial => Instancing by Vertex Attribute Arrays
文章推薦指數: 80 %
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
延伸文章資訊
- 1opengl Tutorial => Instancing by Vertex Attribute Arrays
A single instance represents one object or group of vertices (one grass leaf etc). Attributes ass...
- 2实例化
如果我们能够将数据一次发送给GPU,就会更方便,然后告诉OpenGL使用一个绘制函数,将这些数据绘制为多个物体。这就是我们将要展开讨论的实例化(Instancing)。
- 3Instanced Rendering | Drawing with OpenGL | InformIT
Instancing, or instanced rendering, is a way of executing the same drawing commands many times in...
- 4Instancing - OpenGL ES SDK for Android - GitHub Pages
This sample presents the instanced drawing technique using OpenGL ES 3.0. Overview. Instancing_an...
- 5Instanced Rendering - OpenGL Wiki
Instanced Rendering. From OpenGL Wiki. Redirect page. Jump to navigation Jump to search. Redirect...