bugfix: fix view port transform matrix error, it's for resolution not the projection space.
This commit is contained in:
parent
da941c0fd0
commit
502a48ebdf
|
@ -40,7 +40,7 @@ int main(int argc, char *argv[]) {
|
||||||
const auto &color_image =
|
const auto &color_image =
|
||||||
renderer.render(resolution_width, resolution_height);
|
renderer.render(resolution_width, resolution_height);
|
||||||
cv::imshow("MinusRenderer", color_image);
|
cv::imshow("MinusRenderer", color_image);
|
||||||
} while (cv::waitKey(25) == -1);
|
} while (cv::waitKey(40) == -1);
|
||||||
cv::destroyWindow("MinusRenderer");
|
cv::destroyWindow("MinusRenderer");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
// Author: tianlei.richard@qq.com (tianlei.richard)
|
// Author: tianlei.richard@qq.com (tianlei.richard)
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "minus_renderer.h"
|
#include "minus_renderer.h"
|
||||||
#include "rasterizer.h"
|
#include "rasterizer.h"
|
||||||
|
@ -36,13 +35,10 @@ TransformMatrix MinusRenderer::squish_tranform() {
|
||||||
}
|
}
|
||||||
|
|
||||||
TransformMatrix MinusRenderer::orthographic_tranform() {
|
TransformMatrix MinusRenderer::orthographic_tranform() {
|
||||||
|
|
||||||
const auto right = width_ * 0.5;
|
const auto right = width_ * 0.5;
|
||||||
const auto left = -right;
|
const auto left = -right;
|
||||||
const auto top = height_ * 0.5;
|
const auto top = height_ * 0.5;
|
||||||
const auto bottom = -top;
|
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)},
|
TransformMatrix m{{1, 0, 0, -0.5 * (right - left)},
|
||||||
{0, 1, 0, -0.5 * (top - bottom)},
|
{0, 1, 0, -0.5 * (top - bottom)},
|
||||||
|
@ -55,13 +51,13 @@ TransformMatrix MinusRenderer::orthographic_tranform() {
|
||||||
{0, 0, 0, 1}} *
|
{0, 0, 0, 1}} *
|
||||||
m;
|
m;
|
||||||
|
|
||||||
std::cout << m << '\n';
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
TransformMatrix MinusRenderer::view_port_transform() {
|
TransformMatrix MinusRenderer::view_port_transform(const float width,
|
||||||
return TransformMatrix{{width_ * 0.5, 0, 0, width_ * 0.5},
|
const float height) {
|
||||||
{0, height_ * 0.5, 0, height_ * 0.5},
|
return TransformMatrix{{width * 0.5, 0, 0, width * 0.5},
|
||||||
|
{0, height * 0.5, 0, height * 0.5},
|
||||||
{0, 0, 1, 0},
|
{0, 0, 1, 0},
|
||||||
{0, 0, 0, 1}};
|
{0, 0, 0, 1}};
|
||||||
}
|
}
|
||||||
|
@ -81,7 +77,8 @@ cv::Mat MinusRenderer::render(const int resolution_width,
|
||||||
for (auto &t : primitives) {
|
for (auto &t : primitives) {
|
||||||
t.projective_transform(squish_tranform());
|
t.projective_transform(squish_tranform());
|
||||||
t.affine_transform(orthographic_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};
|
Rasterizer rasterizer{resolution_width, resolution_height};
|
||||||
|
|
|
@ -20,11 +20,12 @@ public:
|
||||||
private:
|
private:
|
||||||
static float calculate_height(const float fov, const float near);
|
static float calculate_height(const float fov, const float near);
|
||||||
static float calculate_width(const float height, const float ratio);
|
static float calculate_width(const float height, const float ratio);
|
||||||
|
static TransformMatrix view_port_transform(const float width,
|
||||||
|
const float height);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TransformMatrix squish_tranform();
|
TransformMatrix squish_tranform();
|
||||||
TransformMatrix orthographic_tranform();
|
TransformMatrix orthographic_tranform();
|
||||||
TransformMatrix view_port_transform();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float near_;
|
float near_;
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
// Copyright 2024 Bytedance Inc. All Rights Reserved.
|
// Copyright 2024 Bytedance Inc. All Rights Reserved.
|
||||||
// Author: tianlei.richard@qq.com (tianlei.richard)
|
// 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 <iostream>
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
#include "rasterizer.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
Rasterizer::Rasterizer(const int width, const int height)
|
Rasterizer::Rasterizer(const int width, const int height)
|
||||||
: width_(width), height_(height),
|
: width_(width), height_(height),
|
||||||
pixels_(std::vector<std::vector<PixelProperty>>(
|
pixels_(std::vector<std::vector<PixelProperty>>(
|
||||||
|
@ -19,8 +18,6 @@ std::vector<std::vector<PixelProperty>>
|
||||||
Rasterizer::rasterize(const std::vector<Triangle> &primitives) {
|
Rasterizer::rasterize(const std::vector<Triangle> &primitives) {
|
||||||
for (const auto &t : primitives) {
|
for (const auto &t : primitives) {
|
||||||
const auto &triangle_points = t.get_points<3>();
|
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 auto &aabb = t.axis_align_bbox();
|
||||||
const int x_min = std::min(std::max(0, aabb.x), width_);
|
const int x_min = std::min(std::max(0, aabb.x), width_);
|
||||||
|
@ -34,8 +31,6 @@ Rasterizer::rasterize(const std::vector<Triangle> &primitives) {
|
||||||
auto &property = pixels_[i][j];
|
auto &property = pixels_[i][j];
|
||||||
const auto &[is_inside, z_screen] =
|
const auto &[is_inside, z_screen] =
|
||||||
inside(Point2d{j + 0.5, i + 0.5}, t);
|
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]) {
|
if (is_inside && z_screen > property[3]) {
|
||||||
property[0] = 0;
|
property[0] = 0;
|
||||||
property[1] = 1;
|
property[1] = 1;
|
||||||
|
@ -53,9 +48,6 @@ std::pair<bool, double> Rasterizer::inside(const Point2d &p_screen,
|
||||||
const auto points = t.get_points<3>();
|
const auto points = t.get_points<3>();
|
||||||
|
|
||||||
const auto plane_normal = t.normal_vector();
|
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 plane_point = points[0];
|
||||||
const auto z_screen =
|
const auto z_screen =
|
||||||
plane_point.z() - (plane_normal.x() * (p_screen.x() - plane_point.x()) +
|
plane_point.z() - (plane_normal.x() * (p_screen.x() - plane_point.x()) +
|
||||||
|
@ -75,6 +67,5 @@ std::pair<bool, double> Rasterizer::inside(const Point2d &p_screen,
|
||||||
const auto r2 = c1.dot(c3);
|
const auto r2 = c1.dot(c3);
|
||||||
const auto r3 = c2.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};
|
return {r1 > 0 && r2 > 0 && r3 > 0, z_screen};
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,12 +35,8 @@ void Triangle::affine_transform(const TransformMatrix &m) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::projective_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_) {
|
for (auto &p : points_) {
|
||||||
auto p_homo = m * Eigen::Vector4d(p.x(), p.y(), p.z(), 1.);
|
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>();
|
p = (p_homo / p_homo.w()).head<3>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue