Transparency

One thing I’d like to mention first: A lot of people tell me that they read my page. Please, if you do, write something in the comments! I always get the impression that nobody is reading what I write anyway.

If someone came and asked you what of the folllowing was the most difficult to implement in computer games:

you’d either ignore the person or guess that this is a trick question and choose the last point. Which is exactly like it is. I really don’t want to write a lengthy article about why this is the case, but I guess I have to. If you already know why, the normal post continues after the screenshot.

One of the main problems of computer graphics is that the polygons will be drawn on screen in the same order as they get fed into the GPU, which is usually wrong. The problem is that at least one polygon will be drawn on top of one that is supposed to be in front and everything will go FUBAR. There are two solutions to this: Either draw the polygons from back to fron or use the Depth Buffer.

This buffer works simple and is easy to use. For every pixel, it stores how close it is to the camera. When a new pixel wants to be drawn at the same location, its distance to the camera is compared to the one already in the buffer. If it is closer, it will be drawn and the depth buffer updated, if not, it will be discarded. Cool, isn’t it?

As soon as transparencies happen, the whole thing gets bad. After all, it’s completely legal to draw a pixel behind another one that is completely transparent. So a complex solution has to be found, which is:

  1. All transparent polygons are collected and drawn only at the end of each frame.
  2. The depth-buffer will be set to read only, so that transparent objects behind solid ones will disappear but not the other way around.
  3. The individual polygons are drawn back to front.

This means that you have to access the individual transparent polygons and give them some sort of special treatment.

Collision meshes for trees, but no tree anywhere

I ran into this problem when implementing trees. To have good trees, I drove into the local mountains here and took pictures of some. When I brought these in, using the traditional method of having to pictures of a tree forming a cross with the rest of square completely transparent, I ran into the problems mentioned above. So I implemented all the features - and it didn’t work, as you can see in the screenshot above.

It would be very boring to list all the problems I encountered and errors in my code I found (some quite embarrasing, I need to sleep more), but there were plenty. Anyhow, in the end I got it working:

The trees in order, but the wrong way around

…well, nearly. They were drawn the wrong way around. I had to change exactly one single character to get this right, and I ended up with this:

The trees in the right order - but where has the ground gone?

Oh, I nearly forgot to mention: I had to disable the solid terrain, because for some reason, the game will always crash. I’ll post it here if anything changes.

Written on November 22nd, 2005 at 05:21 pm

0 Comments

    New comments can no longer be posted because it got to annoying to fight all the spam.