1

我正在尝试在 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();
}

任何帮助将不胜感激!

4

1 回答 1

1

Your blending configuration seems to be fine. They are exactly like mine, which actually work. However the error I think lies on the newTextureData(GLProfile glp... method. Your method says newTextureData(file... the newtexturedata() method doesn't accept File objects instead it is expecting a GLProfile profile instead as the first argument. As I read in the documentation http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/texture/TextureIO.html

I suggest you change that lines:

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

to

textTexture = TextureIO.newTexture(file,mipmap);

or

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

instead. If your file variable is correct, it should work.

For further JOGL readings you should consider these tutorials: http://www3.ntu.edu.sg/home/ehchua/programming/opengl/JOGL2.0.html

For JOGL documentation you should consider reading: http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc

于 2013-01-18T14:25:06.173 回答