Java OpenGL-纹理透明的白色
正如你可以看到上面有白色的图像是透明的。奇怪的是,就在炮塔旁边,还有一个不是白色的透明物 以下是原始图像: 加载纹理:Java OpenGL-纹理透明的白色,java,opengl,textures,opengl-3,Java,Opengl,Textures,Opengl 3,正如你可以看到上面有白色的图像是透明的。奇怪的是,就在炮塔旁边,还有一个不是白色的透明物 以下是原始图像: 加载纹理: 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(),
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);
编辑:
我解决了这个问题。我仍然不知道确切的问题是什么。我刚刚在Pixelmator Photoshop for Mac中打开了图像并将其导出。然后白色边框消失了。我刚刚读了一篇关于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;
}
你可以试试这个:希望这个助手,我还没有看到你自己解决它的便条。忽略它。
glBindTexture(GL_TEXTURE_2D, id);
shader.bind();
mesh.render();
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;
}