Sign in to follow this  
IYP

applying normal map

Recommended Posts

I calculate the matrix H in the pic below :

zvtmhg.jpg
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


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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this