# Incorrect bump mapping problem (solved)

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

## Recommended Posts

Hi, (hey, first post :D) I'm trying to add bump mapping to my engine, and it basically works, but it looks like light position is displaced from its place. This gets worse when I add the attenuation, as you can see that the brightest spot is not in the middle. Here's a screenshot without attenuation: Clicky The white sphere is the light and, as you can see, the light is displaced. I once saw a thread regarding this same topic, but after some hours searching i couldn't find it. The code I'm using to calculate the light vector is this:
	Light& light = lighting->GetLight(i);
Vector3D pos = *light.GetPosition();

for(unsigned int j = 0; j<numTriangles; j++)
{
EXENGINE_TRIANGLE& tri = triangles[j];
for(int k = 0; k<3; k++)
{
Vector3D lightVector = Vector3D(tri.v[k]->x, tri.v[k]->y, tri.v[k]->z) - pos;
lightVector.Normalize();

// S = sTangent.DotProduct(lightVector);
tri.v[k]->s = lightVector.Dot_P(Vector3D(tri.v[k]->sTangent[0], tri.v[k]->sTangent[1], tri.v[k]->sTangent[2]));

// T = tTangent.DotProduct(lightVector);
tri.v[k]->t = lightVector.Dot_P(Vector3D(tri.v[k]->tTangent[0], tri.v[k]->tTangent[1], tri.v[k]->tTangent[2]));

// R = normal.DotProduct(lightVector);
tri.v[k]->r = lightVector.Dot_P(Vector3D(tri.v[k]->normal[0], tri.v[k]->normal[1], tri.v[k]->normal[2]));
}
}


And this is the code that generates the tangents:
		Vector3D normal = GetNormal(*tri->v[0], *tri->v[1], *tri->v[2]);

Vector3D side0(tri->v[0]->x - tri->v[1]->x, tri->v[0]->y - tri->v[1]->y, tri->v[0]->z - tri->v[1]->z);
Vector3D side1(tri->v[2]->x - tri->v[1]->x, tri->v[2]->y - tri->v[1]->y, tri->v[2]->z - tri->v[1]->z);

// Calculate S tangent
float deltaS0 = tri->v[0]->tv - tri->v[1]->tv;
float deltaS1 = tri->v[2]->tv - tri->v[1]->tv;
Vector3D sTan = side0*deltaS1 - side1*deltaS0;
sTan.Normalize();

// Calculate T tangent
float deltaT0 = tri->v[0]->tu - tri->v[1]->tu;
float deltaT1 = tri->v[2]->tu - tri->v[1]->tu;
Vector3D tTan = side0*deltaT1 - side1*deltaT0;
tTan.Normalize();

// Make sure their cross product faces the same direction as the normal
Vector3D tangentCross = sTan.Cross_P(tTan);
if(tangentCross.Dot_P(normal) < 0.0f)
{
sTan = -sTan;
tTan = -tTan;
}

// ... add them to the vertex's tangents and normal
// ... later all the vectors are normalized


I'm sorry to ask for help in such a way, but I've ran out of ideas. [Edited by - Gaenor on August 1, 2005 12:10:22 PM]

##### Share on other sites
Actually, this should be in Graphics Programming and Theory...

##### Share on other sites
Any ideas anyone?

I've also made a small program where you can see its behaviour in motion.
Remember to remove the .jpg

Press the buttons 1-7 to change the movement. What am I doing wrong? :(

##### Share on other sites
Removing

 lightVector.Normalize();
happened to fix it.

Thanks to anyone who thought about it.

• 32
• 15
• 23
• 10
• 19