nmdist-log

Animated models and weapon rendering

This week I further refined the movement code I wrote about last time, and I’ve come to a point where I’m happy with it for now. The next thing I turned to was support for animated models in the nmdist engine. I’ve written a system to import, render, and animate GLTF models before for a previous engine, so this was mainly a porting exercise. But it turns out there were some bugs to fix…

Previously, I had implemented a system to parse MDL files and export them as GLTF files. Most models looked fine, but a few of them were totally broken:

After writing some tools to dump and analyze all the models, I eventually realized that all the broken models shared something in common: they contained more than one root in their bone hierarchies. My import code didn’t properly handle this case, and I ended up with bones that had relationships to eachother when they shouldn’t. I fixed it by creating a new virtual root, which all roots were children of. After that it worked great:

From there I ported my skinning and rendering system from a previous project and had everything up and running fairly easily. I also introduced a new rendering mode for the player’s hands and weapons. However, I quickly noticed a problem with many of the weapons:

An unused enemy from Half-Life and the first person view of a revolver. The revolver is black when it should be reflective.

I had not implemented reflective materials! After some digging, I found an old page made by Half-Life modders that contained the information I was looking for. A technique referred to as “matcap” was used to map the normal of a surface to spherical coordinates on a texture. With the right texture, a model can look reflective especially as either the camera moves or the model moves. This is because the texture coordinates will change since they’re derived from the camera direction and the surface normal. I put together something quick and dirty together and it looks alright!

As a final bonus, here’s a picture of an unreleased Half-Life weapon I’m sure you’ve seen floating around before.

An unused weapon from Half-Life, a purple and red toad looking creature being held by the player.

I’m at a point now where I can’t procrastinate the entity refactor any longer. It’s now getting in the way of hooking animated models up to entities (e.g. Barney, scientists, headcrabs, etc). Time to untangle that knot.