Tuesday, May 10, 2011

RH to LH, texture coord oddities, and instancing

MS3D model geometry is now loaded correctly.  One problem I did encounter was that the models are stored in a right-handed coordinate system, but the game uses a left-handed coordinate system.  This means that the x coordinate is pointing in the wrong direction, basically.  I initially tried to solve this by inverting the x coordinate of all vertices in the model.  I was a bit surprised to find that the model appeared to have turned itself inside-out!  After a bit of discussion in the #gamedev IRC channel, I figured out what the problem was:  The triangles were wound in the wrong direction.  This was fixed easily enough by reversing the order of the vertices of each triangle, and the model was no longer inside-out.

While adding in texture support, I discovered some rather odd things.  First off, the U and V texture coordinates are named "s" and "t".  After doing some poking around, I think this is an OpenGL thing.  Trying to find the texture coordinates was a bit confusing at first though.

The second odd thing is that texture coordinates for each vertex are stored per triangle, not per vertex.  This means that a single vertex can have multiple texture coordinates.  I suppose this makes sense, since you may want two triangles which share a vertex to have completely unrelated textures.  It did goof up my usual way of doing things though, and I had to scrap my indexed drawing code since each vertex of each triangle needs to be stored.  There may be a way for me to split up the geometry data and the material data using stream frequencies, but further investigation along those lines will have to wait for the moment.

Texturing isn't completely working yet.  Coordinates are stored fine, but the same texture is used for every model.  Also, no other material properties are currently in use.  I will probably get these things working correctly after skinning is in.

The method of instancing which I am using involves separating the model data and per-instance data into different vertex buffers.  The model data is stuff like vertex position, texture coordinates, etc. and per-instance data is the world transformation matrix and anything else which differentiates instances of the same model.  Stream frequencies are then setup so that one "vertex" of instance data is applied to the entire model.

This was my first time working with multiple streams, so I suppose it's not surprising that I made a newbie blunder.  After the instancing code was all finished, the model wouldn't appear.  I spent some time trying to figure out what went wrong, but everything checked out...  I eventually found the culprit though:  When creating the vertex declaration, I didn't start the offset over at 0 when I started specifying stream 1 elements.  This caused all of the "vertices" in stream 1 to have a chunk of padding at their beginning equal in size to the stream 0 vertex size.

No comments:

Post a Comment