4

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 Straight on when I first start the program

When I rotate the camera: enter image description here

My question is this: how do I set up the correct matrix for rendering a skybox using glm:lookAt?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Satchmo Brown
  • 1,409
  • 2
  • 20
  • 47
  • *the camera however circles it as if it is rotating around it in 3d third person camera* - I'm having a difficult time visualizing what you mean by this, maybe an image or a short youtube clip could help? – Tim Aug 22 '12 at 00:50
  • Are you sure that the dome is centered around (0,0,0)? Looks like the vertices might be off center. – Tim Aug 22 '12 at 02:13
  • I guess my question is: how do I set up my matrix for rendering the sky with glm::lookAt? – Satchmo Brown Aug 22 '12 at 07:34
  • Isn't the skybox just an object with its own transform matrix? I don't understand where lookAt would be used? – Aesthete Aug 22 '12 at 08:00
  • Satchmo, can you add the source data for your skybox model to the question? I think it will be relevant to the answer. – Tim Aug 22 '12 at 14:46
  • @Tim I ended up solving it. Answer is below. – Satchmo Brown Aug 25 '12 at 00:09

1 Answers1

2

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.

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380