diff --git a/src/camera.cc b/src/camera.cc index 70e4896..532a9e0 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -6,10 +6,10 @@ #include "spdlog/spdlog.h" 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) - : 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(), 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(); } +const Point3d &Camera::get_position() const { return position_; } + 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}}; + TransformMatrix translate{{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}, + TransformMatrix view_mtx = + 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; + translate; - return m; + return view_mtx; } diff --git a/src/camera.h b/src/camera.h index b1ceff9..f5e93a5 100644 --- a/src/camera.h +++ b/src/camera.h @@ -9,6 +9,7 @@ public: explicit Camera(const Vector3d &up, const float camera_speed); public: + const Point3d &get_position() const; TransformMatrix get_view_transform() const; public: diff --git a/src/main.cc b/src/main.cc index 41d913d..7e8ac3d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -111,7 +111,6 @@ int main(int argc, char *argv[]) { MinusRenderer renderer{near, far, fov, aspect_ratio}; renderer.set_meshes( Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}})); - renderer.set_lamps({std::make_shared(Point3d{1, 1, -1}, 6.)}); TransformMatrix translate{ {1., 0., 0., 3.}, {0., 1., 0., 3.}, {0., 0., 1., -5.}, {0., 0., 0., 1.}}; renderer.model_transform(translate); @@ -179,6 +178,8 @@ int main(int argc, char *argv[]) { } camera.set_gaze(Vector3d{0, 0, -1}); renderer.view_transform(camera.get_view_transform()); + renderer.set_lamps( + {std::make_shared(camera.get_position(), 6.)}); const auto &image = renderer.render(resolution_width, resolution_height); diff --git a/src/minus_renderer.cc b/src/minus_renderer.cc index e960375..9a361ac 100644 --- a/src/minus_renderer.cc +++ b/src/minus_renderer.cc @@ -51,23 +51,19 @@ MinusRenderer::construct_transform(const int resolution_width, // Orthographic transform const float height = calculate_height(fov_, near_); 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 = - TransformMatrix{{2 / (right - left), 0, 0, 0}, - {0, 2 / (top - bottom), 0, 0}, + TransformMatrix{{2 / width, 0, 0, 0}, + {0, 2 / height, 0, 0}, {0, 0, 2 / std::fabs(far_ - near_), 0}, {0, 0, 0, 1}} * - TransformMatrix{{1, 0, 0, -0.5 * (right - left)}, - {0, 1, 0, -0.5 * (top - bottom)}, + TransformMatrix{{1, 0, 0, 0}, + {0, 1, 0, 0}, {0, 0, 1, -0.5 * (far_ - near_)}, {0, 0, 0, 1}}; const TransformMatrix &inv_ortho_transform{ - {(right - left) / 2., 0, 0, 0.5 * (right - left)}, - {0, (top - bottom) / 2., 0, 0.5 * (top - bottom)}, + {width / 2., 0, 0, 0}, + {0, height / 2., 0, 0}, {0, 0, std::fabs(far_ - near_) / 2., 0.5 * (far_ - near_)}, {0, 0, 0, 1}}; @@ -184,7 +180,7 @@ cv::Mat MinusRenderer::render(const int resolution_width, auto primitives = (meshes_[mesh_index]).get_primitives(); for (auto &t : primitives) { 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())); } } @@ -203,17 +199,13 @@ cv::Mat MinusRenderer::render(const int resolution_width, pixel_color = cv::Vec3b(0, 0, 0); const auto &fragment = shading_points[y][x]; - if (fragment.triangle.mesh_index >= 0 && - fragment.triangle.mesh_index < meshes_.size()) { + if (fragment.depth <= 1. && fragment.depth >= -1.) { const auto &mesh = meshes_[fragment.triangle.mesh_index]; - if (fragment.depth <= 1. && fragment.depth >= -1.) { - const Point3d &position = - (inv_ss_transform * - (Point3d{x + 0.5, y + 0.5, fragment.depth}.homogeneous())) - .hnormalized(); - - pixel_color = fragment_shader(fragment, position, mesh); - } + const Point3d &position = + (inv_ss_transform * + (Point3d{x + 0.5, y + 0.5, fragment.depth}.homogeneous())) + .hnormalized(); + pixel_color = fragment_shader(fragment, position, mesh); } } } diff --git a/src/phone_material.cc b/src/phone_material.cc index 9447d22..91a8561 100644 --- a/src/phone_material.cc +++ b/src/phone_material.cc @@ -9,7 +9,7 @@ PhoneMaterial::PhoneMaterial( const std::unordered_map> &textures) - : textures_(textures), Ka(0.2), Ks(1.6), + : textures_(textures), Ka(0.1), Ks(1.2), specular_attenuation_factor_(defaultAttenuationFactor) { spdlog::info("Specular attenuation factor: {}", specular_attenuation_factor_); } diff --git a/src/rasterizer.cc b/src/rasterizer.cc index 618d245..661a5ee 100644 --- a/src/rasterizer.cc +++ b/src/rasterizer.cc @@ -19,8 +19,8 @@ void Rasterizer::rasterize(const int mesh_idx, const std::vector &primitives) { const int thread_num = 8; std::vector rasterize_threads; - for (int begin = 0, offset = primitives.size() / thread_num, - remainer = (primitives.size() % thread_num); + for (size_t begin = 0, offset = primitives.size() / thread_num, + remainer = (primitives.size() % thread_num); begin < primitives.size();) { int end = begin + offset; if (remainer > 0) { diff --git a/src/rasterizer.h b/src/rasterizer.h index 2e89f92..b1466d7 100644 --- a/src/rasterizer.h +++ b/src/rasterizer.h @@ -10,8 +10,8 @@ typedef struct RasterizerResult { double depth{-std::numeric_limits::infinity()}; struct { - int32_t mesh_index{-1}; - int64_t triangle_index{-1}; + size_t mesh_index; + size_t triangle_index; } triangle; } RasterizerResult; diff --git a/src/util/math_util.h b/src/util/math_util.h index f52b250..cc80bab 100644 --- a/src/util/math_util.h +++ b/src/util/math_util.h @@ -2,13 +2,6 @@ // Author: tianlei.richard@qq.com (tianlei.richard) #include "common.h" -#include -#include - -template -bool fequal(const FloatType a, const FloatType b) { - return std::fabs(a - b) <= std::numeric_limits::epsilon(); -} template std::vector> @@ -16,8 +9,7 @@ apply_transform(const TransformMatrix &mtx, const std::vector> &points) { std::vector> res{}; for (const auto &p : points) { - const auto &transform_res = mtx * p.homogeneous(); - res.push_back(transform_res.hnormalized()); + res.push_back((mtx * p.homogeneous()).hnormalized()); } return res; } @@ -26,6 +18,6 @@ template void apply_transform_in_place(const TransformMatrix &mtx, std::vector> &points) { for (auto &p : points) { - p = mtx * p.homogeneous().hnormalized(); + p = (mtx * p.homogeneous()).hnormalized(); } }