【问题标题】:OpenGL - white color where texture is transparentOpenGL - 纹理透明的白色
【发布时间】:2014-08-08 12:23:54
【问题描述】:

正如您在上面看到的那样,图像透明的地方是白色的。奇怪的是炮塔旁边还有透明不是白色的。

这是原图:

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
glEnable(GL_TEXTURE_2D);

加载纹理:

this.image = ImageIO.read(new File("res/textures/"+fileName);

int pixels[] = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0,image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); // 4 for RGBA, 3 for RGB

    
for (int y = 0; y < image.getHeight(); y++) {
    for (int x = 0; x < image.getWidth(); x++) {
        int pixel = pixels[y * image.getWidth() + x];
        buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
        buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
        buffer.put((byte) (pixel & 0xFF)); // Blue component
        buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
    }
}

buffer.flip();

int id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);

// Setup wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

// Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

为了显示它,我只需绑定纹理和着色器并渲染一个网格:

glBindTexture(GL_TEXTURE_2D, id);
shader.bind();
mesh.render();

编辑:

我解决了这个问题。我仍然不知道确切的问题是什么。我刚刚在 Pixelmator(Mac 的 Photoshop)中打开了图像并将其导出。然后白色边框消失了。

【问题讨论】:

标签: java opengl textures opengl-3


【解决方案1】:

我刚刚阅读了一篇关于 MFC 中窗口显示为白色的透明部分的文章。希望它会有所帮助。据说每个像素的值是它的RGB值乘以alpha值。图片是透明的,因为它的alpha值为0,所以RGB * alpha值 是0。你的问题可能是RGB值没有和alpha值相乘。在我阅读的文章中,作者自己做了“RGB*alpha”。


这是一些代码:

for(int i = 0 ;i < m_pngImage.GetWidth();i++)
{
    unsigned char* pucColor = reinterpret_cast<unsigned char *>(m_pngImage.GetPixelAddress(i , j)); 
    pucColor[0] = pucColor[0] * pucColor[3] / 255;   
    pucColor[1] = pucColor[1] * pucColor[3] / 255;   
    pucColor[2] = pucColor[2] * pucColor[3] / 255;   
}

【讨论】:

  • 对不起,我没有看到你自己解决的说明。忽略它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多