OpenGL - 纹理透明的白色

时间:2014-08-08 12:23:54

标签: java opengl textures opengl-3

http://s7.directupload.net/images/140808/6n4nz6ca.png

如上所示,图像是透明的白色。奇怪的是,在炮塔旁边还有透明度,而不是白色。

这是原始图片:

Original image http://s14.directupload.net/images/140808/7fgztxkf.png

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(Photoshop for Mac)中打开图像并将其导出。然后白色边框消失了。

1 个答案:

答案 0 :(得分:0)

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


以下是一些代码:

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;   
}
相关问题