Particle Life Source

May 13th, 2008

Much later than anticipated, here is the source code for my screen saver, Particle Life - and a short discussion of some techniques used to make the screen saver. I used GPU processing, pre-rendered depth blurring, and some fun camera tricks.

Particle Life Source Code

GPU Processing

You’ll see this in PositionsEffect.fx. Basically, I pre-generate a velocities texture with random noise, and pre-generate a positions texture with incremental depths for particles (to ensure a good distribution of particles) and random x/y values (with the camera’s frustum). Then every frame I sample both position and velocity, add the two together (multiplying the velocity by the elapsed time) and write the new values to the positions texture.

Now, here’s where I could use vertex texture fetching, but I wanted something that’d work on shader model 2.0 (and my laptop), so I did something simpler. The position texture is stored as a HalfVector4. Every frame, I dump this straight into an array and then push it into a vertex buffer of HalfVector4 vertices. Since I’m using point sprites, that’s all I need to do. It’s not the most efficient piece of code (the dump sure isn’t nice), but it works.

Pre-rendered Depth Blurring

I wanted particles to look blurry as they became more distant. However, with alpha-blended particles, I can’t quite do this in a post-processing step. I’d have to basically blur the texture whenever I rendered particles by taking several sample taps on the texture for each pixel. The fill rate would become horrible.

Life Particles

So, I took the easier route. Instead of doing all that work on the GPU just to get particles to blur, I just built the mipmaps in the texture by hand. If you open up part.dds, you’ll notice that each successive mipmap is not only smaller, but blurrier. Since the mipmaps are automatically blended when I sample the texture, that does the blurring for me for free.

Camera Work

One of the important things to take from this sample is that sometimes there’s ways you can “cheat” without being caught by the end-user, and save a lot of processing power in the meantime. One more example of this is how the particles are “culled”. Nothing exists outside of the camera’s viewing frustum. There’s no reason for it to - it’s not visible to the user, and it’d just waste processing power if we dealt with it. So when a particle leaves the camera’s frustum, it’s wrapped around to the other side.

So how do I know if the particle has left the view? Well, I could do a bunch of nasty math involving rotation, tilt, field of view, etc. Or I can just align the camera straight down the Z axis. Then I know that there is a linear relationship between the particle’s Z position and the width and height of the camera plane at that Z. This is the tangent of the field of view. So I just set that as a variable in the shader (or variables in this case: FieldWidthFactor and FieldHeightFactor in PositionsEffect.fx). Now I don’t even have to do any transformations of the particle positions.

One final note

GTA IV Depth of Field

You might recognize this, it’s from Grand Theft Auto IV (image from gtanet.com). If you’ve played the game, you’ll probably have noticed how short the full-detail draw-distance is. After a couple blocks, cars turn into headlight circles (a simple circle to show headlights) and everything becomes really blurry. My theory is that, to help with memory streaming and file sizes, a lot of the intermediate mip-maps in textures have been cut out. That way the full-detail texture maps only have to be loaded when you’re close to an object, but it can still have some definition if it’s in the background. However, due to either space or processing constraints, they couldn’t show too far away, so a post-processing effect adds some grainy blur for distant objects, helping to hide the low-resolution textures.

That’s in no way the official word on things, just my guess - but it’d be a good example of how you sometimes can make clever use of graphics tricks to keep your asset size and processing requirements low while still maintaining good visual quality.

XNA 3.0: MP3/WMA/WAV support!

May 7th, 2008

So, I was a little disappointed about the XNA 3.0 CTP when I read about it. It looked like the only big feature was Zune support (being without a Zune, or any friends who have a Zune, I have quite the lack of interest in developing for it). However, after a little prodding, it looks like with Zune support comes much better media support, including the ability to import and process MP3, WMA and WAV files without going through XACT! They seem to be converted to WMA files, which is fine by me, though the options available for compression are pretty simple still.

I made an example project which shows how to process and load an MP3 song in your program:

Download the Example!

It’s pretty trivially easy, but there’s really no how-to that I could find that explains this. The big thing is the MediaPlayer class, which lets you do a lot of different things with Song classes. Check it out in the documentation for the XNA 3.0 CTP.

And of course, you’ll need the XNA 3.0 Community Technical Preview to compile and run this example.

Now, if they add FriendGamer.CurrentTitle by the next release, I’ll be happy!

Alpha of Screensaver Available

March 1st, 2008

I’ve completed an alpha version of the screen saver I’ve been talking about, and it’s now available for download.

Get it here
Edit: The screen saver works! Just copy ParticleLife.scr and FlatRedBall.dll to your Windows/System32 directory, then right click ParticleLife.scr and select Install.

Giant Screenshot

To run this you’ll need to copy the .scr and .dll to the windows directory (I hear Windows\System32 is the place to put it, but you can just right click the .scr and choose install too). Then make sure you have the following prerequisites installed:

The .NET Framework 2.0
DirectX 9.0c (run this even if you have it already - you may be missing files required for XNA)
XNA Runtime 2.0

I know it’s a pain (and ironic that this screen saver is windows-only), but the alternative is…well, not using the screen saver!

The original inspiration was this piece by Hawk: Life. I saw it and thought, “that would make a nice screen saver, and a good test of some GPU programming.” So I went for it. It’s not as pretty as the flash version (the lines aren’t as clean), but I think it looks decent for programmer art, and it runs nicely (and almost all of the calculation is done on the GPU - which was my major focus when making this).

There aren’t any options in this version. Eventually I want it to support user-specified particle counts, but I can’t really do that without storing the value somewhere. The most popular place for screen saver parameters is the registry, and I can’t go writing all over that without an installer/uninstaller (so if you decide you don’t like this it doesn’t leave a bunch of dead information in your registry). So - if this screen saver is a hit (goodness knows smaller things I’ve made have been huge hits), I’ll probably clean it up a bit and add some settings.

You’ll also need a graphics card that supports Pixel Shader Model 2.0 - I created this primarily as a test of some graphics techniques - not necessarily just to make a screen saver (you can compile that flash as an exe with exit-on-input to use it as a screen saver). I’m not doing any detection at the moment - so if it doesn’t run

For the XNA programmers in the audience, I’ll be releasing the source code for this soon.

Feed Fixed!

February 29th, 2008

Looks like when I switched some stuff on my server, I nuked the feed! I fixed it, and hopefully you’re all getting this post as well as my last one! (I’m not dead yet!)

Enjoy this screenshot of my in-development screen saver at huge resolution (not giant yet - haven’t got to test it on 1080p yet):

Giant Screenshot

GPU Particles

February 28th, 2008

Check this out: 10,000 particles running on my new system (with no gfx card!) at a consistent 30fps, all with different velocities, distance blurring, etc.:

I had 65,000 running just fine, but it rendered as a big white blob, which didn’t look very presentable. I’ll cover the techniques behind this sample soon, along with providing full source. Most of this is actually done on the (motherboard-embedded) gpu though - I was surprised it ran well at all!

So stay tuned - I’ve got a couple graphics samples in the works that I’m hoping to start filling my website with soon. I’ll release this one once I clean up the presentation a little (it’s not quite what I want - yet). I’m hoping to make it a screensaver similar to Hawk’s recent flash sample entitled Life. Anyone see the irony?

Site Maintenance

January 23rd, 2008

My site will be down for a few days while I update some stuff. So if you’re watching my site - fear not, I’ll be back!

edit: Looks like it didn’t take long to fix up! Let me know if you encounter any broken links!

Depth of Field Tutorial

January 5th, 2008

I’ve submitted a new tutorial to Ziggyware about creating a Depth of Field effect. It includes a lot of information about Gaussian Blurring as well. Check it out at Ziggyware. And If you like it, vote for it in the Ziggyware Holiday Contest.

Back, with a threading tutorial!

October 3rd, 2007

Alright, so long story short - I didn’t slip off the earth, just found a new cave to inhabit. About the time of Gamefest my University Advisor contacted me and informed me that there had been some mistakes in my scheduling, and instead of having graduated in May, I still had 5 credits left to complete! Imagine my dismay! So I pulled things together pretty quickly and managed to work it out so I can complete those credits by using XNA more - lucky me!

First of all - I’m now helping out on the FlatRedBall engine. It’s a 2.5D engine - meaning it renders 2D objects in 3D space. It’s got tons of great features - collision detection, particle systems, great debugging tools, a wide range of graphical options, and now, post-processing. Go check it out!

Second, I’m rebuilding Dream Sower. Our first version was wonderful, but needed something extra to take it over the top. I’ve got a lot of feedback on it, and I’m rewriting some of the low-level code to get it ready for reimplementation. I really really really need an artist, so if you have services you could offer (or know someone who can), please let me know! I’m decent with 3D art, but I’d much rather have a game that has more than programmer art!

Third, and last for today (may we meet again soon!) - I’ve written an article on multi-threading using XNA for Ziggyware’s XNA Article Contest. It has some good information on writing a background loader, teaches you about some common pitfalls in multi-threading, and I even threw in a managed thread class I developed for my game. Let me know what you think of it, and if you like it a lot, go vote for it!

That’s all for now!

Call for games for XNA Arcade

August 13th, 2007

So, I’d like to add some more links to the XNA Arcade tracker I’m running so I can properly test the program as I develop it further. So, if you’d like your game added, please send me the following (or post it in the comments of this post):

Game Name
Game Download Link (see below)
Executable Name and Location within Zip File
Game Image (a 407×170 image file, uploaded on the web)
Developer Name
Website Link
Game information text (about 6 lines of information about your game)

For the downloadable, you’ll need to zip the contents of your project’s compiled bin/x86/Release/ directory (please compile in release mode). You should navigate to that directory, select all files and add them to an archive (as opposed to zipping up the “Release” folder itself). The Executable name and location will then be something like “myGame.exe.”

You’ll then need to upload that zip file somewhere, and include that link in your submission (I’m sorry, I don’t have the webspace to host everyone’s games).

XNA Arcade Alpha 1

August 8th, 2007

Frustrated over the difficulty of distributing XNA games? Don’t like having to package installers all the time? I sure am.

I figured it’s about time we had a front-end for normal people to get and play our XNA games. As a community we’ve made some amazing stuff, but it’s still difficult to get casual gamers playing our games. Enter XNA Arcade.

This program will grab a games listing from one or more xml trackers on the web, which it will use to populate a categorized listing of games. The user can then navigate to a category, choose a game, then download it, all through the app. Once the game is downloaded, it will install itself, and the user can just click the Play button in the application to play the game.

Easy!

And better - as a developer, all you have to do is zip up your compiled “bin” directory and upload it somewhere, then get your game’s information in a tracker. Since XNA Arcade will install the DirectX, XNA and .NET 2.0 redistributables with itself (it doesn’t do this yet), you don’t have to worry about the end-user having them!

Like the title says, this is still an early alpha (i.e. functional prototype). It’s not necessarily secure, robust or cleaned up yet, but if you’d like to try it out, you can download it here:

http://www.kyleschouviller.com/files/gametest/XNAArcade.zip

I figure with some more development and community support, we could get a couple sites hosting games/trackers which developers can submit their games to. We could even add some ratings system or some other features.

Let me know what you think!

Added: I should note that Chris Hildenbrand, the artist of Shuggy and SSH, did the interface graphics. A great thanks to him.