Problem loading non power of two textures

Started by
3 comments, last by Brother Bob 17 years, 4 months ago
Hi all. This is my problem: I have a 91x91 bitmap, so how can i load this on OpenGl since it can't load non power of two textures? Searching in the forum I found that I have to use glTextSubImage2D. But it doesn't work.


glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_RGB, 
	      GL_UNSIGNED_BYTE, NULL);

glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 91, 91, GL_RGB, GL_UNSIGNED_BYTE, 
		 image->data );

//.......
glBegin(GL_QUADS); 
     
      glTexCoord2f(0,1);	glVertex2i( 0, 0 ); 
		
      glTexCoord2f(1,1);        glVertex2i( 91, 0 ); 
		
      glTexCoord2f(1,0);        glVertex2i( 91, 91 ); 
		
      glTexCoord2f(0,0);        glVertex2i( 0, 91 ); 

glEnd();


The texture doesn't fit the QUAD, and also looks very strange, with a part of the bitmap drawed twice. Thanks in advance.
Advertisement
Your texture coordinates are wrong
glTexCoord2f - try using something other than 0 and 1 here
like maybe calculate the ratio based on how big the texture is compared to the actual adjusted power of 2 size
Since OpenGL 2.0, you can load non-power of two textures without doing anything special. In earlier versions, you have to rely on extensions (ARB_non_power_of_two being the same as how 2.0 works, or ARB_texture_rectangle introducing a new texture object type with some restrctions).

But in general, watch the data alignment. Default alignment is 4, which means each new row of the image must start on an offset which is a multiple of 4 bytes from the start address. 91 pixels times 3 bytes per pixel (GL_RGB) which is not properly aligned by default. So unless you change the alignment (check out glPixelStore and GL_UNPACK_ALIGNMENT) or add 3 bytes padding after each line, the image will not be loaded correctly.
Thanks both for the answer. I solve the problem with this:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

and glTextcoord with 128/91 = 0.7109 .

But still I don't understand very well what's the diferrence beetween RGB, RGBA, etc... and why I had to put GL_UNPACK_ALIGMENT = 1, since my image is a 24 bits bitmap? It only works with 1.

Can someone explain me this?

Thanks!

A row is 91 pixels wide, and each pixel is 3 color components (GL_RGB) and each component is 1 byte (GL_UNSIGNED_BYTE). That makes the total size of a row 273 bytes.

Since 273 bytes is not a multiple of 4 bytes (default unpack alignment), OpenGL must adjust it's read pointer so that, when starting to read the next row, the read pointer is aligned to 4 bytes. In order to do that, the read pointer is moved forward to the next offset that is a multiple of 4 bytes.

The next offset that is a multiple of 4 bytes from 273 is 276, so OpenGL skips 3 bytes and starts reading from there. If your image don't contain this 3 byte padding which OpenGL skips, then OpenGL will, of course, read the image wrong.

This topic is closed to new replies.

Advertisement