-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Open
Description
https://github.com/ssloy/tinyrenderer/blob/f037c7a0517a632c7391b35131f9746a8f8bb235/our_gl.cpp
void lookat(Vec3f eye, Vec3f center, Vec3f up) {
Vec3f z = (eye-center).normalize();
Vec3f x = cross(up,z).normalize();
Vec3f y = cross(z,x).normalize();
ModelView = Matrix::identity();
for (int i=0; i<3; i++) {
ModelView[0][i] = x[i];
ModelView[1][i] = y[i];
ModelView[2][i] = z[i];
ModelView[i][3] = -center[i];
}
}
From the formula we derived, ModelView transformation matrix should be (R.T|-R.T x T) = (M_inv|0) x (Identity|-Tr).
But here the translation is not combined with rotation part.
Should be like this:
void lookat(Vec3f eye, Vec3f center, Vec3f up) {
// find camera local basis to build rotation matrix
Vec3f z = (eye - center).normalize();
// up and z share the same plane
Vec3f x = (up ^ z).normalize();
// up and y not necessarily aligned
Vec3f y = (z ^ x).normalize();
// build transformation matrix
// M_inv = M.T
// R|T inv ==> R.T|-R.T T
Matrix M_inv = Matrix::identity(4);
Matrix Tr = Matrix::identity(4);
for (int i = 0; i < 3; ++i) {
M_inv[0][i] = x[i];
M_inv[1][i] = y[i];
M_inv[2][i] = z[i];
Tr[i][3] = -center[i];
}
ModelView = M_inv * Tr;
}
Metadata
Metadata
Assignees
Labels
No labels