# applying normal map

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

## Recommended Posts

I calculate the matrix H in the pic below :

and then I rotate the normal of the fragment by matrix H(matrix H is "rotmat" in the code and P is tempvec3 in the code).
I consider that H is a 3d rotation matrix only rotating the points over z and x axis (because each vector can be a rotation of vector (0,0,1) over z and x axis (well its provable)). and then I calculate H*Normal and I use that as the fragments normal.

	vec3 tempvec3;
mat3 rotmat3;
float axsin,azsin,azcos;
tempvec3=vec3(texture(Normal_Map_Tex_Sampler,gl_TexCoord[0].xy).xyz-vec3(0.5,0.5,0.5))*2;
axsin=sqrt(1-tempvec3.z*tempvec3.z);
if(axsin!=0)
{
azsin=tempvec3.x/axsin;
azcos=-1*tempvec3.y/axsin;
}
else
{
azsin=0;
azcos=1;
}
rotmat3=mat3(azcos,-1*tempvec3.z*azsin,tempvec3.x,azsin,azcos*tempvec3.z,tempvec3.y,0,axsin,tempvec3.z);
normal=normalize(rotmat3*Normal);


what is the problem in my code im not getting the normals completely right.

Edited by IYP

##### Share on other sites

I consider that H is a 3d rotation matrix only rotating the points over z and x axis (because each vector can be a rotation of vector (0,0,1) over z and x axis (well its provable))

Only if for each vector X -that you want 0,0,1 align onto- you find particular rotation matrix- that would be for example

1 0 Xx

0 1 Xy

0 0 Xz

In case your goal is to transform an arbitrary vector (normalize(rotmat3*Normal)) based upon a difference between (0,0,1) vector and sampled vector, then you can spawn a rotation matrix defined by two direction vectors A and B in this way:

C=normalize(cross(A,B));

angle=acos(dot(A,B));

you now can construct quaternion (Cx,Cy,Cz,angle) and convert the quternion into a 3x3 roation matrix, it will rotate everything as A vector rotated to B vector

##### Share on other sites

well thanks (specially with my confusing explanation) JohnnyCode it worked pretty well

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999571
×