- 定义view要用到的变量,注意cameraFront 用到的是-1
void processInput();
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraUP = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
- 键位控制函数:注意1.速度变量;2.左右移动用到的叉乘,并且规范化
void processInput() {
float cameraSpeed = 0.05f;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos +=cameraSpeed* cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos -= glm::normalize(glm::cross(cameraFront,cameraUP))* cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUP)) * cameraSpeed;
}
- 使用键位控制函数:每一个while循环中都要用到processInput来改变变量;修改view函数使用的参数值
while (!glfwWindowShouldClose(window)) {
processInput();
glClearColor(0.2f, 0.3f, 0.3f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
view = glm::lookAt(cameraPos,
cameraPos +cameraFront,
cameraUP);
- 为了保证每一次进入循环中的速度是一样的:对speed进行如下修改
float deltaTime = 0.0f;
float lastFrame = 0.0f;
float currentFrame = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput();
void processInput() {
float cameraSpeed = 2.5f*deltaTime;