Skip to content

Instantly share code, notes, and snippets.

@showa-yojyo
Created November 16, 2021 13:53
Show Gist options
  • Save showa-yojyo/6180f3a9083f8209a5f131d0761d7e17 to your computer and use it in GitHub Desktop.
Save showa-yojyo/6180f3a9083f8209a5f131d0761d7e17 to your computer and use it in GitHub Desktop.
A research of glMatrix mat4.lookAt and mat4.targetTo implementation
// lookAt:
if(eye == center){
return I;
}
// F := center - eye だから
// -F -> -f の違いがある
z = normalize(eye - center);
// s = f * UP ここでは符号一致
x = up * z; // cross product
if (x.length() == 0){
x = zero();
}
else{
x = normalize(x);
}
// u = s * f ここでも符号一致
y = z * x; // cross product
if(y.length() == 0){
y = zero();
}
else{
y = normalize(y);
}
// z と f の符号は反転しているから求める行列は:
// x[0] x[1] x[2] -dot(x, eye)
// y[0] y[1] y[2] -dot(y, eye)
// z[0] z[1] z[2] -dot(z, eye)
// 0 0 0 1
// targetTo:
z = normalize(eye - target);
x = normalize(up * z); // cross product
y = normalize(z * x);
// result:
// x[0] y[0] z[0] eye[0]
// x[1] y[1] z[1] eye[1]
// x[2] y[2] z[2] eye[2]
// 0 0 0 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment