some tiny fixes.
This commit is contained in:
parent
a30c41dc46
commit
104370fb40
|
@ -6,10 +6,10 @@
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
Camera::Camera(const Vector3d &up)
|
Camera::Camera(const Vector3d &up)
|
||||||
: gaze_(Vector3d::Identity()), up_(up), camera_speed_(0.3) {}
|
: gaze_(Vector3d{0, 0, -1}), up_(up), camera_speed_(0.3) {}
|
||||||
|
|
||||||
Camera::Camera(const Vector3d &up, const float camera_speed)
|
Camera::Camera(const Vector3d &up, const float camera_speed)
|
||||||
: gaze_(Vector3d{0, 0, 1}), up_(up), camera_speed_(camera_speed) {
|
: gaze_(Vector3d{0, 0, -1}), up_(up), camera_speed_(camera_speed) {
|
||||||
spdlog::debug("Gaze: ({}, {}, {}), Up: ({}, {}, {})", gaze_.x(), gaze_.y(),
|
spdlog::debug("Gaze: ({}, {}, {}), Up: ({}, {}, {})", gaze_.x(), gaze_.y(),
|
||||||
gaze_.z(), up_.x(), up_.y(), up_.z());
|
gaze_.z(), up_.x(), up_.y(), up_.z());
|
||||||
}
|
}
|
||||||
|
@ -20,19 +20,22 @@ void Camera::move(const Point3d &offset) {
|
||||||
|
|
||||||
void Camera::set_gaze(const Vector3d &gaze) { gaze_ = gaze.normalized(); }
|
void Camera::set_gaze(const Vector3d &gaze) { gaze_ = gaze.normalized(); }
|
||||||
|
|
||||||
|
const Point3d &Camera::get_position() const { return position_; }
|
||||||
|
|
||||||
TransformMatrix Camera::get_view_transform() const {
|
TransformMatrix Camera::get_view_transform() const {
|
||||||
// Move camera to origin
|
// Move camera to origin
|
||||||
TransformMatrix m{{1, 0, 0, -position_.x()},
|
TransformMatrix translate{{1, 0, 0, -position_.x()},
|
||||||
{0, 1, 0, -position_.y()},
|
{0, 1, 0, -position_.y()},
|
||||||
{0, 0, 1, -position_.z()},
|
{0, 0, 1, -position_.z()},
|
||||||
{0, 0, 0, 1}};
|
{0, 0, 0, 1}};
|
||||||
// Adjust to fit xyz
|
// Adjust to fit xyz
|
||||||
const Vector3d &x_camera{(gaze_.cross(up_)).normalized()};
|
const Vector3d &x_camera{(gaze_.cross(up_)).normalized()};
|
||||||
m = TransformMatrix{{x_camera.x(), x_camera.y(), x_camera.z(), 0},
|
TransformMatrix view_mtx =
|
||||||
|
TransformMatrix{{x_camera.x(), x_camera.y(), x_camera.z(), 0},
|
||||||
{up_.x(), up_.y(), up_.z(), 0},
|
{up_.x(), up_.y(), up_.z(), 0},
|
||||||
{-gaze_.x(), -gaze_.y(), -gaze_.z(), 0},
|
{-gaze_.x(), -gaze_.y(), -gaze_.z(), 0},
|
||||||
{0, 0, 0, 1}} *
|
{0, 0, 0, 1}} *
|
||||||
m;
|
translate;
|
||||||
|
|
||||||
return m;
|
return view_mtx;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ public:
|
||||||
explicit Camera(const Vector3d &up, const float camera_speed);
|
explicit Camera(const Vector3d &up, const float camera_speed);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
const Point3d &get_position() const;
|
||||||
TransformMatrix get_view_transform() const;
|
TransformMatrix get_view_transform() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -111,7 +111,6 @@ int main(int argc, char *argv[]) {
|
||||||
MinusRenderer renderer{near, far, fov, aspect_ratio};
|
MinusRenderer renderer{near, far, fov, aspect_ratio};
|
||||||
renderer.set_meshes(
|
renderer.set_meshes(
|
||||||
Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}}));
|
Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}}));
|
||||||
renderer.set_lamps({std::make_shared<PointLamp>(Point3d{1, 1, -1}, 6.)});
|
|
||||||
TransformMatrix translate{
|
TransformMatrix translate{
|
||||||
{1., 0., 0., 3.}, {0., 1., 0., 3.}, {0., 0., 1., -5.}, {0., 0., 0., 1.}};
|
{1., 0., 0., 3.}, {0., 1., 0., 3.}, {0., 0., 1., -5.}, {0., 0., 0., 1.}};
|
||||||
renderer.model_transform(translate);
|
renderer.model_transform(translate);
|
||||||
|
@ -179,6 +178,8 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
camera.set_gaze(Vector3d{0, 0, -1});
|
camera.set_gaze(Vector3d{0, 0, -1});
|
||||||
renderer.view_transform(camera.get_view_transform());
|
renderer.view_transform(camera.get_view_transform());
|
||||||
|
renderer.set_lamps(
|
||||||
|
{std::make_shared<PointLamp>(camera.get_position(), 6.)});
|
||||||
|
|
||||||
const auto &image = renderer.render(resolution_width, resolution_height);
|
const auto &image = renderer.render(resolution_width, resolution_height);
|
||||||
|
|
||||||
|
|
|
@ -51,23 +51,19 @@ MinusRenderer::construct_transform(const int resolution_width,
|
||||||
// Orthographic transform
|
// Orthographic transform
|
||||||
const float height = calculate_height(fov_, near_);
|
const float height = calculate_height(fov_, near_);
|
||||||
const float width = calculate_width(height, aspect_ratio_);
|
const float width = calculate_width(height, aspect_ratio_);
|
||||||
const float right = width * 0.5;
|
|
||||||
const float left = -right;
|
|
||||||
const float top = height * 0.5;
|
|
||||||
const float bottom = -top;
|
|
||||||
|
|
||||||
const TransformMatrix &ortho_transform =
|
const TransformMatrix &ortho_transform =
|
||||||
TransformMatrix{{2 / (right - left), 0, 0, 0},
|
TransformMatrix{{2 / width, 0, 0, 0},
|
||||||
{0, 2 / (top - bottom), 0, 0},
|
{0, 2 / height, 0, 0},
|
||||||
{0, 0, 2 / std::fabs(far_ - near_), 0},
|
{0, 0, 2 / std::fabs(far_ - near_), 0},
|
||||||
{0, 0, 0, 1}} *
|
{0, 0, 0, 1}} *
|
||||||
TransformMatrix{{1, 0, 0, -0.5 * (right - left)},
|
TransformMatrix{{1, 0, 0, 0},
|
||||||
{0, 1, 0, -0.5 * (top - bottom)},
|
{0, 1, 0, 0},
|
||||||
{0, 0, 1, -0.5 * (far_ - near_)},
|
{0, 0, 1, -0.5 * (far_ - near_)},
|
||||||
{0, 0, 0, 1}};
|
{0, 0, 0, 1}};
|
||||||
const TransformMatrix &inv_ortho_transform{
|
const TransformMatrix &inv_ortho_transform{
|
||||||
{(right - left) / 2., 0, 0, 0.5 * (right - left)},
|
{width / 2., 0, 0, 0},
|
||||||
{0, (top - bottom) / 2., 0, 0.5 * (top - bottom)},
|
{0, height / 2., 0, 0},
|
||||||
{0, 0, std::fabs(far_ - near_) / 2., 0.5 * (far_ - near_)},
|
{0, 0, std::fabs(far_ - near_) / 2., 0.5 * (far_ - near_)},
|
||||||
{0, 0, 0, 1}};
|
{0, 0, 0, 1}};
|
||||||
|
|
||||||
|
@ -184,7 +180,7 @@ cv::Mat MinusRenderer::render(const int resolution_width,
|
||||||
auto primitives = (meshes_[mesh_index]).get_primitives();
|
auto primitives = (meshes_[mesh_index]).get_primitives();
|
||||||
for (auto &t : primitives) {
|
for (auto &t : primitives) {
|
||||||
const auto [z_min, z_max] = t.get_depth_range();
|
const auto [z_min, z_max] = t.get_depth_range();
|
||||||
if (z_max >= far_ && z_min <= near_) {
|
if (z_max >= far_ && z_min <= near_) { // because the camera looks at -z
|
||||||
t.set_points(apply_transform(ss_transform, t.get_vertex_position()));
|
t.set_points(apply_transform(ss_transform, t.get_vertex_position()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -203,17 +199,13 @@ cv::Mat MinusRenderer::render(const int resolution_width,
|
||||||
pixel_color = cv::Vec3b(0, 0, 0);
|
pixel_color = cv::Vec3b(0, 0, 0);
|
||||||
|
|
||||||
const auto &fragment = shading_points[y][x];
|
const auto &fragment = shading_points[y][x];
|
||||||
if (fragment.triangle.mesh_index >= 0 &&
|
if (fragment.depth <= 1. && fragment.depth >= -1.) {
|
||||||
fragment.triangle.mesh_index < meshes_.size()) {
|
|
||||||
const auto &mesh = meshes_[fragment.triangle.mesh_index];
|
const auto &mesh = meshes_[fragment.triangle.mesh_index];
|
||||||
if (fragment.depth <= 1. && fragment.depth >= -1.) {
|
const Point3d &position =
|
||||||
const Point3d &position =
|
(inv_ss_transform *
|
||||||
(inv_ss_transform *
|
(Point3d{x + 0.5, y + 0.5, fragment.depth}.homogeneous()))
|
||||||
(Point3d{x + 0.5, y + 0.5, fragment.depth}.homogeneous()))
|
.hnormalized();
|
||||||
.hnormalized();
|
pixel_color = fragment_shader(fragment, position, mesh);
|
||||||
|
|
||||||
pixel_color = fragment_shader(fragment, position, mesh);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
PhoneMaterial::PhoneMaterial(
|
PhoneMaterial::PhoneMaterial(
|
||||||
const std::unordered_map<Texture::TextureType, std::shared_ptr<Texture>>
|
const std::unordered_map<Texture::TextureType, std::shared_ptr<Texture>>
|
||||||
&textures)
|
&textures)
|
||||||
: textures_(textures), Ka(0.2), Ks(1.6),
|
: textures_(textures), Ka(0.1), Ks(1.2),
|
||||||
specular_attenuation_factor_(defaultAttenuationFactor) {
|
specular_attenuation_factor_(defaultAttenuationFactor) {
|
||||||
spdlog::info("Specular attenuation factor: {}", specular_attenuation_factor_);
|
spdlog::info("Specular attenuation factor: {}", specular_attenuation_factor_);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,8 @@ void Rasterizer::rasterize(const int mesh_idx,
|
||||||
const std::vector<Triangle> &primitives) {
|
const std::vector<Triangle> &primitives) {
|
||||||
const int thread_num = 8;
|
const int thread_num = 8;
|
||||||
std::vector<std::thread> rasterize_threads;
|
std::vector<std::thread> rasterize_threads;
|
||||||
for (int begin = 0, offset = primitives.size() / thread_num,
|
for (size_t begin = 0, offset = primitives.size() / thread_num,
|
||||||
remainer = (primitives.size() % thread_num);
|
remainer = (primitives.size() % thread_num);
|
||||||
begin < primitives.size();) {
|
begin < primitives.size();) {
|
||||||
int end = begin + offset;
|
int end = begin + offset;
|
||||||
if (remainer > 0) {
|
if (remainer > 0) {
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
typedef struct RasterizerResult {
|
typedef struct RasterizerResult {
|
||||||
double depth{-std::numeric_limits<double>::infinity()};
|
double depth{-std::numeric_limits<double>::infinity()};
|
||||||
struct {
|
struct {
|
||||||
int32_t mesh_index{-1};
|
size_t mesh_index;
|
||||||
int64_t triangle_index{-1};
|
size_t triangle_index;
|
||||||
} triangle;
|
} triangle;
|
||||||
} RasterizerResult;
|
} RasterizerResult;
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,6 @@
|
||||||
// Author: tianlei.richard@qq.com (tianlei.richard)
|
// Author: tianlei.richard@qq.com (tianlei.richard)
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <cmath>
|
|
||||||
#include <numeric>
|
|
||||||
|
|
||||||
template <typename FloatType>
|
|
||||||
bool fequal(const FloatType a, const FloatType b) {
|
|
||||||
return std::fabs(a - b) <= std::numeric_limits<FloatType>::epsilon();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <int d>
|
template <int d>
|
||||||
std::vector<Eigen::Vector<double, d>>
|
std::vector<Eigen::Vector<double, d>>
|
||||||
|
@ -16,8 +9,7 @@ apply_transform(const TransformMatrix &mtx,
|
||||||
const std::vector<Eigen::Vector<double, d>> &points) {
|
const std::vector<Eigen::Vector<double, d>> &points) {
|
||||||
std::vector<Eigen::Vector<double, d>> res{};
|
std::vector<Eigen::Vector<double, d>> res{};
|
||||||
for (const auto &p : points) {
|
for (const auto &p : points) {
|
||||||
const auto &transform_res = mtx * p.homogeneous();
|
res.push_back((mtx * p.homogeneous()).hnormalized());
|
||||||
res.push_back(transform_res.hnormalized());
|
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -26,6 +18,6 @@ template <int d>
|
||||||
void apply_transform_in_place(const TransformMatrix &mtx,
|
void apply_transform_in_place(const TransformMatrix &mtx,
|
||||||
std::vector<Eigen::Vector<double, d>> &points) {
|
std::vector<Eigen::Vector<double, d>> &points) {
|
||||||
for (auto &p : points) {
|
for (auto &p : points) {
|
||||||
p = mtx * p.homogeneous().hnormalized();
|
p = (mtx * p.homogeneous()).hnormalized();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue