Although my rainbow cube was pretty spiffy looking, it didn’t move. I don’t know about you, but most modern games I’ve scene have some kind of animation, so I figured the next step would be to try and animate the cube, and maybe the camera as well.
Some concerns I had were that I wasn’t sure what the best method was of separating concerns between my game and the rendering thereof. I was worried that I should be abstracting away the underlying rendering engine (OpenGL vs Direct3D, for instance) and hiding it from my game as much as possible. The problem is of course that abstracting away a framework (let alone multiple frameworks) is hard to do when you don’t have an intimate knowledge of the frameworks you’re hiding, and the parallels between, and nuances of each. The advice I got back from a gamedev.net forum post I made said as much, as did another useful post I found afterwards. And really, the Windows market is huge. My first few games are hardly going to suffer from not targeting Mac or Linux users. So I will of course separate the concerns of my game as I should anyway, but I’ll stick to SharpDX and won’t worry too much if my underlying structures reference SharpDX and Direct3D11 specifically.
Now, in the code for my 3D cube, I’d begun to explore some early refactoring and what that might look like. I’d tidied up my rendering code a bit, within the limits of what I understood about what I was doing, and I had separated my mesh code out into a separate class, but that was about it. If I was going to animate the cube, and potentially the camera, and maybe even add some more cubes to the scene, I needed to take this a few steps further though, which led me to create a Scene class, which contains a list Model objects, each of which contains a Mesh object and some positional data. I also created a class to contain mapping between my scene data and the Direct3D buffers that my models map to, in order to keep the scene data pure and allow it to be loaded and unloaded in the future if necessary, and perhaps loaded and exported to and from an external source. In order to create mapping abstraction, I needed to learn a bit more about how to deal with inserting different objects into the graphics pipeline, especially when there might be different shaders at play depending on the object being rendered.
Though MSDN can be helpful, sometimes it leaves me with more questions than answers, so, based on recommendations I saw elsewhere, I picked up my next book, Introduction to 3D Game Programming with DirectX 11, this time for Kindle, so I could get it straight away, and can read it in the Amazon Kindle Cloud Reader in Chrome.
It’s a really good book and from what I’ve read so far, it’s going to be enormously helpful, and in fact it already helped me finish the code for the link at the end of this blog post. The author, Frank Luna, has been writing DirectX books for years, so he really knows his stuff, and in addition, having yet another reference which covers the groundwork of vectors and matrices is really good because it’s helping me cement that knowledge in further.
Anyway, as per this post’s title, this exercise was all about abstraction and my first foray into animation. The code is significantly tidied up since the previous version, and I’m starting to feel like the code I have now is a crude, early stage basis for a simple 3D engine. Undoubtedly, ten blog posts from now, the code will look completely different, but I’ve refactored the hell out of it, and it feels a lot tidier now, and the parts handling the actual rendering and Direct3D interfacing have now been separated with the parts specifically related to the game and its geometry.
Regarding the “animation”, I haven’t yet quite got enough matrix math down to actually animate the cube, as such, but I did make the camera move around a bit, mainly because I wanted to see if all of the sides of my cube were rendering correctly. There’s an anomoly in my code I haven’t figured out yet where I’m having to call DrawIndexed with 3 times the number of triangles actually present, and I’m not really sure why that is, because supposedly I’m supposed to be telling the drawing context how many triangles to draw. Anyway, the result is below, in a very poorly-filmed YouTube video, captured by pointing my iPhone at the screen, and then emailed to myself. I intend to pick up a copy of DXtory for future video capturing, which was recommended in a YouTube video I watched by someone who has apparently tried all the main packages for capturing game footage and swears by this one.
Here’s a link to the source code.