OpenGL中的压缩纹理批处理 [英] Compressed texture batching in OpenGL
问题描述
我想创建一个压缩贴图的地图集,但我似乎不能得到它的工作。这是一个代码片段:
I'm trying to create an atlas of compressed textures but I can't seem to get it working. Here is a code snippet:
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轴。
I'm testing this with just 2 small KTX textures that have the same size and as you can see from the code I'm trying to append the second one next to the first one on the x axis.
我的KTX解析工作正常,因为我可以渲染单个纹理,但一旦我尝试批处理(即一旦我使用glCompressedTexSubImage2d)我没有在屏幕上得到任何东西。
My KTX parsing works fine as I can render individual textures but as soon as I try to batch (that is as soon as I use glCompressedTexSubImage2d) I get nothing on the screen.
如果我用PNG替换压缩纹理,并将glCompressedTexImage2d和glCompressedTexSubImage2d与其非压缩版本交换,则所有这些都很有用...
It might be useful to know that all of this works fine if I replace compressed textures with PNGs and swap the glCompressedTexImage2d and glCompressedTexSubImage2d with their non-compressed versions...
其中一个我找不到任何信息的东西是纹理在图集的x和y位置。如何抵消?因此,如果第一个纹理的宽度为60像素,例如,我只是将第二个位置在61?
One of the things that I cannot find any information on is the x and y position of the textures in the atlas. How do I offset them? So if the first texture has a width of 60 pixels for example, do I just position the second one at 61?
我看过一些在线的代码,人们计算x和y位置如下:
I've seen some code online where people calculate the x and y position as follows:
x &= ~3;
y &= ~3;
这是我需要做什么,为什么?我尝试了它,但它似乎不工作。
Is this what I need to do and why? I've tried it but it doesn't seem to work.
此外,我尝试上面的代码在ARM i.mx6四核与Vivante GPU,我在网上看到glCompressedTexSubImage2d可能无法在这个板子上工作。
Also, I'm trying the above code on an ARM i.mx6 Quad with a Vivante GPU, and I get the suspicion from what I read online that glCompressedTexSubImage2d might not be working on this board.
任何人都可以帮助我吗?
Can anyone please help me out?
推荐答案
毕竟,这是一个错误,让你想在墙上打你的头。 GL_COMPRESSED_RGBA8_ETC2_EAC
实际上不支持在主板上。
After all, this was one of those mistakes that make you want to hit your head on a wall. GL_COMPRESSED_RGBA8_ETC2_EAC
was actually not supported on the board.
我从标头复制了它,但没有查询设备支持的格式。我可以使用 DXT5
格式很好用这个代码。
I copied it from the headers but it did not query the device for supported formats. I can use a DXT5
format just fine with this code.
这篇关于OpenGL中的压缩纹理批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!