【发布时间】:2017-08-22 20:58:51
【问题描述】:
我正在使用 STB 库加载一个看起来像这样的 PNG 图像(是的,它很小)。这是我在 5 分钟内制作的精灵表,角色背后是透明的。
鸡蛋床单:
我创建了一个类来解析精灵表并将适当的纹理坐标提供给渲染循环。它完美无瑕,但我没有更多的透明度,只有黑色。我使用调试器检查过,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()返回实际纹理表的尺寸,而image.width()和image.height()返回单元格的尺寸。-
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(); }
附:另外,当我在这里时,我一直在尝试弄清楚如何在放大图像时消除混合。我试过glDisable(GL_BLEND),但没有这样做。那里有任何帮助和背景信息吗?
【问题讨论】:
-
你检查原生图片数据的alpha通道是否小于255甚至0?
-
源肯定有一个有效的 alpha 通道,它在我使用的任何查看器中都是透明的,除非数据中有一些我不知道的隐藏值?
-
你没有使用着色器,对吗?
-
尚未实现着色器。
-
glBlendFunc成功了。谢谢@Rabbid76
标签: opengl graphics png lwjgl game-engine