Texture loading problem

Hello,

      I have a problem regarding texture loading. When i load a texture it rotate about 180 degree. Means, its top part comes down and down comes at top. 

     What could be a reason for such texture rotation?

Thanks In Adcance

this could have several reasons.

one could be, that the texture-coordinates in your application are flipped.

others could be the way you are reading the texture and what format it is as a file and as a openGL-texture object. for that we would need more information about your textureloader.

Hello emzic,

          I am loading JPEG image as a texture. In animation, all texture are in JPEG format and i have converted it to bitmaps. 
         Here i am posting a code to load a texture.

/******************************/
IntBuffer textureid = IntBuffer.allocate(1);

		m_gl.glGenTextures(1, textureid);
		m_gl.glBindTexture(GL.GL_TEXTURE_2D, textureid.get(0));
		m_gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 2);
				
		if (format == RENDER_TEXTURE_FORMAT_RGB_24) {
			m_gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, (int) width,(int) height, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE,ByteBuffer.wrap(buffer));
		} else {
			m_gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 4, (int) width,(int) height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,ByteBuffer.wrap(buffer));
		}

		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T,GL.GL_REPEAT);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);

		m_gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE);

		texture.setTextureID(textureid.get(0));

/******************************************/

     I have a confusion that same code is working for other part of the animation and other objects. 

    what factors may affects for texturing a object?. I mean, what parameters (normals, UVs, vertex etc.)?

Thanks and Regards

Your texture coordinates are what control how the texture is put onto the triangle/quad/whatever. These are given per vertex and are used to look up the texture information. You could just flip the v (essentially the y-coord) to fix your problem, or look into the Texture and TextureCoords utility classes that come with jogl (TextureCoords can automatically flip the texture coordinates if you tell it to). Also keep in mind that the raster position is the bottom left corner for openGL while the raster position for most things is the upper left, which means that y-coordinates are flipped from your normal way of thinking with computer drawing.

Hello lhkbob,

          You are correct but one question distrubing me.
          I am using same technique to load texture for all objects in my scene. All objects doing rtexturing correct but only one has such problem. 

         Also, i have same application in c++ and i have compared all texture coordinates one by one. Those are all same. Same texture in c++ working correctly. I have just dump a code in JAVA with same structure and code.

        is it possible that openGL automatically flipping cordinates or texture image?

       This one problems makes my apllication deployment very late. 

      Thanks a lot for kind help.

Thanks and Regards

Hi,

You should have a look to the Texture related class in jogl, the 180 degrees flip you meet is related to the fact that 0,0 coordinates are at top left in java and bottom left in jogl.
If I remember well you have a flag indicating if you want to flip the texture coordinates or not in the image loading process.

Hope it helps

Hello,

   [b]  Does OpenGL automatically does flipping of texture coordinates at the time of loading a texture?[/b] 
     Because texturing of all objects is right, only one objects is doing such problem.

Thanks and Regards

Hello,

I am loading an image using following code.

		Iterator itrReader = ImageIO.getImageReadersByFormatName("JPEG");
		ImageReader imageReader = null;
		if(itrReader.hasNext()){
			imageReader = (ImageReader) itrReader.next();
		}
		
		//sourceBuffer contains whole image file in it
		ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(sourceBuffer));
		
		imageReader.setInput(iis);
		BufferedImage bImage = imageReader.read(0);
		
		DataBufferByte byteBuffer = (DataBufferByte) bImage.getRaster().getDataBuffer();
		
		width = bImage.getWidth();
		height = bImage.getHeight();
		bitCount = 24;
		
		//bitMapBits is my final bitmap
		bitMapBits = byteBuffer.getData();

Now, i loading texture using generated bitMap (bitMapBits)

		IntBuffer textureid = IntBuffer.allocate(1);

		gl.glGenTextures(1, textureid);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureid.get(0));
		gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 2);		
		
		gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, (int) width,(int) height, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE,ByteBuffer.wrap(bitMapBits));
		
		
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T,GL.GL_REPEAT);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
		m_gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);

		m_gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE);

Thanks and Regards

NO, OpenGL doesn’t flip anything by itself.

The problem might be in the buffer you use to store pixels… I thought you used Texture features provided by jogl but you seem to load your textures by yourself…
You can try another texture on the suspect object to see if the problem is on the object or on the texture data.

Hello,

      I have written a small test application in C++ and JAVA (JOGL). In c++, it shows texture currectly but in JOGL it flips texture at 180 degree. The code is same.

        Following code i am using to display loaded texture on drawable.

f_glBegin(GL_QUADS);
	f_glTexCoord2f(0.0f, 0.0f); f_glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
	f_glTexCoord2f(1.0f, 0.0f); f_glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
	f_glTexCoord2f(1.0f, 1.0f); f_glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
	f_glTexCoord2f(0.0f, 1.0f); f_glVertex3f(-1.0f,  1.0f,  1.0f);	
f_glEnd();

Thanks and Regards

That’s a known “problem” because the ImageIO classes load the images for the common coordinate system used by java (afaik), which is flipped regarding the OpenGL coordinate system. The TextureLoader contained in the JOGL release generates flipped texture coordinated for you to correct this shortcomming.

Hello,

      JOGL TextureData object contains method setMustFlipVertically but it not flips the image. Could you provide me example to flip image. I need byte array contains flipped image.

Thanks and Regards

Just flip the texture coordinates and you are done.

Hello,

    Thanks to all of you who has directly or indirectly help me. Finally, i have solved problem using folowing code....

		//rotate an image at 180
		BufferedImage rotImage = new BufferedImage(bImage.getWidth(),bImage.getHeight(),bImage.getType());
		Graphics2D g2d = rotImage.createGraphics();
		g2d.rotate(180*(Math.PI/180),bImage.getWidth()/2,bImage.getHeight()/2);
		g2d.drawImage(bImage,0,0,null);
		bImage = rotImage;

   Thanks again...

Thanks and Regards

Take a closer look to the result. A rotation may not be what you want, if the texture was only flipped…

Hello cylab,

           Now both c++ and JAVA program display image same way. Also, in my application, now textures rendering works correctly.

           Could you help me for post "http://www.java-gaming.org/forums/index.php?topic=17462.0"

and “http://www.java-gaming.org/forums/index.php?topic=17445.0
Thanks and Regards

The above solution isn’t all that nice. There is a flipImageVertically() method which is possibly much faster, and there is a TextureData class that loads the specific properties of the image (color model etc) to help showing the texture correctly independent of image/pixel type.

Plz consider the below code (which took *(#%@!& days to develop). The section “Generate The Texture” is concerned with the topic of this thread. It is uses the above two features of the library (jogl-1.1.0-win and jdk1.6) to convert any type of image to an OpenGL texture. The code is part of my JOGL version of NeHe tutorial 6. It is supposed to be as fast as possible (unless you would rewrite ImageIO, BufferedImage, flipImageVertically and TextureData into 1 method good luck.)

The vars:
texture: int[]
IMAGE: “Data/NeHe.png” // (or JPG or BMP. All three tested and work fine)

/** Loads Images */
private void loadGLTextures(final GL gl) throws Exception {
    // Create The Texture
    gl.glGenTextures(1, texture, 0);

    // Typical Texture Generation Using Data From The Bitmap
    gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);

    // Generate The Texture
    BufferedImage img = ImageIO.read(getClass().getResource(IMAGE));
    if (img == null) throw new Exception("Could not read: " + IMAGE);
    ImageUtil.flipImageVertically(img);
    TextureData textureData = new TextureData(0, 0, false, img);
    gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, img.getWidth(),
            img.getHeight(), 0, textureData.getPixelFormat(), 
            GL.GL_UNSIGNED_BYTE, textureData.getBuffer());

    // Linear Filtering
    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);
}

For people that like searching, this was developed with jsr-231-1.1.0:


// JOGL 1-1.1.0
import com.sun.opengl.util.texture.Texture;

  public static Texture loadTexture(String filename) {
        Texture texture = null;
        try{
                texture = TextureIO.newTexture(new File(filename), false);
                texture.enable();
                texture.bind();
                texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
                texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
                texture.disable();
                float kilobytes = (float) texture.getEstimatedMemorySize() / 1024.0f;
                System.out.printf("Loaded %s Kb into memory", kilobytes);
        }catch(Exception e){
                System.out.println(e.getMessage());
                System.out.println("Error loading texture " + filename);
        }
        return texture;
    }


    public void render(GL gl) {
        gl.glPushMatrix();

        gl.glTranslatef(this.position[0], this.position[1], this.position[2]);
        gl.glRotatef(this.rotation[0], 1.0f,  0f  ,   0f);
        gl.glRotatef(this.rotation[1], 0f  ,  1.0f,   0f);
        gl.glRotatef(this.rotation[2], 0f  ,  0f  , 1.0f);
        gl.glScalef(this.scale[0], this.scale[1], this.scale[2]);

        if(this.texture != null) {
            this.texture.enable();
            this.texture.bind();
            gl.glEnable(gl.GL_CULL_FACE);
        }

        gl.glCallList(this.getDisplayId()); // or just run your gl.TexCoord / gl.Vertex

        if(this.texture != null) {
            this.texture.disable();
            gl.glDisable(gl.GL_CULL_FACE);
        }
        gl.glPopMatrix();
    }