Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dot3 Bump Mapping Problem

Started by jeppa Feb 27, 2002 at 2:05 AM 7 replies 400+ views
Original Post
jeppa
jeppa
I'' Have one problem whit dot3 bump:what is the correct way to make the ''Tanget'' of the all vertex of a polygons??? es: Tangent Space Matrix ( for all vertex) N.x N.y N.z ; Simply T.x T.y T.z ; ????? (PROBLEM) B.x B.y B.x ; NxT (Simply) Thanks in Advance
Sorrow
Sorrow
lol.. oh god.. took me like a week to figure out bump mapping. I made the mistake of trying to understand the tangent space math from first principles.

I ended up modelling my code after one of the nvidia tutorials (no, not the bumpmaker code which is uselss.. the other one). To calcluate the T and B you must, basically, do partial derivatives on u and v in relation to x y and z. That''s my understanding of it anyways. It''s a gradient calcluation. That said the function I''m using is doing these gradient calcluations with odd cross products and it''s all a little over my head.

You calcluate the gradients for each face associated with a vertex, then average them, and you end up with both T and B.

I''ll post a copy of the function I''m using when I get home tonight, it''s not like I wrote it myself so I can hardly consider it proprietary

Sorrow
Sorrow
Sorrow
ok... here's the function...
I took it from one of the NVidia tutorials

                

#define UL_EPS 0.000000000001


void calc_gradients(D3DXVECTOR3 * du, D3DXVECTOR3 * dv,
D3DXVECTOR3 * vertex0, float u0, float v0,
D3DXVECTOR3 * vertex1, float u1, float v1,
D3DXVECTOR3 * vertex2, float u2, float v2)

{
D3DXVECTOR3 edge01( vertex1->x - vertex0->x, u1 - u0, v1 - v0 );
D3DXVECTOR3 edge02( vertex2->x - vertex0->x, u2 - u0, v2 - v0 );



D3DXVECTOR3 cp;
D3DXVec3Cross(&cp,&edge01,&edge02);


if ( fabs(cp.x) > UL_EPS )
{
du->x = -cp.y / cp.x;
dv->x = -cp.z / cp.x;
}

edge01 = D3DXVECTOR3( vertex1->y - vertex0->y, u1 - u0, v1 - v0 );
edge02 = D3DXVECTOR3( vertex2->y - vertex0->y, u2 - u0, v2 - v0);

D3DXVec3Cross (&cp,&edge01,&edge02);

if ( fabs(cp.x) > UL_EPS )
{
du->y = -cp.y / cp.x;
dv->y = -cp.z / cp.x;
}


edge01 = D3DXVECTOR3 ( vertex1->z - vertex0->z, u1 - u0, v1 - v0 );
edge02 = D3DXVECTOR3 ( vertex2->z - vertex0->z, u2 - u0, v2 - v0 );


D3DXVec3Cross (&cp, &edge01, &edge02);

if ( fabs(cp.x) > UL_EPS )
{
du->z = -cp.y / cp.x;
dv->z = -cp.z / cp.x;
}
}



vertex 0, 1, and 2 are the three points of a face. The u and v values are the accompanying texture co-ordinates. After the function is complete du and dv contain the gradient vectors for the face. You then average the gradient vectors for every face attached to a vertex, the du becomes your T and the dv becomes your B. The cross product of the two should correspond to your vertex normal vector (tells you you're doing something right).

You said you've got everything else figured out so I guess I'll leave it at that then

Sorrow
Discord -- Work in Progress



Edited by - Sorrow on February 27, 2002 6:27:31 PM
EvilDecl81
EvilDecl81
Of course, you probally want to take care of texture wrapping too, and watch out for orientation changes. Mirrored textures will cause some rather interesting (read: unwanted) things to happen at the mirror point.


EvilDecl81
Sorrow
Sorrow
ick.. never even thought of that... at this point it doesn''t seem to be an issue but I see how that could be ugly. Might show up when I start getting more models to play with. If a poly spanned the texture gap for a wrap it would look ugly.

probably easily corrected though, just have to make sure that .2 - .8 = .4 instead of -6. Bound everything to the 0-1 range.
JD
JD
Here''s tangent space code from microsoft''s dx8.1 website from ''per pixel lighting'' article

NOTE: My own comments are between /*...*/ c-style comments.

/*On left is a basis matrix on right is a negated light vector*/

[U.x U.y U.z] * [-L.x]
[V.x V.y V.z] * [-L.y]
[W.x W.y W.z] * [-L.z]

Expanding out, this becomes

L.x'' = DOT3(U,-L)
L.y'' = DOT3(V,-L)
L.z'' = DOT3(W,-L)

where

/*I think it should read u-tangent, v-binormal, w-normal*/

U = the tangent along the x-axis of the texture;
V = the normal;
W = the binormal U x V (cross product).
Note that one performs the dot product with the negative of the Light direction.

Computing a tangent space vector is just like computing a normal. Remember that w is defined as the normal, so we should already have that piece of information. Now, we need to generate u and v. To generate the tangent space basis vectors (u and v), use the following equation:

Vec1 = Vertex3 – Vertex2
Vec2 = Vertex1 – Vertex2
DeltaU1 = Vertex3.u - Vertex2.u
DeltaU2 = Vertex1.u - Vertex2.u
DirectionV = |DeltaU2*Vec1-DeltaU1*Vec2|
DirectionU = |DirectionV x Vertex.N|
DirectionW = |DirectionU x DirectionV|

JD
JD
It was late when I posted the above but I looked at that tangent space code closer and it''s quite clever. They''re taking advantage of the edge vectors which are always orthogonal to the surface''s normal then they scale one or both vectors by the DeltaU value only and subtract one from the other yielding a 3D vector in object space that they call V. Then they cross V with surface Normal to get U. Notice that their V vector is not in anyway parallel to the texture and its orientation in texture space. Meaning if you rotate the texture on the poly the rotation will not be accounted for. I think nvidia''s code takes the texture rotation into account although I have yet to work out their math on paper and convince myself that this is happening.
JD
JD
I''ve tried nvidia''s tangent space algo along side with ms algo and both do the same thing. I''ve also worked both algos on a paper and both do not account for texture rotations. The graphical results from both of these look exactly the same. Also, the shared vertices with their averaged base would throw off the texture rotation from each wall since the u&v in texture space are averaged from other polys so one can''t account for rotation. I think I will use the one from ms since it''s computationally inexpensive plus I can understand better what''s happening just from looking at the equations. Again, my concern was that the surface normals could be rotated as one rotates the normal map. I''ll just not worry about this since the bumps look good enough even rotated. Enough of my babble

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.