我正在尝试在我的 C++ OpenGL 项目中实现 Skybox,并且我想使用从我的纹理库中检索的 6 个预加载的
GL_TEXTURE_2D
,它将所有纹理加载到应用程序中作为 GL_TEXTURE_2D
。
我遇到的问题是我不能对
GL_TEXTURE_2D
(据我所知)的每个面使用GL_TEXTURE_CUBE_MAP
。我尝试了各种不同的方法,但是似乎我无法将图像数据从GL_TEXTURE_2D
引用或复制到GL_TEXTURE_CUBE_MAP
面,例如GPU端的GL_TEXTURE_CUBE_MAP_POSITIVE_X
。
为了当前工作,纹理将作为
GL_TEXTURE_2D
在 CPU 上加载到我的 TextureLibrary 中,然后在我的 Skybox 类中作为 GL_CUBE_MAP
面的目标再次加载到 CPU 上,就像临时的一样解决这个问题的方法效率低下。
有什么办法可以拥有
GL_CUBE_MAP
的脸参考或复制GL_TEXTURE_2D
的数据吗?
我计划在未来的整个应用程序中将这些纹理用作
GL_TEXTURE_2D
,因此我希望将它们一次加载到我的TextureLibrary中的CPU端,并且只有当它们被Skybox使用时,数据才会被引用或在 GPU 端复制到 GL_TEXTURE_CUBE_MAP 中。
或者,我可以放弃使用 GL_TEXTURE_CUBE_MAP 的想法,并传递我的天空盒着色器 6 x
Sampler2D
;但是,我想知道是否有任何方法可以按照上述方法做到这一点 - 或任何其他想法。
你可以试试这个:
//generate cubemap texture object name
glGenTextures(1, &my_cube_map_texture);
//make it a cubemap texture
glBindTexture(GL_TEXTURE_CUBE_MAP, my_cube_map_texture);
//for each cubemap face
for (int i=0; i < 6; ++i) {
//create face
//where i
//0: GL_TEXTURE_CUBE_MAP_POSITIVE_X
//1: GL_TEXTURE_CUBE_MAP_NEGATIVE_X
//2: GL_TEXTURE_CUBE_MAP_POSITIVE_Y
//3: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
//4: GL_TEXTURE_CUBE_MAP_POSITIVE_Z
//5: GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, //target
0, //level
iformat, //internalformat
width, //width
height, //height
0, //border
format, //data format
GL_UNSIGNED_BYTE, //data type
NULL //no copy, allocate only
);
//copy data from existent 2d texture into the specific face
glCopyImageSubData(
my_texture_2d[i], //srcName
GL_TEXTURE_2D, //srcTarget
0, //srcLevel
0, //srcX
0, //srcY
0, //srcZ
my_cube_map_texture, //dstName
GL_TEXTURE_CUBE_MAP, //dstTarget
0, //dstLevel
0, //dstX
0, //dstY
i, //dstZ
width, //srcWidth
height, //srcHeight
1 //srcDepth
);
}
先决条件:
参考:
还有一个替代解决方案,请点击链接了解更多信息:
如果其他一切都失败了:
GL_PIXEL_PACK_BUFFER
目标glGetTexImage
GL_PIXEL_UNPACK_BUFFER
目标不涉及客户端内存,全部在 GPU 端。