当前位置:网站首页>【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
2022-06-29 16:33:00 【ycrsw】
1. Preface
This is a problem I encountered recently , As shown in the title , I need to drag the mouse to rotate the camera around the point , The original idea was simple , similar Note 8 、 The camera As mentioned in , From the perspective of focusing on the central point , Distance to move the mouse xoffset and yoffset Convert to yaw and pitch horn , Then calculate the vector of the angle of view , Intersect with the sphere to get the current position of the camera , But there is a serious problem in the actual process , It is the fatal defect of Euler angle —— Omnidirectional lock , If pitch Angular excess ±90°, We will encounter various problems , When designing the first perspective, we can control pitch Angle in 90° To -90° Between , It doesn't affect our experience , But if we want to control the camera to rotate the sphere , It may be wrong to be lazy , So can there be other ways to achieve this effect ? Of course there are , One is the quaternion , It can realize smooth interpolation transition of spherical rotation , But today I want to introduce another more intuitive implementation
2. Camera's spherical rotation
alike , We need to record the distance of each mouse movement xoffset and yoffset, How to use these two values ?
You can draw a diagram :
among A and B Is the point before and after the camera moves ,O Point is the focus of our camera , So what we have to do now is obvious , Originally, the camera's viewing angle was a parallel circular surface , We think of it as a projection plane , While moving the camera , We are in OAB Move on the formed circular surface , Then our task is to make use of OA Get OB
First, we calculate OA vector , Find the current rotation radius
void updateCameraVectors()
{
if (xoffset == 0 && yoffset == 0)
return;
glm::vec3 target, offset;
target = Position - Front;
double radius = glm::length(target);
target = glm::normalize(target);
After that, we compare the current camera up with OA The right vector of the projection plane is obtained by cross multiplication of the vectors
Right = glm::normalize(glm::cross(Up, target));
Second, use OA The vector and the obtained cross product get the upper vector of the projection plane
Up = glm::normalize(glm::cross(target, Right));
Then we find the vector corresponding to the screen displacement on the projection plane AB( Multiply the right vector and the upper vector of the projection plane by xy The displacement values are added )
offset = Up * yoffset + Right * xoffset ;
Then we convert the arc length of the displacement to OAB The radian of a circle is negative , Ensure that the relative rotation direction of the object is consistent with the direction of the mouse ( The camera rotation direction is opposite to the mouse displacement direction )
double len = glm::length(offset);
double angle = -len / radius;
offset = glm::normalize(offset);
Finally, we calculate the new target vector OB = (OAcos(OAB)+ABsin(OAB)), And multiply it by the length of the radius and add it to the focal point , So we can get the position of our camera
double cr = cos(angle) * radius, sr = sin(angle) * radius;
Position = Front + glm::vec3(target.x * cr + offset.x * sr, target.y * cr + offset.y * sr, target.z * cr + offset.z * sr);
}
3. effect

边栏推荐
- 『计组』CPU 如何区分指令和数据
- 我想网上注册股票开户,如何操作?另外,手机开户安全么?
- 南京大学:新时代数字化人才培养方案探讨
- leetcode:42. Rain water [double hands are elegant]
- 穩定幣風險狀况:USDT 和 USDC 安全嗎?
- Blue bridge cup several full array questions
- Apache atlas breakpoint view
- 2022年有哪些适合穷人的理财产品?
- 为防止被00后整顿,一公司招聘要求员工不能起诉公司
- Sophon autocv: help AI industrial production and realize visual intelligent perception
猜你喜欢

全面剖析Seata 分布式事务 AT 与XA

DAP large screen theme development description

我,大厂测试员,降薪50%去国企,后悔了...

Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog

In order to prevent being rectified after 00, a company requires employees not to sue the company

数学知识:求组合数 II—求组合数

如何配置 logback?30分鐘讓你徹底學會代碼熬夜敲

Self taught programming can understand the code, but what if you can't write it yourself

After studying this series of notes about software testing, it is a "bonus" to enter the factory

Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
随机推荐
MySQL error: expression 1 of order by claim is not in group by claim and contains nonaggregated column
Blue bridge cup several full array questions
How to distinguish between instructions and data in the "group counting" CPU
稳定币风险状况:USDT 和 USDC 安全吗?
隐私计算助力数据的安全流通与共享
Key wizard play monster learning - multi window and multi thread background judgment of character, pet blood volume and pet happiness
In order to prevent being rectified after 00, a company requires employees not to sue the company
Sophon autocv: help AI industrial production and realize visual intelligent perception
Nanjing University: Discussion on the training scheme of digital talents in the new era
元数据管理Apache Atlas编译集成部署及测试
ssl免费证书申请,免费的ssl证书实际效果怎么样啊?
UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
[rust daily] rust 2021 stability
STM32按键消抖——入门状态机思维
DTCC2022 中国数据库技术大会最新议程出炉
Small programs have a "big" role in the industrial Internet
Accelerate the implementation of intelligent driving projects? You still lack a truth evaluation system
[proteus simulation] progressive increase / decrease of nixie tube with flashing blanking display
实战 | Change Detection And Batch Update
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台