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 17:21:53 +08:00
|
|
|
#include "minus_renderer.h"
|
2024-03-01 21:31:47 +08:00
|
|
|
#include "rasterizer.h"
|
2024-03-06 17:21:53 +08:00
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
#include <cmath>
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
MinusRenderer::MinusRenderer(float near, float far, float fov,
|
|
|
|
float aspect_ratio,
|
|
|
|
std::vector<Triangle> &&primitives)
|
|
|
|
: near_(near), far_(far), fov_(fov), aspect_ratio_(aspect_ratio),
|
|
|
|
height_(calculate_height(fov, near)),
|
|
|
|
width_(calculate_width(height_, aspect_ratio)), primitives_(primitives) {}
|
2024-03-01 21:31:47 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
int MinusRenderer::calculate_height(const float fov, const float near) {
|
|
|
|
return std::fabs(near) * std::tan(fov * 0.5) * 2;
|
|
|
|
}
|
2024-03-01 21:31:47 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
int MinusRenderer::calculate_width(const float height, const float ratio) {
|
|
|
|
return height * ratio;
|
|
|
|
}
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
void MinusRenderer::model_transform(const Triangle::AffineTransformType &m) {
|
|
|
|
for (auto &t : primitives_) {
|
|
|
|
t.affine_transform(m);
|
|
|
|
}
|
|
|
|
}
|
2024-03-01 16:00:08 +08:00
|
|
|
|
2024-03-06 17:21:53 +08:00
|
|
|
cv::Mat MinusRenderer::render(const int resolution_width,
|
|
|
|
const int resolution_height) {
|
|
|
|
Rasterizer rasterizer{resolution_width, resolution_height};
|
|
|
|
const auto &pixels = rasterizer.rasterize(primitives_);
|
|
|
|
assert(!pixels.empty());
|
|
|
|
cv::Mat color_image(pixels.size(), (pixels[0]).size(), CV_8UC3);
|
|
|
|
|
|
|
|
for (int i = 0; i < pixels.size(); ++i) {
|
|
|
|
const auto &row = pixels[i];
|
|
|
|
for (int j = 0; j < row.size(); ++j) {
|
|
|
|
auto &pixel_color = color_image.at<cv::Vec3b>(pixels.size() - i - 1, j);
|
|
|
|
pixel_color =
|
|
|
|
cv::Vec3b{static_cast<unsigned char>(
|
|
|
|
row[j].x() * std::numeric_limits<char>::max()),
|
|
|
|
static_cast<unsigned char>(
|
|
|
|
row[j].y() * std::numeric_limits<char>::max()),
|
|
|
|
static_cast<unsigned char>(
|
|
|
|
row[j].z() * std::numeric_limits<char>::max())};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return color_image;
|
2024-03-01 16:00:08 +08:00
|
|
|
}
|