let camera move-able.
This commit is contained in:
parent
acd472bacf
commit
41f4770099
|
@ -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
|
||||
|
|
|
@ -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_;
|
||||
};
|
||||
|
|
50
src/main.cc
50
src/main.cc
|
@ -1,8 +1,6 @@
|
|||
// Copyright 2024 SquareBlock Inc. All Rights Reserved.
|
||||
// Author: tianlei.richard@qq.com (tianlei.richard)
|
||||
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#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<float>(resolution_width) /
|
||||
static_cast<float>(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<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);
|
||||
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<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});
|
||||
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);
|
||||
|
|
|
@ -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<cv::Vec3b>(shading_points.size() - y - 1, x);
|
||||
pixel_color = cv::Vec3b(0, 0, 0);
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "mesh.h"
|
||||
#include "triangle.h"
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <vector>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
|
Loading…
Reference in New Issue