【问题标题】:How to rotate around "camera" in opengl and c++ [closed]如何在opengl和c ++中围绕“相机”旋转[关闭]
【发布时间】:2017-06-06 08:09:33
【问题描述】:

我的问题是 OpenGL 总是围绕 (0,0,0) 旋转。但如果我翻译 “相机”,例如,如果我向后移动,它也会围绕 (0,0,0) 旋转。但是我需要围绕相机旋转,所以我必须将这些点平移回 (0,0,0),旋转并将它们平移到它们的旧位置。
但是这里出现错误:旋转后,平移是相对于旋转的。
那么,我该如何翻译回来呢?
PS:我不想使用 gluLookAt()

所以我相对于相机旋转的代码是:

//set rotation to zero
glRotatef(-yr,0.0f,1.0f,0.0f);  
glRotatef(-xr,1.0f,0.0f,0.0f);
//set points relative to camera
glTranslatef(-cam_pos[0],-cam_pos[1],-cam_pos[2]);
//reset rotation
glRotatef(yr,0.0f,1.0f,0.0f);   
glRotatef(xr,1.0f,0.0f,0.0f);
//rotate
glRotatef(angley,0.0f,1.0f,0.0f);   
glRotatef(anglex,1.0f,0.0f,0.0f);
//and now, how to translate back,
//movement is relative to the cam ?
//I would simply use :  
//glTranslatef(cam_pos[0],cam_pos[1],cam_pos[2]);
//which wont work because its relative to the new rotation.

【问题讨论】:

  • 你能分享你的代码来“翻译”相机吗?
  • 可以,但没关系。
  • 请提供MCVE 并尝试更明确地说明您的问题,这个问题对我来说很不清楚
  • 我会再问这个问题,因为几乎没有 [on hold] 问题回来。

标签: c++ opengl rotation


【解决方案1】:

OpenGL 不直接支持相机变换。它假设相机始终位于精确的中心 (0, 0, 0)。因此,您应该使用反向相机变换来变换所有光源、所有对象和所有其他事物(所有场景)。

gluLookAt() 使用“反转相机变换矩阵”变换元素。如果你不想使用它,你应该使用OpenGL的模型转换方法。

https://stackoverflow.com/a/3483744/4725899 https://www.gamedev.net/topic/421529-manual-alternative-to-glulookat-/

来自OpenGL's documentation;

就 OpenGL 而言,没有相机。进一步来说, 相机始终位于眼睛空间坐标 (0., 0., 0.)。 为了给出移动相机的外观,您的 OpenGL 应用程序 必须以与相机变换相反的方式移动场景。

【讨论】:

  • 对不起,但这并不是我真正要搜索的内容。希望我的新代码更清楚。
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 2012-12-27
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2015-02-07
相关资源
最近更新 更多