PDA

View Full Version : cannot load non-square image as texture!



billconan
09-29-2006, 01:43 AM
hello guys, is it true that opengl only support texture in square?
i'm working on my 3d modeller which allows people to choose an image as reference, and i can't guarantee that the image is in square. my program is ok if the image i specified is in square but becomes very weird if it is not. so how to deal with it. do i need to manually map the image to a square texture? thank you.

this is the how the app looks if the texture for those buttons is 256*256:
http://billconan.googlepages.com/right.jpg

and this is how it looks when i expand the texture to 300*256(in photoshop)
http://billconan.googlepages.com/wrong.jpg

and this is my code for generating texture from image(using wxwidgets' image class):


GLuint loadTexture(char *fileName)
{
GLuint txtnumber;
wxImage texture(_T(fileName),wxBITMAP_TYPE_PNG,-1);
int theHeight=texture.GetHeight(); //read into height and width
int theWidth=texture.GetWidth();
unsigned char *RGBData=texture.GetData();
unsigned char *alphaData=texture.GetAlpha();
size_t pixelCount=theHeight*theWidth;
size_t dataSize=pixelCount*4;
unsigned char *data=new unsigned char[dataSize];
for(size_t i=0;i<pixelCount;++i)
{
data[i*4]=RGBData[i*3];
data[i*4+1]=RGBData[i*3+1];
data[i*4+2]=RGBData[i*3+2];
data[i*4+3]=alphaData[i];
}
glGenTextures(1, &amp;txtnumber);
glBindTexture(GL_TEXTURE_2D, txtnumber);
glTexImage2D(GL_TEXTURE_2D, 0, 4, theHeight, theWidth, 0,GL_RGBA,GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
delete data;
return txtnumber;
};
by the way,my opengl is 2.0.

Ehsan Kamrani
09-29-2006, 02:08 AM
The with and height of your textures should be a power of 2. As an example, you can use a 128 * 512 texture.However you can not use a 300 * 256 texture as an example. Some implementations of OpenGL convert the such values( 300 in my example )to the nearest value that is power of 2.However it's not guaranteed that your OpenGL version supports this feature.
-Ehsan-

jide
09-29-2006, 02:11 AM
No this is not true, at least since NPOT and texture rectangles which are supported (at least NPOT) on GL 2.x.

To my point of view what you have is simply a bad loading of the texture. Try to have a look at the wx png loading code for ensuring that or choose another format (rgb or tga).
Or it could be your unpack alignment that is not correct. But since I do not use PNG I really cannot ensure that. Try to google for more information.

billconan
09-29-2006, 02:34 AM
thank you

billconan
09-29-2006, 02:48 AM
oh, i know, i swapped the height and width, i was so silly!

k_szczech
09-29-2006, 03:11 AM
Two things to remember when using non power of two textures:
-these textures require Radeon 9500 or GeForce 6. Other GPU's support texture rectangles.
-when calling glTexImage2D make sure you use the proper alignment - try using 301x255 image for example