Monday, February 26, 2007

NeHe Lesson 09: Moving Bitmaps In 3D Space

The 9th lesson covers more ground on moving things around and applying color to black and white textures. The lesson can be read here.

This is the first time in these tutorials where we see a nice little example on how to use the D's foreach loop. It is used to initialize all the stars in the array. It is very clean looking and easy to follow. The only thing to remember in a situation like this, is to use the 'inout' modifier on the star variable so that the changes stick.

foreach (i, inout star ; stars)
{
star.angle = 0.0f;
star.dist = cast(float)i / stars.length * 5.0f;
star.r = rand.next() % 256;
star.g = rand.next() % 256;
star.b = rand.next() % 256;
}

The foreach loop is used in the drawing function also. There too the 'inout' modifier must be used because we update the values in the star at the end.

The rest of the code can be read here.