Posts Tagged ‘photorealism’

Cityscape – part 3

Wednesday, May 13th, 2009

The march towards photorealism continues ever onwards:

Look, okay, one day I promise it’ll look more like real buildings. Today’s (first? we’ll see) update isn’t such a big one, but it adds a couple of useful bits and bobs.

Firstly, there’s that there framerate counter. To do that, I’ve added a new GameComponent, and in the Update() method simply keep track of the number of frames and the elapsed time (and have a ‘resettable’ counter that resets on 1second intervals, to get an idea of the ‘current’ framerate, as well as the average). This provides a service for the main Game engine to use to get the current framerate (which might be useful later for doing adaptive LOD cleverness). The display of the counter is achieved using a simple SpriteFont – the XNA Content Pipeline handles all the heavy lifting in turning a .TTF into something you can draw in your SpriteBatch, and there’s a handy utility method to draw the text to the screen.

(the reason for adding the counter was that I was running the code on my Samsung NC10 netbook, and whilst it looked like it was coping reasonably well, it was hard to tell exactly how well. Turns out it’s getting about 40fps, which is quite respectable, but with a lot of jitter and tearing. For what it’s worth, the machine I’m doing most of the dev on is a Dell laptop with GeForce 8600M GT, which is where the screenshots are from – it hovers around 60fps; I assume XNA is locking Update()/Draw() calls to vsyncs.

Next, I’ve added procedural texture generation. This is extremely early days for this yet; the texture generator class has a single static member that creates a 1024×1024 texture, grabs the data into an array, overwrites it with a checkerboard pattern and then sets the data back into the texture. Simples.

(actually, whilst doing this, I ended up bashing my head against a brick wall for a while – with texture sizes above a certain size, I was getting entirely black models, and couldn’t figure out why. It turns out that in texture creation, I was specifying the texture should be created with mipmap levels all the way down, but had turned off autogeneration of mipmaps, so only the highest resolution version of the texture had mipmaps. The textures were of sufficiently high resolution that the objects were being rendered using lower mipmap levels of the texture – which were all black! Turning on mipmap autogeneration fixed the problem.)

Oh, and obviously, I’ve added a few more buildings, moved the camera back slightly and added some ambient lighting. The latest revision in the bzr repo is 11, if you’re following along.

Cityscape – part 2

Monday, May 11th, 2009

Okay, I know I said that I was going to make something a bit more building like next and, well, here you go:

Look! It’s got a plinth and everything! Well, okay, it’s not very exciting, and I’ll be honest, there’s very little extra making-it-look-like-a-building code in there – I’ve simply added another box and changed their sizes slightly. But there’s a bit more gone on behind the scenes:

First, in the first pass, each face had the texture coordinates clamped to (0,0) -> (1,1) across the face, which meant that the textures looked oddly squished or stretched on anything other than a perfectly square face. Now, the texture coordinates are calculated according to face size; a 1×1 world-unit square will have the entire texture exactly mapped to it – we’ll worry about changing this scale to more useful units later.

Secondly, in the first pass, I wasn’t doing things in a very XNA-like style: from my Building class, I was reaching inside the Game to grab the camera matrices to set up for rendering. Whilst this is entirely possible, it’s a bit ugly and leads to lots of unpleasant tangled dependencies. XNA provides a concept of “Game Services” – essentially public interfaces to objects that can be queried for from the main Game object. So, instead of reaching inside the Game for the camera matrices, we now have a Camera GameObject, and an ICamera service for it. The Camera is registered as a GameObject and a service, and then later on, the Building can query the Game for an ICamera object, and get the view and projection matrices from that instead. We also use the IGraphicsDeviceService that is provided by default, rather than grabbing that from inside Game, too.

Thirdly, the application can now be quit simply by hitting Escape, as I’ve added some simple keyboard state monitoring.

And lastly, I’ve changed the game from the default fixed update rate to variable update rate; I’m not entirely sure why XNA defaults to a fixed update rate – most games I know of use variable timesteps – and in XNA, it actually causes problems. The Update() (and Draw()) methods of GameComponents can lag behind in certain circumstances – for example, if the window is moved, or if input focus is switched – leading to unpleasant stalls in your game. Simply switching off fixed timesteps fixes this – and hey, it’s not 1993 any more, we can cope with writing our physics/AI solvers to deal with variable framerates.

As before, the code is at http://bzr.parm.net/Cityscape/ – this post refers to trunk revision 8.