简体   繁体   English

Opengl 相机绕X旋转

[英]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.在 Visual Studio 中处理 opengl 项目。我试图围绕 X 和 Y 轴旋转相机。 Thats the math i should use这就是我应该使用的数学

Im having trouble because im using glm::lookAt for camera position and it takes glm::vec3 as arguments.我遇到了麻烦,因为我将 glm::lookAt 用于相机 position 并且它需要 glm::vec3 作为 arguments。 Can someone explain how can i implement this in opengl?有人可以解释我如何在 opengl 中实现这个吗?

PS:i cant use quaternions PS:我不能使用四元数

The lookAt function should take three inputs:外观 function 应采用三个输入:

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,它们是你在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:)由于移动相机相当于将 object 移动到反向移动,同时保持相机不动:)

Below the camera is rotated around the z-axis.摄像机下方围绕 z 轴旋转。

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);

粗漆绘图

Check out https://learnopengl.com/ , its a great site to learn!查看https://learnopengl.com/ ,这是一个学习的好网站!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM