官术网_书友最值得收藏!

Look at

The view matrix is the inverse of the camera's transformation (the position, rotation, and scale of the camera). Instead of having to create the camera's transform matrix and then invert it, you will be implementing a lookAt function that generates this matrix directly.

A lookAt function typically takes a position, the target point at which the camera is looking and a reference up direction. The rest of the work is finding the inverted basis vectors and figuring out where the position is.

Since the basis vectors are orthonormal, their inverse is the same as their transpose. The position can be calculated by negating the dot product of the position column vector with the inverted basis vectors.

Implement the lookAt function in mat4.cpp. Don't forget to add the function declaration to mat4.h. Remember, the view matrix maps the game world forward to the positive Z axis:

mat4 lookAt(const vec3& position, const vec3& target,

            const vec3& up) {

    vec3 f = normalized(target - position) * -1.0f;

    vec3 r = cross(up, f); // Right handed

    if (r == vec3(0, 0, 0)) {

        return mat4(); // Error

    }

    normalize(r);

    vec3 u = normalized(cross(f, r)); // Right handed

    vec3 t = vec3(

        -dot(r, position),

        -dot(u, position),

        -dot(f, position)

    );

    return mat4(

        // Transpose upper 3x3 matrix to invert it

        r.x, u.x, f.x, 0,

        r.y, u.y, f.y, 0,

        r.z, u.z, f.z, 0,

        t.x, t.y, t.z, 1

    );

}

The lookAt function is the most convenient way of constructing a view matrix. All of the code samples throughout the rest of this book will use the lookAt function to set up a view matrix.

主站蜘蛛池模板: 九江市| 辽宁省| 衡东县| 华安县| 高安市| 丹凤县| 长海县| 邯郸县| 西吉县| 明溪县| 濮阳县| 黄石市| 龙海市| 乌审旗| 阿克苏市| 任丘市| 两当县| 吴川市| 绍兴市| 吴川市| 建昌县| 灵丘县| 龙川县| 牙克石市| 湾仔区| 凤阳县| 精河县| 瓮安县| 即墨市| 肃南| 镇原县| 鸡东县| 炉霍县| 安泽县| 紫云| 新乡市| 西安市| 杭锦后旗| 南昌县| 丹巴县| 云阳县|