kostger
kostger

Reputation: 11

Opengl Camera rotation around X

Working on an opengl project in visual studio.Im trying to rotate the camera around the X and Y axis. Thats the math i should use

Im having trouble because im using glm::lookAt for camera position and it takes glm::vec3 as arguments. Can someone explain how can i implement this in opengl?

PS:i cant use quaternions

Upvotes: 0

Views: 1497

Answers (2)

David Håland
David Håland

Reputation: 55

Below the camera is rotated around the z-axis.

const float radius = 10.0f;
float camX = sin(time) * radius;
float camZ = cos(time) * radius;
glm::vec3 cameraPos = glm::vec3(camX, 0.0, camZ);
glm::vec3 objectPos = glm::vec3(0.0, 0.0, 0.0);
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0);
glm::mat4 view = glm::lookAt(cameraPos, objectPos, up);

crude paint drawing

Check out https://learnopengl.com/, its a great site to learn!

Upvotes: 1

Alasses
Alasses

Reputation: 16

The lookAt function should take three inputs:

vec3 cameraPosition
vec3 cameraLookAt
vec3 cameraUp 

For my past experience, if you want to move the camera, first find the transform matrix of the movement, then apply the matrix onto these three vectors, and the result will be three new vec3, which are your new input into the lookAt function.

vec3 newCameraPosition = movementMat4 * cameraPosition
//Same for other two

Another approach could be finding the inverse movement of the one you want the camera to do and applying it to the whole scene. Since moving the camera is kind of equals to move the object onto inverse movement while keep the camera not move :)

Upvotes: 0

Related Questions