Skip to content

Bugs in Lesson 6 code, function lookat #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sihan-chen-yes opened this issue Mar 15, 2025 · 1 comment
Open

Bugs in Lesson 6 code, function lookat #154

sihan-chen-yes opened this issue Mar 15, 2025 · 1 comment

Comments

@sihan-chen-yes
Copy link

sihan-chen-yes commented Mar 15, 2025

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@sihan-chen-yes and others