Sign in to follow this  

Texture stretching

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a 256 x 256 2D image, and I want to map this to a quad. Because of the black area around the bitmap, when I try to make the quad smaller to fit the bitmap the bitmap image shrinks. How do I remove the black areas of the bitmap so the image fit’s more precisely to a quad? Thanks, Steve

Share this post


Link to post
Share on other sites
I would guess that you need to fix the image (texture), and not the quad. The four corners of the texture (regardless of the image size used to make the texture) are "pinned" to the four corners of the quad using the texture coordinates, so whatever your do to the quad (making it smaller for example), will not change the fact that the four corners of the texture are still pinned to the four corners of the quad. Hope that simplification makes sense :-)

When you view the image in the folder, does it have the black border there? If so, you need to edit it using image editing software (Gimp, Paint, PaintShop Pro etc) and remove it.

If not, then it might be that you have specified a border on the texture when generating it. Quote from Nehe Lesson 6:

Quote:

Next we create the actual texture. The following line tells OpenGL the texture will be a 2D texture (GL_TEXTURE_2D). Zero represents the images level of detail, this is usually left at zero. Three is the number of data components. Because the image is made up of red data, green data and blue data, there are three components. TextureImage[0]->sizeX is the width of the texture. If you know the width, you can put it here, but it's easier to let the computer figure it out for you. TextureImage[0]->sizey is the height of the texture. zero is the border. It's usually left at zero. GL_RGB tells OpenGL the image data we are using is made up of red, green and blue data in that order. GL_UNSIGNED_BYTE means the data that makes up the image is made up of unsigned bytes, and finally... TextureImage[0]->data tells OpenGL where to get the texture data from. In this case it points to the data stored in the TextureImage[0] record.

// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);


You could also do something funky with your texture coordinates also. If you know that the border on the image is say 5 pixels of the 256, then you could calculate the offset for the texture coordinate, so instead of them running from 0.0 to 1.0, then run from (5/256 = 0.02) 0.0+0.2 to 1.0-0.2.

Check out the NeHe tutorial above.

hth
F451

Share this post


Link to post
Share on other sites
Quote:
Original post by steveharper
I have a 256 x 256 2D image, and I want to map this to a quad. Because of the black area around the bitmap, when I try to make the quad smaller to fit the bitmap the bitmap image shrinks. How do I remove the black areas of the bitmap so the image fit’s more precisely to a quad?

Thanks,

Steve
So you have some image that is smaller than 256x256 inside a 256x256 texture and the empty space is padded with black pixels. Am I understanding you correctly?

If that's the case all you need to do is adjust the texture coordinates for your quad. Instead of going from 0 to 1, you go from 0 to s/256.0 where s is the size of the packed image in either dimension.

EDIT: I'm very slow.

Share this post


Link to post
Share on other sites
Thanks for the post's. Altering the U/V coord's is a really good idea, i didn't even consider. I was going to use alpha blending to make the black transparent, but needed to reduce the size of the image, so that collision detection of the 2D sprites looks more acurate, but most size's i tried to load won't show up on the screen.

Share this post


Link to post
Share on other sites
Remember that if you change the size/shape of the quad, the texture will change too.

To display only a small region of a texture, you need to pick texture coordinates which represent that region.

For example, for the middle 80% of a texture, your texture coords should go from 0.1 to 0.9 rather than 0 to 1. This applies of course, in both axes.

Mark

Share this post


Link to post
Share on other sites

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this