• 第 8 篇:创建摄像机类


    1、摄像机/观察空间

     

    2、Look At矩阵

     

    1. glm::mat4 Camera::GetViewMatrix()
    2. {
    3. return glm::lookAt(Position, Position + Forward, Up);
    4. }

     

    3、自由移动

    1. //键盘输入
    2. void processInput(GLFWwindow *window)
    3. {
    4. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    5. glfwSetWindowShouldClose(window, true);
    6. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    7. camera.ProcessKeyboard(FORWARD, deltaTime);
    8. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    9. camera.ProcessKeyboard(BACKWARD, deltaTime);
    10. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    11. camera.ProcessKeyboard(LEFT, deltaTime);
    12. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    13. camera.ProcessKeyboard(RIGHT, deltaTime);
    14. }

     

     

    1. float deltaTime = 0.0f;
    2. float lastFrame = 0.0f;
    1. //计算每帧的时间差
    2. float currentFrame = glfwGetTime();
    3. deltaTime = currentFrame - lastFrame;
    4. lastFrame = currentFrame;
    5. processInput(window);

     

     

    4、视角移动

     

    5、鼠标输入

     

     

    1. //鼠标移动响应
    2. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
    3. {
    4. if (firstMouse)
    5. {
    6. lastX = xpos;
    7. lastY = ypos;
    8. firstMouse = false;
    9. }
    10. float xoffset = xpos - lastX;
    11. float yoffset = lastY - ypos;
    12. lastX = xpos;
    13. lastY = ypos;
    14. camera.ProcessMouseMovement(xoffset, yoffset);
    15. }
    16. //鼠标滚轮响应
    17. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
    18. {
    19. camera.ProcessMouseScroll(yoffset);
    20. }

     

     

     

    6、缩放

    1. //鼠标滚轮响应
    2. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
    3. {
    4. camera.ProcessMouseScroll(yoffset);
    5. }

     

     

    7、创建摄像机类代码:

    工程文件结构:

     

    camera.h

    1. #ifndef __CAMERA_H__
    2. #define __CAMERA_H__
    3. #include
    4. #include
    5. #include
    6. #include
    7. const float YAW = -90.0f;
    8. const float PITCH = 0.0f;
    9. const float SPEED = 2.5f;
    10. const float SENSITIVITY = 0.1f;
    11. const float ZOOM = 45.0f;
    12. //摄像机移动方向
    13. enum Camera_Movement {
    14. FORWARD,
    15. BACKWARD,
    16. LEFT,
    17. RIGHT
    18. };
    19. class Camera
    20. {
    21. public:
    22. glm::vec3 Position;
    23. glm::vec3 Forward;
    24. glm::vec3 Up;
    25. glm::vec3 Right;
    26. glm::vec3 World_up;
    27. float Yaw;
    28. float Pitch;
    29. float MovementSpeed;
    30. float Mouse_Sensiticity;
    31. float Zoom;
    32. bool flip_y = false;
    33. 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);
    34. Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch);
    35. ~Camera();
    36. glm::mat4 GetViewMatrix();
    37. void ProcessKeyboard(Camera_Movement direction, float deltaTime);
    38. void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true);
    39. void ProcessMouseScroll(float yoffset);
    40. private:
    41. void UpdateCameraVectors();
    42. };
    43. #endif

    Texture.h

    1. #ifndef __TEXTURE_H__
    2. #define __TEXTURE_H__
    3. #include "stb_image.h"
    4. #include
    5. class Texture
    6. {
    7. public:
    8. static unsigned int LoadTextureFromFile(const char* path);
    9. };
    10. #endif // !__TEXTURE_H__

    camera.cpp

    1. #include "Camera.h"
    2. Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch)
    3. : Forward(glm::vec3(0.0f, 0.0f, -1.0f))
    4. , MovementSpeed(SPEED)
    5. , Mouse_Sensiticity(SENSITIVITY)
    6. , Zoom(ZOOM)
    7. {
    8. this->Position = position;
    9. this->World_up = up;
    10. this->Yaw = yaw;
    11. this->Pitch = pitch;
    12. UpdateCameraVectors();
    13. }
    14. Camera::Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch)
    15. : Forward(glm::vec3(0.0f, 0.0f, -1.0f))
    16. , MovementSpeed(SPEED)
    17. , Mouse_Sensiticity(SENSITIVITY)
    18. , Zoom(ZOOM)
    19. {
    20. this->Position = glm::vec3(pos_x, pos_y, pos_z);
    21. this->World_up = glm::vec3(up_x, up_y, up_z);
    22. this->Yaw = yaw;
    23. this->Pitch = pitch;
    24. UpdateCameraVectors();
    25. }
    26. Camera::~Camera()
    27. {
    28. }
    29. glm::mat4 Camera::GetViewMatrix()
    30. {
    31. return glm::lookAt(Position, Position + Forward, Up);
    32. }
    33. //对应键盘移动事件
    34. void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
    35. {
    36. float velocity = MovementSpeed * deltaTime;
    37. if (direction == FORWARD)
    38. Position += Forward * velocity;
    39. if (direction == BACKWARD)
    40. Position -= Forward * velocity;
    41. if (direction == LEFT)
    42. Position -= Right * velocity;
    43. if (direction == RIGHT)
    44. Position += Right * velocity;
    45. }
    46. //对应鼠标移动事件
    47. void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch)
    48. {
    49. xoffset *= Mouse_Sensiticity;
    50. yoffset *= Mouse_Sensiticity;
    51. Yaw += xoffset;
    52. Pitch += yoffset;
    53. if (constrainPitch)
    54. {
    55. if (Pitch > 89.0f)
    56. Pitch = 89.0f;
    57. if (Pitch < -89.0f)
    58. Pitch = -89.0f;
    59. }
    60. UpdateCameraVectors();
    61. }
    62. //对应鼠标滚轮事件
    63. void Camera::ProcessMouseScroll(float yoffset)
    64. {
    65. if (Zoom >= 1.0f && Zoom <= 45.0f)
    66. Zoom -= yoffset;
    67. if (Zoom <= 1.0f)
    68. Zoom = 1.0f;
    69. if (Zoom >= 45.0f)
    70. Zoom = 45.0f;
    71. }
    72. void Camera::UpdateCameraVectors()
    73. {
    74. glm::vec3 front;
    75. front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
    76. front.y = sin(glm::radians(Pitch));
    77. front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
    78. Forward = glm::normalize(front);
    79. Right = glm::normalize(glm::cross(Forward, World_up));
    80. Up = glm::normalize(glm::cross(Right, Forward));
    81. }

    Texture.cpp

    1. #include "Texture.h"
    2. #include "iostream"
    3. unsigned int Texture::LoadTextureFromFile(const char* path)
    4. {
    5. unsigned int texture_id;
    6. glGenTextures(1, &texture_id);
    7. int width, height, nr_channels;
    8. unsigned char *data = stbi_load(path, &width, &height, &nr_channels, 0);
    9. if (data)
    10. {
    11. GLenum format;
    12. if (nr_channels == 1)
    13. {
    14. format = GL_RED;
    15. }
    16. else if (nr_channels == 3)
    17. {
    18. format = GL_RGB;
    19. }
    20. else if (nr_channels == 4)
    21. {
    22. format = GL_RGBA;
    23. }
    24. glBindTexture(GL_TEXTURE_2D, texture_id);
    25. glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
    26. glGenerateMipmap(GL_TEXTURE_2D);
    27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
    28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    31. }
    32. else
    33. {
    34. std::cout << "Failed to load texture" << std::endl;
    35. }
    36. stbi_image_free(data);
    37. return texture_id;
    38. }

    输出结果:

     

  • 相关阅读:
    vue 动态渲染本地图片不显示的解决方法
    损失函数——机器学习
    并发编程day03
    判断一个字符串是否为另外一个字符串旋转之后的字符串
    1537170-85-6,DBCO-PEG4-acid,DBCO-PEG4-COOH,二苯并环辛炔-四聚乙二醇-羧酸科研用
    再说Mdx的字典文件处理
    GO语言-栈的应用-表达式求值
    反恐验厂所需材料清单
    微信小程序开发07 数据监控:善用数据驱动产品迭代
    [云原生 | k8s ]k8s中Pod、ReplicaSet、Deployment、Service的区别
  • 原文地址:https://blog.csdn.net/qq_51701007/article/details/126029401