25 lines
830 B
C++
25 lines
830 B
C++
|
// Copyright 2024 Bytedance Inc. All Rights Reserved.
|
||
|
// Author: tianlei.richard@qq.com (tianlei.richard)
|
||
|
|
||
|
#include "camera.h"
|
||
|
|
||
|
Camera::Camera(const Vector3d &up)
|
||
|
: position_(Point3d::Identity()), gaze_(Vector3d::Identity()), up_(up) {}
|
||
|
|
||
|
TransformMatrix Camera::get_view_transform() const {
|
||
|
// Move camera to origin
|
||
|
TransformMatrix m{{1, 0, 0, -position_.x()},
|
||
|
{0, 1, 0, -position_.y()},
|
||
|
{0, 0, 1, -position_.z()},
|
||
|
{0, 0, 0, 1}};
|
||
|
// Adjust to fit xyz
|
||
|
const Vector3d &x_camera{(gaze_.cross(up_)).normalized()};
|
||
|
m = TransformMatrix{{x_camera.x(), x_camera.y(), x_camera.z(), 0},
|
||
|
{up_.x(), up_.y(), up_.z(), 0},
|
||
|
{-gaze_.x(), -gaze_.y(), -gaze_.z(), 0},
|
||
|
{0, 0, 0, 1}} *
|
||
|
m;
|
||
|
|
||
|
return m;
|
||
|
}
|