2

我正在使用 JOGL 渲染图像,使用一个Texture对象,但是它被颠倒渲染(图片: http: //puu.sh/Q2QT)。任何建议都会很棒,代码如下:

private void renderImage(GL2 gl, String filename, int width, int height) {
  Texture texture = null;
  try {
    texture = TextureIO.newTexture(new File(this.getClass().getResource(filename).toURI()), true);
  }
  catch (URISyntaxException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
  catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }

  int left = 0;
  int top = 0;

  texture.enable(gl);
  texture.bind(gl);

  gl.glBegin(GL2.GL_POLYGON);
  gl.glTexCoord2d(0, 0);
  gl.glVertex2d(left, top);
  gl.glTexCoord2d(1, 0);
  gl.glVertex2d(left + width, top);
  gl.glTexCoord2d(1, 1);
  gl.glVertex2d(left + width, top + height);
  gl.glTexCoord2d(0, 1);
  gl.glVertex2d(left, top + height);
  gl.glEnd();
  gl.glFlush();

  texture.disable(gl);
  texture.destroy(gl);
}
4

2 回答 2

2

Java 和 OpenGL 对坐标系的默认方向有不同的想法。Java 将y = 0 作为坐标系所描述的任何东西的边缘,从那里向下。OpenGL 将y = 0 作为参考矩形的底部。您可以在多个位置翻转图像。在您的情况下,最简单的方法是更改​​场景和纹理坐标之间的关联:

  gl.glTexCoord2d(0, 1);
  gl.glVertex2d(left, top);
  gl.glTexCoord2d(1, 1);
  gl.glVertex2d(left + width, top);
  gl.glTexCoord2d(1, 0);
  gl.glVertex2d(left + width, top + height);
  gl.glTexCoord2d(0, 0);
  gl.glVertex2d(left, top + height);

编辑:
一个版本newTexture提供了一个mustFlipVertically标志,但从文件创建纹理的版本显然没有。处理不同方向的“官方”方法是使用getImageTexCoords

  TextureCoords tc = texture.getImageTexCoords();
  gl.glTexCoord2f(tc.left(), tc.top());
  gl.glVertex2d(left, top);
  gl.glTexCoord2f(tc.right(), tc.top());
  gl.glVertex2d(left + width, top);
  gl.glTexCoord2f(tc.right(), tc.bottom());
  gl.glVertex2d(left + width, top + height);
  gl.glTexCoord2f(tc.left(), tc.bottom());
  gl.glVertex2d(left, top + height);
于 2012-08-09T14:38:19.443 回答
1

我通常将图像文件作为 BufferedImage 读取,然后使用名为 ImageUtil.flipImageVertically(BufferedImage image) 的便捷函数垂直翻转它们。这是一个例子:

for (int i= 0; i < imgPaths.length; i++){
        try {
            BufferedImage image= ImageIO.read(this.getClass().getResourceAsStream ("/resources/"+imgPaths[i]));
            ImageUtil.flipImageVertically(image);

            textures[i]= AWTTextureIO.newTexture(glProfile, image, false);
            loadingBar.increaseProgress(1);

        } catch (IOException e) {
            say("Problem loading texture file " + imgPaths[i]);
            e.printStackTrace();
        }
    }
于 2015-05-06T16:44:23.017 回答