-
- glm::mat4 Camera::GetViewMatrix()
- {
- return glm::lookAt(Position, Position + Forward, Up);
- }
- //键盘输入
- 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;
- //计算每帧的时间差
- float currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- processInput(window);
-
- //鼠标移动响应
- 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);
- }
- //鼠标滚轮响应
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
- {
- camera.ProcessMouseScroll(yoffset);
- }
- //鼠标滚轮响应
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
- {
- camera.ProcessMouseScroll(yoffset);
- }
工程文件结构:
camera.h
- #ifndef __CAMERA_H__
- #define __CAMERA_H__
-
- #include
- #include
- #include
- #include
-
- 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;
- //摄像机移动方向
- 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();
- };
-
- #endif
Texture.h
- #ifndef __TEXTURE_H__
- #define __TEXTURE_H__
-
- #include "stb_image.h"
-
- #include
-
- 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);
- }
-
- //对应键盘移动事件
- 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;
- }
- //对应鼠标移动事件
- 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();
- }
- //对应鼠标滚轮事件
- 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;
- }
输出结果: