Java JOGL倒转渲染
我正在使用JOGL渲染图像,使用Java JOGL倒转渲染,java,jogl,Java,Jogl,我正在使用JOGL渲染图像,使用纹理对象,但是它被颠倒渲染(图片:)。任何建议都很好,代码如下: 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()),
纹理
对象,但是它被颠倒渲染(图片:)。任何建议都很好,代码如下:
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和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);
编辑:其中一个提供了
mustflipvertical
标志,但从文件创建纹理的那个显然没有。处理不同取向的“官方”方式是:
我通常以BuffereImage的形式读入图像文件,然后使用一个名为ImageUtil.FlipImageVertical(BuffereImage图像)的简便函数垂直翻转它们。下面是一个例子:
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();
}
}
for(int i=0;i
谢谢你的建议,但是答案的第一部分没有起作用(出于某种原因,只有一个黑色正方形被渲染)关于编辑,新文本的最后一个参数是是否生成mipmap,布尔)@mikeythemissile,抱歉,我在一个版本的newTexture
中读到了这个标志,并错误地认为所有版本都是这样使用它的。相应地更新了m的答案。我仍然很惊讶原来的第一个答案不适合你。
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();
}
}