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

編集:
の 1 つのバージョンは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 に答える