Friday, June 29, 2007

Size matters?

Screenshot posted from inside Bungie's realm. Apparantly this is the backend storage that will support the video/screenshot capabilities of the upcoming Halo 3. You can store as many as you like, on your local storage, but there limits, yet to be announced for public viewable ones. That's a lot of bytes though!




Thursday, June 21, 2007

Compressing Textures.....the DirectX way

Compression of textures is something that DirectX can take care of for us, but it makes sense to understand how it does this.



Consider the following gradient:




If you were required to store this in a texture, you would end up having to store hunderds of RGB values to represent it If you instead stored say 2 colors (the ends) you perform linear interpolation to get the remaining colors and you have are only storing to values. A crude calculation to do just that is here:


Color_Gradient = (Color2 - Color1) / (GradientWidth - 1);
for(i=0; i <>
{
SetPixelColor(i, 0, Color1 + (Color_Gradient * i));

}



This is roughly how DirectX can compress textures resulting in massive savings. Of course on a big texture you will probably have more than 2 colors and a simple bar object, so DirectX will partition the texture into a 4 x 4 matrix and based on the Texture Compression state chosen, will use either 1 or 2 of these grids (one for RGB and one for ALPHA) or just one and two RGB color values. The specifics of how the interpolation is performed again is dictated by the state of the Texture Compression chosen. More to come on this..

Sunday, June 17, 2007

Binary Serialization in .NET

Persisting objects to other medium for future use is one advantage of the serialization mechanisms built into the .NET Framework. Listed below is are 2 code sections showing serialization and deserialization to disk.

SERIALIZE:


FileStream fStream = new FileStream(ConfigurationManager.AppSettings[1].ToString(), FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
htObject = bin.Deserialize(fStream) as Hashtable;

DESERIALIZE:


Stream binstream = File.OpenWrite(ConfigurationManager.AppSettings[1].ToString());
BinaryFormatter binFmtr = new BinaryFormatter();
binFmtr.Serialize(binstream, m_hashTable);
binstream.Close();

This can be useful in XNA programming. More C++ for DirectX9 posts will be coming next week.

GetHashCode() in .NET

So in .NET, there is a virtual function defined for all object types (everything) named GetHashCode(). This function can be used to generated a hash value for an object, but the main caveat is this hash will not be guaranteed unique. The implementation can be used for very small amounts of data. The hash calculation returns a 32-bit integer. This means that at most there 4,294,967,295 different integer values possible.
To help the situation, an overridden GetHashCode() can be used. When first confronted with this dilemma, I was going to just XOR the values I was using to generated the hash.


public override int GetHashCode()
{
return value1.GetHashCode() ^
value2.GetHashCode() ^
value3.GetHashCode();
}


Turns out this is not the best way to do this. If you look deeper at the case where value1 and value2 are switched, you will see the calculation would come out the same (a hence 2 different sets of values would result in the same hash). This is really caused by the fact that addition and multiplication operations are commutative. You should remove this to remove this failure in the algorithm you are constructing.


public override int GetHashCode()
{
int result = 21;
result = result*47 + value1.GetHashCode();
result = result*47 + value2.GetHashCode();
result = result*47 + value3.GetHashCode();

return result;
}


This will work!

Friday, June 15, 2007

Halo 2 Vista.....we hardly knew ya!

So I have finished Normal level campaign of Halo 2 Vista. This game is definately worth the buy, even for gamers who have played H2 on XBox/360. The graphics are that good! I will continue the fight for Heroic and Legendary acheivements. Btw, I will be getting back to more DirectX centric blogging now. ;)

Wednesday, June 6, 2007

Pac Man World Championships - NYC

Got back a bit ago from the Pac Man World Championships. Had a great time hanging out with the Microsoft crew and the fellow gamers. We went to the Marriott in Times Square after the gaming for a community get together. Interesting pics can be found on http://picasaweb.google.com/caleteeter/PacManWorldChampionships , these were courtesy of John Porcaro from the Gamerscore Blog.