ccc
ccc

Reputation: 55

Have problem understanding translating the camera in opengl

I am having trouble about understand the translate the camera. I already can successfully rotate the camera, but I am still confused about translating the camera. I include the code about how to rotate the camera, Since translating and rotating need to use the lookat function. The homework says translating the camera means that both the eye and the center should be moved with the same amount. I understand I can change the parameters in the lookat function to implement this.

The definition of lookat function is below:

Lookat(cameraPos, center, up)
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
glm::vec3 center(0.0f, 0.0f, 0.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);

modelViewProjectionMatrix.Perspective(glm::radians(fov), float(width) / float(height), 0.1f, 100.0f);
modelViewProjectionMatrix.LookAt(cameraPos, center, cameraUp);
void CursorPositionCallback(GLFWwindow* lWindow, double xpos, double ypos)
{
    int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);

    if (state == GLFW_PRESS)
    {
        if (firstMouse)
        {
          lastX = xpos;
          lastY = ypos;
          firstMouse = false;
        }

        float xoffset = xpos - lastX;
        float yoffset = lastY- ypos; 
        lastX = xpos;
        lastY = ypos;

        yaw += xoffset;
        pitch += yoffset;

        glm::vec3 front;
        front.x = center[0] + 5.0f*cos(glm::radians(yaw)) * cos(glm::radians(pitch));
        front.y = center[1] + 5.0f*sin(glm::radians(pitch));
        front.z = center[1] + 5.0f*sin(glm::radians(yaw)) * cos(glm::radians(pitch));
        cameraPos = front;
    }
}

Upvotes: 2

Views: 297

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

If you want to translate the camera by an offset, then you've to add the same vector (glm::vec3 offset) to the camera position (cameraPos) and the camera target (center):

center    = center    + offset;
cameraPos = cameraPos + offset;

When you calculate a new target of the camera (center), by a pitch and yaw angle, then you've to update the up vector (cameraUp) of the camera, too:

glm::vec3 front(
    cos(glm::radians(pitch)) * cos(glm::radians(yaw)),
    sin(glm::radians(pitch)),
    cos(glm::radians(pitch)) * sin(glm::radians(yaw))
);

glm::vec3 up(
    -sin(glm::radians(pitch)) * cos(glm::radians(yaw)),
     cos(glm::radians(pitch)),
    -sin(glm::radians(pitch)) * sin(glm::radians(yaw))
);

cameraPos = center + front * 5.0f;
cameraUp  = up;

To translate the camera along the x axis (from left to right) in viewspace you've to calculate the vector to the right by the Cross product of the vector to the target (front) and the up vector (cameraUp or up):

glm::vec3 right = glm::cross(front, up);

The y axis (from bottom to top) in viewspace, is the up vector.

To translate about the scalars (float trans_x) and (trans_y), the scaled right and up vector have to be add to the camera position (cameraPos) and the camera target (center):

center    = center    + right * trans_x + up * trans_y;
cameraPos = cameraPos + right * trans_x + up * trans_y;

Use the manipulated vectors to set the view matrix:

modelViewProjectionMatrix.LookAt(cameraPos, center, cameraUp);

Upvotes: 1

Related Questions