在前面我们学习OpenGL时,不管绘制如球,立方体,平面,地面,动画模型中最常用的几个操作有创建缓冲区,写入缓冲区.在Axiom中,相关的操作被整合与组织到VertexData,IndexData中,所以在后面,我们会看到Axiom里元素如果要绘画在屏幕上的元素,几乎都会包含这二个类的实体.
1 var decl = mesh.SharedVertexData.vertexDeclaration; 2 var binding = mesh.SharedVertexData.vertexBufferBinding; 3 4 var offset = 0; 5 decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Position ); 6 offset += VertexElement.GetTypeSize( VertexElementType.Float3 ); 7 decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Normal ); 8 offset += VertexElement.GetTypeSize( VertexElementType.Float3 ); 9 decl.AddElement( 0, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0 ); 10 offset += VertexElement.GetTypeSize( VertexElementType.Float2 );
在中间有AddElement就是增加一个VertexElement,其中Float3,Float2就是指如上的3F,2F.而Position,Normal,TexCoords分别对应上面的P,N,T.说到底,这个decl就是指定了如P3F_N3F_T2F这种结构.
1 var vertices = new float[32] 2 { 3 -100, -100, 0, // pos 4 0, 0, 1, // normal 5 0, 1, // texcoord 6 100, -100, 0, 0, 0, 1, 1, 1, 100, 100, 0, 0, 0, 1, 1, 0, -100, 100, 0, 0, 0, 1, 0, 0 7 }; 8 var vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( decl, 4, BufferUsage.StaticWriteOnly ); 9 binding.SetBinding( 0, vbuf ); 10 vbuf.WriteData( 0, vbuf.Size, vertices, true );
1 this.vboID = glGenBuffers(1) 2 glBindBuffer(GL_ARRAY_BUFFER,this.vboID) 3 glBufferData (GL_ARRAY_BUFFER, len(vdata)*4, vdata, GL_STATIC_DRAW) 4 this.eboID = glGenBuffers(1) 5 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this.eboID) 6 glBufferData (GL_ELEMENT_ARRAY_BUFFER, len(vIndex)*4, vIndex,GL_STATIC_DRAW)
可以发现少了点什么,是的,我们只是写入了顶点数据,对应顶点索引没有,看看在Axiom里如何使用.
1 var ibuf = HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, 6, BufferUsage.StaticWriteOnly ); 2 3 var faces = new short[6] 4 { 5 0, 1, 2, 0, 2, 3 6 }; 7 sub.IndexData.indexBuffer = ibuf; 8 sub.IndexData.indexCount = 6; 9 sub.IndexData.indexStart = 0; 10 ibuf.WriteData( 0, ibuf.Size, faces, true );
1 /// <summary> 2 /// Creates a plane as a submesh of the given mesh 3 /// </summary> 4 [OgreVersion( 1, 7, 2 )] 5 private static void _createPlane( Mesh mesh ) 6 { 7 var sub = mesh.CreateSubMesh(); 8 var vertices = new float[32] 9 { 10 -100, -100, 0, // pos 11 0, 0, 1, // normal 12 0, 1, // texcoord 13 100, -100, 0, 0, 0, 1, 1, 1, 100, 100, 0, 0, 0, 1, 1, 0, -100, 100, 0, 0, 0, 1, 0, 0 14 }; 15 16 mesh.SharedVertexData = new VertexData(); 17 mesh.SharedVertexData.vertexCount = 4; 18 var decl = mesh.SharedVertexData.vertexDeclaration; 19 var binding = mesh.SharedVertexData.vertexBufferBinding; 20 21 var offset = 0; 22 decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Position ); 23 offset += VertexElement.GetTypeSize( VertexElementType.Float3 ); 24 decl.AddElement( 0, offset, VertexElementType.Float3, VertexElementSemantic.Normal ); 25 offset += VertexElement.GetTypeSize( VertexElementType.Float3 ); 26 decl.AddElement( 0, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0 ); 27 offset += VertexElement.GetTypeSize( VertexElementType.Float2 ); 28 29 var vbuf = HardwareBufferManager.Instance.CreateVertexBuffer( decl, 4, BufferUsage.StaticWriteOnly ); 30 binding.SetBinding( 0, vbuf ); 31 32 vbuf.WriteData( 0, vbuf.Size, vertices, true ); 33 34 sub.useSharedVertices = true; 35 var ibuf = HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, 6, BufferUsage.StaticWriteOnly ); 36 37 var faces = new short[6] 38 { 39 0, 1, 2, 0, 2, 3 40 }; 41 sub.IndexData.indexBuffer = ibuf; 42 sub.IndexData.indexCount = 6; 43 sub.IndexData.indexStart = 0; 44 ibuf.WriteData( 0, ibuf.Size, faces, true ); 45 46 mesh.BoundingBox = new AxisAlignedBox( new Vector3( -100, -100, 0 ), new Vector3( 100, 100, 0 ) ); 47 mesh.BoundingSphereRadius = Utility.Sqrt( 100*100 + 100*100 ); 48 }
原文:http://www.cnblogs.com/zhouxin/p/3585325.html