diff --git a/src/main.cc b/src/main.cc index 7fde665..cbc55aa 100644 --- a/src/main.cc +++ b/src/main.cc @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) { const auto &color_image = renderer.render(resolution_width, resolution_height); cv::imshow("MinusRenderer", color_image); - } while (cv::waitKey(25) == -1); + } while (cv::waitKey(40) == -1); cv::destroyWindow("MinusRenderer"); return 0; diff --git a/src/minus_renderer.cc b/src/minus_renderer.cc index 5b4d3fc..be5f7d7 100644 --- a/src/minus_renderer.cc +++ b/src/minus_renderer.cc @@ -2,7 +2,6 @@ // Author: tianlei.richard@qq.com (tianlei.richard) #include -#include #include "minus_renderer.h" #include "rasterizer.h" @@ -36,13 +35,10 @@ TransformMatrix MinusRenderer::squish_tranform() { } TransformMatrix MinusRenderer::orthographic_tranform() { - const auto right = width_ * 0.5; const auto left = -right; const auto top = height_ * 0.5; const auto bottom = -top; - spdlog::info("near: {}, far: {}, top: {}, bottom: {}, left: {}, right: {}", - near_, far_, top, bottom, left, right); TransformMatrix m{{1, 0, 0, -0.5 * (right - left)}, {0, 1, 0, -0.5 * (top - bottom)}, @@ -55,13 +51,13 @@ TransformMatrix MinusRenderer::orthographic_tranform() { {0, 0, 0, 1}} * m; - std::cout << m << '\n'; return m; } -TransformMatrix MinusRenderer::view_port_transform() { - return TransformMatrix{{width_ * 0.5, 0, 0, width_ * 0.5}, - {0, height_ * 0.5, 0, height_ * 0.5}, +TransformMatrix MinusRenderer::view_port_transform(const float width, + const float height) { + return TransformMatrix{{width * 0.5, 0, 0, width * 0.5}, + {0, height * 0.5, 0, height * 0.5}, {0, 0, 1, 0}, {0, 0, 0, 1}}; } @@ -81,7 +77,8 @@ cv::Mat MinusRenderer::render(const int resolution_width, for (auto &t : primitives) { t.projective_transform(squish_tranform()); t.affine_transform(orthographic_tranform()); - t.affine_transform(view_port_transform()); + t.affine_transform( + view_port_transform(resolution_width, resolution_height)); } Rasterizer rasterizer{resolution_width, resolution_height}; diff --git a/src/minus_renderer.h b/src/minus_renderer.h index 50f1459..b1d58ba 100644 --- a/src/minus_renderer.h +++ b/src/minus_renderer.h @@ -20,11 +20,12 @@ public: private: static float calculate_height(const float fov, const float near); static float calculate_width(const float height, const float ratio); + static TransformMatrix view_port_transform(const float width, + const float height); private: TransformMatrix squish_tranform(); TransformMatrix orthographic_tranform(); - TransformMatrix view_port_transform(); private: float near_; diff --git a/src/rasterizer.cc b/src/rasterizer.cc index faaf7c3..091e7a5 100644 --- a/src/rasterizer.cc +++ b/src/rasterizer.cc @@ -1,12 +1,11 @@ // Copyright 2024 Bytedance Inc. All Rights Reserved. // Author: tianlei.richard@qq.com (tianlei.richard) -#include "rasterizer.h" -#include "spdlog/fmt/ostr.h" // github.com/gabime/spdlog/issues/1638 -#include "spdlog/spdlog.h" -#include #include +#include "rasterizer.h" +#include "spdlog/spdlog.h" + Rasterizer::Rasterizer(const int width, const int height) : width_(width), height_(height), pixels_(std::vector>( @@ -19,8 +18,6 @@ std::vector> Rasterizer::rasterize(const std::vector &primitives) { for (const auto &t : primitives) { const auto &triangle_points = t.get_points<3>(); - std::cout << "triangle: " << triangle_points[0] << ", " - << triangle_points[1] << ", " << triangle_points[2] << '\n'; const auto &aabb = t.axis_align_bbox(); const int x_min = std::min(std::max(0, aabb.x), width_); @@ -34,8 +31,6 @@ Rasterizer::rasterize(const std::vector &primitives) { auto &property = pixels_[i][j]; const auto &[is_inside, z_screen] = inside(Point2d{j + 0.5, i + 0.5}, t); - // spdlog::debug("is_inside: {}, current depth: {}, closest depth: {}", - // is_inside, z_screen, property[3]); if (is_inside && z_screen > property[3]) { property[0] = 0; property[1] = 1; @@ -53,9 +48,6 @@ std::pair Rasterizer::inside(const Point2d &p_screen, const auto points = t.get_points<3>(); const auto plane_normal = t.normal_vector(); - // spdlog::debug("normal vector: ({},{},{})", plane_normal.x(), - // plane_normal.y(), - // plane_normal.z()); const auto plane_point = points[0]; const auto z_screen = plane_point.z() - (plane_normal.x() * (p_screen.x() - plane_point.x()) + @@ -75,6 +67,5 @@ std::pair Rasterizer::inside(const Point2d &p_screen, const auto r2 = c1.dot(c3); const auto r3 = c2.dot(c3); - // spdlog::debug("r1: {}, r2: {}, r3: {}", r1, r2, r3); return {r1 > 0 && r2 > 0 && r3 > 0, z_screen}; } diff --git a/src/triangle.cc b/src/triangle.cc index 988c416..d084fd5 100644 --- a/src/triangle.cc +++ b/src/triangle.cc @@ -35,12 +35,8 @@ void Triangle::affine_transform(const TransformMatrix &m) { } void Triangle::projective_transform(const TransformMatrix &m) { - // std::cout << "triangle: " << points_[0] << ", " << points_[1] << ", " - // << points_[2] << '\n'; for (auto &p : points_) { auto p_homo = m * Eigen::Vector4d(p.x(), p.y(), p.z(), 1.); - // std::cout << "triangle: " << p_homo << '\n'; - // spdlog::info("w: {}", p_homo.w()); p = (p_homo / p_homo.w()).head<3>(); } }