简体   繁体   English

OpenGL-白色,纹理透明

[英]OpenGL - white color where texture is transparent

As you can see above there is white color where the image is transparent. 如您在上方看到的,白色是图像透明的地方。 The weird thing is that right next to the turret there also is transparency which is not white. 奇怪的是,在炮塔旁边也有不是白色的透明度。

Here is the original image: 这是原始图像:

原始图片

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

Loading the texture: 加载纹理:

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);

To display it I simply bind the texture and shader and render a mesh: 要显示它,我只需绑定纹理和着色器并渲染网格:

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

EDIT: 编辑:

I fixed the problem. 我解决了这个问题。 I still don't know what the exact problem is though. 我仍然不知道确切的问题是什么。 I just opened the image in Pixelmator (Photoshop for Mac) and exported it. 我只是在Pixelmator(适用于Mac的Photoshop)中打开图像并将其导出。 Then the white borders were gone. 然后白色边框消失了。

I've just read a article about the transparent part that is displayed as white by windows in the MFC. 我刚刚读了一篇有关透明部分的文章,该部分在MFC中被窗口显示为白色。 Hope it will do help.It's said that the every pixel'value is its RGB value multiply the alpha value.The picture is transparent because its alpha value is 0,so the RGB*alpha value is 0.Your problem may be that the RGB value hasn't multiply with alpha value.In the article I read,the author do "RGB*alpha" by oneself. 希望能对您有所帮助。据说每个像素的值就是其RGB值乘以alpha值。图片是透明的,因为其alpha值为0,所以RGB * alpha值为0。您的问题可能是RGB值未与alpha值相乘。在我阅读的文章中,作者自己做“ RGB * alpha”。


Here is some code: 这是一些代码:

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;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM