Skip to content

Commit

Permalink
[Week8-1]实现摄像机视角移动的鼠标和滚轮事件
Browse files Browse the repository at this point in the history
  • Loading branch information
toulzx committed Nov 13, 2021
1 parent 8c28056 commit 1f0d24d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
35 changes: 31 additions & 4 deletions Source/HelloCG/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const GLfloat DEFAULT_PITCH = 0.0f;
// 视域(控制缩放)
const GLfloat DEFAULT_ZOOM = 45.0f;
// 相机移动速度
const GLfloat DEFAULT_SPEED = 6.0f;
const GLfloat DEFAULT_SPEED = 6.0f;
// 鼠标灵敏程度
const GLfloat DEFAULT_SENSITIVITY = 0.1f;

const glm::vec3 DEFAULT_POSITION = glm::vec3(0.0f, 0.0f, 0.0f);

Expand Down Expand Up @@ -48,7 +50,8 @@ class Camera
pitch(DEFAULT_PITCH),
movementSpeed(DEFAULT_SPEED),
cameraFront(DEFAULT_CAMERA_FRONT),
zoom(DEFAULT_ZOOM)
zoom(DEFAULT_ZOOM),
mouseSensitivity(DEFAULT_SENSITIVITY)
{
this->updateCameraVectors();
}
Expand All @@ -64,6 +67,7 @@ class Camera
this->movementSpeed = DEFAULT_SPEED;
this->cameraFront = DEFAULT_CAMERA_FRONT;
this->zoom = DEFAULT_ZOOM;
this->mouseSensitivity = DEFAULT_SENSITIVITY;
this->updateCameraVectors();
}
*/
Expand All @@ -73,7 +77,7 @@ class Camera
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
GLfloat yaw = DEFAULT_YAW,
GLfloat pitch = DEFAULT_PITCH) :cameraFront(glm::vec3(0.0f, 0.0f, -1.0f)), zoom(DEFAULT_ZOOM), movementSpeed(DEFAULT_SPEED)
GLfloat pitch = DEFAULT_PITCH) :cameraFront(glm::vec3(0.0f, 0.0f, -1.0f)), zoom(DEFAULT_ZOOM), movementSpeed(DEFAULT_SPEED), mouseSensitivity(DEFAULT_SENSITIVITY)
{
this->position = position;
this->worldUp = up;
Expand Down Expand Up @@ -116,13 +120,36 @@ class Camera
}
}

void ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset)
{
xOffset *= this->mouseSensitivity;
yOffset *= this->mouseSensitivity;

this->yaw += xOffset;
this->pitch += yOffset;

this->updateCameraVectors();

}

void ProcessScroll(GLfloat xOffset, GLfloat yOffset)
{
if (this->zoom >= 1.0f && this->zoom <= 45.0f)
this->zoom -= yOffset;
if (this->zoom <= 1.0f)
this->zoom = 1.0f;
if (this->zoom >= DEFAULT_ZOOM)
this->zoom = 45.0f;
}


private:
GLfloat yaw;
GLfloat pitch;
GLfloat pitch;
GLfloat zoom;

GLfloat movementSpeed;
GLfloat mouseSensitivity;

glm::vec3 cameraPosition;

Expand Down
37 changes: 36 additions & 1 deletion Source/HelloCG/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ bool keys[1024];
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode);
// 处理相机前后左右移动
void DoMovement();
// 实现滚轮触发操作
void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset);
// 实现鼠标移动触发操作
void MouseCallback(GLFWwindow* window, double xPos, double yPos);

// 用摄像机坐标对相机视角的实例化
Camera camera(glm::vec3(0.0f, 0.0f, 2.0f));

GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;

// 判断是否是第一次获取鼠标位置
bool firstMouse = true;

GLfloat lastX = WIDTH / 2.0f;
GLfloat lastY = HEIGHT / 2.0f;


int main()
{
Expand Down Expand Up @@ -56,8 +67,10 @@ int main()
// 设置焦点为当前窗口
glfwMakeContextCurrent(window);

// 在 GLFW 中注册键盘响应
// 在 GLFW 中注册响应
glfwSetKeyCallback(window, KeyCallback);
glfwSetCursorPosCallback(window, MouseCallback);
glfwSetScrollCallback(window, ScrollCallback);


glewExperimental = GL_TRUE;
Expand Down Expand Up @@ -249,3 +262,25 @@ void DoMovement()
}

}

void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset)
{
camera.ProcessScroll(xOffset, yOffset);
}

void MouseCallback(GLFWwindow* window, double xPos, double yPos)
{
if (firstMouse)
{
lastX = xPos;
lastY = yPos;
firstMouse = false;
}

GLfloat xOffset = xPos - lastX;
GLfloat yOffset = lastY - yPos;

lastX = xPos;
lastY = yPos;
camera.ProcessMouseMovement(xOffset, yOffset);
}

0 comments on commit 1f0d24d

Please sign in to comment.