简体   繁体   English

OpenGL / GLSL / GLM-Skybox旋转就像在第三人称中一样

[英]OpenGL/GLSL/GLM - Skybox rotates as if in 3rd person

I have just gotten into implementing skyboxes and am doing so with OpenGL/GLSL and GLM as my math library. 我刚开始实现天空盒,并且使用OpenGL / GLSL和GLM作为我的数学库来实现。 I assume the problem is matrix related and I haven't been able to find an implementation that utilizes the GLM library: 我认为问题与矩阵有关,并且我无法找到利用GLM库的实现:

The model for the skybox loads just fine, the camera however circles it as if it is rotating around it in 3d third person camera. 天空盒的模型可以很好地加载,但是相机会对其进行环绕,就像它在3d第三人称相机中围绕它旋转一样。

For my skybox matrix, I am updating it every time my camera updates. 对于我的Skybox矩阵,每次相机更新时都会对其进行更新。 Because I use glm::lookAt, it is essentially created the same way as my view matrix except I use 0, 0, 0 for the direction. 因为我使用glm :: lookAt,所以它的创建方式与我的视图矩阵基本相同,除了我将0、0、0用作方向。

Here is my view matrix creation. 这是我的视图矩阵创建。 It works fine in rendering of objects and geometry: 它可以很好地渲染对象和几何体:

direction = glm::vec3(cos(anglePitch) * sin(angleYaw), sin(anglePitch), cos(anglePitch) * cos(angleYaw));
right = glm::vec3(sin(angleYaw - 3.14f/2.0f), 0, cos(angleYaw - 3.14f/2.0f));
up = glm::cross(right, direction);
glm::mat4 viewMatrix = glm::lookAt(position, position+direction, up);

Similarly, my sky matrix is created in the same way with only one change: 类似地,我的天空矩阵是通过相同的方式创建的,只有一个更改:

glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 skyView = glm::lookAt(position, position + direction, up);

I know a skybox does not apply translation and only considers rotations so I am not sure what the issue is. 我知道Skybox不会应用翻译,而只会考虑旋转,因此我不确定问题出在哪里。 Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

Visual aids: 视觉辅助:

Straight on without any movement yet 继续前进,没有任何动静 当我第一次启动程序时直接

When I rotate the camera: 当我旋转相机时: 在此处输入图片说明

My question is this: how do I set up the correct matrix for rendering a skybox using glm:lookAt? 我的问题是:如何使用glm:lookAt设置正确的矩阵以渲染天空盒?

Aesthete is right skybox/skydome is only object that means you do not change projection matrix !!! Esthete是正确的skybox / skydome是仅表示不更改投影矩阵的对象!

your render should be something like this: 您的渲染应该是这样的:

  1. clear screen/buffers 清除屏幕/缓冲区
  2. set camera 设置相机
  3. set modelview to identity and then translate it to position of camera you can get the position directly from projection matrix (if my memory serves at array positions 12,13,14) to obtain the matrix see this https://stackoverflow.com/a/18039707/2521214 将modelview设置为identity,然后将其转换为相机的位置,您可以直接从投影矩阵获取位置(如果我的内存位于数组位置12,13,14)以获取矩阵,请参见此https://stackoverflow.com/a / 18039707/2521214
  4. draw skybox/skydome (do not cross your z_far plane,or disable Depth Test) 绘制天空盒/天空穹顶(请勿越过z_far平面,或禁用深度测试)
  5. optionaly clear Z-buffer or re-enable Depth Test 可选的清除Z缓冲区或重新启用深度测试
  6. draw your scene stuf .... ( do not forget to set modelview matrix for each of your drawed model) 绘制场景图....(不要忘记为每个绘制的模型设置modelview矩阵)

of course you can temporary set your camera position (projection matrix) to (0,0,0) and leave modelview matrix with identity, it is sometimes more precise approach but do not forget to set the camera position back after skybox draw. 当然,您可以将摄像机位置(投影矩阵)临时设置为(0,0,0)并保留具有相同标识的modelview矩阵,这有时是更精确的方法,但不要忘记在绘制天窗之后重新设置摄像机位置。

hope it helps. 希望能帮助到你。

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

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