Created
June 28, 2012 11:36
-
-
Save zulman/3010816 to your computer and use it in GitHub Desktop.
LooToRotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Matrix4x4::BuildLookAtMatrix(float posx,float posy,float posz,float eyex,float eyey, float eyez,float upx,float upy, float upz) | |
{ | |
float x[3], y[3], z[3]; | |
float mag; | |
z[0] = eyex - posx; | |
z[1] = eyey - posy; | |
z[2] = eyez - posz; | |
mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] ); | |
if (mag>.0001) | |
{ | |
mag = 1/mag; | |
z[0] *= mag; | |
z[1] *= mag; | |
z[2] *= mag; | |
} | |
y[0] = upx; | |
y[1] = upy; | |
y[2] = upz; | |
x[0] = y[1]*z[2] - y[2]*z[1]; | |
x[1] = -y[0]*z[2] + y[2]*z[0]; | |
x[2] = y[0]*z[1] - y[1]*z[0]; | |
y[0] = z[1]*x[2] - z[2]*x[1]; | |
y[1] = -z[0]*x[2] + z[2]*x[0]; | |
y[2] = z[0]*x[1] - z[1]*x[0]; | |
mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] ); | |
if (mag > .0001) | |
{ | |
mag = 1/mag; | |
x[0] *= mag; | |
x[1] *= mag; | |
x[2] *= mag; | |
} | |
mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] ); | |
if (mag > .0001) | |
{ | |
mag = 1/mag; | |
y[0] *= mag; | |
y[1] *= mag; | |
y[2] *= mag; | |
} | |
#define M(row,col) mat[row][col] | |
M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = -x[0]*eyex + -x[1]*eyey + -x[2]*eyez; | |
M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = -y[0]*eyex + -y[1]*eyey + -y[2]*eyez; | |
M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = -z[0]*eyex + -z[1]*eyey + -z[2]*eyez; | |
M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0; | |
#undef M | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment