锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

【OpenGL】笔记九、相机类

时间:2023-08-11 09:07:02 电感磁珠upz1005d121

1.流程

在最后一节完成相机的各种操作后,我们的主程序代码充满了各种全球变量和未知的函数调用。为了使代码更简单、更容易读取,我们还定制了一个相机类别

1.1 Camera类

首先给出Camera类别的粗略定义,首先是其坐标向量:

class Camera { 
         public:     // camera Attributes     glm::vec3 Position;     glm::vec3 Front;     glm::vec3 Up;     glm::vec3 Right;     glm::vec3 WorldUp; 

还有它的两个角yaw和pitch:

    // euler Angles     float Yaw;     float Pitch; 

最后,设置相机移动速度、鼠标敏感度和fov:

    // camera options     float MovementSpeed;     float MouseSensitivity;     float Zoom; 

接下来设置camera两个构造函数,一个默认,一个赋值:
(本文件可设置默认值)

// Default camera values 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; 
    // constructor with vectors     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) : ront(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
    { 
        
        Position = position;
        WorldUp = up;
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }
    // constructor with scalar values
    Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
    { 
        
        Position = glm::vec3(posX, posY, posZ);
        WorldUp = glm::vec3(upX, upY, upZ);
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }

剩下的就是上节中对相机移动转向和缩放的函数了:

    void ProcessKeyboard(Camera_Movement direction, float deltaTime);
    void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true);
    void ProcessMouseScroll(float yoffset);
}  

其中Camera_Movement其实是一个枚举类,包含相机可移动的四个方向,仅为使代码更加易读:

// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
enum Camera_Movement { 
        
    FORWARD,
    BACKWARD,
    LEFT,
    RIGHT
};

1.2 三个回调函数

具体的代码其实和之前的差不多:

// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
    void ProcessKeyboard(Camera_Movement direction, float deltaTime)
    { 
        
        float velocity = MovementSpeed * deltaTime;
        if (direction == FORWARD)
            Position += Front * velocity;
        if (direction == BACKWARD)
            Position -= Front * velocity;
        if (direction == LEFT)
            Position -= Right * velocity;
        if (direction == RIGHT)
            Position += Right * velocity;
    }

    // processes input received from a mouse input system. Expects the offset value in both the x and y direction.
    void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
    { 
        
        xoffset *= MouseSensitivity;
        yoffset *= MouseSensitivity;

        Yaw += xoffset;
        Pitch += yoffset;

        // make sure that when pitch is out of bounds, screen doesn't get flipped
        if (constrainPitch)
        { 
        
            if (Pitch > 89.0f)
                Pitch = 89.0f;
            if (Pitch < -89.0f)
                Pitch = -89.0f;
        }

        // update Front, Right and Up Vectors using the updated Euler angles
        updateCameraVectors();
    }

    // processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
    void ProcessMouseScroll(float yoffset)
    { 
        
        Zoom -= (float)yoffset;
        if (Zoom < 1.0f)
            Zoom = 1.0f;
        if (Zoom > 45.0f)
            Zoom = 45.0f;
    }

1.3 主程序中回调函数的修改

那么我们该怎么在主程序中调用这几个方法呢?其实很简单,只在之前设置的几个回调函数中将这几个方法替换原来的赋值代码就行了:

void processInput(GLFWwindow* window)
{ 
        
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)//按下ESC退出
        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);
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ 
        
    glViewport(0, 0, width, height);//前两个参数控制窗口左下角的位置,第三个和第四个参数控制渲染窗口的宽度和高度(像素)
}

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(static_cast<float>(yoffset));
}

1.4 view矩阵

如果要将更多方法封装进camera类的话,还可以将view矩阵的计算加入:

	// returns the view matrix calculated using Euler Angles and the LookAt Matrix
    glm::mat4 GetViewMatrix()
    { 
        
        return glm::lookAt(Position, Position + Front, Up);
    }
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章