JOGL颠倒渲染

时间:2012-08-09 10:14:21

标签: java jogl

我正在使用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);
}

2 个答案:

答案 0 :(得分: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);

修改
一个version of 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);

答案 1 :(得分:1)

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

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();
        }
    }