// `gCamera.right()` returns a unit vector, therefore `leftDirection` will also be a unit vector. // Negation only affects the direction, not the magnitude. glm::vec3 leftDirection = -gCamera.right(); //`displacement` will have a magnitude of 12 glm::vec3 displacement = leftDirection * 12; //`newPosition` will be 12 units to the left of `oldPosition` glm::vec3 newPosition = oldPosition + displacement;
// the commented-out code below was removed /* gProgram->use();
//set the "projection" uniform in the vertex shader, because it's not going to change glm::mat4 projection = glm::perspective<float>(50.0, SCREEN_SIZE.x/SCREEN_SIZE.y, 0.1, 10.0); //glm::mat4 projection = glm::ortho<float>(-2, 2, -2, 2, 0.1, 10); gProgram->setUniform("projection", projection);
//set the "camera" uniform in the vertex shader, because it's also not going to change glm::mat4 camera = glm::lookAt(glm::vec3(3,3,3), glm::vec3(0,0,0), glm::vec3(0,1,0)); gProgram->setUniform("camera", camera);
gProgram->stopUsing(); */ }
然后,在Render函数中设置camera着色器变量:
1 2 3 4 5 6 7 8 9 10 11
// draws a single frame static void Render() { // clear everything glClearColor(0, 0, 0, 1); // black glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind the program (the shaders) gProgram->use();
// set the "camera" uniform gProgram->setUniform("camera", gCamera.matrix());
// update the scene based on the time elapsed since last update void Update(float secondsElapsed) { //rotate the cube const GLfloat degreesPerSecond = 180.0f; gDegreesRotated += secondsElapsed * degreesPerSecond; while(gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f;
//move position of camera based on WASD keys const float moveSpeed = 2.0; //units per second if(glfwGetKey(gWindow, 'S')){ gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.forward()); } else if(glfwGetKey(gWindow, 'W')){ gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.forward()); } if(glfwGetKey(gWindow, 'A')){ gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.right()); } else if(glfwGetKey(gWindow, 'D')){ gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.right()); } }
void MoveCameraBackwards(float secondsElapsed) { //`direction` is a unit vector, set to the "backwards" direction glm::vec3 direction = -gCamera.forward();
void MoveCameraBackwards(float secondsElapsed) { //`direction` is a unit vector, set to the "backwards" direction glm::vec3 direction = -gCamera.forwards();
//`distance` is the total distance to move the camera float distance = moveSpeed * secondsElapsed;
void MoveCameraBackwards(float secondsElapsed) { //`direction` is a unit vector, set to the "backwards" direction glm::vec3 direction = -gCamera.forwards(); //vector negation
//`distance` is the total distance to move the camera float distance = moveSpeed * secondsElapsed;
//`displacement` is a combination of `distance` and `direction` glm::vec3 displacement = distance * direction; //scalar multiplication
void MoveCameraBackwards(float secondsElapsed) { //`direction` is a unit vector, set to the "backwards" direction glm::vec3 direction = -gCamera.forwards(); //vector negation
//`distance` is the total distance to move the camera float distance = moveSpeed * secondsElapsed;
//`displacement` is a combination of `distance` and `direction` glm::vec3 displacement = distance * direction; //scalar multiplication
//change the position of the camera glm::vec3 oldPosition = gCamera.position(); glm::vec3 newPosition = oldPosition + displacement; //vector addition gCamera.setPosition(newPosition); }
// run while the window is open double lastTime = glfwGetTime(); while(!glfwWindowShouldClose(gWindow)){ // process pending events glfwPollEvents();
// update the scene based on the time elapsed since last update double thisTime = glfwGetTime(); Update((float)(thisTime - lastTime)); lastTime = thisTime;
//rotate camera based on mouse movement const float mouseSensitivity = 0.1f; double mouseX, mouseY; glfwGetCursorPos(gWindow, &mouseX, &mouseY); gCamera.offsetOrientation(mouseSensitivity * (float)mouseY, mouseSensitivity * (float)mouseX); glfwSetCursorPos(gWindow, 0, 0); //reset the mouse, so it doesn't go out of the window