Dav1dde / gl3n

OpenGL Maths for D (not glm for D).
http://dav1dde.github.com/gl3n/
Other
103 stars 49 forks source link

Matrix look_at optimization #4

Closed PhilipWitte closed 12 years ago

PhilipWitte commented 12 years ago

Thought I'd just make a few optimization reports when I find them. Here's the one from Bitbucket:

static auto look_at(Vector!(mt, 3) eye, Vector!(mt, 3) target, Vector!(mt, 3) up) {
    auto look_dir = (target - eye).normalized;
    auto right_dir = cross(look_dir, up).normalized;
    auto perp_up_dir = cross(right_dir, look_dir);

    auto ret = Matrix.identity;

    // rotate
    ret.matrix[0][0..3] = right_dir.vector;
    ret.matrix[1][0..3] = perp_up_dir.vector;
    ret.matrix[2][0..3] = (-look_dir).vector;

    // translate
    ret.matrix[0][3] = eye.dot(-perp_up_dir.vector);
    ret.matrix[1][3] = eye.dot(-right_dir.vector);
    ret.matrix[2][3] = eye.dot(look_dir.vector);

    return ret;
}
Dav1dde commented 12 years ago

Thanks, avoiding the matrix multiplication makes definitly sense. I had to change your code though (it wouldn't compile like that and it's slightly wrong).