256x256pixel textures

Started by
48 comments, last by Harrism 18 years, 2 months ago
I want to be able to map a texture of 640x480 onto a square. Currently I can only do it if the texture is 256x256 if it's a pixel bigger I can't. Why? Can't put it any simplier, how can I get around this? or is there a reason for this restriction? I havent tried a smaller image, but all the textures on NeHe seem to all be 256x256. Thankyou
Advertisement
Normal graphic hardware requires that textures are a power of 2.

So width and height should each be 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, ...

Even though i've heard that some graphic hardware supports textures that arent a power of 2, I think that you should still have a power of 2.
What if I said I wanted it to be a backdrop. Say I was writing a simple opengl program to display any chosen bitmap. (a bit pointless but the princible is the same)
You can actually use any power of 2 texture size (though there may be an upper and lower bound to that, I'm not sure what they are offhand.)

So 256x256, 512x512, 1024x1024 are all valid.

To display a 640x480 texture on a 640x480 quad you can bind by these texture coordinates assuming a 1024x1024 sized texture:

top left: 0, 0
bottom right: (640/1024) which is.625, (480/1024) which is .46875


example (note, I'm just using 2d coordinates, but the glTexCoord2d function is the important part anyway):
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d((640.0/1024.0),0.0); glVertex2d(640.0,0.0);
glTexCoord2d((640.0/1024.0),(480.0/1024.0)); glVertex2d(640.0,480.0);
glTexCoord2d(0.0,(480.0/1024.0)); glVertex2d(0.0,480.0);
glEnd();
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Which kind of texture do you use? if it is GL_TEXTURE_2D, width and height must be a power of 2. So you need to create a 1024*512 texture containing your 640*480 texture, and then use the appropriate normalized texture coordinates ie 640/1024 and 480/512. You waste memory but graphic cards handle it very fast. If you don't want to waste memory, you can use GL_TEXTURE_RECTANGLE_ARB, and then it is possible to use non power of 2 size, but it will be slower than with GL_TEXTURE_2D (don't forget to use non normalized texture coordinates ie 640 and 480). There is also GL_TEXTURE_NON_POWER_OF_TWO_ARB but i don't know the differences.

[edit]
I was a little too slow to answer :S.
I just tried the exact code you wrote. It just gave me a white shape. I also tried merging it with mine same thing happened. The image is only displayed it seems if the saved bitmap is 256x.
You might want to look at one or other of these extensions:
ARB_texture_non_power_of_two
ARB_texture_rectangle

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

OpenGL 2.0 relaxes the requirement for textures to be power of two. On earlier implementations you can use the ARB_texture_rectangle extension for this functionality, with the caveat that rectangle textures cannot be mipmapped. If you only want to render it as a background image, this should not be an issue.

Edit - Mr Bunny beat me to the punch. The non_power_of_two extension is standard in GL 2.0.
try one that is 512x512 with your working 256x256 code. It should work. After you get that working, try a 1024x1024 one. Then after that try changing the values of the texture binding to (0, 0), (.5, .5) which should result in half the texture appearing over the whole quad.

Alkiem, I don't believe 1024x512 is a valid texture size for OpenGL. I think it's got to be both a power of 2 AND square.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
OpenGL has no requirement that textures be square, even before NPOT. I know a certain graphics chip with that restriction, but that piece of hardware is quite obsolete now.

This topic is closed to new replies.

Advertisement