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.

6 comments:

Mason said...

Where can I find a good star.bmp?

OdeFu said...

Don't know about good, but the original image can be downloaded from here.

Mason said...

Great, thanks a lot.

I also appreciate you porting all the lessons to D, and with Tango!!!! Tango is definitely the way to go. Hopefully it will leave the beta stage soon....

Your lessons are a big help!

Have you taken a look at the ArcLib library project over at dsource.org? Do you think it's better to stick with SDL and openGL, or use a library?

OdeFu said...

Well, these are not my lessons. I'm just writing the code in D and putting them up for others. But thanks just the same. :)

I haven't really looked at the ArcLib, but as far as I know it's 2D only. So if that is what you need and want to do games then I suggest using a library like ArcLib. If you want to dig deeper then it would be a good idea to write one yourself.

matthew said...

I've compiled & run all the examples that you've created up to this one.

But when I try to compile it, I get 6 warnings/errors which refer to 'function tango.math.Random.Random.next is deprecated'.

Any ideas what to do?

OdeFu said...

It means that I have to update the code to the latest Tango. :)

Deprecation doesn't mean that the code wont work, but that it might be removed in a later release.

If you want to remove the warning you can change the Random object to tango.math.random.Kiss. See here: Kiss