// Copyright 2024 SquareBlock Inc. All Rights Reserved. // Author: tianlei.richard@qq.com (tianlei.richard) #include #include "spdlog/spdlog.h" #include #include "minus_renderer.h" #include "rasterizer.h" MinusRenderer::MinusRenderer(float near, float far, float fov, float aspect_ratio) : near_(near), far_(far), fov_(fov), aspect_ratio_(aspect_ratio), projection_matrix_(orthographic_transform() * squish_transform()) { spdlog::info("near: {}, far: {}, fov: {}, aspect ratio: {}", near_, far_, fov_, aspect_ratio_); } float MinusRenderer::calculate_height(const float fov, const float near) { return std::fabs(near) * std::tan(fov * 0.5) * 2; } float MinusRenderer::calculate_width(const float height, const float ratio) { return height * ratio; } TransformMatrix MinusRenderer::squish_transform() { // Frustum to Cuboid return TransformMatrix{{near_, 0, 0, 0}, {0, near_, 0, 0}, {0, 0, near_ + far_, -near_ * far_}, {0, 0, 1, 0}}; } TransformMatrix MinusRenderer::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; TransformMatrix m{{1, 0, 0, -0.5 * (right - left)}, {0, 1, 0, -0.5 * (top - bottom)}, {0, 0, 1, -0.5 * (far_ - near_)}, {0, 0, 0, 1}}; m = TransformMatrix{{2 / (right - left), 0, 0, 0}, {0, 2 / (top - bottom), 0, 0}, {0, 0, 2 / std::fabs(far_ - near_), 0}, {0, 0, 0, 1}} * m; return m; } 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}}; } std::tuple MinusRenderer::calculate_barycentric_coordinate(const Triangle &t, const Point2d &p) { const auto &points = t.get_vertex_position(); const auto &A = (points[0]).head(2); const auto &B = (points[1]).head(2); const auto &C = (points[2]).head(2); double alpha = (-(p.x() - B.x()) * (C.y() - B.y()) + (p.y() - B.y()) * (C.x() - B.x())) / (-(A.x() - B.x()) * (C.y() - B.y()) + (A.y() - B.y()) * (C.x() - B.x())); double beta = (-(p.x() - C.x()) * (A.y() - C.y()) + (p.y() - C.y()) * (A.x() - C.x())) / (-(B.x() - C.x()) * (A.y() - C.y()) + (B.y() - C.y()) * (A.x() - C.x())); double gamma = 1. - alpha - beta; return {alpha, beta, gamma}; } void MinusRenderer::model_transform(const TransformMatrix &mtx) { for (auto &m : meshes_) { for (auto &t : m.get_primitives()) { t.set_points(apply_transform(mtx, t.get_vertex_position())); } } } void MinusRenderer::view_transform(const TransformMatrix &mtx) { for (auto &m : meshes_) { for (auto &t : m.get_primitives()) { t.set_points(apply_transform(mtx, t.get_vertex_position())); } } } cv::Mat MinusRenderer::render(const int resolution_width, const int resolution_height) { std::vector meshes = meshes_; Rasterizer rasterizer{resolution_width, resolution_height}; for (auto &m : meshes) { auto &primitives = m.get_primitives(); for (int i = 0; i < primitives.size(); ++i) { auto &t = primitives[i]; const auto &triangle_vertices = t.get_vertex_position(); auto tmp = apply_transform(projection_matrix_, triangle_vertices); t.set_points(apply_transform( view_port_transform(resolution_width, resolution_height), tmp)); } } const auto &shading_points = rasterizer.rasterize(meshes); assert(!shading_points.empty()); cv::Mat color_image(shading_points.size(), (shading_points[0]).size(), CV_8UC3); for (int y = 0; y < shading_points.size(); ++y) { for (int x = 0; x < shading_points[y].size(); ++x) { auto &pixel_color = color_image.at(shading_points.size() - y - 1, x); pixel_color = cv::Vec3b(0, 0, 0); const auto &p = shading_points[y][x]; if (p.triangle.mesh_index >= 0 && p.triangle.mesh_index < meshes.size()) { const auto &mesh = meshes[p.triangle.mesh_index]; const auto &primitives = mesh.get_primitives(); if (p.triangle.triangle_index >= 0 && p.triangle.triangle_index < primitives.size()) { const auto &triangle = primitives[p.triangle.triangle_index]; const auto &[alpha, beta, gamma] = calculate_barycentric_coordinate( triangle, Point2d{x + 0.5, y + 0.5}); const auto &triangle_indices = triangle.get_vertex_index(); std::vector triangle_uv; for (int j = 0; j < triangle_indices.size(); ++j) { const auto &uv = mesh.get_texture_coordinate(triangle_indices[j]); triangle_uv.push_back(Point3d{uv.x(), uv.y(), 1}); } auto projective_uv = apply_transform(projection_matrix_, triangle_uv); // TODO(tianlei): 推导一下为什么需要除以下面这个 auto w_reciprocal = alpha * (projective_uv[0].z()) + beta * (projective_uv[1].z()) + gamma * (2 * projective_uv[2].z()); Point2d pixel_uv{ (alpha * projective_uv[0].x() + beta * projective_uv[1].x() + gamma * projective_uv[2].x()) / w_reciprocal, (alpha * projective_uv[0].y() + beta * projective_uv[1].y() + gamma * projective_uv[2].y()) / w_reciprocal}; auto &material = mesh.get_material(); pixel_color = material->sample_texture(0, pixel_uv); } } } } return color_image; } void MinusRenderer::load_mesh(const std::string &file_path) { meshes_ = Mesh::load_mesh(file_path); spdlog::info("Mesh size: {}, the primitives size of the first mesh: {}", meshes_.size(), meshes_.front().get_primitives().size()); }