   Correct Lightmapping
  Hi again,I have a question about lightmapping: I'm able to calculate the texture coordinates for the lightmap of a triangle and calculate the intensity at each lume. I'm experiencing the well-known density problem: so I decided to split a polygon into smaller polygons to assure this way an uniform density of lumels across all the surfaces in the game. I don't know how this splitting should be done, but the way I do it prevents me(or maybe I'm just dumb :D) from calculating the TEXTURE coordinates of the polygons "born" from splitting the initial big polygon so that when all these little polygons get busy :) and are rendered together they display exactly as the initial polygon. Also what type of filtering should the lightmaps have and what about texture clamping and such?I'm also interested in learning about other artefacts that a lightmap compiler throws to the Game Programmer in his own struggle to make a decent living :).   
  When I was solving this issue with my own radiosity lightmapper, I decided to split each triangle into real quadratic patches (not smaller triangles) - i.e. as if you took a regular 2D grid and placed it over the triangle so that the hypothenus is aligned with horizontal axis (u - coordinate). Left point of the hypothenus represents a middle of the first patch of the first row. You`re adding more patches one behind another until you find a patch that doesn`t get intersected with the right side (this is very important so that you don`t get artifacts, because even if the right/left side intersects the final patch (in the current row) just a little bit, it`ll be visible when you turn on filtering.Also, the distance between the patches should be constant along all rows or you`ll get the artifacts this way too (i.e. don`t make the mistake of placing the first patch of each row at side of the triangle - start placing them right above the patch of previous row so that all patches have same distance to all others among themselves in right/left/up/down direction or you`ll get terrible artifacts this way because physically, texel can be placed inside lightmap only on multiplies of 1 texel and you are be able to change the UV coordinates with higher accuracy only on triangle vertices - the inside of the triangle gets filtered beyond your control - so if the next row starts in one texel further than it should, you`ll get a whole lot of artifacts - that`s why the patch density must be uniform all around the triangle).Definitely start with Huge texels (patches) (or you`ll be debugging it till the end of the days) so you can easily visually see if each row of patches of given triangle has correct amount of patches (i.e. small numbers like 3,5,6).Also, don`t forget that UV coords for each triangle vertex must be with the precision of 0.5 texel (look into DX docs for more info on this). Make sure, you can easily (with a press of a key) turn on/off lightmap filtering since then the results are different at edges (in terms of which texels are being filtered compared to non-filtered case).You probably can`t program this in a weekend, just few bugs can take you a day or two here. And you won`t find a paper on this (dividing triangles into quadratic patches) anywhere, it`s a just basic trigonometry here.But the final result is definitely worth it.BTW, what algorithm do you use for calculating intensity of each lumel ?   ;  Deliverance:If you are going to follow VladR's method with rectangular patches (which i think is the way to go for making lightmaps), you may find this tutorial usefull. Look at the patch generation algorithm, not how he calculates the lighting (it's just a n.l).Hope that helps a bit.HellRaiZer   ;  Thanks for the info guys.For VladR: I'm not using any complicated algorithm for calculating the intensity to each lumel just the basic stuff.For HellRaiZer: I've already takn that tutorial and it does not solve the density problem, thanks anyway. I wanted to solve first this problem and then attempt radiosity (take them one step at a time).;  Just to make sure we`re talking about same thing here: Regarding density, you have your mesh tesselated as in upper-left part of following picture and want to have it tesselated more (i.e. as in upper-right part of the picture) ?Note that floor and ceiling don`t form a cube and extend below the walls (as can be seen on bottom part of picture), so that`s why the light continuity is broken there (more apparent with the lower tesselation) - but that`s just an art issue(not algorithm).EDIT:  At This Screenshot  you can see how it looks when several triangles are tesselated into quadratic patches.[Edited by - VladR on May 15, 2006 9:48:34 AM];  Yes we're talking about the same thing the only difference from the way I do it is that the patches I subdivide a triangle into are not always quadratic, there are times when a patch is a triangle and this way a rendering the patches does not affect the shape of the initial triangle, it will make it look like it's more tessalated, that's all.   ;Quote:For HellRaiZer: I've already takn that tutorial and it does not solve the density problem, thanks anyway. I wanted to solve first this problem and then attempt radiosity (take them one step at a time).If you tried it and density was the problem, then here is a thought. Instead of specifying the width and height of the produced lightmap, specify the width and height of the patch (usually they'll be equal) and calculate the width and height of the lightmap from this and the edges lengths. Here is the code with the notation from the above tut:// From the tut...edge1.x = Vect1.x - UVVector.x;edge1.y = Vect1.y - UVVector.y;edge1.z = Vect1.z - UVVector.z;edge2.x = Vect2.x - UVVector.x;edge2.y = Vect2.y - UVVector.y;edge2.z = Vect2.z - UVVector.z;// Now calculate the dimensions of the lightmap...int my_width = (int)ceilf(edge1.Length() / patchSizeInWorldUnits);int my_height = (int)ceilf(edge2.Length() / patchSizeInWorldUnits);// Continue as in the tutorial with the calculated dimensions...for(int iX = 0; iX < my_width; iX++){  for(int iY = 0; iY < my_height; iY++)  {// ...  }}Edge1 will always give the width because it's used that way in the loop, and edge2 will always give the height.One thing to note about this tut is that the position of each patch is defined as one of its corners. In case to set it to the center of the patch, you must add 0.5 to iX and iY when calculating ufactor and vfactor.I think this will give you a constant density of patches. Notice that patches may end up being a little bigger or smaller than the specified value due to the rounding when calculating the dimensions of the lightmap.HellRaiZer   ;  No, that wasn't the problem. It's 0.5*size_of_patch over there ;)The problem occurs when I have to calculate the decal texture coordinates for all the small quadriletarls and triangles generated to tessalate the big initial triangle.   