Code Story

Monday, October 02, 2006

An Eye (Vi) Short of a Graphics Engine

Learning 3D transformations for game development can be a painful exercise. You will need to know 3D matrix and vector operations including dot and cross products, vector addition, subtraction, scaling and of course transformations, to name the basics. There is a lot to learn, lots of tricks and pitfalls.

Where is the pain in that, I hear you ask. Isn’t this like learning anything else? Why is it painful? I’ll explain…

I have designed and written quite a few 3D graphics engines and APIs in my life, and I still do. In addition to writing the stuff I had to provide it to other development teams and further explain what this 3D business is all about. I’ll take one case of a development house in the south of England, who were writing a 3D strategy game which needed 3D capabilities and they didn’t have the necessary expertise, even though they had over £1M in investment. It goes to show that it is not what you know, but who you know.

On my first visit to their nice empty offices (5 people present in a place that could house 30) that overlooked a beautiful stream and lots of green things (trees), quite a contrast from our offices in London, I installed the latest graphics terrain engine I was working on on the lead programmer’s computer. The engine named iVi, which was partly Roman for 4: IV (ie version 4 of the engine) then I added an ‘i’ for Interactive, clever or what? Therefore, I had the very imaginative and media savvy name iVi. You’ll be hard pressed to get a name better than that. Don’t throw in my face B-Render or Render(Under)Ware or RenderMorphics or The Reality Engine or even Demis Hassabis’ Totality Engine. iVi is the final word in 3D Graphics Engine naming and that’s that.

Sorry I got side-tracked there; back to the story, after the installation, including the source code of course, the lead-programmer asked me to explain to him 3D viewing transformations. Wait, you mean, what is your average 3D viewing transformation for your average terrain system? Oh easy, easy peasy, right down my particular field of expertise that is. You wouldn't prefer a space question?

Well I could have explained the transformations as a set of three coordinate systems, the world coordinate system, the system that represents the entire 3D world, the objects/models coordinate system, which is the local representation of the objects in the world, and last but not least (or could be least I don’t know), the screen coordinate system.

Initially we transform our objects in their local space then concatenate the transformations with those of the world’s so the objects appear in their right position, then we apply the viewing transformations. The viewing transformations take into account what the viewer is looking at, and his/her orientation. We transform our world coordinates with the viewer’s matrix (this is basically multiplying two matrices or two quaternions or whatever’s your poison, together) and finally we apply perspective projection on the resulting coordinates to translate them from 3D coordinates to 2D (screen) coordinates, this is when our final image can be rendered on the flat screen giving the appearance that the whole thing is actually 3D. There!

Sometimes, things can be even simpler. For example, some implementations don’t require the viewer to change their orientation; they will always be looking down the world at a fixed angle. You must’ve seen many products like that, particularly strategy titles. In this case, we can skip a lot of operations, saving along the way, a few multiplies here and there. Well a few thousand actually.

In the case of our developer, their game had what we call a fixed viewer; I was explaining all this to the lead programmer who was sitting to my right as I was facing the computer ahead. I had my arm fully extended in front of me representing a vector from the eye of the viewer to the centre of projection (the screen), I was explaining that when the view is fixed the eye doesn’t move (my head stays fixed towards the screen), it’s really the vector (my arm) that rotates inversely to the world orientation. In this case, inversely meant that I rotated, with speed and force, my arm (the eye vector), to the right, without moving my head, so I didn’t realise when I heard a scream that my eye vector (arm) had landed straight onto the face of the lead programmer. I looked and he had his face in his hands in agony.

WOOOooops! I should’ve used pen and paper to explain, that would have been better! Not knowing exactly how to respond to this turn of events, I simply turned to the programmer and told him while he was still holding his eye and nose, “You won’t forget the fixed-viewing transformations now will you.” I left the offices with haste shortly after that. Although I was invited to the offices many times after that and everybody was very pleasant but I could see a certain look on the lead programmer’s face, and he always stood a few feet distance to my left. I don’t think he ever forgave me!

A while later, there was a review in Edge Magazine about the same game that was using my graphics engine iV(awesome)i. The head of the development studio was interviewed and he said: “The best thing about our game is the 3D Graphics Engine…” Cool, that’s me, how nice of them. But wait, he continued, “…which has been written by our lead programmer.” Oh! I see. No mention of the man who actually spent years writing it then! Come to think of it, I don’t blame them, since the lead programmer probably still has a black eye from my fixed viewing lesson and has endured physical pain with evidence to prove what he went through using iVi. But as far as I am concerned, he was still one black eye (Vi) short of a graphics engine. If I see him again, I’ll give him another black eye for his blatant lie, only then can he claim that he wrote the engine, he would have truly earned it.

Oh I forgot, I did get a "Special thanks to Sam kerbeck" in the game's documentation, which is still a lot more than other teams I could mention.