Generate lightmap for triangle mesh

I wish to generate lightmap for triangle mesh
,but cant find tutorial on it.

The only detailed tutorial(with source code) i’ve founded on the Internet is this one (
but it a quads mesh not triangle

Can I use the method present in this tutorial to generate lightmap for triangle mesh ?

All I need to know is how to transform texel coordinate in to world space.

can some one provide me more info on this.just the basic is fine

Thank in advance and I have to apologize for my bad english


I think this may be useful:

float3 TexcoordToPos(const float3 &v1, const float3 &v2, const float3 &v3,
                     const float2 &t1, const float2 &t2, const float2 &t3,
                     const float2 &p)
 float i;
 float s;
 float t;
 float3 r;
 i = 1 / ((t2.x - t1.x) * (t3.y - t1.y) - (t2.y - t1.y) * (t3.x - t1.x));
 s = i * ( (t3.y - t1.y) * (p.x - t1.x) - (t3.x - t1.x) * (p.y - t1.y));
 t = i * (-(t2.y - t1.y) * (p.x - t1.x) + (t2.x - t1.x) * (p.y - t1.y));
 r.x = v1.x + s * (v2.x - v1.x) + t * (v3.x - v1.x);
 r.y = v1.y + s * (v2.y - v1.y) + t * (v3.y - v1.y);
 r.z = v1.z + s * (v2.z - v1.z) + t * (v3.z - v1.z);
 return r;

v1, v2, v3 are the triangle world space vertices
t1, t2, t3 are the triangle texcoords
p is the pixel coordinate on the texture in [0-1] range

This code converts a coordinate (in texcoord space) anywhere on a triangle to a world space texel position. It assumes [0,0] lies at the bottom-left of the lightmap texture. You can use the same function to calculate the normal (by passing the vertex normals instead of vertices).

I have used this in a hardware accelerated gi/radiosity (non-realtime) lightmap renderer, loosly based on this excelent article:


Thank for your answer , But I also have another

The only way i can single-passed render the scene with lightmaps combine with base textures, using vertex array or VBO is to create all lightmaps coresponding to base textures right?

I dont think I can use glBineTexture for every triangle when render with vertext array,if i create lightmaps per tringle

this is my rendering code


void StaticModel::renderMesh(){


	unsigned int i;

		if(vaGroupList[i].isUseMaterial == true){
			glMaterialfv( GL_FRONT, GL_AMBIENT, vaGroupList[i].ambient );
            glMaterialfv( GL_FRONT, GL_DIFFUSE, vaGroupList[i].diffuse );
            glMaterialfv( GL_FRONT, GL_SPECULAR, vaGroupList[i].specular );
            glMaterialfv( GL_FRONT, GL_EMISSION, vaGroupList[i].emissive );
            glMaterialf( GL_FRONT, GL_SHININESS, vaGroupList[i].shininess );
			if(vaGroupList[i].isusingTexture == true){

		if(vaGroupList[i].isUseMaterial == true){
			if(vaGroupList[i].isusingTexture == true){



Thank in advance


What kind of triangle mesh is this? Are we talking entire world/level or just an airplane or something?

By the way, you can combine several lightmaps into a single texture.

You should try to pack the textures into a larger one. This can be a bit tricky though. Look for something called ‘bin-packing’.

Here’s a good tutorial that includes some pseudo-code:


You should also keep in mind that you’ll need some padding to avoid bleeding between triangle edge pixels.
Also, try to clamp the texture coordinates to the center of pixels. If the lightmaps are mipmapped, keep in mind that it may still cause some bleeding in some conditions, you can increase padding to counter that.

Good luck!

Now I’ve succeed implemented my first lightmap.It’s a triangle-based texture and not very fast (i have to render it using the old glVertex3f call).

Thank for your answer remdel & caveman,I will try packing lightmap in to a large texture as you two suggested