移除我未使用的头文件, 添加摄像机类

This commit is contained in:
jie 2023-10-16 13:35:01 +08:00
parent 6831aa63db
commit 61ec828dab
3 changed files with 172 additions and 57 deletions

156
include/cameraService.h Normal file
View File

@ -0,0 +1,156 @@
#ifndef CAMERA_H
#define CAMERA_H
#include "pch.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
enum class CAMERA_MOVEMENT
{
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN
};
const float YAW = -90.f;
const float PITCH = 0.f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
class CameraService
{
public:
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
float Yaw;
float Pitch;
float MovementSpeed;
float MouseSensitivity;
float Zoom;
CameraService(glm::vec3 position = glm::vec3{0.f, 0.f, 0.f}, glm::vec3 up = glm::vec3{0.f, 1.f, 0.f}, float yaw = YAW, float pitch = PITCH) : Front(glm::vec3{0.f, 0.f, -1.f}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
UpdateCameraVectors();
}
CameraService(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw = YAW, float pitch = PITCH) : Front(glm::vec3{0.f, 0.f, -1.f}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3{posX, posY, posZ};
WorldUp = glm::vec3{upX, upY, upZ};
Yaw = yaw;
Pitch = pitch;
UpdateCameraVectors();
}
glm::mat4 GetViewMartix()
{
return this->LookAt(Position, Position + Front, Up);
}
void ProcessKeyboard(CAMERA_MOVEMENT direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
switch (direction)
{
case CAMERA_MOVEMENT::FORWARD:
Position += Front * velocity;
break;
case CAMERA_MOVEMENT::BACKWARD:
Position -= Front * velocity;
break;
case CAMERA_MOVEMENT::LEFT:
Position -= Right * velocity;
break;
case CAMERA_MOVEMENT::RIGHT:
Position += Right * velocity;
break;
case CAMERA_MOVEMENT::UP:
break;
Position += Up * velocity;
case CAMERA_MOVEMENT::DOWN:
break;
Position -= Up * velocity;
default:
break;
}
Position.y = 0.f;
}
void ProcessMouseMovement(float xOffset, float yOffset, GLboolean constrainPitch = true)
{
xOffset *= MouseSensitivity;
yOffset *= MouseSensitivity;
Yaw += xOffset;
Pitch += yOffset;
if (constrainPitch)
{
if (Pitch > 89.f)
{
Pitch = 89.f;
}
if (Pitch < -89.f)
{
Pitch = -89.f;
}
}
UpdateCameraVectors();
}
void ProcessMouseScroll(float yOffset)
{
Zoom -= yOffset;
if (Zoom > 45.f)
{
Zoom = 45.f;
}
if (Zoom < 1.f)
{
Zoom = 1.f;
}
}
private:
void UpdateCameraVectors(){
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
glm::mat4 LookAt(glm::vec3 position, glm::vec3 target, glm::vec3 worldUp){
glm::vec3 direction = position - target;
glm::vec3 right = glm::normalize(glm::cross(glm::normalize(worldUp), direction));
glm::vec3 up = glm::normalize(glm::cross(direction, right));
glm::mat4 trans = glm::mat4{1.f};
trans[3][0] = -position.x;
trans[3][1] = -position.y;
trans[3][2] = -position.z;
glm::mat4 rotation = glm::mat4{1.f};
rotation[0][0] = right.x;
rotation[1][0] = right.y;
rotation[2][0] = right.z;
rotation[0][1] = up.x;
rotation[1][1] = up.y;
rotation[2][1] = up.z;
rotation[0][2] = direction.x;
rotation[1][2] = direction.y;
rotation[2][2] = direction.z;
return rotation * trans;
}
};
#endif // !CAMERA_H

View File

@ -4,11 +4,11 @@
#include "matrix.h"
#include "iostream"
#include "stb_image.h"
#include "opencv2/opencv.hpp"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "cameraService.h"
constexpr int width = 800;
constexpr int height = 600;
@ -18,18 +18,12 @@ namespace MATRIX {
float mixValue = 0.5f;
glm::vec3 cameraPos = glm::vec3{0.f, 0.f, 3.f};
glm::vec3 cameraFront = glm::vec3{0.f, 0.f, -1.f};
glm::vec3 cameraUp = glm::vec3{0.f, 1.f, 0.f};
float deltaTime = 0.f;
float lastTime = 0.f;
bool firstRender = true;
bool firstMouse = true;
float lastX = width / 2;
float lastY = height / 2;
float yaw = -90.f;
float pitch = 0.f;
float fov = 45.f;
CameraService camera{{0.f, 0.f, 3.f}};
void frameBufferSizeChange(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
@ -41,66 +35,42 @@ namespace MATRIX {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraPos += cameraSpeed * cameraFront;
camera.ProcessKeyboard(CAMERA_MOVEMENT::FORWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraPos -= cameraSpeed * cameraFront;
camera.ProcessKeyboard(CAMERA_MOVEMENT::BACKWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cameraPos += glm::normalize(glm::cross(cameraUp, cameraFront)) * cameraSpeed;
camera.ProcessKeyboard(CAMERA_MOVEMENT::LEFT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
cameraPos -= glm::normalize(glm::cross(cameraUp, cameraFront)) * cameraSpeed;
camera.ProcessKeyboard(CAMERA_MOVEMENT::RIGHT, deltaTime);
}
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS){
cameraPos += cameraUp * cameraSpeed;
camera.ProcessKeyboard(CAMERA_MOVEMENT::UP, deltaTime);
}
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS){
cameraPos -= cameraUp * cameraSpeed;
camera.ProcessKeyboard(CAMERA_MOVEMENT::DOWN, deltaTime);
}
}
void mouse_callback(GLFWwindow *window, double xposIn, double yposIn) {
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstRender) {
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstRender = false;
firstMouse = false;
}
float xOffset = xpos - lastX;
float yOffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f; // change this value to your liking
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.f) {
pitch = 89.f;
}
if (pitch < -89.f) {
pitch = -89.f;
}
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw) * cos(glm::radians(pitch)));
cameraFront = glm::normalize(front);
camera.ProcessMouseMovement(xOffset, yOffset);
}
void scroll_callback(GLFWwindow *window, double xOffset, double yOffset) {
if (fov >= 1.f && fov <= 45.f) {
fov -= yOffset;
}
if (fov <= 1.f) {
fov = 1.f;
}
if (fov >= 45.f) {
fov = 45.f;
}
camera.ProcessMouseScroll(static_cast<float>(yOffset));
}
int Matrix() {
@ -246,12 +216,11 @@ namespace MATRIX {
glm::mat4 model = glm::mat4{1.f};
glm::mat4 projection = glm::mat4{1.f};
projection = glm::perspective(glm::radians(fov), static_cast<float>(width) / height, .1f, 100.f);
std::cout<<std::format("x: {}, y: {}, z: {}", cameraFront.x, cameraFront.y, cameraFront.z)<<std::endl;
auto view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
projection = glm::perspective(glm::radians(camera.Zoom), static_cast<float>(width) / height, .1f, 100.f);
std::cout<<std::format("x: {}, y: {}, z: {}", camera.Front.x, camera.Front.y, camera.Front.z)<<std::endl;
auto view = camera.GetViewMartix();
shader.SetUniform("mixCount", mixValue);
shader.SetUniform("model", model);
shader.SetUniform("view", view);
shader.SetUniform("projection", projection);
for (unsigned int i = 0; i < 10; i++) {
@ -276,13 +245,4 @@ namespace MATRIX {
glfwTerminate();
return 0;
}
void MatrixTest() {
glm::vec4 vec(1.f, 0.f, 0.f, 1.f);
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3{1.f, 1.f, 0.f});
vec = trans * vec;
std::cout << vec.x << vec.y << vec.z << std::endl;
}
}

View File

@ -4,7 +4,6 @@
#include "texture.h"
#include "iostream"
#include "stb_image.h"
#include "opencv2/opencv.hpp"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"