# Need a resource for lightmapping

This topic is 4670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I can't find a very good resource on lightmapping that goes through all the details. I would appreciate if anyone can help me with my problem. Input: I have a list of triangles that I would like applied with a lightmap. Each triangle has a variable sized lightmap that depends on how fast the lighting changes over the triangle's surface. The size of the lightmap does not depend on the dimensions of the triangle. Output: A set of texture coordinates and packed textures for each of the triangles. I have tested my global illumination algorithm in non-realtime by tessalating the geometry sufficiently and using vertex colors but I cannot figure out how to store the results for realtime rendering with lightmaps.

##### Share on other sites
I too havent found any good resources regarding lightmapping, so I thought out my own method. OK, heres how I understood your current situation, so correct me if my assumptions are wrong:

1. You have tesselated your original low-detail mesh (offline) enough (into high-detail mesh) so that the vertices could be regarded as texels, right ?

2. You are using some GI algorithm (e.g. radiosity,raytracing,photon mapping,...) to calculate color values at each vertex.

3. Now you need to transfer those color values from each vertex to a texture map and calculate UV coordinates for each vertex of the original low-detail mesh, right ?

I have done this some time ago with Radiosity. Heres how I did it:

1. I have tesselated each triangle of original low-detail mesh in such a way, that the final high-detail mesh consisted of many physical quads (of constant dimension) that represented texels.

2. Each original low-poly triangle had several rows of these quads that can be viewed as texels inside a texture. First row was equivalent to a triangles hypothenus. Each subsequent row was placed right over the previous row. However, dont do the same mistake as I did the first time when I was placing the first quad (to be more precise, placing its center) of current row at the respective left edge, because the result looked good when in high-polygonal form, but later when you are putting the color values of each quad into texture, you are effectively placing them only in multiplies of 1 texel (which represents the quad width). The problem is, that some rows then get shifted 1 texel left or right and this creates artifacts with blending especially on edges of two different low-poly triangles. Currently dont get your head mixed with this issue. Its easier understood when you see it in real.

3. Start easy, dont try to pack all triangles into the texture ideally (i.e. two different triangles rotated in such a way that they form a rectangle with minimal amount of empty texels). When you have implemented the easy way first, youll be able to implement a more optimal algorithm fast.
So, right now just consider each triangle as a rectangle - thus the problem becomes how to pack rectangles into a texture.

3.1 First of all we must find out and store the dimensions of these rectangles. In my radiosity renderer, I have a data structure that has variables TexelWidth and TexelHeight for each triangle of original low-poly mesh.
TexelWidth is same as amount of quads of the hypothenus and TexelHeight is amount of these rows of quads.

3.2 Define the constant FinalTextureWidth - this is the minimal width that the texture shall have. Its height shall ber calculated later. Make sure that each Rectangle fits there by traversing the arrray, otherwise double the FinalTextureWidth. Set the FinalTextureHeight = 0

3.3 Now there are countless options depending how optimal storage you really want. Well be making several "Rows" of the Rectangles.
For start, just traverse the array and pick the Rectangle with highest TexelHeight. Set its X-Position (in texels) to current cumulated XP-Position of current "Rectangle Row". Y-Position is equivalent to YP-Position of current "Rectangle Row".
Other dimensions are Rectangles TexelWidth and TexelHeight

3.4 When you see that current rectangle is wider than remaining width in current "Rectangle Row" (i.e. it would reach behind FinalTextureWidth), start with new "Rectangle Row" at XP-Position=0 and YP-Position= (YP-Position of last "Rectangle Row" + Height of heighest Rectangle in last "Rectangle Row").
Raise FinalTextureHeight accordingly.
Continue doing so until you have placed all rectangles.

3.5 Only now you know the FinalTextureHeight of whole texture. Therefore you can easily calculate UV coordinates for each vertex of the original low-poly triangle. Traverse the array of all rectangles and just calculate U = (X-Position / FinalTextureWidth) ; V = (X-Position / FinalTextureHeight) per each vertex.

4. This is far from optimal storage, but it gets the job done. For better storage, you could just select the Rectangle based on its total area, not just Height. For even better storage, you could try matching triangles into rectangular area of the texture in such a way, that one (or both) triangle would be mirrorred so that they would form an ideal rectangle with minimum amount of empty texels.

##### Share on other sites
I was talking to Drilian not so long ago about this, and apparently its quite complex to derive your tex coords back for the low-poly mesh. He suggests using UVAtlas which ships with DirectX SDK.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 38
• 16
• ### Forum Statistics

• Total Topics
634128
• Total Posts
3015693
×