如果我尝试使用四通道RGBA纹理进行此操作,则完全正常,但出于某种原因,当我尝试仅加载单个通道时,我会得到一个混乱的图像,而我无法弄清原因。 我通过以下代码将一系列字形的纹理位图数据结合成单个纹理来创建纹理:
int texture_height = max_height * new_font->num_glyphs;
int texture_width = max_width;
new_texture->datasize = texture_width * texture_height;
unsigned char* full_texture = new unsigned char[new_texture->datasize];
// prefill texture as transparent
for (unsigned int j = 0; j < new_texture->datasize; j++)
full_texture[j] = 0;
for (unsigned int i = 0; i < glyph_textures.size(); i++) {
// set height offset for glyph
new_font->glyphs[i].height_offset = max_height * i;
for (unsigned int j = 0; j < new_font->glyphs[i].height; j++) {
int full_disp = (new_font->glyphs[i].height_offset + j) * texture_width;
int bit_disp = j * new_font->glyphs[i].width;
for (unsigned int k = 0; k < new_font->glyphs[i].width; k++) {
full_texture[(full_disp + k)] =
glyph_textures[i][bit_disp + k];
}
}
}
然后我调用以下代码来加载纹理数据:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->x, texture->y, 0, GL_RED, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(full_texture));
我的片段着色器执行以下代码:
#version 330
uniform sampler2D texture;
in vec2 texcoord;
in vec4 pass_colour;
out vec4 out_colour;
void main()
{
float temp = texture2D(texture, texcoord).r;
out_colour = vec4(pass_colour[0], pass_colour[1], pass_colour[2], temp);
}
我得到了一张图像,可以看出它是由纹理生成的,但是它非常扭曲,我不确定原因。顺便说一句,我使用GL_RED,因为Opengl 4中删除了GL_ALPHA。
真正让我困惑的是,当我从字形生成一个4 RGBA纹理,然后使用它的alpha通道时,为什么它可以正常工作?