let camera move-able.

This commit is contained in:
tianlei.richard 2024-03-29 21:34:07 +08:00
parent acd472bacf
commit 41f4770099
8 changed files with 55 additions and 29 deletions

View File

@ -3,8 +3,19 @@
#include "camera.h" #include "camera.h"
#include "spdlog/spdlog.h"
Camera::Camera(const Vector3d &up) 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 { TransformMatrix Camera::get_view_transform() const {
// Move camera to origin // Move camera to origin

View File

@ -6,18 +6,19 @@
class Camera { class Camera {
public: public:
explicit Camera(const Vector3d &up); explicit Camera(const Vector3d &up);
explicit Camera(const Vector3d &up, const float camera_speed);
public: public:
TransformMatrix get_view_transform() const; TransformMatrix get_view_transform() const;
public: public:
void set_position(const Point3d &position) { void move(const Point3d &offset);
position_ = position.normalized(); void set_gaze(const Vector3d &gaze);
}
void set_gaze(const Vector3d &gaze) { gaze_ = gaze.normalized(); }
private: private:
Point3d position_; Point3d position_;
Vector3d gaze_; Vector3d gaze_;
Vector3d up_; Vector3d up_;
float camera_speed_;
}; };

View File

@ -1,8 +1,6 @@
// Copyright 2024 SquareBlock Inc. All Rights Reserved. // Copyright 2024 SquareBlock Inc. All Rights Reserved.
// Author: tianlei.richard@qq.com (tianlei.richard) // Author: tianlei.richard@qq.com (tianlei.richard)
#include <opencv2/core/core.hpp>
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3.h"
#include "imgui_impl_sdl2.h" #include "imgui_impl_sdl2.h"
@ -44,6 +42,8 @@ int main(int argc, char *argv[]) {
const float aspect_ratio = static_cast<float>(resolution_width) / const float aspect_ratio = static_cast<float>(resolution_width) /
static_cast<float>(resolution_height); static_cast<float>(resolution_height);
const float camera_speed = 1.0;
const std::string window_name{"Minor Render"}; const std::string window_name{"Minor Render"};
// Setup SDL // Setup SDL
@ -107,12 +107,12 @@ int main(int argc, char *argv[]) {
// Our state // Our state
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 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}; MinusRenderer renderer{near, far, fov, aspect_ratio};
renderer.set_meshes( renderer.set_meshes(
Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}})); Mesh::load_mesh(obj_path, {{Texture::DiffuseMap, diffuse_map_path}}));
TransformMatrix translate{ 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); renderer.model_transform(translate);
// Main loop // 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 // 2. Show a simple window that we create ourselves. We use a Begin/End pair
// to create a named window. // to create a named window.
{ {
TransformMatrix move2origin{ ImGui::Begin(window_name.c_str()); // Create a window
{1, 0, 0, 0}, {0, 1, 0, -3}, {0, 0, 1, 4}, {0, 0, 0, 1}};
TransformMatrix rotation{
Eigen::Transform<double, 3, Eigen::Affine>(
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);
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<double, 3, Eigen::Affine>(
// 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}); camera.set_gaze(Vector3d{0, 0, -1});
renderer.view_transform(camera.get_view_transform()); renderer.view_transform(camera.get_view_transform());
const auto &image = renderer.render(resolution_width, resolution_height); const auto &image = renderer.render(resolution_width, resolution_height);
ImGui::Begin(window_name.c_str()); // Create a window
GLuint texture; GLuint texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);

View File

@ -135,7 +135,8 @@ cv::Mat MinusRenderer::render(const int resolution_width,
construct_transform(resolution_width, resolution_height); construct_transform(resolution_width, resolution_height);
Rasterizer rasterizer{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(); auto primitives = (meshes_[mesh_index]).get_primitives();
for (auto &t : primitives) { for (auto &t : primitives) {
t.set_points(apply_transform(ss_transform, t.get_vertex_position())); 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()); assert(!shading_points.empty());
cv::Mat color_image(shading_points.size(), (shading_points[0]).size(), cv::Mat color_image(shading_points.size(), (shading_points[0]).size(),
CV_8UC3); CV_8UC3);
for (int y = 0; y < shading_points.size(); ++y) { for (size_t y = 0; y < shading_points.size(); ++y) {
for (int x = 0; x < shading_points[y].size(); ++x) { for (size_t x = 0; x < shading_points[y].size(); ++x) {
auto &pixel_color = auto &pixel_color =
color_image.at<cv::Vec3b>(shading_points.size() - y - 1, x); color_image.at<cv::Vec3b>(shading_points.size() - y - 1, x);
pixel_color = cv::Vec3b(0, 0, 0); pixel_color = cv::Vec3b(0, 0, 0);

View File

@ -4,7 +4,6 @@
#pragma once #pragma once
#include <filesystem> #include <filesystem>
#include <opencv2/core/core.hpp>
#include <vector> #include <vector>
#include "common.h" #include "common.h"

View File

@ -3,7 +3,6 @@
#pragma once #pragma once
#include "mesh.h"
#include "triangle.h" #include "triangle.h"
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <vector> #include <vector>

View File

@ -12,7 +12,7 @@ Texture::Texture(const std::filesystem::path &texture_path) {
throw std::filesystem::filesystem_error("File not found", throw std::filesystem::filesystem_error("File not found",
std::error_code{}); std::error_code{});
} }
texture_ = (cv::imread(texture_path)); texture_ = (cv::imread(texture_path.c_str()));
spdlog::debug("Texture at {}, shape: ({},{})", texture_path.c_str(), spdlog::debug("Texture at {}, shape: ({},{})", texture_path.c_str(),
texture_.cols, texture_.rows); texture_.cols, texture_.rows);
} }

View File

@ -4,7 +4,6 @@
#pragma once #pragma once
#include <filesystem> #include <filesystem>
#include <opencv2/core/core.hpp>
#include "common.h" #include "common.h"