Proper way of transforming 3D primitives in C#/XNA? -
i have block class contains array of 36 vertices, constructor builds block around origin (0, 0, 0), , method updating.
i'm using physics library manipulate blocks. assign body , collision skin block , update physics step every update. after every update matrix block's new position, orientation, etc.
now, can't wrap head around best way go applying matrix block , efficiently updating vertex buffer every frame.
this have @ moment, i'm positive there's better method...
class block { const float fullblocksize = 20f; const float halfblocksize = fullblocksize / 2; public vertexpositionnormaltexture [] vertices = new vertexpositionnormaltexture[36]; public vertexpositionnormaltexture [] currentvertices = new vertexpositionnormaltexture[36]; public dynamicvertexbuffer vbuffer; vector3 position; public block(vector3 blockposition) { position = blockposition * fullblocksize; /* * build 6 faces of block here. * block built around origin, * (0, 0, 0) being center of block. */ vbuffer = new dynamicvertexbuffer(game.device, vertexpositionnormaltexture.vertexdeclaration, vertices.length, bufferusage.writeonly); vbuffer.setdata(vertices); } public matrix worldmatrix { { return matrix.createtranslation(position); } } public void update() { currentvertices = (vertexpositionnormaltexture[])vertices.clone(); matrix currentworld = worldmatrix; for(int = 0; < currentvertices.length; ++i) { currentvertices[i].position = vector3.transform(currentvertices[i].position, currentworld); } vbuffer.setdata(currentvertices); } }
let shader it...one of parameters of shader worldmatrix...
to draw it:
effect.parameters["worldmatrix"].setvalue(world); effect.apply(0); if have problems drawing block... try create world transform matrix , test it...
effect.parameters["worldmatrix"].setvalue(matrix.createrotationz(angle)); effect.apply(0); if have several instances of block, can use instancing pass trasnform matrix in vertexbuffer shader...
here can find info instancing: http://blogs.msdn.com/b/shawnhar/archive/2010/06/17/drawinstancedprimitives-in-xna-game-studio-4-0.aspx
Comments
Post a Comment