无法使PNG透明度在JOGL中工作

时间:2013-01-17 15:43:37

标签: png textures jogl

我正在尝试使用jogl中的透明png来编写文本,但我不能为我的生活弄清楚如何使其工作。我在互联网上到处都是,但JOGL的正确文档很少。

以下是我加载纹理的方法:

private void loadTEXTure()    //Har har, get it?
{
    File file = new File(fontMap);

    try 
    {
        TextureData data = TextureIO.newTextureData(file, GL.GL_RGBA, GL.GL_SRGB8_ALPHA8, false, TextureIO.PNG);
        textTexture = TextureIO.newTexture(data);
    }
    catch (GLException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); }
}

这就是png的显示方式:

public void displayCharacter(GL gl, int[] textureBounds, int x1, int y1, int x2, int y2)
{
    float texCordsx1 = ((float) textureBounds[0])/((float) textTexture.getWidth());
    float texCordsy1 = ((float) textureBounds[1])/((float) textTexture.getHeight());
    float texCordsx2 = ((float) textureBounds[2])/((float) textTexture.getWidth());
    float texCordsy2 = ((float) textureBounds[3])/((float) textTexture.getHeight());

    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

    textTexture.enable();
    textTexture.bind();

    gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2f(texCordsx1, texCordsy1);
    gl.glVertex2f(x1, y1);
    gl.glTexCoord2f(texCordsx1, texCordsy2);
    gl.glVertex2f(x1, y2);
    gl.glTexCoord2f(texCordsx2, texCordsy2);
    gl.glVertex2f(x2, y2);
    gl.glTexCoord2f(texCordsx2, texCordsy1);
    gl.glVertex2f(x2, y1);
    gl.glEnd();

    textTexture.disable();
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您的混合配置似乎没问题。它们与我的完全一样,实际上是有效的。但是我认为错误在于newTextureData(GLProfile glp ...方法。你的方法说newTextureData(file ... newtexturedata()方法不接受File对象,而是期望GLProfile配置文件作为第一个参数。正如我在文档http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/texture/TextureIO.html

中所读到的那样

我建议你更改这些内容:

TextureData data = TextureIO.newTextureData(file, GL.GL_RGBA, GL.GL_SRGB8_ALPHA8,    false, TextureIO.PNG);
textTexture = TextureIO.newTexture(data);

textTexture = TextureIO.newTexture(file,mipmap);

textTexture = TextureIO.newTexture(cl.getResource("/my/file/path/myimage.png"), false, null);

代替。如果你的文件变量是正确的,它应该可以工作。

对于进一步的JOGL读数,您应该考虑以下教程:http://www3.ntu.edu.sg/home/ehchua/programming/opengl/JOGL2.0.html

对于JOGL文档,您应该考虑阅读:http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc