# lightmapping problem...

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

## Recommended Posts

when the triangles are not axis-aligned, they are black when rendered... I tried to use the direct3d lighting and theres no problem, so the triangle normals are ok.. original code from http://members.net-tech.com.au/alaneb/lightmapping_tutorial.html please see if theres any problem with my code thanks!!
inline void lighttriangle(MeshTriangle* tri,wchar_t* file)
{
//put tri normal to trinorm2
D3DXVECTOR3 trinorm2;
{
D3DXVECTOR3 trinorm1;
trinorm1.x=(tri->vertices[0].nx/3+tri->vertices[1].nx/3+tri->vertices[2].nx/3);
trinorm1.y=(tri->vertices[0].ny/3+tri->vertices[1].ny/3+tri->vertices[2].ny/3);
trinorm1.z=(tri->vertices[0].nz/3+tri->vertices[1].nz/3+tri->vertices[2].nz/3);
D3DXVec3Normalize(&trinorm2,&trinorm1);

}
//project tri's 3d coords to uv

int flag=0;
{
if (fabs(trinorm2.x) > fabs(trinorm2.y) &&
fabs(trinorm2.x) > fabs(trinorm2.z))
{
flag = 1;
tri->vertices[0].u2 = tri->vertices[0].y;
tri->vertices[0].v2 = tri->vertices[0].z;

tri->vertices[1].u2 = tri->vertices[1].y;
tri->vertices[1].v2 = tri->vertices[1].z;

tri->vertices[2].u2 = tri->vertices[2].y;
tri->vertices[2].v2 = tri->vertices[2].z;
}
else if (fabs(trinorm2.y) > fabs(trinorm2.x) &&
fabs(trinorm2.y) > fabs(trinorm2.z))
{
flag = 2;
tri->vertices[0].u2 = tri->vertices[0].x;
tri->vertices[0].v2 = tri->vertices[0].z;

tri->vertices[1].u2 = tri->vertices[1].x;
tri->vertices[1].v2 = tri->vertices[1].z;

tri->vertices[2].u2 = tri->vertices[2].x;
tri->vertices[2].v2 = tri->vertices[2].z;
}
else
{
flag = 3;
tri->vertices[0].u2 = tri->vertices[0].x;
tri->vertices[0].v2 = tri->vertices[0].y;

tri->vertices[1].u2 = tri->vertices[1].x;
tri->vertices[1].v2 = tri->vertices[1].y;

tri->vertices[2].u2 = tri->vertices[2].x;
tri->vertices[2].v2 = tri->vertices[2].y;
}
}

//scale tri's uv
float minu,minv,maxu,maxv;
{
minu = tri->vertices[0].u2;
minv = tri->vertices[0].v2;
maxu = tri->vertices[0].u2;
maxv = tri->vertices[0].v2;

for (int i = 0; i < 3; i++)
{
if (tri->vertices.u2 < minu )
minu = tri->vertices.u2;
if (tri->vertices.v2 < minv )
minv= tri->vertices.v2;
if (tri->vertices.u2 > maxu )
maxu = tri->vertices.u2;
if (tri->vertices.v2 > maxv )
maxv = tri->vertices.v2;
}

float du = maxu - minu;
float dv= maxv - minv;

for (int i = 0; i < 3; i++)
{
tri->vertices.u2 -= minu;
tri->vertices.v2 -= minv;
tri->vertices.u2 /= du;
tri->vertices.v2 /= dv;
}
}

//light tri
{
D3DXVECTOR3 newedge1,newedge2,lumel,lightvector1,lightvector2;
SBitmap lightmap=Bitmap_Init(LIGHTMAPWIDTH,LIGHTMAPHEIGHT);
//-(Ax + By +Cz) =D
float D=-(trinorm2.x*tri->vertices[0].x+
trinorm2.y*tri->vertices[0].y+
trinorm2.z*tri->vertices[0].z);

for(float iX = 0; iX < LIGHTMAPWIDTH; iX++)
{
for(float iY = 0; iY < LIGHTMAPHEIGHT; iY++)
{
float ufactor = (iX / LIGHTMAPWIDTH);
float vfactor = (iY / LIGHTMAPHEIGHT);
//1 yz
//2 xz
//3 xy

if(flag==1)
{
//(-(By+ Cz + D))/A=x
lumel.y=(maxu-minu)*ufactor+minu;
lumel.z=(maxv-minv)*vfactor+minv;
lumel.x=(-(trinorm2.y*lumel.y+ trinorm2.z*lumel.z + D))/trinorm2.x;

}
else if(flag==2)
{
//(-(By+ Cz + D))/A=x
lumel.x=(maxu-minu)*ufactor+minu;
lumel.z=(maxv-minv)*vfactor+minv;
lumel.y=(-(trinorm2.x*lumel.x+ trinorm2.z*lumel.z + D))/trinorm2.y;

}
else if(flag==3)
{
//(-(By+ Cz + D))/A=x
lumel.x=(maxu-minu)*ufactor+minu;
lumel.y=(maxv-minv)*vfactor+minv;
lumel.z=(-(trinorm2.x*lumel.x+ trinorm2.y*lumel.y + D))/trinorm2.z;

}

float combinedred = 0.0;
float combinedgreen = 0.0;
float combinedblue = 0.0;
for (int i = 0; i < numlights; i++)
{
lightvector1.x =lights.x - lumel.x;
lightvector1.y = lights.y - lumel.y;
lightvector1.z = lights.z - lumel.z;
D3DXVec3Normalize(&lightvector2,&lightvector1);
float cosAngle =
//iY/LIGHTMAPHEIGHT;
D3DXVec3Dot(&trinorm2,&lightvector2);
//1-sqrt((lightvector1.x)*(lightvector1.x)+(lightvector1.y)*(lightvector1.y)+(lightvector1.z)*(lightvector1.z))/1000;

combinedred +=((float)lights.r)*  cosAngle;
combinedgreen +=((float)lights.g) *  cosAngle;
combinedblue += ((float)lights.b) *  cosAngle;

}

if (combinedred > 255.0)
combinedred = 255.0;
if (combinedgreen > 255.0)
combinedgreen = 255.0;
if (combinedblue > 255.0)
combinedblue = 255.0;

if (combinedred < 0)
combinedred = 0;
if (combinedgreen < 0)
combinedgreen = 0;
if (combinedblue < 0)
combinedblue = 0;

BitmapDrawPixel(lightmap,(int)iX,(int)iY,(int)combinedred,(int)combinedgreen,(int)combinedblue);
}
}
Bitmap_Write(lightmap,file);
Bitmap_Kill(lightmap);
}
}

##### Share on other sites
I don't even know if this problem is related but once I implimented normal interpolation via barycentric coordinates. basicaly this is
PT = aV1 + bV2 + cV3;

these a, b, c are then used as the weights for the normals when interpolating for phong like shading, I assume you're doing something like this in some way. when I first implimentd this I turned it into a matrix equation and inverted to find a, b, c for a specific pt. then used these a, b, c to calculate N = a*N1 + b*N2 + c*N3. a b and c are related to s, t just longhand verson. the problem with this approach is singularites in the solution when the triangles are access aligned, basically some of these coefficents are zero when they're not supposed to be. I changed my implimentation to the normal one with the area interpretation of barycentric coordinates. this cleared up any singularities in the matrix method. the problem is when interpreting this as just a regular linear equation. PT = M*[a,b,c] is that it is asking a much more general question. and will not work when there are infinate solutions, which I observed happen when the triangles are aligned to one of the axis planes. you basically have infinite solutions and really only want the one such that a + b + c = 1. I don't know if it's the same problem but I kinda expect it though I didn't read through your code. if you're interpolating triangles purely algebraicly and not taking into account the geomoretic interpretation. (namely the interpretation that the areas of the 3 sub triangles that you form by drawing a lines from PT to V1, V2 and V3 are probortional to a, b, and c) then you're going to run into certian angles where the scheme produces infinite solutions and give you incorrect result.

##### Share on other sites
I just found out what the problem is, the texture manager loaded wrong textures for the lightmaps because of this:
inline unsigned int __fastcall hashstring(wchar_t* file){	int p=0;	unsigned int h=0;	while(file!=0)	{		h+=(p+1)*file;		p++;	}	return h;}

The texturemanager used it to avoid duplicated texture loadings and it didn't work well, anyone have a better hashing algorithm? Or does Direct3D already do that for me?

thanks

##### Share on other sites
umm, hasing-functions doesn't result in automatic texture management. My guess is that your "hash-table-collision-manager" isn't working corectly.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 24
• 42
• 75