简体   繁体   中英

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. I assume the problem is matrix related and I haven't been able to find an implementation that utilizes the GLM library:

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.

For my skybox matrix, I am updating it every time my camera updates. 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.

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. 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?

Aesthete is right skybox/skydome is only object that means you do not change projection matrix !!!

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
  4. draw skybox/skydome (do not cross your z_far plane,or disable Depth Test)
  5. optionaly clear Z-buffer or re-enable Depth Test
  6. draw your scene stuf .... ( do not forget to set modelview matrix for each of your drawed model)

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.

hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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