2024-03-01 21:31:47 +08:00
|
|
|
// Copyright 2024 Bytedance Inc. All Rights Reserved.
|
|
|
|
// Author: tianlei.richard@qq.com (tianlei.richard)
|
|
|
|
|
2024-03-06 22:23:48 +08:00
|
|
|
#include <cmath>
|
|
|
|
|
2024-03-08 11:54:37 +08:00
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
#include "minus_renderer.h"
|
2024-03-01 21:31:47 +08:00
|
|
|
#include "rasterizer.h"
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
MinusRenderer::MinusRenderer(float near, float far, float fov,
|
2024-03-08 11:54:37 +08:00
|
|
|
float aspect_ratio)
|
2024-03-06 17:21:53 +08:00
|
|
|
: near_(near), far_(far), fov_(fov), aspect_ratio_(aspect_ratio),
|
2024-03-21 21:34:29 +08:00
|
|
|
projection_matrix_(orthographic_transform() * squish_transform()) {
|
2024-03-08 11:54:37 +08:00
|
|
|
spdlog::info("near: {}, far: {}, fov: {}, aspect ratio: {}", near_, far_,
|
|
|
|
fov_, aspect_ratio_);
|
2024-03-06 22:23:48 +08:00
|
|
|
}
|
2024-03-01 21:31:47 +08:00
|
|
|
|
2024-03-06 22:23:48 +08:00
|
|
|
float MinusRenderer::calculate_height(const float fov, const float near) {
|
2024-03-06 17:21:53 +08:00
|
|
|
return std::fabs(near) * std::tan(fov * 0.5) * 2;
|
|
|
|
}
|
2024-03-01 21:31:47 +08:00
|
|
|
|
2024-03-06 22:23:48 +08:00
|
|
|
float MinusRenderer::calculate_width(const float height, const float ratio) {
|
2024-03-06 17:21:53 +08:00
|
|
|
return height * ratio;
|
|
|
|
}
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-21 21:34:29 +08:00
|
|
|
TransformMatrix MinusRenderer::squish_transform() {
|
2024-03-06 22:23:48 +08:00
|
|
|
// Frustum to Cuboid
|
|
|
|
return TransformMatrix{{near_, 0, 0, 0},
|
|
|
|
{0, near_, 0, 0},
|
|
|
|
{0, 0, near_ + far_, -near_ * far_},
|
|
|
|
{0, 0, 1, 0}};
|
|
|
|
}
|
|
|
|
|
2024-03-21 21:34:29 +08:00
|
|
|
TransformMatrix MinusRenderer::orthographic_transform() {
|
2024-03-07 14:29:24 +08:00
|
|
|
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;
|
2024-03-06 22:23:48 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-07 11:11:04 +08:00
|
|
|
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},
|
2024-03-06 22:23:48 +08:00
|
|
|
{0, 0, 1, 0},
|
|
|
|
{0, 0, 0, 1}};
|
|
|
|
}
|
|
|
|
|
2024-03-21 21:34:29 +08:00
|
|
|
Point3d 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 Point3d{alpha, beta, gamma};
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:54:37 +08:00
|
|
|
void MinusRenderer::model_transform(const TransformMatrix &mtx) {
|
|
|
|
for (auto &m : meshes_) {
|
2024-03-21 21:34:29 +08:00
|
|
|
for (auto &t : m.get_primitives()) {
|
|
|
|
t.set_points(apply_transform(mtx, t.get_vertex_position()));
|
2024-03-08 11:54:37 +08:00
|
|
|
}
|
2024-03-06 17:21:53 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-08 11:54:37 +08:00
|
|
|
void MinusRenderer::view_transform(const TransformMatrix &mtx) {
|
|
|
|
for (auto &m : meshes_) {
|
2024-03-21 21:34:29 +08:00
|
|
|
for (auto &t : m.get_primitives()) {
|
|
|
|
t.set_points(apply_transform(mtx, t.get_vertex_position()));
|
2024-03-08 11:54:37 +08:00
|
|
|
}
|
2024-03-07 14:29:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
cv::Mat MinusRenderer::render(const int resolution_width,
|
|
|
|
const int resolution_height) {
|
2024-03-08 11:54:37 +08:00
|
|
|
std::vector<Mesh> meshes = meshes_;
|
2024-03-06 17:21:53 +08:00
|
|
|
Rasterizer rasterizer{resolution_width, resolution_height};
|
2024-03-21 21:34:29 +08:00
|
|
|
for (auto &m : meshes) {
|
|
|
|
auto &primitives = m.get_primitives();
|
2024-03-08 11:54:37 +08:00
|
|
|
for (int i = 0; i < primitives.size(); ++i) {
|
|
|
|
auto &t = primitives[i];
|
2024-03-21 21:34:29 +08:00
|
|
|
const auto &triangle_vertices = t.get_vertex_position();
|
|
|
|
auto tmp = apply_transform(projection_matrix_, triangle_vertices);
|
|
|
|
|
|
|
|
const auto &triangle_indices = t.get_vertex_index();
|
|
|
|
std::vector<Point3d> triangle_uv;
|
|
|
|
for (int j = 0; j < triangle_indices.size(); ++j) {
|
|
|
|
const auto &uv = m.get_texture_coordinate(triangle_indices[j]);
|
|
|
|
triangle_uv.push_back(Point3d{uv.x(), uv.y(), 1});
|
|
|
|
}
|
|
|
|
apply_transform(projection_matrix_, triangle_uv);
|
|
|
|
|
|
|
|
t.set_points(apply_transform(
|
|
|
|
view_port_transform(resolution_width, resolution_height), tmp));
|
2024-03-08 11:54:37 +08:00
|
|
|
}
|
|
|
|
rasterizer.rasterize(primitives);
|
|
|
|
}
|
2024-03-21 21:34:29 +08:00
|
|
|
const auto &shading_points = rasterizer.get_picture();
|
|
|
|
assert(!shading_points.empty());
|
|
|
|
cv::Mat depth_image(shading_points.size(), (shading_points[0]).size(),
|
|
|
|
CV_8UC1);
|
2024-03-06 17:21:53 +08:00
|
|
|
|
2024-03-21 21:34:29 +08:00
|
|
|
for (int i = 0; i < shading_points.size(); ++i) {
|
|
|
|
const auto &row = shading_points[i];
|
2024-03-06 17:21:53 +08:00
|
|
|
for (int j = 0; j < row.size(); ++j) {
|
2024-03-08 11:54:37 +08:00
|
|
|
auto &pixel_depth =
|
2024-03-21 21:34:29 +08:00
|
|
|
depth_image.at<unsigned char>(shading_points.size() - i - 1, j);
|
|
|
|
if (std::isinf(std::fabs(row[j].depth))) {
|
2024-03-08 11:54:37 +08:00
|
|
|
pixel_depth = 0;
|
|
|
|
} else {
|
|
|
|
const float k =
|
|
|
|
static_cast<float>(std::numeric_limits<unsigned char>::max()) * 0.5;
|
|
|
|
const float b = k;
|
2024-03-21 21:34:29 +08:00
|
|
|
pixel_depth = static_cast<unsigned char>(k * row[j].depth + b);
|
2024-03-08 11:54:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return depth_image;
|
|
|
|
}
|
|
|
|
|
2024-03-21 21:34:29 +08:00
|
|
|
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());
|
2024-03-01 16:00:08 +08:00
|
|
|
}
|