3D Model Converter & Quaternion Rotations
Note: These Game Engine blogs are a series of write-ups I did when I was in school for my master’s degree. I took a series of three courses run by Ed Keenan where we built a game engine from scratch in C++.
3D Model Converter
Cubes, pyramids, and hourglasses are underwhelming no matter what texture or cool lighting you put onto them. So let’s get into some interesting, more complex models with lots of polygons. To do that, we’d have to use a modeling program. To use that file in our engine, we’d have to convert that file. So which modeling program should we choose write a converter for? The answer is all of them in a single converter.
FBX is a third-party company that has plugins for all of the major modeling software. They also have an SDK to read their file format through code. So what we’ll do is use the SDK to read their file format, put our own header on top (with important file information), and then write the file back to the disk in our own format. The benefit of this is because we can go from FBX to our engine. And FBX can go to or from most modeling programs.
Quaternion Rotations
Okay this about to get math heavy, but I have some sweet GIFs for you. A lot of rotations are done using matrices, meaning they are done using the Euler method. There’s a problem with this in 3D because you can get something called Gimbal Lock.
Watch the inner blue ring of the above GIF. You can see it switch directions as it lines up with the green ring. This is Gimbal Lock. It’s not the proper behavior of rotating in 3 dimensions.
When the axis line up, you lose a dimension of rotation. Rotating on the blue and the green will do the same thing, this causes the Gimbal Lock. How we fix this behavior is by not using Euler Rotations and using Quaternions instead. Quaternions are…complex. But essentially we use imaginary numbers to go up to 4 dimensions and then resolve it back down to 3 dimensions to use the rotation.
It’s confusing. There’s a lot of math derivations and trig. If you’re interested in the explanation of Quaternions, check out the Wikipedia. I couldn’t derive them for you — I’m not a mathematician. But I know why they’re useful and how to use them (add, multiply, invert, convert, etc). And if you look at that math, you’ll understand why that’s enough for me!