【发布时间】:2015-02-27 09:33:34
【问题描述】:
我正在尝试创建压缩纹理图集,但似乎无法正常工作。这是一个代码sn-p:
void Texture::addImageToAtlas(ImageProperties* imageProperties)
{
generateTexture(); // delete and regenerate an empty texture
bindTexture(); // bind it
atlasProperties.push_back(imageProperties);
width = height = 0;
for (int i=0; i < atlasProperties.size(); i++)
{
width += atlasProperties[i]->width;
height = atlasProperties[i]->height;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glCompressedTexImage2D MUST be called with valid data for the 'pixels'
// parameter. Won't work if you use zero/null.
glCompressedTexImage2D(GL_TEXTURE_2D, 0,
GL_COMPRESSED_RGBA8_ETC2_EAC,
width,
height,
0,
(GLsizei)(ceilf(width/4.f) * ceilf(height/4.f) * 16.f),
atlasProperties[0]->pixels);
// Recreate the whole atlas by adding all the textures we have appended
// to our vector so far
int x, y = 0;
for (int i=0; i < atlasProperties.size(); i++)
{
glCompressedTexSubImage2D(GL_TEXTURE_2D,
0,
x,
y,
atlasProperties[i]->width,
atlasProperties[i]->height,
GL_RGBA,
(GLsizei)(ceilf(atlasProperties[i]->width/4.f) * ceilf(atlasProperties[i]->height/4.f) * 16.f),
atlasProperties[i]->pixels);
x += atlasProperties[i]->width;
}
unbindTexture(); // unbind the texture
}
我只用 2 个大小相同的小型 KTX 纹理对此进行测试,正如您从代码中看到的那样,我试图在 x 轴上将第二个附加到第一个旁边。
我的 KTX 解析工作正常,因为我可以渲染单个纹理,但是一旦我尝试批处理(即一旦我使用 glCompressedTexSubImage2d),屏幕上什么也没有。
如果我用 PNG 替换压缩纹理并将 glCompressedTexImage2d 和 glCompressedTexSubImage2d 与它们的非压缩版本交换,那么知道所有这些都可以正常工作可能会很有用...
我找不到任何信息的一件事是图集中纹理的 x 和 y 位置。我该如何抵消它们?因此,例如,如果第一个纹理的宽度为 60 像素,我是否只需将第二个纹理定位在 61 处?
我在网上看到一些代码,人们计算 x 和 y 位置如下:
x &= ~3;
y &= ~3;
这是我需要做的吗?为什么?我已经尝试过了,但它似乎不起作用。
另外,我正在使用 Vivante GPU 的 ARM i.mx6 Quad 上尝试上述代码,我从网上阅读的内容中怀疑 glCompressedTexSubImage2d 可能无法在此板上工作。
谁能帮帮我?
【问题讨论】:
-
不应该是
height = max(height, atlasProperties[i]->height)