Friday, April 10, 2009

Testing some shading options

As I've been testing lately, you can purchase all the source code for this sample for $5
(http://store.payloadz.com/go?id=254377).

I’ve been testing some shading options and working on the 3D aspects of XNA that I have been needing to learn. One of the problems is that I have 3DStudioMax, but it is only on a machine I don’t use as much. I’ve been contemplating moving the license, but that has issues. I’ve used Blender in the past and have enough knowledge to be dangerous to myself, but went back to it as I have not been able to use Max when I need it. So, this was as much a Blender session as an XNA lighting session.

I’m using Reimer Grootjan’s book XNA 2.0 Game Programming Recipesand found it very helpful in the shading and rendering aspects. I know it’s for XNA 2.0 and I’m using XNA 3.0, but the stuff I’m looking at has not changed (as is most of the book) so it is still highly relevant to learning XNA 3.0.

The section I used was on directional lighting on page 514. It shows how to get the BasicEffect to work with a directional light.

My code looked like this when I was done.

effect.LightingEnabled = true;
effect.DirectionalLight0.Direction = light0Pos;
effect.DirectionalLight0.DiffuseColor = Color.White.ToVector3();
effect.DirectionalLight0.Enabled = true;
effect.PreferPerPixelLighting = false;
effect.DirectionalLight0.SpecularColor = Color.White.ToVector3();
effect.SpecularPower = specPower;
With, specPower and light0Pos set at the top of the class definition.


Vector3 light0Pos = new Vector3(-0.5f, -1.0f, 0.0f);
float specPower = 64;
After weeks of procrastination and thinking I was going to have to create a custom shader it was nice to find out that adding a directional light was very simple. One of the games I was going to do requires a spotlight and I do think that will require a custom shader so it's off the list until I get some extra time.

So, first up with Blender. I’m trying to display a gemstone. I created a isosphere then scaled and stretched it to look like a gem stone. Then I added a red texture. You can see that I set the Col (Color), Spe (Specular Color) and the Mir (Mirror Color) for the gem.


This was fine, except that the gem came out all smooth.


I know from experience that this is caused by the rendering system using vertex normals to draw the individual triangles of the gem as smoothly as possible. This is almost always the case. But for a gemstone, you want it to have facets. I spent a ton of time trying to figure out how to get blender to remove the vertex normals and only use surface normals.

This ended up being easy inside of blender, but took a while to figure out how to get this information into the direct X file. To do this in blender, you select the object in edit mode, then click the Set Smooth button to use vertex normals for rendering and Set Solid to use surface normals for rendering.


Easy and you can see the two rendered versions here.


That’s all well and good in Blender rendering, but XNA kept displaying the gem as a smooth surface. After tons of fiddling, I noticed one of the options on the Blender DirectX exporter was “no smooth”. The tool tip says exactly what I wanted to hear “Every vertex has the face normal, no smoothing”.



Once I exported with that setting everything worked.



Getting back to Blender, I found it really hard to learn, and hard to relearn, but for the price (Free) it really is an amazing tool. I have a book on it that I've barely touched called Introducing Character Animation with Blender by Tony Mullen. I've had it for a few years and only touched the surface on it and every time I pick it up I'm always impressed. It really does look like a great book. As I write this he has a new version due out soon and another book on Blender that was just released. See below.

My real problem with Blender came in the classes I was teaching on Torque last year and the fact that we could never get the bone systems to work properly. It seemed that 3DSMax was the only real way to do modeling for Torque. Maybe that has changed?

I did find this great reference image on the Blender hotkeys (http://en.wikibooks.org/wiki/File:BlenderHotkeysObjectMode.png. If you are going to use Blender, you are going to want to learn as many as possible. It really is a fast fast tool once you get to know the UI. Mostly I only use Space Bar, B (box select), A (select/deselect all), S (scale), R (rotate X,Y,Z).




As I've been testing lately, you can purchase all the source code for this sample for $5.
(http://store.payloadz.com/go?id=254377).

Wednesday, April 1, 2009

Balance Air Game

This is the best one so far. Didn't have a lot of time again so I went 2D and wished I had enough time to add a little sound (like a hair dryer sound) and do some more complex goals (like a bucket), but I think it's probably the most fun so far. It's not super pretty, but good luck, it's really really hard.


The game is called Balance Air. You have a hair dryer at the bottom of the screen and you need to balance the ball on air and force it into the goal. You move the blower left or right to change the direction of the ball. There are 7 levels which are basically just different locations for the goal. The score for each goal is dropping over time.

As I did last time you can download a free executable of the game. It is available here: http://www.woodsgoods.com/gawxna/02balanceair.

The complete source and Visual Studio project is available for $5 at http://store.payloadz.com/go?id=246205 .

Enjoy and please give me some feedback.