图形学基础 | OpenGL CubeMap图像方向问题

http://www.3dcpptutorials.sk/index.php?id=24
https://stackoverflow.com/questions/11685608/convention-of-faces-in-opengl-cubemapping
https://blog.csdn.net/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

  1. 在CubeMap中. 纹理坐标 (s,t,r) 被作为 向量 ( r x r_x rx, r y r_y ry, r z r_z rz ) 来对待!!!
  2. 下面表格的作用是: 在每个主方向上. 纹理坐标是怎么定义的
    在这里插入图片描述
    在这里插入图片描述
    更形象的:
    在这里插入图片描述
    为什么会是这样呢? 我猜是从每一个面的外侧看. 都是纹理坐标都在左上角.

所以, 当 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)
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值