Jan 12, 2017

Development Snapshot 16

The main issue I ran into this snapshot was inflexibility of old code. I ended up having to revisit alot of code that is over 3 years old. That code was unable to be extended without introducing a horrific mess, so I took the high road and invested some time refactoring those ugly parts. This cost me some time, but the end result was worth it.

The majority of work this time was in the graphics area. I overhauled the look of the UI somewhat, replacing the WindowsXP-like windows with something a little bit more modern (in my opinion). I also rewrote a large section of my render loop in an effort to reduce frequent state changes. Instead of just blasting out draw calls, switching shaders and such on the fly, I now enqueue render jobs that are sorted to minimise changes where possible.

Skinning System


This was the major piece of work this snapshot. The first task was creating a rigged model in Blender.  I took my existing quick and dirty model and threw together a simple bone structure (after I watched a few blender tutorials). This fell together quicker than I anticipated, which perfectly set me up for bitter disappointment when I failed at the next few hurdles....

Next was modifying my collada importer to support bones. My previous implementation was done strangely to say the least. Looking at the code again after several years, I drastically rewrote it to be more flexible, and added the bone/joint data parsing as well. I lost a bit of time debugging why all the transform matrices had no translation component before I finally realised that collada matrices are Column-Major and I was assuming Row-Major. Once I got that fixed, the matrices looked right.

Then I had to rewrite my model class to handle the fact that there will be joints pushing and pulling the vertices all over the place, instead of being static. My previous incarnation of my model class was a hastily slapped together mess that came out of an attempt to get the code working again after one of my aggressive refactoring efforts over the years. I treated it as such and gutted it, also making it work nicely with my new render job system.

I then added in the vertex blending code. To aid debugging other parts of the skinning chain, I decided to implement software tranformation of the vertices, rather than use the vertex shader at this stage. (It's much easier to inspect values if they are in the CPU vs the GPU). The only bit of excitement here was initially my bone indices were off by 1, leading to the following image:

That sweet looking cape is actually his mangled torso.

After fixing that I decided that visualising the bones and joints would make it much easier to debug the vertex blending. I got really messed up by this. No matter what I did, the bones wouldn't render right when moved. I got the joints rendering correctly in their bind pose, but anything else and it would stretch at a weird angle. It took me several days of hair pulling, but the problem turned out to be the order of some matrix multiplications.

I knew I needed to multiply the joint's bind-pose Transform Matrix (BPTM) by the desired transform (Y), (in this case, a simple rotation), but whatever order I used, eg:  Y x BPTM or BPTM x Y, the end result was not right. Then it suddenly occurred to me that the bind-pose transform matrix is actually composed of a rotation and translation, and there was a 3rd place I hadn't tried putting the Transform multiply... in between! So I decomposed BPTM into R x T and put Y in the middle : R x Y x T, and holy crap... Success!

At this point I realised the next step of creating an animation system was going to have to wait until the next snapshot, which was disappointing.  On the plus side, the model's vertices are being transformed as per the skinning equation, and is rendering correctly, which I'm pretty pleased with.


Waypoints


To close out the snapshot I also overhauled the look of the player's waypoints. I'm not completely happy with the look yet, but its a great improvement over the temp graphics that I've had in there for several years now. It also forced me break down the paths into runs of straights and turns, which will be required for smooth walking animations later on.

Original style waypoints


New style waypoints


Next Snapshot


For Snapshot 17, the animation system is high on the list. There's quite a few sub-tasks to tackle though, so I'm not sure if I'll get through them all. The toughest stuff should be behind me, but I'll see what happens.

No comments:

Post a Comment