mikeythemissile
mikeythemissile

Reputation: 162

JOGL upside down rendering

I'm rendering an image with JOGL, using a Texture object, however it's being rendered upside down (picture: http://puu.sh/Q2QT). Any advice would be great, code follows:

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

Upvotes: 2

Views: 873

Answers (2)

Neo
Neo

Reputation: 158

I usually read in image files as a BufferedImage, then flip them vertically with a handy function called ImageUtil.flipImageVertically(BufferedImage image). Here's an example:

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

Upvotes: 2

MvG
MvG

Reputation: 60858

Java and OpenGL have different ideas of the default orientation of a coordinate system. Java takes y = 0 as the upper rim of whatever the coordinate system is describing, going down from there. OpenGL takes y = 0 as the bottom of the reference rectangle. There are various locations places where you can flip the image. In your case, the simplest approach would be changing the association between scene and texture coordinates:

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

EDIT:
One version of newTexture provides a mustFlipVertically flag, but the one creating a texture from a file apparently does not. The “official” way to deal with different orientations is using 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);

Upvotes: 2

Related Questions