【发布时间】:2018-05-03 08:41:55
【问题描述】:
当我光栅化字体时,我的代码为我提供了一个纹理可见性通道。目前,我只是将其复制到 4 个不同的通道,并将其作为纹理发送。现在这可行,但我想尝试避免 CPU 上不必要的内存分配和取消分配。
unsigned char *bitmap = new unsigned char[width*height] //How this is populated is not the point.
位图,现在包含二维图形。
看来这家伙也有同样的问题:Opengl: Use single channel texture as alpha channel to display text
我现在做的工作与解决方法相同,我只是将数组大小乘以 4 并将数据复制到其中 4 次。
unsigned char* colormap = new unsigned char[width * height * 4];
int offset = 0;
for (int d = 0; d < width * height;d++)
{
for (int i = 0;i < 4;i++)
{
colormap[offset++] = bitmap[d];
}
}
当我把它相乘时,我使用:
glTexParameteri(gltype, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(gltype, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(gltype, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, colormap);
得到:
这是我想要的。
当我只使用单通道时:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(gltype, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(gltype, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
然后得到:
它没有透明度,只有红色分机。使其难以着色和扩展。稍后。
不必做我认为在 cpu 端 id 上不必要的分配,比如告诉 OpenGL:“嘿,你只得到一个通道。将它乘以所有 4 个颜色通道。”
有这个命令吗?
【问题讨论】:
标签: c++ opengl textures rasterizing