首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOGL颠倒渲染

JOGL颠倒渲染
EN

Stack Overflow用户
提问于 2012-08-09 18:14:21
回答 2查看 772关注 0票数 2

我正在用JOGL渲染一个图像,使用的是一个Texture对象,但是它是颠倒呈现的(图片:http://puu.sh/Q2QT)。任何建议都很棒,代码如下:

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-09 22:38:19

Java和OpenGL对坐标系的缺省方向有不同的看法。Java把y=0作为坐标系所描述的任何东西的上边缘,从那里往下。OpenGL将y=0作为参考矩形的底部。您可以在不同的位置翻转图像。在您的示例中,最简单的方法是更改场景和纹理坐标之间的关联:

代码语言:javascript
复制
  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

代码语言:javascript
复制
  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);
票数 2
EN

Stack Overflow用户

发布于 2015-05-07 00:44:23

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

代码语言:javascript
复制
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();
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11881150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档