Jump to content
  • Advertisement
Sign in to follow this  
WhardieJones

Need a resource for lightmapping

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
I too haven`t found any good resources regarding lightmapping, so I thought out my own method. OK, here`s 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. Here`s 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 triangle`s hypothenus. Each subsequent row was placed right over the previous row. However, don`t 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 don`t get your head mixed with this issue. It`s easier understood when you see it in real.

3. Start easy, don`t 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, you`ll 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. We`ll be making several "Rows" of the Rectangles.
For start, just traverse the array and pick the Rectangle with highest TexelHeight. Set it`s 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 Rectangle`s 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.

This should help you to get started.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!