Texture mapping problem

I trying TextureMapping with JOGL(didn’t use com.sun.opengl.util.texture package , just use opengl method)

i use this method to create a texture:

   private boolean LoadTexture() {
        gl.glGenTextures(1,texture,0);
        gl.glBindTexture(gl.GL_TEXTURE_2D,texture[0]);
        try {
            BufferedImage bi = ImageIO.read(new File("./NeHe.png"));
            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            ByteBuffer bb = ByteBuffer.allocateDirect(data.length);
            bb.order(ByteOrder.nativeOrder());
            bb.put(data,0,data.length);
            bb.rewind();
            gl.glTexImage2D(GL.GL_TEXTURE_2D,0,3,bi.getWidth(),bi.getHeight(),0,GL.GL_RGB,GL.GL_UNSIGNED_BYTE,bb);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); // Linear Filtering
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); // Linear Filtering
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

texture[] is a variable of class

then i draw a quad with texture

gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);
        gl.glBegin(GL.GL_QUADS);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
gl.glEnd(); 

But the texture isn’t look like as i thought, the buttom and top is reverse.
i must change my code like

gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);
        gl.glBegin(GL.GL_QUADS);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
gl.glEnd();

any hint to solve this problem~?

Thanks.

This is happening because Java2D’s origin is at the upper left but OpenGL’s is at the lower left. The TextureIO classes take care of details like this for you and I would strongly recommend you use them – that’s why they’re there.

    private boolean LoadTexture() {
        gl.glGenTextures(1,texture,0);
        gl.glBindTexture(gl.GL_TEXTURE_2D,texture[0]);
        try {
            BufferedImage bi = ImageIO.read(new File("./NeHe.png"));
            
            //New Add Line
            java.awt.geom.AffineTransform tx = java.awt.geom.AffineTransform.getScaleInstance(1, -1); 
            tx.translate(0, -bi.getHeight(null)); 
            AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
            bi = op.filter(bi, null); 
            
            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            ByteBuffer bb = ByteBuffer.allocateDirect(data.length);
            bb.order(ByteOrder.nativeOrder());
            bb.put(data,0,data.length);
            bb.rewind();
            System.out.println(bb);
            gl.glTexImage2D(GL.GL_TEXTURE_2D,0,3,bi.getWidth(),bi.getHeight(),0,GL.GL_RGB,GL.GL_UNSIGNED_BYTE,bb);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); // Linear Filtering
            gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); // Linear Filtering            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

I resolve this problem use the new method by add some lines right now.

And thanks for your replay, it make me understand the really reason of this.

BTW,Is there any tutorial for using for Texture()? Is it efficient?

The TextureIO implementation is pretty efficient. The source code in the demos.texture directory of the jogl-demos workspace has a couple of examples of how to use the classes.

Thanks~~~

I will try to learn it. :slight_smile: