http://www.3dcpptutorials.sk/index.php?id=24
https://stackoverflow.com/questions/11685608/convention-of-faces-in-opengl-cubemapping
https://blog.youkuaiyun.com/Nhsoft/article/details/1398630
CubeMap贴图的问题困扰了我一个早上.
让我们从头分析一下.
0 翻转纹理
在使用 stb
导入纹理的时候.
经常需要设置 stbi_set_flip_vertically_on_load(ture)
来实现翻转y坐标.
未开启翻转:
开启翻转:
这是为什么呢?
因为 OpenGL的纹理坐标起点在于左下角. 而图像纹理坐标的起点在于左上角.
1 问题的出现
按照正常的方式贴图. 即:
stbi_set_flip_vertically_on_load(ture)
这时的对应方式为:
0 = +X = right
1 = -X = left
2 = +Y = top
3 = -Y = bottom
4 = +Z = front
5 = -Z = back
(+y) | (2) | (to)
(-x) (+z) (+x) (-z) | (1) (4) (0) (5) | (le) (fr) (ri) (ba)
(-y) | (3) | (bo)
这时候会出现以下问题:
左 / 右 / 前 / 后的 图像上下颠倒了(旋转了180度).而 前 / 后的 图像位置颠倒了(前后交换了).
这里. 我主要想知道为什么会出现这种情况?
在 OpenGL 2.1 specification
中有说明.
chapter 3.8.6 of the OpenGL 2.1 specification and the documentation of the textureCube function in the chapter 8.7 of the GLSL 1.2 specification.
Cube Map Texture Selection
- 在CubeMap中. 纹理坐标
(s,t,r)
被作为 向量 ( r x r_x rx, r y r_y ry, r z r_z rz ) 来对待!!! - 下面表格的作用是: 在每个主方向上. 纹理坐标是怎么定义的
更形象的:
为什么会是这样呢? 我猜是从每一个面的外侧看. 都是纹理坐标都在左上角.
所以, 当 stbi_set_flip_vertically_on_load(ture)
时.
就会出现:前 / 后 / 左 / 右 的纹理颠倒了180°的情况.
2 解决方法
解决方法可以看 GLSL cube mapping
2.1 交换对应 +y 和 -y 对应关系. 从而得到连续的纹理
char *CubeMapTextureFiles[6] = {"right.jpg", "left.jpg", "bottom.jpg", "top.jpg", "front.jpg", "back.jpg"};
0 = +X = right
1 = -X = left
2 = +Y = bottom
3 = -Y = top
4 = +Z = front
5 = -Z = back
(+y) | (2) | (bo)
(-x) (-z) (+x) (+z) | (1) (5) (0) (4) | (le) (ba) (ri) (fr)
(-y) | (3) | (to)
2.2 在顶点着色器中. 旋转纹理采样向量180°
rotate the cube map texture coordinates 180 degrees around the X axis in the sky.vs vertex shader.
TexCoords = vec3(position.x,-position.yz);
(-y) | (3) | (to)
(-x) (+z) (+x) (-z) | (1) (4) (0) (5) | (le) (fr) (ri) (ba)
(+y) | (2) | (bo)