diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d725151..04f283a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/main.cc b/src/main.cc index c5c11ab..2568411 100644 --- a/src/main.cc +++ b/src/main.cc @@ -34,8 +34,8 @@ int main(int argc, char *argv[]) { const std::string obj_path{result["obj"].as()}; const std::string diffuse_map_path{result["diffuse_map"].as()}; - 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); } diff --git a/src/minus_renderer.cc b/src/minus_renderer.cc index c621e8a..182698d 100644 --- a/src/minus_renderer.cc +++ b/src/minus_renderer.cc @@ -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); diff --git a/src/texture.cc b/src/texture.cc index 0ac8ad1..74f1922 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -4,6 +4,7 @@ #include "texture.h" #include "spdlog/spdlog.h" #include "util/interpolation.h" +#include #include 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(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(y + 1.)); const float t_y = y - y_f; - const cv::Vec3b &a = texture_.at(x_f, y_f); - const cv::Vec3b &b = texture_.at(x_f, y_c); - const cv::Vec3b &c = texture_.at(x_c, y_f); - const cv::Vec3b &d = texture_.at(x_c, y_c); + const cv::Vec3b &a = texture_.at(y_f, x_f); + const cv::Vec3b &b = texture_.at(y_c, x_f); + const cv::Vec3b &c = texture_.at(y_f, x_c); + const cv::Vec3b &d = texture_.at(y_c, x_c); res = bilerp(t_x, t_y, a, b, c, d);