From 41f4770099404c4e3886a87618bb8d427cb20c31 Mon Sep 17 00:00:00 2001 From: "tianlei.richard" Date: Fri, 29 Mar 2024 21:34:07 +0800 Subject: [PATCH] let camera move-able. --- src/camera.cc | 13 ++++++++++- src/camera.h | 9 ++++---- src/main.cc | 50 ++++++++++++++++++++++++++++--------------- src/minus_renderer.cc | 7 +++--- src/phone_material.h | 1 - src/rasterizer.h | 1 - src/texture.cc | 2 +- src/texture.h | 1 - 8 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/camera.cc b/src/camera.cc index c7ec665..e5ca6e7 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -3,8 +3,19 @@ #include "camera.h" +#include "spdlog/spdlog.h" + Camera::Camera(const Vector3d &up) - : position_(Point3d::Identity()), gaze_(Vector3d::Identity()), up_(up) {} + : gaze_(Vector3d::Identity()), up_(up), camera_speed_(0.3) {} + +Camera::Camera(const Vector3d &up, const float camera_speed) + : gaze_(Vector3d::Identity()), up_(up), camera_speed_(camera_speed) {} + +void Camera::move(const Point3d &offset) { + position_ = (offset * camera_speed_); +} + +void Camera::set_gaze(const Vector3d &gaze) { gaze_ = gaze.normalized(); } TransformMatrix Camera::get_view_transform() const { // Move camera to origin diff --git a/src/camera.h b/src/camera.h index 7677f84..b1ceff9 100644 --- a/src/camera.h +++ b/src/camera.h @@ -6,18 +6,19 @@ class Camera { public: explicit Camera(const Vector3d &up); + explicit Camera(const Vector3d &up, const float camera_speed); public: TransformMatrix get_view_transform() const; public: - void set_position(const Point3d &position) { - position_ = position.normalized(); - } - void set_gaze(const Vector3d &gaze) { gaze_ = gaze.normalized(); } + void move(const Point3d &offset); + void set_gaze(const Vector3d &gaze); private: Point3d position_; Vector3d gaze_; Vector3d up_; + + float camera_speed_; }; diff --git a/src/main.cc b/src/main.cc index 1775532..c5c11ab 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,8 +1,6 @@ // Copyright 2024 SquareBlock Inc. All Rights Reserved. // Author: tianlei.richard@qq.com (tianlei.richard) -#include - #include "imgui.h" #include "imgui_impl_opengl3.h" #include "imgui_impl_sdl2.h" @@ -44,6 +42,8 @@ int main(int argc, char *argv[]) { const float aspect_ratio = static_cast(resolution_width) / static_cast(resolution_height); + const float camera_speed = 1.0; + const std::string window_name{"Minor Render"}; // Setup SDL @@ -107,12 +107,12 @@ int main(int argc, char *argv[]) { // Our state ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - Camera camera{Vector3d{0, 1, 0}}; + Camera camera{Vector3d{0, 1, 0}, camera_speed}; MinusRenderer renderer{near, far, fov, aspect_ratio}; renderer.set_meshes( Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}})); TransformMatrix translate{ - {1., 0., 0., 3.}, {0., 1., 0., 3.}, {0., 0., 1., -4.}, {0., 0., 0., 1.}}; + {1., 0., 0., 3.}, {0., 1., 0., 3.}, {0., 0., 1., -5.}, {0., 0., 0., 1.}}; renderer.model_transform(translate); // Main loop @@ -145,25 +145,41 @@ int main(int argc, char *argv[]) { // 2. Show a simple window that we create ourselves. We use a Begin/End pair // to create a named window. { - TransformMatrix move2origin{ - {1, 0, 0, 0}, {0, 1, 0, -3}, {0, 0, 1, 4}, {0, 0, 0, 1}}; - TransformMatrix rotation{ - Eigen::Transform( - Eigen::AngleAxisd(0.0625 * M_PI, Eigen::Vector3d::UnitX()) - .toRotationMatrix()) - .matrix()}; - TransformMatrix move_back{ - {1, 0, 0, 0}, {0, 1, 0, 3}, {0, 0, 1, -4}, {0, 0, 0, 1}}; - renderer.model_transform(move_back * rotation * move2origin); + ImGui::Begin(window_name.c_str()); // Create a window - camera.set_position(Point3d{0, 0, 0}); + // TransformMatrix move2origin{ + // {1, 0, 0, 0}, {0, 1, 0, -3}, {0, 0, 1, 5.}, {0, 0, 0, 1}}; + // TransformMatrix rotation{ + // Eigen::Transform( + // Eigen::AngleAxisd(0.0625 * M_PI, Eigen::Vector3d::UnitX()) + // .toRotationMatrix()) + // .matrix()}; + // TransformMatrix move_back{ + // {1, 0, 0, 0}, {0, 1, 0, 3}, {0, 0, 1, -5.}, {0, 0, 0, 1}}; + // renderer.model_transform(move_back * rotation * move2origin); + + if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) { + 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_W)) { + offset.z() -= 1; + } else if (ImGui::IsKeyPressed(ImGuiKey_E)) { + offset.y() += 1; + } else if (ImGui::IsKeyPressed(ImGuiKey_Q)) { + offset.y() -= 1; + } + camera.move(offset); + } camera.set_gaze(Vector3d{0, 0, -1}); renderer.view_transform(camera.get_view_transform()); const auto &image = renderer.render(resolution_width, resolution_height); - ImGui::Begin(window_name.c_str()); // Create a window - GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); diff --git a/src/minus_renderer.cc b/src/minus_renderer.cc index cd1bdf8..c621e8a 100644 --- a/src/minus_renderer.cc +++ b/src/minus_renderer.cc @@ -135,7 +135,8 @@ cv::Mat MinusRenderer::render(const int resolution_width, construct_transform(resolution_width, resolution_height); Rasterizer rasterizer{resolution_width, resolution_height}; - for (int mesh_index = 0; mesh_index < meshes_.size(); ++mesh_index) { + for (decltype(meshes_)::size_type mesh_index = 0; mesh_index < meshes_.size(); + ++mesh_index) { auto primitives = (meshes_[mesh_index]).get_primitives(); for (auto &t : primitives) { t.set_points(apply_transform(ss_transform, t.get_vertex_position())); @@ -147,8 +148,8 @@ cv::Mat MinusRenderer::render(const int resolution_width, assert(!shading_points.empty()); cv::Mat color_image(shading_points.size(), (shading_points[0]).size(), CV_8UC3); - for (int y = 0; y < shading_points.size(); ++y) { - for (int x = 0; x < shading_points[y].size(); ++x) { + for (size_t y = 0; y < shading_points.size(); ++y) { + for (size_t x = 0; x < shading_points[y].size(); ++x) { auto &pixel_color = color_image.at(shading_points.size() - y - 1, x); pixel_color = cv::Vec3b(0, 0, 0); diff --git a/src/phone_material.h b/src/phone_material.h index d7532a2..3b5da5f 100644 --- a/src/phone_material.h +++ b/src/phone_material.h @@ -4,7 +4,6 @@ #pragma once #include -#include #include #include "common.h" diff --git a/src/rasterizer.h b/src/rasterizer.h index d21cd00..2021db8 100644 --- a/src/rasterizer.h +++ b/src/rasterizer.h @@ -3,7 +3,6 @@ #pragma once -#include "mesh.h" #include "triangle.h" #include #include diff --git a/src/texture.cc b/src/texture.cc index b1f59af..0ac8ad1 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -12,7 +12,7 @@ Texture::Texture(const std::filesystem::path &texture_path) { throw std::filesystem::filesystem_error("File not found", std::error_code{}); } - texture_ = (cv::imread(texture_path)); + texture_ = (cv::imread(texture_path.c_str())); spdlog::debug("Texture at {}, shape: ({},{})", texture_path.c_str(), texture_.cols, texture_.rows); } diff --git a/src/texture.h b/src/texture.h index 17db0f0..6e07553 100644 --- a/src/texture.h +++ b/src/texture.h @@ -4,7 +4,6 @@ #pragma once #include -#include #include "common.h"