Monday, July 8, 2013

We should teach the beauty of Mathematics

Have a look at this link for Michael Gove's vision for mathematics education

In school, we learned how to multiply fractions. I can distinctly remember turning to my friend and saying 'Well, that's it then. We've learned maths. We know addition, subtraction, division, multiplication and fractions - what else is there?'. To my shame, I genuinely thought that I'd exhausted the subject. How could I be so incurious and arrogant to think that I knew everything about maths? Thirty years of learning later, I've mostly worked out how little I know. I'm not even sure the map of my ignorance is anywhere near complete.

   One thing I have learned is that Maths is far from the dreary subject we had to put with in school - it's beautiful, complex, universal, satisfying and the basis of (or at least useful to) just about every interesting vocation and to our very understanding of the universe. If we ever meet another space faring species, they'd be unlikely to speak any natural human language. I think that we would be able to communicate using mathematics, however: You'd be unlikely to get in to space without it.

   So how is it that our education secretary, Michael Gove, is harking back to look-up-tables and learn-by-rote. Times tables are heuristics, a small set of short cuts that do not scale well. Try asking a kid raised on times tables what 202 x 32 is - they can't, unless they have another method. As a kid, I worked out that you could split any complex multiplication in to simpler ones, honing in on the target number quite quickly. At the very least, you can get the magnitude quickly, even if you're off by a few smaller digits.

   I don't want to state the obvious but you only really need to know short cuts for multiplying by 2, 3 and 5, addition and that adding a 0 to the end of a number multiplies by 10.  You can composite answers using factoring and far less rote memorization - learning how to play with expressing a question in different ways in fundamental to learning to use mathematics creatively so that's a more important lesson than a head full of dumb tables.

202 * 32 = (200 * 32) + (2 * 32) 
200 * 32 = 32 * 100 * 2 = 3200 * 2 = 6400
2 * 32   64 
202 * 32 6400 + 64 = 6464 

Or a more 'time table' example:

7 * 9 = (7 * 10) - 7 = 70 - 7 = 63

 We teach kids times tables up to 12 and then stop - it's arbitrary and largely useless for anything than counting change whilst shopping. I'd rather we gave our kids the tools to aspire to something more fulfilling and worthwhile than merely the efficient purchase of consumer goods.

   There's nothing wrong with a look-up-table for a given task: Most programmers know powers of 2 off by heart (2, 4, 8, 16, 32 etc.) because that sequence is useful to our profession but more importantly, we appreciate why. The method and rational behind it is much more important than the mindless repetition of numbers.

landscape entirely generated by mathematics (c) Inigo Quilez
  My early experience of mathematics in school, as with many kids, was dull.  I did have a brief introduction to SMP, which was the most positive school mathematics experience I had and the polar opposite of Gove's Victorian rote learning. However, they they phased that out rather quickly.
Everything changed when I started teaching myself programming, particularly graphics programming and discovered how useful, how beautiful maths could be.

  If you have a fast computer and a modern web browser, check out ShaderToy. This website contains beautiful animations entirely generated by mathematics - there's no art or modelling software used to create these other than a text editor and the author's vision expressed through mathematics and GLSL shader code, which is all available for you to tinker with directly on the website.
  That's what schools should show kids - just look at this, this is what maths and programming can create: Stunning, beautiful flights of the imagination expressed in a few pages of code.

  We should be showing kids that their computer games are made from maths, how that third person camera using quaternions to interpolate orientation, how the light reflecting from that burnished metal is calculated using vectors and matrices and fresnel equations, how the A.I. is weighing up probabilities before deciding how to counter the player's move. It's all maths - times tables are a dull footnote. As interesting to maths as a pencil sharpener is to art. 

  And yet, they seem central to Gove's vision for the next generation's education in the UK. This is not the vector along which we should be heading. Gove has also been accused of side-lining arts and music in favour of his stunted vision of science - again, that is wrong. Teach the art and beauty in Maths and Science, teach the Science in art too - as ShaderToy shows, they're often the same thing.

  I have to conclude that either our current administration is either like my 9 year old self - ignorant to the true depth and beauty of mathematics as demonstrated by the fact that they place inordinate weight on archaic teaching methods or else they know exactly what they're doing and want to create a population as ignorant as the child I once was.

Saturday, June 15, 2013

When I'm trying to code something I typically start with one problem and end up with fifteen, one of which will involve regular expressions. Today, I wanted to revamp my old Clayworks website as it's a stuck-in-1999 embarrassment (not quite geocities bad but bad enough).
  I'm using sublime text for my HTML editing but I found that I needed to re-format my old code and sublime-text doesn't have that functionality built in.
  Sublime-text does have rather nice python integration for easy plugin development so I did a little search, found this chap's website ( and adapted his code, which makes use of xmllint (a unix tool but here's a windows version:
  My version doesn't need the text to be selected (if text is selected, only that part will be beautified) and will shows a pop-up error message and also takes you to the error line.

Anyhow, here's the code:

import sublime, sublime_plugin, subprocess

def find_lint_error_line(errString):    
  startpos = errString.find("-:")
  endpos = -1
  err_line = -1;
  if startpos != -1:
    startpos += 2
    endpos = errString.find(":", startpos)
    if endpos != -1:
      numstr = errString[startpos:endpos]        
      if numstr.isdigit():
        err_line = int(numstr)        
  return err_line
class TidyXmlLintCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    command = "XMLLINT_INDENT='\t' xmllint --format --encode utf-8 -"
    self.view.set_status('self', "")
    #xmllint.view.set_status('Hey there Tim', "Hey hey hey")
    # help from
    xmlRegion = sublime.Region(0, self.view.size())
    p = subprocess.Popen(command, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)

    sel_regions = self.view.sel()
    selection = sel_regions[0]
    sel_region_count = len(selection)
    if sel_region_count == 0:      
      selection = xmlRegion
    result, err = p.communicate(self.view.substr(selection).encode('utf-8'))    
    if err != "":          
      self.view.set_status('xmllint', "xmllint: "+err)
      error_line = find_lint_error_line(err)
      #lines[] = parse("line {}", err)
      if error_line != -1:            
        pt = self.view.text_point(error_line, 0)

        #self.view.set_viewport_position((0, error_line * 16), True)      
        sublime.message_dialog('could not find error line')

      sublime.message_dialog('xmllint, error at line: '+ str(error_line) + " \n" + err )

      self.view.replace(edit, self.view.sel()[0], result.decode('utf-8'))

  def clear(self):

And if you're wondering how I did that nice syntax colouring on the python code, check this out: SyntaxHighlighter

Wednesday, April 24, 2013

Further adventures in android NDK

Following some good advice on forums, I've gone and got an NVidia Tegra based tablet (Nexus 7). The reason for doing this was to get access to their android tools, one-stop-shop tools for android development. It'd be nice to say that the installation was entirely painless but, unfortunately, that wasn't quite the case. Partly, this was due to our internet connection dying which, due to this being a massive ~2 GB download, wasn't very nice. Eventually, I did get it to download but there were still some troubles: The kit includes a nice visual studio integration (which is great, that means I have more options) along-side a tailored release of Eclipse.  When trying out their 'hello-world' example, I got various errors that would change each time I downloaded the NDK (which I had to do several times).
  Seeing as my co-worker managed to download and install it without problems last Friday, I think what may have happened (although I have no direct proof of this, take with a pinch of salt) is that the server has been updated in between then and now and therefore, I received a corrupted download. For one thing, his installation includes this directory:  c:\NVPACK\NVNDK , which mine does not. That directory contains another copy of the android SDK and the first time I installed the SDK, the Visual Studio template 'hello world' example project referred to that directory.
  Today, when I cleaned, reinstalled and recreated the template sample project again. This time, it didn't refer to the NVNDK directory but still referred to a tool-chain that didn't exist (4.7.2). Fortunately, you can select a new tool-chain (visual-studio10->project-properties->android directories->Toolchain version) and it all just works. And debugs!
  The next problem I had was on my Nexus 7 - it wan't appearing on my 'device' menu in visual studio or in eclipse. On my old Samsung phone, you had to enable developer mode in the settings but I couldn't find the option on the Nexus 7. After a bit of forum hunting, I found that you have to click seven times on (on the tablet) 'settings->about tablet->build number' in order to enter developer mode. That's pretty cryptic, guys.

Incidentally, if you click many times on 'android version' you get a picture of a jelly bean (on this version of android (code name: jelly-bean), anyway.) Swiping the screen gives a little 'game' where you flick jelly beans around. Doing the same on my Galaxy S II, I get this picture:
Android & gingerbread zombies by Jack Larson
Easter eggs are great.  So, anyway. I've finally got debugging working on android thanks to NVidia. One caveat is that this only really works on NVidia hardware; I can't debug on my Samsung phone right now. However, this is a big improvement on feeling utterly defeated, pulling out hair and swearing at unseen and presumably beleaguered tools developers.Thanks NVidia!

Sunday, April 21, 2013

I'm busy, so I'd like development tools that 'Just Work'. Failing that, I'll settle for ones that 'Make sense'.

This weekend, I finally finished converting our game libraries to Linux - before now, they only ran on windows and in iOS. We've been converting to Android as well over the past month. There's something a little magical about seeing code all of a sudden working on a new platform - perhaps because it goes from 'not working' to 'oh look, a nearly finished project' much faster than normal development. Also, out of curiosity, I installed Clang and built using that, too. Amazingly, this compiled the whole project perfectly (save for a few tiny initialiser problems in a handful of files out of hundreds), gave really useful warning messages, compiled pretty quickly and and ran silky smooth. I love things that work better than you'd expect them to; it's like finding a tenner you weren't aware of. Also, I'm really excited by the possibilities of Clang and LLVM. The shared p-code thing allows for a lot of possibilities for domain-specific front end languages and compiler hacking.

Android, on the other hand, has been challenging - working with the NDK (Native Development Kit - it's what lets you write C++ on Android) is a lot less fun than developing pure Java code for android.  In fact, there should be a clue in the home page where they explicitly warn that this isn't for most people.  One set of people that it is for, although they don't mention this, is developers with large, well developed and tested engines written in C++.
  There's not a lot of 'it just works' in NDK-Land and I suspect that this is intentional. Google, Microsoft and Apple would rather we all use (respectively) Java, C# and Objective-C exclusively to each platform, ensuring an amount of lock-in. Of course, that's not what most developers want. Who wants to recreate 
the same work in three languages and then maintain three sets of code? Sounds like a recipe for a maintenance nightmare to me. Whilst using eclipse for Java was pretty good, smooth and even helpful (despite a few oddities and the ongoing struggle to migrate a workspace smoothly), C++ development (at least, in my android related experience and at the present moment in time) is less straightforward.
   I've found Eclipse to be very fragile for C++ development - I've had several show stopping GUI freezes on IDE startup and numerous cases where things mysteriously stopped working or gave entirely misleading and irrelevant error messages (be warned - you'll want to install plugins whilst running eclipse in administrator mode).
  Also, I'm never quite sure if Eclipse has really built all my data before sending it to the phone, although that was a problem with android development in Java too. That kind of spongy, floaty behaviour makes systematic debugging a non-deterministic chore. There's a lot that seems to go on under the hood in eclipse, lots of data squirrelled away in myriad different files. All of this makes predicting its behaviour rather difficult and I'm coding on edge, waiting for the next Heisenbug.

Fortunately, the command line tools are pretty good. You can export ANT command line java builds from your eclipse project - I recommend doing this even if you carry on building and debugging in Eclipse. Having a one-stop batch file that builds and deploys is no bad thing. Even if Eclipse has one its seizures, having the command line tools in place means that shouldn't stop you developing or shipping.   
   Debugging is still a challenge. As in: C++ android debugging doesn't seem to work. We had it working briefly (and slowly) but after restarting Eclipse, no dice. Following a tip from the NDK mailing list/google group, I've ordered a Tegra based tablet and will try out Nvidia's DevKit  which people have been saying good things about. For now, debugging is standard output and forced crashes.
Early, non-textured, shot of the game,
now running on Windows, Linux, iOS, Android and MacOS
I used to be more frustrated with Visual Studio and really, really wanted Eclipse to answer all the shortcomings with Microsoft's offering. However, since upgrading to a faster 64bit machine, Visual Studio has been on its best behaviour.  
  On Linux, I'm rather happy with CodeLite (which, unfortunately, usually Googles as COD elite) - it's simple, open source and tends to 'Just Work'.  Codeblocks is also fine.

  All in all, despite the inevitable speed-bumps that almost always come with development, I'm very pleased that we have our engine running on three new platforms and hopefully many more to come. My raspberry pi looks like it could do with a snazzy space game...
  That said, I'm looking forward to getting back to graphics and gameplay coding and also to revealing more details about our new game! 

Wednesday, April 3, 2013

The Eclipse IDE is full of magic. Unfortunately, I don't mean that in a good way. Whilst I do like Eclipse when it's all set up and running (right-click problem solving and their 'intellisense' actually works most of the time), setting up and copying projects is consistently a pain.
 Take, for example, special folders in an android project. When creating a new project and adding JNI support through the IDE, certain folders get magic attributes associated with them - right click the JNI folder and it knows that it should display C++ related options and android folders are also identified. However, you try importing or copying an existing project in and you'll find that your magic folders are gone - no, they're just normal folders now. I can't find where this extra information is stored but I'm still looking. Hopefully it's not in a binary format.
  I'd rather eclipse 'just worked'. If I were coding this, I'd write a more forgiving system that inferred metadata from standard names or folder contents (hey, you've got some C++ files in there, have some C++ options buddy!) rather than storing it in some external metadata which also has to be tediously maintained. That is, at least, how I do things in Clayworks.
  Context menus are great; if there's something that somebody 
conceivably wants to do to a file or folder then bloody well stick that in the context menu. That's what they're for. Programming is, in and of itself, a challenging activity. I've got my own problems to solve: I'd rather not waste too much of my time having to second guess somebody else's work. I would like my IDE to be easier than dicking around with makefiles, not make life harder by being opaque. I've wasted hours, days trying to balance delicate eclipse projects so that they work, for a while. I know it won't last: like a neighbour's car alarm, it'll be calm for a while but it'll be keeping you awake at night again as soon as something changes.

There seems to be a file called .cproject that has CDT project information inside. However, copying that over to the other project seems to have no effect. Perhaps there's another file that tells eclipse that it has a .cproject file, rather than just inferring this from the fact the file exists.


Friday, February 22, 2013

Clayworks: a background.

An alternative history of 3D modelling programs

(some of the software you probably haven't heard of).

This blog is largely going to be about the continued development of Clayworks, a 3D modelling program I've been developing in various guises as a personal project for most of my professional career. First off, a brief history:

I started writing Clayworks to fill a simple need: I needed to create 3D models but didn't have access to any software that did what I wanted.
  Back in the early 90s, I was a student studying programming at the local college. My friends and I had started writing simple 3D engines. This was the most positive educational experience I've ever had - we were largely left to our own devices but were spurred on by continual, intense but friendly competition. Every day, we'd have some new trick to show; it was great fun. Most of our 3D data was hand entered vertex and polygon data or else procedurally generated. I wanted something that looked a bit better and as my first love is art, I wanted a tool to help me create it. I couldn't afford 3D studio (not Max, that came later) so I did the only sensible thing and wrote my own. My plan, all those years ago, was to use the editor to put together the graphics for this great space game I'd be thinking about.

1992, First version in DOS

This was some time ago and you didn't get a lot for free back then. I had to program my own rasterizer (the code the draws triangles, lines and images), a task that took quite a while in itself. I was lucky enough to find a copy of Richard Ferraro's 'Programmers Guide to the EGA and VGA cards' which sounds pretty dry but, for me, it was life changing. I learned to code in assembly for one thing, a kind programming zen that had quasi-mythological status amongst my geeky teenage peer group. Before home internet, books like this were gold dust. Staples do not sell this sort of tome these days. 

AC3D © Inivis
Aside from writing the basic code to make images appear, I also needed a user interface.
  Windows was at version 3 at this point and wasn't really up to the performance required for a 3D modelling program - DirectX was a while away yet (does anybody remember winG?)

  So I wrote a system I called TWIN, standing for Tim's WINdows. Hell, if Linus could get away with nominative ego-insertion, so could I. To rather less spectacular success but still, there it is.
  With that foundation, I wrote the first version of Clayworks.  I left for University that year and continued writing Clayworks whilst there. In retrospect, I should perhaps have paid a bit more attention to what I was supposed to be studying but having access to the nascent world wide web was exhilarating; you could see how this was going to change the world.
  I also spent more time in the research lab than in the undergraduate labs; they had SGI machines - hardware acceleration cost you tens of thousands back then and even with my total immersion in the world of 3D graphics, I wouldn't have predicted the rise of affordable graphics cards at the time. If I had, I might have spent less time writing my software rasterizer, which was where I was concentrating most of my programming effort.
  It was at this time that I met Andy Colebourne of (more nominative ego-insertion!) AC3D fame.  Andy wanted me to re-write Clayworks in C++ and OpenGL (back then it was Pascal/ASM and software rendering) so that it'd run on SGI machines. Not having access to such a machine on a daily basis, I wasn't able to do that, so Andy wrote AC3D. Andy's program was much better poised for future hardware than mine and he's converted AC3D to dozens of platforms.

  During this time, I put the DOS version of Clayworks up for download on the Hensa archive and it was the most downloaded program on there for several months, racking up hundreds of thousands of downloads.

  Now, at this point, what I should perhaps have done was get a loan, start a company and make a proper go of things. However, I was young and knew a bit about how to code, draw pictures and make models but knew nothing of business. So, instead I landed my first programming job with a small team working on V.R. and 3D modelling software, Themekit.

 At this point, I had to stop working on Clayworks. In a theme that's been repeated again and again through my career: there was too much conflict of interest.
  Martin Robinson, my boss at Themekit, had written a great set of tools for 3D modelling. I have very fond memories of working on and with this software. It was quirky, did things unapologetically its own way but it became one of those programs for which I fully developed muscle memory - like DPaint, I didn't have to think about using it, the short cut keys were fully mapped out in a special area of my brain.

MindFormer © Themekit
 Again, a product of the time, these tools were software rendered and written in DOS. It was around this time (1997-8) that hardware acceleration really started to take off and one of my tasks was to rewrite the engine for OpenGL. I worked at Themekit for around 2 years and learned a great deal. 

  Leaving that company was one of the hardest decisions I've ever made. 
  It wasn't over a better offer at a rival company but rather the chance to work in Japan, where my girlfriend of the time (whom I'd met at University) lived.

    The new job was categorically not what I wanted to do in life - it actually involved making webpages at a travel agency but the chance to live and work in Japan was too good an opportunity to ignore. Besides, even with that newfangled Instant Messenging thing, long distance relationships suck. 

 The story of how I got that job is largely down to Clayworks and not a small amount of luck. So much so that it looked for all the world like fate taking a back handed slap at my face and demanding that I pay attention.  My girlfriend had just finished her masters degree and was, like just about every other Japanese graduate, traipsing around Tokyo looking for a job. It was around this time that I was in Japan for a visit. Tokyo looked at my hard saved British money, made a polite little nervous laugh and asked where the rest was.  

  Around half way through my stay, she had a job interview. With little else to do, I stated in the foyer where they had a little 'cyber-cafe' thing going on, all beige boxes, bulky CRTs and noisy modems. After checking my email, I downloaded a copy of Clayworks and started passing the time making models of this and that. At that point, a manager appeared behind me and I thought I was about to be removed for doing non email related tasks.

  Instead, he asked me what the software was. When I mentioned that it was a little personal project that I'd written myself, he became rather excited and, in charmingly broken English that was none the less better than my Japanese would ever become, offered me a job on the spot. My girlfriend arrived in the cafe with a big smile on her face and announced that she'd got the job. I surprised her a great deal by mentioning that I had, too.

Clayworks 2. A rewrite, still in DOS but prettier now.
Six months later, I was working in Japan. At first, I didn't have a place to live and wasn't entirely sure how to go about getting one. Worse, I didn't have a computer. Being a man of sometimes outwardly dubious priority management, I rushed off to Akihabara to buy a bunch of computer bits with the money that should have gone towards the key money (key money? I know, I had no idea what it was either) on an apartment. Again, in retrospect, I should have bought that dinky little 486 palm top that was around 10 years ahead of its time but instead got a familiar big ugly beige box, got the parts back to my hotel room, built the machine and coded up a few graphics demos. I was suffering programming withdrawal symptoms. My girlfriend was not exactly amused at this purchase but one of her wonderfully diplomatic friends pointed out that I wasn't in Kansas any more and probably needed something to anchor myself in reality. I've always found that virtual reality does the trick.

  The job at the travel agency had its ups and downs but I did manage to do a lot of rather nice pixel art and learned to code Java. However, as I said, this wasn't the job I wanted to do.  Fortunately, through a friend of a friend, I heard about a vacancy at a company called A.I. Cube over in Shintomicho. The interview was strange in that, 6000 miles away from home, I'd already met everybody in the room. It turns out that A.I. Cube had risen from the ashes of the Japanese software development arm of the now sadly defunct British V.R. headset manufacturer, Virtuality. I'd met them all on a business trip a few years previously. They said that they thought it was me on the interview form and subsequently offered me a job.
  Now, this part of living in Japan was great. I loved my job, I was working on the new Playstation 2 a year before it came out as well as working on the GSCube (a prototype PS3, noisy bugger, looked like Borg cube. It warmed my feet under my desk during winter). My Japanese was improving and life was generally rather good.  At A.I.Cube we developed 'middleware' engine software that other companies used to build their games. We worked with, amongst others, Koei, developing the engine that was used in Gitaroo man (a great little game)

Gitaroo Man! © Koei

 Of course, that didn't last. The Japanese immigration people didn't like the fact that I'd changed jobs - and that, although lacking a higher degree, I was doing a well paid software engineering job whilst being reprehensibly foreign. After numerous appeals and several months of anguish of the sort that only the officious can inflict, I was back in the UK.

  At this point, I dived back in to academia as a way of attaining that qualification and getting back to Japan. I wrote a thesis on generative modelling and got the degree but, unfortunately for me at the time, the relationship that was my reason for returning didn't last another bout of long distance. I settled down to live in the UK again, working for a couple of years as a researcher at the University of Wales in Swansea where I met the lovely lady who is now my wife.
  I rewrote Clayworks for the third time and this forms the code base that I now work from.
When the research project came to an end, I attempted to start a company to develop Clayworks further. Unfortunately, I failed to get any local traction on that and, with a young child to care for, I took the conventional route again and got a job.

  This took me to the Creative Assembly, a games company in Sussex famous for the Total War series of games. I met a lot of good people at C.A., worked on the graphics engine for the series, worked on four separate titles, did a spot of voice over work (I'm the Welsh soldiers in Napoleon, mind you)  and developed TED, the total war editor. TED's UI takes its skin and design all the way back to the very early versions of Clayworks, although the program itself is not a modeller - rather, it's a scene builder 

TED © The Creative Assembly. The Total War Editor.
specifically designed for creating total war levels.
TED was released last year, a little after I left C.A.  It's the first time anybody has made you tube videos showing how to use something that I've had a large hand in writing. Thanks, RobinHood2109, you made my day.

After five years at C.A., I felt it was time for a new challenge. I trained up my replacement and headed back to Wales to start my own company, Totga Games

Chinese New Year app © Totga Games
  After releasing a few android titles, (which were more tech demos than anything else, as I developed my android engine), I had another chance encounter, via a convoluted series of events involving my wife's rock band cousin suffering a heart attack. From his hospital bed and hearing what I was attempting to set up in Swansea, he got me in touch with his good friend Darren, who for the last four years has been running Swansea's sole dedicated games development company (that I know of).  I've been working with that company, Chaos Trend, for almost a year now and I'm now on board as a director. The bulk of my work has been adding a full 3D engine to their already well developed code base and working on a new game, working title of 'Solar Escape'.  Finally, I'm actually working on that space game! And, as always, developing the tools to help do it.

  I've been developing 3D software for around 20 years now.  It's taken me all over the world but during all that time but, due to various obligations, I've only released one real version of Clayworks - the old 16 colour DOS app that I started it all in 1992.

  Now that I'm again captain of my destiny to some degree, I hope to finally release an up to date version of the project that's been a continuous thread throughout my rather circuitous career in 3D graphics and games development .
  In this blog, I'll be keeping a more regular diary of that development and of the space game I'm finally working on.

Clayworks in 2013. It's getting there.