bugfix: fix key triggered move driection bug, and texture mapping bug.

This commit is contained in:
tianlei.richard 2024-03-30 22:49:46 +08:00
parent 41f4770099
commit 27419f73b5
4 changed files with 32 additions and 17 deletions

View File

@ -4,9 +4,14 @@ PROJECT(renderer)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS_DEBUG "-gdwarf-4 -g")
else()
set(CMAKE_CXX_FLAGS_DEBUG "-g")
endif()
file(GLOB_RECURSE CPP_FILES ${CMAKE_CURRENT_SOURCE_DIR} *.cc)

View File

@ -34,8 +34,8 @@ int main(int argc, char *argv[]) {
const std::string obj_path{result["obj"].as<std::string>()};
const std::string diffuse_map_path{result["diffuse_map"].as<std::string>()};
const int resolution_width = 640;
const int resolution_height = 320;
const int resolution_width = 1280;
const int resolution_height = 720;
const int far = -100;
const int near = -1;
const float fov = 120. / 180. * M_PI;
@ -146,6 +146,7 @@ int main(int argc, char *argv[]) {
// to create a named window.
{
ImGui::Begin(window_name.c_str()); // Create a window
ImGui::SetWindowSize({resolution_width, resolution_height});
// TransformMatrix move2origin{
// {1, 0, 0, 0}, {0, 1, 0, -3}, {0, 0, 1, 5.}, {0, 0, 0, 1}};
@ -162,16 +163,16 @@ int main(int argc, char *argv[]) {
Point3d offset{0, 0, 0};
if (ImGui::IsKeyPressed(ImGuiKey_A)) {
offset.x() -= 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_S)) {
offset.z() += 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_D)) {
offset.x() += 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_S)) {
offset.z() += 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_W)) {
offset.z() -= 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_E)) {
offset.y() += 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_Q)) {
offset.y() -= 1;
} else if (ImGui::IsKeyPressed(ImGuiKey_Q)) {
offset.y() += 1;
}
camera.move(offset);
}

View File

@ -130,6 +130,8 @@ void MinusRenderer::view_transform(const TransformMatrix &mtx) {
cv::Mat MinusRenderer::render(const int resolution_width,
const int resolution_height) {
spdlog::trace("Tick once.");
// 构造 camera space -> screen space 的变换矩阵
const auto [ss_transform, inv_ss_transform] =
construct_transform(resolution_width, resolution_height);

View File

@ -4,6 +4,7 @@
#include "texture.h"
#include "spdlog/spdlog.h"
#include "util/interpolation.h"
#include <cmath>
#include <opencv2/highgui/highgui.hpp>
Texture::Texture(const std::filesystem::path &texture_path) {
@ -20,20 +21,26 @@ Texture::Texture(const std::filesystem::path &texture_path) {
cv::Vec3b Texture::sample(const Point2d &texture_coordinate) {
cv::Vec3b res{0, 0, 0};
const double x = texture_coordinate.x() * texture_.cols - 0.5;
const double y = texture_coordinate.y() * texture_.rows - 0.5;
double x = texture_coordinate.x() * (texture_.cols - 1);
if (x < 0.) {
x += texture_.cols;
}
double y = (1. - texture_coordinate.y()) * (texture_.rows - 1);
if (y < 0.) {
y = std::fabs(y);
}
const int x_f = x;
const int x_c = x + 1.;
const int x_f = std::max(0., x);
const int x_c = std::min(texture_.cols - 1, static_cast<int>(x + 1.));
const float t_x = x - x_f;
const int y_f = y;
const int y_c = y + 1.;
const int y_f = std::max(0., y);
const int y_c = std::min(texture_.rows - 1, static_cast<int>(y + 1.));
const float t_y = y - y_f;
const cv::Vec3b &a = texture_.at<cv::Vec3b>(x_f, y_f);
const cv::Vec3b &b = texture_.at<cv::Vec3b>(x_f, y_c);
const cv::Vec3b &c = texture_.at<cv::Vec3b>(x_c, y_f);
const cv::Vec3b &d = texture_.at<cv::Vec3b>(x_c, y_c);
const cv::Vec3b &a = texture_.at<cv::Vec3b>(y_f, x_f);
const cv::Vec3b &b = texture_.at<cv::Vec3b>(y_c, x_f);
const cv::Vec3b &c = texture_.at<cv::Vec3b>(y_f, x_c);
const cv::Vec3b &d = texture_.at<cv::Vec3b>(y_c, x_c);
res = bilerp(t_x, t_y, a, b, c, d);