当前位置:网站首页>Part 8: creating camera classes
Part 8: creating camera classes
2022-07-28 22:06:00 【Code Knight】
1、 The camera / Observe the space

2、Look At matrix

glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(Position, Position + Forward, Up);
}
3、 Move freely

// Keyboard entry
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
} 
float deltaTime = 0.0f;
float lastFrame = 0.0f;// Calculate the time difference of each frame
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
4、 Perspective movement


5、 Mouse input
// Mouse movement response
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
// Mouse wheel response
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
6、 The zoom

// Mouse wheel response
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
} 
7、 Create camera class code :
Engineering document structure :
camera.h
#ifndef __CAMERA_H__
#define __CAMERA_H__
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
// Camera moving direction
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT
};
class Camera
{
public:
glm::vec3 Position;
glm::vec3 Forward;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 World_up;
float Yaw;
float Pitch;
float MovementSpeed;
float Mouse_Sensiticity;
float Zoom;
bool flip_y = false;
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH);
Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch);
~Camera();
glm::mat4 GetViewMatrix();
void ProcessKeyboard(Camera_Movement direction, float deltaTime);
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true);
void ProcessMouseScroll(float yoffset);
private:
void UpdateCameraVectors();
};
#endifTexture.h
#ifndef __TEXTURE_H__
#define __TEXTURE_H__
#include "stb_image.h"
#include <glad/glad.h>
class Texture
{
public:
static unsigned int LoadTextureFromFile(const char* path);
};
#endif // !__TEXTURE_H__
camera.cpp
#include "Camera.h"
Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch)
: Forward(glm::vec3(0.0f, 0.0f, -1.0f))
, MovementSpeed(SPEED)
, Mouse_Sensiticity(SENSITIVITY)
, Zoom(ZOOM)
{
this->Position = position;
this->World_up = up;
this->Yaw = yaw;
this->Pitch = pitch;
UpdateCameraVectors();
}
Camera::Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch)
: Forward(glm::vec3(0.0f, 0.0f, -1.0f))
, MovementSpeed(SPEED)
, Mouse_Sensiticity(SENSITIVITY)
, Zoom(ZOOM)
{
this->Position = glm::vec3(pos_x, pos_y, pos_z);
this->World_up = glm::vec3(up_x, up_y, up_z);
this->Yaw = yaw;
this->Pitch = pitch;
UpdateCameraVectors();
}
Camera::~Camera()
{
}
glm::mat4 Camera::GetViewMatrix()
{
return glm::lookAt(Position, Position + Forward, Up);
}
// Corresponding to keyboard movement event
void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Forward * velocity;
if (direction == BACKWARD)
Position -= Forward * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}
// Corresponding to the mouse movement event
void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch)
{
xoffset *= Mouse_Sensiticity;
yoffset *= Mouse_Sensiticity;
Yaw += xoffset;
Pitch += yoffset;
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}
UpdateCameraVectors();
}
// Corresponding to the mouse wheel event
void Camera::ProcessMouseScroll(float yoffset)
{
if (Zoom >= 1.0f && Zoom <= 45.0f)
Zoom -= yoffset;
if (Zoom <= 1.0f)
Zoom = 1.0f;
if (Zoom >= 45.0f)
Zoom = 45.0f;
}
void Camera::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));
Forward = glm::normalize(front);
Right = glm::normalize(glm::cross(Forward, World_up));
Up = glm::normalize(glm::cross(Right, Forward));
}Texture.cpp
#include "Texture.h"
#include "iostream"
unsigned int Texture::LoadTextureFromFile(const char* path)
{
unsigned int texture_id;
glGenTextures(1, &texture_id);
int width, height, nr_channels;
unsigned char *data = stbi_load(path, &width, &height, &nr_channels, 0);
if (data)
{
GLenum format;
if (nr_channels == 1)
{
format = GL_RED;
}
else if (nr_channels == 3)
{
format = GL_RGB;
}
else if (nr_channels == 4)
{
format = GL_RGBA;
}
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
return texture_id;
}Output results :

边栏推荐
猜你喜欢

Openeuler embedded sig | distributed soft bus

字节一面:TCP 和 UDP 可以使用同一个端口吗?

Information fusion method and application of expert opinion and trust in large group emergency decision-making based on complex network

Kubevera plug-in addons download address

内网渗透学习(三)域横向移动——计划任务

How to search images efficiently and accurately? Look at the lightweight visual pre training model

分而治之,大型文件分片上传

In Kingbase, the user is specified to search the schema by default, or the user cannot use the function under the public schema

DHCP和PPPoE协议以及抓包分析

Lt7911d type-c/dp to Mipi scheme is mature and can provide technical support
随机推荐
Implementation of sequence table
开放式耳机哪个品牌好、性价比最高的开放式耳机排名
Chinese patent keyword extraction based on LSTM and logistic regression
Oracle built-in functions
39. Combined sum
小程序 监听目标节点出现到屏幕中
The University was abandoned for three years, the senior taught himself for seven months, and found a 12K job
How many tips do you know about using mock technology to help improve test efficiency?
04.toRef 默认值
[Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol
Oracle triggers
Log slimming operation: how to optimize from 5g to 1g! (glory Collection Edition)
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
字节一面:TCP 和 UDP 可以使用同一个端口吗?
迪赛智慧数——折线图(堆叠面积图):2022年不同职业人群存款额占月收入比例排名
标准C语言学习总结10
Openeuler embedded sig | distributed soft bus
Data interpolation -- normalize data of different magnitude
株洲市九方中学开展防溺水、消防安全教育培训活动
如何在 Web3中建立一个去中心化社区