Jump to content
• Advertisement

# applying normal map

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

If you intended to correct an error in the post then please contact us.

## 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.

my fragment shaders code:

	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.

thanks in advance

Edited by IYP

#### Share this post

##### Share on other sites
Advertisement

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 this post

##### Share on other sites

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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013450
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!