简体   繁体   English

使用GLKit加载立方体贴图后,iOS上openGL ES 2.0的正确立方体贴图坐标是什么?

[英]What are the correct cube map coordinates for openGL ES 2.0 on iOS after loading the cube map with GLKit?

In order to debug my shader, I am trying to display the just the front face of the cube map. 为了调试着色器,我试图仅显示立方体贴图的正面。 The cube map is a 125x750 image with the 6 faces on top of each other: 立方体贴图是一张125x750的图像,六个面彼此重叠:

多维数据集地图

First, I load the cube map with GLKit: 首先,我使用GLKit加载多维数据集映射:

_cubeTexture = [GLKTextureLoader cubeMapWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"uffizi_cube_map_ios" ofType:@"png"] options:kNilOptions error:&error];

Then I load it into the shader: 然后将其加载到着色器中:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, self.cubeTexture.name);
glUniform1i( glGetUniformLocation( self.shaderProgram, "cube"), 0);

Then in the fragment shader: 然后在片段着色器中:

gl_FragColor = textureCube(cube, vec3(-1.0+2.0*(gl_FragCoord.x/resolution.x),-1.0+2.0*(gl_FragCoord.y/resolution.y),1.0));

This displays a distorted image which seems to be a portion of the top of the cube map: 这将显示扭曲的图像,该图像似乎是立方体贴图顶部的一部分:

影像失真

It shouldn't be distorted, and it should show the right face, not the top face. 它不应该失真,并且应该显示正确的面孔,而不是顶面。

I can't find any documentation that describes how the coordinates map to the cube, so what am I doing wrong? 我找不到任何描述坐标如何映射到多维数据集的文档,那我在做什么错呢?

It seems that there is a problem with cubeMapWithContentsOfFile. 似乎cubeMapWithContentsOfFile有问题。 The cubeMapWithContentsOfFiles method (the one that takes an array of 6 images) works perfectly on the simulator. cubeMapWithContentsOfFiles方法(需要6个图像的数组)可以在模拟器上完美运行。 (There is a different issue with both methods on device). (设备上的两种方法都有一个不同的问题)。

To visualize how texture coordinates work for cube maps, picture a cube centered at the origin, with the faces at distance 1 from the origin, and with the specified cube map image on each face. 若要可视化纹理坐标对立方体贴图的工作方式,请绘制一个以原点为中心的立方体,其面与原点的距离为1,并且每个面上都具有指定的立方体贴图图像。

The texture coordinates can then be seen as direction vectors. 然后可以将纹理坐标视为方向向量。 Starting at the origin, the 3 components define a vector that can point in any direction. 从原点开始,这三个分量定义了可以指向任何方向的向量。 The ray defined by the vector will then intersect one of the 6 cube faces at a given point. 然后,由矢量定义的射线将在给定点与6个立方体面之一相交。 This is the point where the corresponding cube map image is sampled during texturing. 这是在纹理化过程中采样相应的立方体贴图图像的点。

For example, take a vector that points in a direction that is closest to the positive z axis. 例如,采用指向最接近正z轴的方向的向量。 The ray defined by this vector intersects the top face of the cube. 该矢量定义的光线与立方体的顶面相交。 Therefore, the top ( POSITIVE_Z ) image of the cube map is sampled, at the point where the ray intersects the face. 因此,在光线与人脸相交的点处对立方体贴图的顶部( POSITIVE_Z )图像进行采样。

Equivalent rules applies to all other directions. 等效规则适用于所有其他方向。 The face corresponding to the largest absolute value of one of the vector components determines which face is sampled, and the intersection point determines the position within the image. 与矢量分量之一的最大绝对值相对应的面部确定了要采样的面部,而交点则确定了图像内的位置。

The exact rules and formula can be found in the spec document. 确切的规则和公式可在规格文档中找到。 For example in the latest spec (OpenGL 4.5), see Section 8.13 "Cube Map Texture Selection", with the matching table 8.19. 例如,在最新的规范(OpenGL 4.5)中,请参见第8.13节“多维数据集贴图纹理选择”,以及匹配表8.19。 But as long as you understand that the texture coordinates define a direction vector, you have the main aspect covered. 但是,只要您了解纹理坐标定义了方向矢量,就可以涵盖主要方面。

How you determine the texture coordinates really depends on what you want to achieve. 您如何确定纹理坐标确实取决于您要实现的目标。 Common cases include: 常见的情况包括:

  • Using normal vector as the cube map texture coordinates. 使用法线向量作为立方体贴图的纹理坐标。 This can for example be used for pre-computed lighting effects, where the content of the cube map image contains pre-computed lighting results for each possible normal direction. 例如,这可以用于预先计算的照明效果,其中立方体贴图图像的内容包含每个可能的法线方向的预先计算的照明结果。
  • Using the reflection vector as the cube map texture coordinate. 使用反射向量作为立方体贴图纹理坐标。 This supports the implementation of environment mapping. 这支持环境映射的实现。 The content of the cube map is a picture of the environment. 立方体贴图的内容是环境的图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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