LWJGL3 STB 图像 Alpha 通道不适用于 PNG 图像
LWJGL3 STB Image Alpha channel not working with PNG Image
我正在使用 STB 库加载看起来像这样的 PNG 图像(是的,它很小)。这是我在 5 分钟内制作的精灵 sheet,角色背后是透明的。
蛋蛋Sheet:
我制作了一个 class 来解析精灵 sheet 并将适当的纹理坐标提供给渲染循环。它完美地工作,但我没有更多的透明度,只有黑色。我用调试器检查过,image.components() == 4
,如果我没看错的话,应该有alpha通道。
鸡蛋错误:
我相当确定这与我使用 STB 解析图像的方式有关,但我不确定。可能还有其他我想念的东西。这是代码:
public void renderBatch(){
DrawableAsset image;
Coordinates coord;
while(!images.isEmpty()){
image = images.dequeue();
coord = coords.dequeue();
int texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
if (image.components() == 3) {
if ((image.absWidth() & 3) != 0) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (image.absWidth() & 1));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.absWidth(), image.absHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.imageData());
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.absWidth(), image.absHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.imageData());
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(coord.x() + image.halfwidth(), coord.y() + image.halfHeight(), 0);
glRotatef(image.getAngle(), 0, 0, 1);
glScalef(image.getScale(), image.getScale(), 1f);
glTranslatef(-coord.x() - image.halfwidth(), -coord.y() - image.halfHeight(), 0);
renderImage(image, coord);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDeleteTextures(texID);
}
一些注意事项:
image.absWidth()
和 image.absHeight()
return 实际纹理的尺寸 sheet,而 image.width()
和 image.height()
return 单元格的尺寸。
renderImage()
只是渲染纹理四边形的代码行,应该是无关的,但这里是为了以防万一:
private void renderImage(DrawableAsset image, Coordinates coord){
float[] texCoords = image.regionCoordinates();
glBegin(GL_QUADS);
{
glTexCoord2f(texCoords[0], texCoords[1]);
glVertex2f(coord.x(), coord.y());
glTexCoord2f(texCoords[2], texCoords[3]);
glVertex2f(coord.x() + image.width(), coord.y());
glTexCoord2f(texCoords[4], texCoords[5]);
glVertex2f(coord.x() + image.width(), coord.y() + image.height());
glTexCoord2f(texCoords[6], texCoords[7]);
glVertex2f(coord.x(), coord.y() + image.height());
}
glEnd();
}
P.S。另外,当我在这里时,我一直在尝试弄清楚如何在放大图像时消除混合。我试过 glDisable(GL_BLEND)
,但没有成功。那里有任何帮助和背景信息吗?
问题是因为我没有正确设置glBlendFunc
。在任何绘图之前输入这行代码就成功了!
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
关于该主题的更多信息:Blending
我正在使用 STB 库加载看起来像这样的 PNG 图像(是的,它很小)。这是我在 5 分钟内制作的精灵 sheet,角色背后是透明的。
蛋蛋Sheet:
我制作了一个 class 来解析精灵 sheet 并将适当的纹理坐标提供给渲染循环。它完美地工作,但我没有更多的透明度,只有黑色。我用调试器检查过,image.components() == 4
,如果我没看错的话,应该有alpha通道。
鸡蛋错误:
我相当确定这与我使用 STB 解析图像的方式有关,但我不确定。可能还有其他我想念的东西。这是代码:
public void renderBatch(){
DrawableAsset image;
Coordinates coord;
while(!images.isEmpty()){
image = images.dequeue();
coord = coords.dequeue();
int texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
if (image.components() == 3) {
if ((image.absWidth() & 3) != 0) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (image.absWidth() & 1));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.absWidth(), image.absHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.imageData());
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.absWidth(), image.absHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.imageData());
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(coord.x() + image.halfwidth(), coord.y() + image.halfHeight(), 0);
glRotatef(image.getAngle(), 0, 0, 1);
glScalef(image.getScale(), image.getScale(), 1f);
glTranslatef(-coord.x() - image.halfwidth(), -coord.y() - image.halfHeight(), 0);
renderImage(image, coord);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDeleteTextures(texID);
}
一些注意事项:
image.absWidth()
和image.absHeight()
return 实际纹理的尺寸 sheet,而image.width()
和image.height()
return 单元格的尺寸。renderImage()
只是渲染纹理四边形的代码行,应该是无关的,但这里是为了以防万一:private void renderImage(DrawableAsset image, Coordinates coord){ float[] texCoords = image.regionCoordinates(); glBegin(GL_QUADS); { glTexCoord2f(texCoords[0], texCoords[1]); glVertex2f(coord.x(), coord.y()); glTexCoord2f(texCoords[2], texCoords[3]); glVertex2f(coord.x() + image.width(), coord.y()); glTexCoord2f(texCoords[4], texCoords[5]); glVertex2f(coord.x() + image.width(), coord.y() + image.height()); glTexCoord2f(texCoords[6], texCoords[7]); glVertex2f(coord.x(), coord.y() + image.height()); } glEnd(); }
P.S。另外,当我在这里时,我一直在尝试弄清楚如何在放大图像时消除混合。我试过 glDisable(GL_BLEND)
,但没有成功。那里有任何帮助和背景信息吗?
问题是因为我没有正确设置glBlendFunc
。在任何绘图之前输入这行代码就成功了!
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
关于该主题的更多信息:Blending