Posts Tagged ‘c++’

Generating random terrain

June 26, 2009

I have implemented a random terrain generator, and after some tweaking it, it produces pretty good results.

Randomly generated terrain

Randomly generated terrain

I can seed the generator as I wish, and get the same result every time, or use some other value like the system time to get a new terrain every time.

Another view

Another view

Another view

Another view of the terrain

Also, the artifacts from the previous post have been eliminated.:) The light is working as I want it now.

Light model working

Light model working

The lighting is nice, but it doesn’t do shadows. At least not yet. That will be the next focus for me.

-Knut

Adding light part 2

June 20, 2009

I’ve changed the way the program calculates the light level on the textures, so it should be more realistic. The sun’s position is configurable, and is used to shade one side of a hill, while the other one will be bright.

Better light model

Better light model

The sun would on the left side of this picture, you can see a small valley in the middle with a hill behind it. Notice the difference in light levels between the right and left sides of the valley and hill. This is actually looking very nice. 🙂

From above

From above

Same valley, seen from above. The walls are about the same steepness. However, there are a few problems and limitations. Firstly, the sun’s position can only be specified in the east-west range. This is not much of a problem, and is easily corrected if necessary. Secondly, there are no shadows yet, but I plan to add support for this. And thirdly, there are these artifacts here on the textures:

Artifacts on the terrain

Artifacts on the terrain

See those lines there. They are everywhere. Actually, they appeared when I implemented the texture splatting as well, but then I managed to work around them, for the most part. But now, I have a plan to get rid of them. The problem is, the height map has integer values. So on many slopes the steepness map will be like a stair instead of a smooth slope. To counter this, I think I need to create a random terrain generator.

I’ve been searching around a bit, and I want too implement a fractal generator for the terrain, preferably in a way that allow me to feed it a number to generate a terrain from(seed), or use an random one. Hopefully this will get me smooth slopes as well as interesting terrain, and i hope to complete this as soon as possible.

-Knut

Adding light to the terrain

June 7, 2009

I wanted to add lighting to the terrain engine, but I did not want to use OpenGL’s implementation, instead I wanted to create my own. So I added a light map to the terrain data, and set the program to modify the pixels when creating the textures. Then I added a very simple algorithm to fill it with some interesting data; I just use the height map so that hight pixels will be bright, and low pixels will be dark. The result is better than I hoped for:

With light

With light

Even with this very simple light scheme, the terrain looks a lot better 🙂 It looks even better when moving the camera around. This was a lot easier to add than I expected. Here is another angle:

Another angle

Another angle

But as you can see, the deep valleys are where the simple scheme falls through. The terrain is just too dark.

Dark valley

Dark valley

I need to change the lighting scheme to use normals compared to a artificial sun. I will also add some minimum amount of light, to simulate ambient light. Hopefully this will not be too difficult. 🙂

-Knut