【问题标题】:Texture turns to gray in opengl when added alpha channel manually手动添加 Alpha 通道时,纹理在 opengl 中变为灰色
【发布时间】:2015-11-05 11:56:39
【问题描述】:
int j,k,t3,t4;
for (int i = 0;i < width;i++)
    for (j = 0;j < height;j++)
    {
        t3 = (i*height + j)*3 ;
        t4 = (i*height + j) * 4;
        for (k = 0;k < 3;k++)
            texture[t4+k] = data[t3+k];
        texture[t4 + 3] = (data[t3 + 1]==255 && data[t3 + 2]==255 && data[t3]==255) ? 0 : 255;
    }


GLuint textureID;
glGenTextures(1, &textureID);

glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, texture);

这是一个位图文件,我用原始数据成功加载了它。

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

但是当我手动添加 Alpha 通道时,纹理失去了颜色。图片附在下面。

【问题讨论】:

  • 纹理和数据是如何定义的?
  • @BDL data = new unsigned char[imageSize];无符号字符*纹理=新无符号字符[imageSize * 2];数据是从位图文件读取的原始 24 位图像数据,而纹理是修改后的数据,我在第一个代码块中通过循环在每个 BGR 数据之后附加了一个 alpha 通道。
  • @BillSun:BMP 文件通常以 4 字节的行对齐方式存储,OpenGL 默认需要这种格式的数据。在访问源数据时,转换数据的代码不会考虑这种对齐方式。 (当您使用每像素 4 个字节时,目标将始终对齐,因此这不是问题)。

标签: opengl textures transparent alpha texture-mapping


【解决方案1】:

我不能完全解释确切的症状,但你代码中的索引算法看起来不对:

for (int i = 0;i < width;i++)
    for (j = 0;j < height;j++)
    {
        t3 = (i*height + j)*3 ;
        t4 = (i*height + j) * 4;

由于图像通常在内存中逐行排列,因此迭代行中像素的索引应该是不带乘数相加的索引,而另一个索引乘以width(不是height) .这应该是:

for (j = 0; j < height; j++)
    for (int i = 0; i < width; i++)
    {
        t3 = (j * width + i) * 3;
        t4 = (j * width + i) * 4;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2013-10-13
    • 2012-04-08
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多