Archive for My Graphics Programming

Reflections on the Water Demo*

// March 7th, 2011 // No Comments » // My Graphics Programming, Portfolio

*Pun definitely intended

Since it was GDC time again, I ended up thinking about the water demo I created with my fellow grad student Ada.  It was our first foray into shaders, and I think it turned out pretty well for a 10 week project.  We had gone into the project thinking we might buy a book and follow a few tutorials.  Instead, we were given the task of creating an impressive-looking scrolling water system that combined deep ocean & shallow water wave systems.

As is always the case, the more I look at the water demo the more problems and mistakes I find.  I’ve been thinking about creating a new version for a few years now, if only to appease the constant requests for a boat.  Since this project is comprised of the first real shaders I wrote, digging through the code makes me want to rewrite it even more.  I’ve learned (and done) so much since we first toyed around with shaders.

It was originally written in XNA 1.0 Refresh, and upgraded to XNA 3.1 recently (before 4.0 arrived).  Since I’ve been using XNA so much lately (mostly working on the Darkwind Media game Blocks), it might be nice to start over in C++ & DirectX 10.  I’ve been itching to do something in that area again, and I think it’d be fun to improve upon the project that got me into graphics programming.

-Chris

Dynamic Billboarded Integer Rendering

// June 13th, 2010 // No Comments » // My Graphics Programming, My Stuff

Aliens Dynamic Score

A few months ago I was working on Aliens and implementing a new scoring system. I ran into an issue: I needed a way to dynamically render a number (basically a point value) on a billboarded quad. It had to be dynamic because the value of each item would change based on a multiplier, which could also change at any time. I could have done it as a UI element instead of actual geometry, but that didn’t feel right for the game. I was committed to having the scores be billboarded quads in the scene.

Aliens Dynamic Score

My first thought was to simply render a number to a texture and apply that texture to the quads. This would work great if I needed few unique numbers. I could render the current numbers to a few textures, or create a large texture with multiple numbers and map the numbers to different quads. I quickly realized that this wouldn’t work for Aliens; if different items were worth different amounts, and your multiplier was frequently changing, the quantity of different numbers that were required would be changing as well. Since the quads fade in and out, the player could destroy an object worth 500 points, get a multiplier, and then destroy the same type of object which is now worth 1000 points. Both quads would be on the screen at the same time. It could get hairy fast.

I needed the whole process to be dynamic, so that I could feed an integer to the shader and have it render. I wrestled with it for a few days until I came up with the solution I ended up using: If I could get both the number of digits in a number, and any one digit based on the position inside the quad, I could render that number.  The results were exactly what I wanted: Dynamic numbers on geometry in the scene, in a font that matched the rest of the game.

So how does it work?  I’ve got some shader code below that shows the process.  Several pieces of data are passed in: the number itself, the number of digits, the age of the quad, a scale and a color.  The number, scale and color are self-explanatory.  The number of digits is used in several places.  It could have been calculated in the shader, but it seemed better to calculate it once when the quad was created.  The age of the quad is used to fade it in and out, as well as make it float upwards.  The numbers are pulled from a greyscale texture:

Digit Texture

Below is my vertex shader code.  The first block of code does the billboarding and scaling.  The overall scale and Y position are based on age.  This allows the quad to grow and move upwards over time.  The width is based on the number of digits that we need to render; the quad auto-adjusts to fit the number. The BigUV output parameter scales the overall UV by the number of digits, which is used in the pixel shader to determine which digit a particular pixel represents. There are constants mixed in which adjust the height and alpha (to achieve the exact look I wanted).

1
2
3
4
5
6
7
8
9
10
11
12
13
// Calculate the position
scale *= 1.0f + age;
output.Position = mul(float4(0, 0, 0, 1), mul(World, View));
output.Position.x += input.Position.x * scale * numDigits;
output.Position.y += input.Position.y * scale * 1.5f + (15.0f * age) + 5.0f;
output.Position = mul(output.Position, Projection);

// Pass the UVs through
output.UV = input.UV;
output.BigUV = input.UV.x * numDigits;

// Adjust the alpha based on age
output.Alpha = saturate(5.0f - abs(age * 10.0f - 5.0f));

The pixel shader code below determines which place the current pixel represents and which digit from the digit texture it needs to sample.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Get the current digit
float place = numDigits - floor(input.BigUV);
float digit = floor((number % pow(10, place)) / pow(10, place - 1));

// Calculate the correct U coord
float u = input.BigUV.x - floor(input.BigUV.x);
u *= 0.1f;
u += digit * 0.1f;
input.UV.x = u;

// Return the correct color
color.a = input.Alpha;
return tex2D(digitSampler, input.UV) * color;

Dynamic Billboarded Integer Rendering

That’s about it! I haven’t really done any optimization, and my math may be a little verbose, but it works pretty well. I’ve been meaning to make a post about this for a while, but I haven’t been working on Aliens since development started on Blocks. Once Blocks wraps up, I’ll probably revisit this (and start refactoring most of Aliens). I made a simple info-graphic on the overall process, which may or may not help visualize what’s going on.

-Chris

XNA Fur Rendering

// November 22nd, 2009 // No Comments » // My Graphics Programming

I was pretty sick last weekend, and sticking to my odd “get sick, code something” habit, I decided to finally try to make a fur rendering demo. I read a few articles about it years ago, so I knew the general concept behind it: render lots of “shells” around the model with an appropriate fur texture. The fur texture is basically a transparent texture with dots on it, and as the shells get layered on top of each other, the dots are essentially extruded into individual furs (without having to render each fur individually).

I’ve seen fur effects used in a few games, notably Star Fox Adventures for the GameCube, and the more recent Super Mario Galaxy for the Wii. I have no idea if they are using the same technique, but the Super Mario Galaxy queen bee looks similar.

What really got me thinking about finally starting this demo was an article I found on reddit. The article is from xbdev.net, and is (appropriately) called Generating Fur in DirectX or OpenGL Easily. While it’s not the most well-written article I’ve ever read (especially the comments in the provided code), it has some nice diagrams and screenshots of the technique in progress. If you’re interested in learning more about fur rendering, I’d suggest starting there.

After reading that article, I decided to go for it. I got the basics up and running pretty quickly. I added some camera controls and the ability to change a few things on the fly (like the fur length), mostly for testing purposes. One of the first things you notice with basic fur rendering is that everything is the same color, so you only notice the “fur” aspect of it around the edges. I added some basic N dot L lighting and I modified the fur by the model’s texture so it wasn’t all the exactly the same. This helped a bit, but the hairs were still bleeding into each other and not looking very fur-like. Clearly some sort of individual fur shading was needed.

I went back to the fur article linked above to check out their shadowing technique. To be honest, I’m not 100% sure I even implemented it the way they explained it (which I would have to describe as “poorly”). The gist of it is to re-render each shell, offsetting the texture a bit and making it look like a shadow. I didn’t like it from the start. Double the number of shells that need to be rendered? No thanks. I didn’t need to render an actual shadow of each fur. I just wanted the fur to get darker the “deeper” it was, so that’s what I did. The shells closer to the model are rendered a bit darker, and they get brighter as they get further away. The last piece of this puzzle was to make sure the model itself was rendered slightly darker, giving the illusion that the fur is shading the scalp.

One thing I did implement from the article was the concept of gravity affecting the fur. Each shell is offset a tiny bit in the negative Y direction (based on a power function) to give the impression that the fur is bending due to gravity. It’s a very nice effect, and it was easy to implement. I decided to take it a step further and make the fur react as you drag the model around. When you drag it, the fur gets stretched in the opposite direction and slowly interpolates back to its normal “bent” position. It’s fake physics, but it looks neat.

I’ve got some plans to continue this the next time I’m bored. A few features off the top of my head would be: multiple models & swapping at run time, more controls for different settings (gravity, fur density, etc) and adding a fur length map, so certain sections of the model can have different lengths of fur (or be bald).

UPDATE: I created a newer version of my fur demo using Luster.  This one uses a fur map to mask out non-fur areas.

-Chris