On Writing, On Thinking

It’s been some time since I last posted on here, which is surprising since for the last few months I’ve been working entirely on Vitae.

I’ve found that when working for myself, it’s much harder to justify spending time writing, against time spent writing code. I think this is wrong, and consciously I tell myself to spend more time writing, but when I’m in front of the computer it’s much more tempting to fire up Vim[1] and work on the next feature.

Recently, I’ve noticed that I seem to have two distinct modes of working – a high level, and a low level.

When I am in ‘low level’, I put my head down and code. I can get a lot done, but only if what needs doing is very clearly specified – either explicitly in my task list, or implicitly by a gap in the code. I can spend hours like this being very productive, but I’m not going to produce anything amazing.

When I am in ‘high level’, I can think about problems in a much more abstract mode. I do most of my ‘high level’ work away from the computer. When I’m walking somewhere, it’s a great time to plan how certain modules are going to work, or fit together, or generalise a nice new algorithm for something. This is where most of the ‘clever’ work gets done – that is to say, the work that is not immediately obvious, which means it is the work that potentially can differentiate me from my competitors.

Sometimes I find myself stuck in the wrong mode. In particularly, if I am stuck in ‘high level’ but really need to finish some boring subsystem, then I find it incredibly hard to switch and hard to concentrate on the work. My productivity plummets.

Also, I find it hard to hold both mental models at the same time – this can be a pain when I can’t seem to bridge the gap. I can think about the problem top-down, or bottom-up, but there’s a chasm in the middle I have trouble bridging. I think that this results in a situation where I can have a lot of good ideas, but the code I end up producing doesn’t adequately reflect that.

I think this issue is one of the things holding me back from moving to a more functional style of programming. It’s very easy for me to see how some algorithms can be expressed beautifully by a few simple functions and mappings, but when it comes to implementing it there’s trouble in actually realizing that vision.

I hope that by writing more often, I will be better able to collate my thoughts, and better able to notice my thought patterns and therefore improve them. ‘You improve what you measure’ goes the saying. I think writing is one of the best ways to measure thinking.

—-

[1] Technically this post was also written in Vim, but you get the point.

Useful Game Development Resources

Only a short post this time, just a selection of useful links I’ve come across recently for Gamedev resources.

http://altdevblogaday.com/ – AltDevBlogADay – a selection of brilliant blog posts from different developers each day, covering every aspect of game development from programming to design to business, across all platforms and sectors.

http://bitsquid.blogspot.com/ – BitSquid Blog – this blog is written by ex-GRIN employees now writing their own engine for new projects. A good insight into engine development, and covers some clever techniques and good engineering practices.

id_aa_carmack @ twitter – John Carmack’s twitter, this man needs no introduction, and whilst I’ve generally found twitter to be unsuited for distributing development tips, Carmack manages to keep his knowledge succinct enough to at least give some interesting suggestions that will definitely give you some inspiration to go searching.

The Joy of Hex

My latest short project is a programmable texture filtering program – a piece of software that will let me write short scripts to do image processing such as edge detection, blurring, color balancing and other mathematical operations. I started writing it two days ago on the way home from work, and the first stage is complete today.

I decided to write the program, currently called Viridian, because firstly I needed it (for producing normal maps and other textures for Vitae), and secondly because it seemed like a short, fun program to write. The idea is not mine; I was largely inspired by a similar program written by a co-worker at FRD – but the implementation is all mine.

Leaving aside the issues of parsing and grammars that would be necessary for a scriptable version, my first step was to build a program that could read in an image, run a hardcoded C transformation on it, and then write it out as a valid file format. I chose to use TrueVision TARGA (.tga) for my first format, as it is a remarkably simple format. I began with using the RGB (24-bit) form, where it uses 8 bits for each channel (and no alpha channel).

In order to load the file and correctly extract the data into the C program, I used the wikipedia reference page for the TGA spec. Creating a struct of the right order that I could alias to the header worked well enough, and then it was simply a matter to adjust for the variable length fields and grab a pointer to the image data itself.

typedef struct tga_header_s {
     u8 id_length;
     u8 color_map;
     u8 image_type;
     u8 color_map_spec[5]; // This can’t be declared as a struct due to aliasing
     u16 origin_x;
     u16 origin_y;
     u16 width;
     u16 height;
     u8 pixel_depth;
     u8 image_desc;
} tga_header;

My first implementation had a struct within a struct for the colormap specification (TGAs can use an indexed palette), but this broke the code as the compiler added alignment padding to keep the 5-byte struct correctly aligned[1].

After that it was easy enough to create the rest of the code to reconstitute a new image header, append the image data, and write it out to a destination file. Once all that framework is in place, writing the code to actually apply a basic image transform was trivial.

I did have a couple of hiccups along the way though, and those were largely smoothed over thanks to an old friend – the hex editor. I was coding on the train at the time with no internet connection, and no saved offline version of the TGA reference. In order to make sure I was loading the right fields from the right bytes, I simply opened up one of my source example TGAs, and checked to see if the insides made sense.

Hex editing.

With modern techniques and libraries, I think Hex editing – or at least reading Hex – is becoming somewhat of a lost art, but it’s a very useful ability. Being comfortable with taking a look at exactly what is under the hood to confirm your assumptions, and also to be able test output before writing any code, is often extemely helpful.

I simply used the Hex mode in Vim rather than resorting to a specialised editor, which was completely up to the task as I was purely reading.

Taking a look at the top line, this simply corresponds to the main parts of the header.

u8 id_length

0000 0200 0000 0000 0000 0000 5505 0004

(length 0, so no ID)

u8 color_map

0000 0200 0000 0000 0000 0000 5505 0004

(0 = no color map))

u8 image_Type

0000 0200 0000 0000 0000 0000 5505 0004

(2 = uncompressed, true-color)

u8 color_map_spec[5]

0000 0200 0000 0000 0000 0000 5505 0004

(There is no color map, per byte #2)

u16 origin_x
u16 origin_y

0000 0200 0000 00000000 0000 5505 0004

(origin starts at 0, 0, the default)

u16 width
u16 height

0000 0200 0000 0000 0000 0000 5505 0004

Now hear is something slightly odd – These last 4 bytes of the line should be the width and height, but the values here don’t seem to work out. 0x5505 is 0d21765, and I’m fairly certain the image wasn’t 21765 * 4. Opening it up in the gimp confirmed it was 1365 * 1024, a much more reasonable size, but then what was going on here?

Experienced programmers will probably get this one pretty quickly. Luckily reading the TGA spec earlier had already reminded me, and so it came back to me quickly even though it’s not something I’ve personally had to deal with before – endianness.

The endianness of data refers to the ordering of bytes[2], and whether they should be stored with the biggest first (big-endian), or the littlest (little-endian). In the English speaking world we tend to think in Big-endian, as that is how we describe our numbers[3] (”two-hundered and forty seven”, not “seven, forty and two-hundred”). Many computer systems, including the x86 platform that powers billions of PCs worldwide, use little-endian ordering, which means that the two bytes 55 05 actually correspond to the hex number 05 55, and 00 04 is 04 00. As expected, 0x0555 = 0d1365, and 0x0400 = 0d1024.

With that mystery sorted, it all makes sense, and being able to tweak my code until I get exactly what I expect in the file output takes only a matter of minutes. Could I have done it without inspecting the Hex? Yes, of course, but it was easier to have that resource with me, and when dealing with less-well documented file formats (or reverse-engineering formats, as I once did with Blizzard’s Warcraft 3 map scenery file in order to build a tileset converter), it is invaluable.

As with my other projects, Viridian is open-source and can be found on github.

—-

[1] C compilers will pad structs so that multi-byte fields are correctly aligned for the target platform. This often means they are even-byte aligned, and also often aligned with their size, so a 4-byte field will be at an address divisible by 4. This is due to most hardware having poor or no ability to read incorrectly aligned values.

[2] Actually endianness can refer to the ordering of bits in a byte, but pretty much every sane person now has agreed on big-endian for that, ie. 128 64 32 16 8 4 2 1, and not the reverse.

[3] Although confusingly, we use little-endian format for teens – ie. fourteen is ‘four-ten’ not ‘ten-four’, yet we say ‘twenty-four’ not ‘four-twenty’. Yet another arbitrary confusion in our language!

Website update

I’ve been making some changes to the site recently, and re-implementing some of the wordpress theme using CSS 3 features, in particular colour gradients. As a result, the site currently does not render exactly correctly in browsers other than Chrome. I hope to get this fixed as soon as possible, in the meantime you’ll likely notice a couple of missing areas if you are using another browser such as Opera or Firefox.

In other news, I’ve finally got my hands on a Nexus One, hopefully I can find time to start learning the Android NDK soon.

Manta now available on GitHub

Yes, Manta is now available as open source software hosted on GitHub!

I’d been considering this for a while, and finally decided to make the move in order to secure off-site hosting in case of a huge issue with my PC, and also to share my work with anyone who is interested in using it.

UPDATE I’ve now officially released the Manta source under the New BSD License; a copy of the license agreement is now stored in the GitHub repository and should be included with any version of the code released.

For more info, see the code page.

Glorious adventures in Game Programming

And so is born yet another blog in the wilderness, attempting to sail the windy seas of curiosity and spark at least some kind of intelligent thought.

I’ve been thinking about writing a blog for a while now, but the assumed arrogance of it always put me off – why write something that you think is important when the chance of anyone actually reading it is, to put it lightly, slim? It is only recently that I have come to think that I was missing the point – which is not that anyone is reading the blog, but that you are writing the blog. Just as I will talk to myself to wring out some sense of understanding from a problem, so writing is itself a worthwhile trick to unwrap difficult thoughts, and my lack of it to this point has contributed to my lack of skill – the only way to get good at writing is to write more, and hence here I am in an attempt to spark my brain and focus my thoughts. So, I write this not under the expectation that anyone will necessarily read it, but that I will write it and that alone makes it worth the effort.

As well as exploring tangled ideas I might have, I’ll also be using this blog to report on my own endeavours in the gaming and programming spaces – firstly to motivate myself and secondly to show the world if I manage to create anything compelling. Right now that means my (eventually) real-time raytracer, Manta, so I’ll spend a bit of time introducing it.

What is Manta?

Manta is a 3d graphics engine that uses raytracing (the calculation of the intersection of individual light rays to determine appearance) to create a detailed, beautiful representation of a scene, in real-time. Or rather, that is what it will eventually be – at present it runs quite slowly and does not yet implement many of the techniques crucial to good 3d rendering – texturing, shadows, normal-mapping, reflections etc. What it is though, is a good base from which to build. The core ray tracing algorithm seems solid, the kd-Tree traversal method I now use is almost complete, and I’m ready to start multi-threading and optimising until I can reach my milestone of 30fps.

Why Manta?

Some might (and have) asked me why I am building a ray tracer. As I see it, this question could be taken in two steps; firstly ‘Why build a 3d renderer?’, and secondly ‘Why use ray tracing and not rasterizing?’

The answer to the first is a simple one – because I want to. It is something that interests me, both from a technical and creative side, and I’ve always wanted to build a professional-level 3d engine (make no bones about it, I intend for Manta to have a quality close to those at the top of the game, or die trying. I have no desire to just create a basic, amateur renderer, a little bauble of code only interesting as a curiosity and with no comparable merit on which to stand). It is also because I want to learn, and to do that I need to push myself, to attempt the very things that I wish to be able to do. Just as I am writing this blog to improve my writing, so I am coding a renderer to improve my coding. During my day job, the tight deadlines, large team sizes and huge, stagnant code bases limit my ability to explore. I need to be coding in my spare time to test out new algorithms, coding methods and languages whilst I am still unfamiliar with them.

As to the second question, it comes down to aspiration and competition. I am but one developer working in a field of established studios and large corporations, many of which throw millions of dollars and teams of seasoned professionals at the graphics problem, creating polished megalithic engines which are licensed out for vast sums and set the bar for rendering in games. It might seem naive to have any expectation of competing with these juggernauts, and assuredly I’m not going to have studios knocking on my door asking for licenses any time soon, but on some base level I need to have the possibility of doing something no-one else has, of pushing further in a particular direction, of carving out a niche such that people will look at Manta and it will bring something new to the table, however small. Consider it that I am harnessing my own arrogance and ambition to motivate myself through learning.

This leads directly to ray tracing – most current engines use a process called Rasterization, one which is well-understood, and despite the many improvements that are constantly being added, I feel is in many ways a solved problem. Teams at Epic, Crytek and elsewhere are all getting every last drop of beauty they can from these algorithms, and the results are rightly impressive. Ray tracing, whilst well understood and widely used for off-line (read: time uncritical) rendering, is still a dark horse when it comes to real-time, which makes it more interesting for me. It also gives me a good base to gain more experience of optimization, something which I find fascinating but don’t get to do a lot of at work. The basic raytracing algorithm is trivial, and people have written ray tracers in about 20 lines of good code, but the difference between a naive ray tracer and one that can efficiently render a rapidly changing scene is huge. This means that creating the initial ray tracer was very simple, but streamlining it, polishing it, and generally learning where and when I can cut corners means I can build on that simple base to eventually create something that is innovative, efficient and different.

To Code or Not to Code

Here is a code test, using the code div so that I will hopefully get some decent formatting for source code:

for (int i = 0; i < max; i++)
{
// Some comment;
int j = i * sqrt(4);
printf("a number: %d", j);
}

How'd that look?