Fast Computation of Terrain Shadow Maps
IntroductionThe topic of terrain rendering is a very big one. This paper will focus on the lighting and shadowing aspects of terrain rendering. In the pictures below you can see the effect of this technique in action.
Algorithm descriptionThe algorithm is quite simple in fact. For every grid point, we check if the ray from the light position to the point intersects the map. This is done very quickly by checking only the points that are under the projection of the vector L, as shown in Figure 1. Let us consider the following notations:
The points P are generated using a 2D line algorithm from the working point (A) to the projection of the sun position point (B). Then, for every point P, you check to see if the height map value for point P is bigger than the y value of point X(P). If it turns out to be bigger, then we know that the ray L intersects the map, and the illumination at point A will be equal to the 'ambient_color_value', and we can safely move to the next working point. If all the P-type points have been tested and no intersection was found, then the illumination at point A is computed using the following formula: Illum(A) = ambient_color_value + (L dot N) clamped to the range [0,1]. Algorithm implementationIf the above description wasn't clear, the code should help. The most important function of this algorithm's implementation is the function intersect_map. This function checks to see if the working point is occluded, by testing to see if the light direction ray intersects the map. This function is called for every point in the height map. When an intersection point is found, the testing for the current working point stops, and another working point from the lightdir projection is fetched to be tested (the gray points from the picture at the right). As I said in the algorithm description, if the working point isn't occluded, its illumination value is computed using the formula shown above. The function genLightmap handles the light map generation. It tests and illuminates every point on the height map. Note: If you plan to copy & paste this code into your own program you should know that the normals are compressed from 1 float per component, to 1 byte per component. (see the note at the end of the paper) int intersect_map(const vector3& iv,const ray& r,Image* hm,float fHeightScale){ int w,hits; float d,h,D; vector3 v,dir; v = iv + r.direction; w = hm->w; hits = 0; while (!(( v.x >= w-1 ) || ( v.x <= 0 ) || ( v.z >= w-1 ) || ( v.z <= 0 ))){ // length of lightdir's projection D = Magnitude(vector3(v.x,0,v.z)-vector3(r.origin.x,0,r.origin.z)); d = Magnitude(iv-v); // light direction h = iv.y + (d*r.origin.y) / D; // X(P) point // check if height in point P is bigger than point X's height if (hm->data[ifloor(v.z)* w + ifloor(v.x)] * fHeightScale > h){ hits++; // if so, mark as hit, and skip this work point. break; }; dir = r.direction; dir.y = 0; v += Normalize(dir); // fetch new working point }; return hits; }; Image* genLightmap(char* normal,Image* hm,vector3 fSunDir,int w,float fAmbient){ int i,j,hits; float f,dot; vector3 n,fVertex; Image* lmap; ray r; float fHeightScale = 10.0f / 255.0f; lmap = new Image(w,w,1); if (!lmap){printf("(!) Error: cannot alloc lightmap!\n");return 0;}; for (j=0; j After you have successfully created your light maps, you will probably want to use them. I will tell you two ways to add them to your own terrain engine: one is to use them as light maps, and the other is to set the color of each point in the terrain according to its corresponding color in the shadow map.
LightmappingIf you intend to use the shadow map as a light map, you should consider the use of a supplementary texture unit. In any case, if you decide to use this technique, you should set the texture environment mode to modulate. In OpenGL, this is done in the following manner : glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); Color componentWhen using the shadow map pixels as color components for the terrain geometry, you should consider using separate buffers for each vertex component. By doing this, you will be able to send the data returned by genLightmap directly to the renderer, by setting the color array pointer to the address of the returned data. In OpenGL, this is done with the glColorPointer function. NoteFor any questions or suggestions, feel free to e-mail me at nervus@go.ro The full sources for this article can be found at http://nervus.go.ro/ in the Downloads section. The sources are free, you can do whatever you want with them, but if you include them in your project, or use this algorithm for any commercial or non-commercial use, please give me credit. BibliographyGlobal Illumination Compendium
Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|