我想做的是这个,想象我有一个小瓷砖(32x32),里面有太阳,那是一个黑色背景的黄色圆圈。
我想画出天空中的太阳(浅蓝色)。显然黑色边框会破坏我的构图。我必须让 OpenGL 删除那个黑色。
在 Photoshop 中,我会使用魔术工具选择所有黑色像素,然后将它们删除,并使用 alpha 通道保存新文件。
但是,如果您有数百万张图像,这可能太长了。我必须在运行时处理这个问题。
我一直在寻找这种glStencilMask
方法,但如果你真的有一个纹理可以用作遮罩,那它就会起作用。
我找到了一个 C# 示例,它讨论了将 24 位图像转换为带有 alpha 通道的 32 位,这对我来说听起来不错,但可能在耗时和资源消耗过多的问题上,特别是如果瓷砖的数量很高(大约30x20 瓦片,每秒 60 帧)
问题是这很难达到,达到这个目标的人不会告诉任何人......
实际上绘制瓷砖的代码是这样的,它将剪切、平移、旋转和所有需要的东西。
GL11.glPushMatrix();
// bind to the appropriate texture for this sprite
this.texture.bind();
// translate to the right location and prepare to draw
GL11.glColor3f(1, 1, 1);
GL11.glTranslated(x + ((32 - this.texture.getImageWidth()) / 2) + (this.texture.getImageWidth() / 2), y + ((32 - this.texture.getImageHeight()) / 2)
+ (this.texture.getImageHeight() / 2), 0);
// System.out.println(this.angle);
GL11.glRotated(this.angle, 0, 0, 1);
GL11.glTranslated(-this.texture.getImageWidth() / 2, -this.texture.getImageHeight() / 2, 0);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, this.texture.getHeight());
GL11.glVertex2f(0, this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight());
GL11.glVertex2f(this.texture.getImageWidth(), this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), 0);
GL11.glVertex2f(this.texture.getImageWidth(), 0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
texture.bind 是这样的:
public void bind() {
GL11.glBindTexture(this.target, this.textureID);
}
对于包含透明层的图像,一切都是完美的。
一旦我找到了如何删除该特定颜色,我希望根据左上角的像素删除颜色,这将完成glReadPixels()
这是装载机:
public Texture getTexture(String resourceName, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException {
int srcPixelFormat = 0;
// create the texture ID for this texture
int textureID = this.createTextureID();
Texture texture = new Texture(target, textureID);
// bind this texture
GL11.glBindTexture(target, textureID);
BufferedImage bufferedImage = this.loadImage(resourceName);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL11.GL_RGBA;
} else {
srcPixelFormat = GL11.GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = this.convertImageData(bufferedImage, texture);
if (target == GL11.GL_TEXTURE_2D) {
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
GL11.glTexImage2D(target, 0, dstPixelFormat, this.get2Fold(bufferedImage.getWidth()), this.get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat,
GL11.GL_UNSIGNED_BYTE, textureBuffer);
return texture;
}