Tuesday, August 10, 2010

Basic Terrain Rendering

Its been a while am working on some rendering stuff using DirectX, but never actually thought about putting everything together. Obviously as we all know, things start to crumble once they come together in Game Programming if not integrated properly. This led me to start writing a decent framework for myself where in I can bring together all the small small things I learn or willing to learn. And it's not that easy!

The Quest to pull the stuff together began & it's teaching me hell lot of stuff than I actually thought. I started using STL more efficiently, I started thinking more on a Code design which i never did before, I started thinking of providing facility to easily expand whats in there already, though I have not used any of the predefined Design Patters while doing this but am sure, sooner or later the time will come where I may need to dig deep into those as well.

Right now, My DirectX framework takes care of Creating Window, Using High Precision clock timer to provide TICK for the engine, Calculating FPS, Providing mechanism to Log down everything to help in debugging in case something goes wrong, D3D initialization. Apart from these, it also provides support for Keyboard & Mouse as standard input devices using Direct Input. Got mechanism to load & render X files using STL where a Manager class manages efficient adding or deletion of Static Geometries in the scene at runtime. Static Geometry class handles basic rendering per object which is controlled by Manager class. Every static geometry once created just need to be handed over to Manager class which takes care of setting up proper render states, actual rendering & updation of each Static Geometry object.

Right now, it also features Free to move Camera Object to see whats happening down there. Few days back I decided to add support for Terrain Rendering using Heightmap method wherein grayscale image is used to generate Terrain mesh at runtime.
The basic idea is pretty straight forward, based on the Image size ( power of 2 ), number of vertices are created & later on based on the each pixel value in the range of 0-255, appropriate height value is calculated for each vertex.

Later on, UV coordinates are calculated for texturing using cell spacing. Cell spacing is customizable parameter along with Height Scale. Using proper combination of Cell Spacing & HeightScale desired terrain can be achived using same heightmap file. 

Image above shows the HeightMap file generated using any Image Processing software, which later used by code to generate the Terrain as shown in the image below & few above as well.

Here are few more screenshots but with different HeightMap file :)

Basic one time lighting for terrain was done using existing vertex positions, no extra normal information was used in the vertex structure. Standard NdotL math was used to precompute shade of each Quad in the terrain. Later on, Image used for texturing is accessed & shade parameter for each the pixel is re-written in the image which is used for texturing.

There is one more way of texturing a terrain! Since, we already have heights of each vertices, we can color the terrain based on the height. In this, first we create a Blank Texture & then write pixel data based on the height & attaching specific color value for that particular height at that pixel in the texture. Just like we did while writing light information in the terrain texture, we need to lock the surface, enter data at each pixel based on height & then save the texture for rendering. Light information technique can even be applied in this method to add increase the realness of overall scene. Here is the output image :

Ofcourse, the level of smoothness solely depends on how many precalculated levels of color values exist in the system. More color variations, more is the realism :)

No comments: