我正在用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);
}发布于 2012-08-09 22:38:19
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);发布于 2015-05-07 00:44:23
我通常将图像文件读入为图像,然后使用一个名为ImageUtil.flipImageVertically(BufferedImage 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();
}
}https://stackoverflow.com/questions/11881150
复制相似问题