【问题标题】:JOGL upside down renderingJOGL 倒置渲染
【发布时间】:2012-08-09 10:14:20
【问题描述】:

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

【问题讨论】:

    标签: java jogl


    【解决方案1】:

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

    【讨论】:

    • 感谢您的建议,但是答案的第一部分没有奏效(由于某种原因,只呈现了一个黑色方块)关于编辑,newTexture 的最后一个参数是是否或不生成 mipmap download.java.net/media/jogl/builds/archive/jsr-231-beta5/…, boolean)
    • @mikeythemissile,抱歉,我在newTexture 的一个版本中读到了该标志,并错误地认为所有版本都以这种方式使用它。相应地更新了 m 答案。我仍然很惊讶原来的第一个答案不适合你。
    【解决方案2】:

    我通常将图像文件作为 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多