当前位置:网站首页>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 :

边栏推荐
- 小程序 canvas 生成海报
- ESP8266-Arduino编程实例-SPIFFS及数据上传(Arduino IDE和PlatformIO IDE)
- Aimbetter insight into your database, DPM and APM solutions
- Mesh data generation function meshgrid
- Brief introduction to PCB materials
- Two global variables__ Dirname and__ Further introduction to common functions of filename and FS modules
- Oracle triggers
- Is it necessary to calibrate the fluke dtx-1800 test accuracy?
- I have been in the industry for 4 years and changed jobs twice. I have understood the field of software testing~
- Detailed explanation of JVM memory layout (glory collection version)
猜你喜欢

Apifox: satisfy all your fantasies about API

Oracle database objects

An end-to-end aspect level emotion analysis method for government app reviews based on brnn

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

开放式耳机哪个品牌好、性价比最高的开放式耳机排名

Professional Committee of agricultural water and soil engineering of China Association of Agricultural Engineering - 12th session - Notes

系统分析师

Byte side: can TCP and UDP use the same port?

Practice and exploration of overseas site Seata of ant group

Cross domain transfer learning of professional skill word extraction in Chinese recruitment documents
随机推荐
90. Subset II
网格数据生成函数meshgrid
Nano gold coupled antibody / protein Kit (20nm, 1mg/100 μ g/500 μ G coupling amount) preparation
Use pl/sql
学习 Kotlin - 扩展函数
标准C语言学习总结10
如何高效、精准地进行图片搜索?看看轻量化视觉预训练模型
Research on weapon equipment attribute extraction based on attribute word completion
第三方软件测试机构提供哪些测试服务?软件测试报告收费标准
The University was abandoned for three years, the senior taught himself for seven months, and found a 12K job
Gateway technology of IOT technology stack
腾讯云数据库负责人借了一亿元炒股?知情人士:金额不实
In Kingbase, the user is specified to search the schema by default, or the user cannot use the function under the public schema
从 Web3到Web2.5,是倒退还是另辟蹊径?
C语言编程规范学习笔记和总结
Research on the recognition method of move function information of scientific paper abstract based on paragraph Bert CRF
纳米金偶联抗体/蛋白试剂盒(20nm,1mg/100μg/500 μg偶联量)的制备
Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
array_diff_assoc 元素是数组时不比较数组值的办法
DHCP和PPPoE协议以及抓包分析