i have a 3D scene with three equally spaced boxes, i want the camera to be able to rotate around the camera target and move in any direction, typically like the camera in a 3D modeling application, however my setup works only when my camera target is positioned at the origin, whenever i move the camera at any direction my camera position and camera target translates to the new position correctly, however my camera still rotates around the origin of the scene
the image below shows my camera target and rotation at the origin of coordinate system
the image below shows my camera target shifted left however the camera rotates with respect to the origin of the coordinate system
the following is my code for camera rotation and movement on x/y plane:
if (event.type == SDL_MOUSEMOTION && leftMouseButtonPressed == true)
{
New.x = event.button.x;
New.y = event.button.y;
delta = New - prev;
if (delta.x != 0) {
angleX = delta.x / 2.0f;
rotMat = glm::mat4(1);
rotMat = glm::rotate(rotMat, glm::radians(angleX), up);
cameraUp = glm::normalize(glm::vec4(up, 1) * rotMat);
cameraRight = glm::normalize(glm::cross(cameraUp, cameraDirection));
cameraPos = rotMat * glm::vec4(cameraPos, 1) ;
cameraDirection = glm::normalize(cameraPos - cameraTarget);
camera = glm::lookAt(cameraPos, cameraTarget, up);
}
if (event.type == SDL_MOUSEMOTION && rightMouseButtonPressed == true)
{
New.x = event.button.x;
New.y = event.button.y;
delta = New - prev;
if (delta.x != 0) {
delta.x /= 10.0f;
translateMat = glm::mat4(1);
translateMat = glm::translate(translateMat, cameraRight * delta.x);
cameraPos = translateMat * glm::vec4(cameraPos, 1);
cameraTarget = translateMat * glm::vec4(cameraTarget, 1) ;
camera = glm::lookAt(cameraPos, cameraTarget , cameraUp);
}
cameraPos
, you should keep track of the total rotation angle itself. It's more straightforward to construct the transformation from scratch than to accumulate it. – meowgoesthedog