20

What is the convention OpenGL follows for cubemaps?

I followed this convention (found on a website) and used the correspondent GLenum to specify the 6 faces GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT but I always get wrong Y, so I have to invert Positive Y with Negative Y face. Why?

          ________
         |        |
         | pos y  |
         |        |
  _______|________|_________________
 |       |        |        |        |
 | neg x | pos z  |  pos x |  neg z |
 |       |        |        |        |
 |_______|________|________|________|
         |        |
         |        |
         | neg y  |
         |________|
linello
  • 8,451
  • 18
  • 63
  • 109

2 Answers2

23

but I always get wrong Y, so I have to invert Positive Y with Negative Y face. Why?

Ah, yes, this is one of the most odd things about Cube Maps. Rest assured, you're not the only one to fall for it. You see:

Cube Maps have been specified to follow the RenderMan specification (for whatever reason), and RenderMan assumes the images' origin being in the upper left, contrary to the usual OpenGL behaviour of having the image origin in the lower left. That's why things get swapped in the Y direction. It totally breaks with the usual OpenGL semantics and doesn't make sense at all. But now we're stuck with it.

Take note that upper left, vs. lower left are defined in the context of identity transformation from model space to NDC space

datenwolf
  • 159,371
  • 13
  • 185
  • 298
17

Here is a convenient diagram showing how the axes work in OpenGL cubemaps:

enter image description here

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • This image clarifies everything now! Thanks! – linello Jul 30 '12 at 09:10
  • 2
    Doesn't -z in OpenGL normally point forward instead of +z? Is this different for cubemaps? Also, if the bottom left of the Positive X texture is the bottom, furthest, right hand corner of the cube, isn't -Y pointing in the wrong direction compared to your axes? – user673679 Nov 10 '14 at 16:24
  • "*Doesn't -z in OpenGL normally point forward instead of +z?*" Not in cubemaps. The spec is quite clear on this. "*if the bottom left of the Positive X texture is the bottom, furthest, right hand corner of the cube*" It isn't. The bottom left of the +X face maps to the *top*, right, far corner of the cube. Remember: OpenGL textures tend to be upside-down, due to the way that GL uses a bottom-left coordinate system for its faces. – Nicol Bolas Mar 13 '16 at 14:55
  • Does the above image show cube map faces from inside (cube map center) or outside ? – Irbis Sep 30 '19 at 08:13
  • @Irbis: Cubemaps compute the texel from a directional texture coordinate as if the direction were inside of the cube. So the faces represent the inside of the cube, as does the above diagram. – Nicol Bolas Sep 30 '19 at 13:42
  • When I'm sampling in the shader, do I have to also take into account that the Z is flipped? i.e if I have a world-space direction, should I invert the Z of this direction to sample the cubemap correctly? I have made a small experiment and it looks like, yes, the Z must be inverted. But I'm not completely sure, maybe I have a mistake elsewhere. – tuket Oct 26 '21 at 20:23
  • A) @Irbis and Nicol Bolas, sorry for necroposting but I'm glad you mentioned this. I realise that there are at least 3 things to take into account: 1) The cubemap uses a left-handed instead of right-handed coordinate system. 2) The cubemap textures convention is that "down" in texture space is the positive, not negative direction, and 3) If you want to use the cubemap as a skybox, then you are looking at its faces from *inside* the cube, but if you want to use it to texture an object, e.g. an approximate sphere (which is one way to avoid problems with seams, when using an equirectangular... – Simon Jun 11 '22 at 14:05
  • B) ...aka panoramic projection - i.e. convert it to the 6 cubemap projections first, e.g. using this: https://jaxry.github.io/panorama-to-cubemap/) then you are looking at the faces from *outside* the cube. – Simon Jun 11 '22 at 14:07