# [SOLVED] New texture problem

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

## Recommended Posts

I'm texturing a sphere with a jpeg image. EDIT: This is the image in question, btw: clix0r (Beware, 3MB+ size) Most of the time it works great. However, there are some images that come out distorted and lose their colour. Screenshot: Clicky Is this because they have an alpha channel? Can the jpeg format store alpha info? When I pass the texture type as GL_RGBA, the skewing goes, but it shows up as a reptitive texture with some strange results (Of course, this is just voodoo, but I thought it may be helpful in diagnosing the problem) Screenshot: Clicky So, what could be the problem and how may I resolve it? Thanks. [Edited by - runningwithscissors on October 31, 2007 2:21:48 AM]

##### Share on other sites
Well, I'd guess it's either some error in your image loading code or the bit-depth of those images isn't always the same, e.g. GL_RGBA expects 32-bit images,so if you pass 16 or 24-bit images you'd get wrong results.

I'm not sure this is the problem though. Did you check and verify that the image stats (bpp, dimension etc.) fits the parameters for glTexImage2D()?

##### Share on other sites
Quote:
 Original post by Lord_EvilWell, I'd guess it's either some error in your image loading code or the bit-depth of those images isn't always the same, e.g. GL_RGBA expects 32-bit images,so if you pass 16 or 24-bit images you'd get wrong results.I'm not sure this is the problem though. Did you check and verify that the image stats (bpp, dimension etc.) fits the parameters for glTexImage2D()?

The image loading code is working fine with other textures, so if there's a bug, it'd be hard to track.

The dimensions of the image are not powers of 2 and I am using gluBuild2DMipmaps to generate the textures.

About the bit-depth, I am using libjpeg to compress and decompress the images, and the library reports that the image has 3 components, and I am assuming the jpeg data to be 8 bit, so the bits per pixel would be 24. And the dimensions are also as reported by libjpeg.
This is the relevant line of code:
/* cinfo is the libjpeg decompression struct *//* type is GL_RGB due to no of components being = 3 and pixel data assumed to be 8 bit */gluBuild2DMipmaps(GL_TEXTURE_2D, 3, cinfo.image_width, cinfo.image_height, type, GL_UNSIGNED_BYTE, texture->bytes);

libjpeg also says that I need to compile a seperate library for 12 bit jpeg support, but I don't think 12 bit jpegs are common, and that this image contains 12 bit data. Is there a way to check that?

[Edited by - runningwithscissors on October 29, 2007 5:11:56 AM]

##### Share on other sites
Well, I'm not sure if you always can assume a 3-component image to be 24-bit, since 16-bit images also might have 3 components, just with fewer bits per component.

I personally don't have experience with libjpeg but there should be some field/function that you can query. Alternatively, get the size of the data buffer and devide by the number of pixels. That should give you the bpp.

Another important thing: do you employ the non-power-of-2 extension (can't remember the name right now)? If not that could be your problem, since without it OpenGL assumes the textures to be power-of-2.

##### Share on other sites
Quote:
 Original post by Lord_EvilWell, I'm not sure if you always can assume a 3-component image to be 24-bit, since 16-bit images also might have 3 components, just with fewer bits per component.I personally don't have experience with libjpeg but there should be some field/function that you can query. Alternatively, get the size of the data buffer and devide by the number of pixels. That should give you the bpp.
That's a good tip. About me assuming 8 bit image data, I think jpeg only allows 8 or 12 bit depth. But, I'll poke around to extract that bit of info from the library.

Quote:
 Original post by Lord_EvilAnother important thing: do you employ the non-power-of-2 extension (can't remember the name right now)? If not that could be your problem, since without it OpenGL assumes the textures to be power-of-2.
That shouldn't be a problem, as gluBuild2DMipmaps sclaes the image dimensions to powers of 2 anyway.

##### Share on other sites
You should also check the gluBuild2DMipmaps return code and gluErrorString. That might give you a hint of what's wrong.

##### Share on other sites
Quote:
 You should also check the gluBuild2DMipmaps return code and gluErrorString. That might give you a hint of what's wrong.

Will do that.
EDIT: No luck. The returned string says "no error".

Relevant info:
It doesn't seem to be a problem with the jpeg image at all.

I decompressed it and recompressed the buffer back as a jpeg with 8 bit RGB data. And it works fine. The output is exactly the same as the original.

The problem is completely in the texture generation/rendering code.

I suspect the texture generation.

This is the code that generates it:
glGenTextures(1, &(texture->id));glBindTexture(GL_TEXTURE_2D, texture->id);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture->width, texture->height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)texture->bytes);

So... if anyone can spot anything irregular, it would be appreciated.

While I pay extra attention to the man page for gluBuild2DMipmaps...

##### Share on other sites
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);

I'm pretty sure you'd have to change this to either GL_CLAMP or GL_REPEAT. But I'm not sure it would fix your problem.

##### Share on other sites
Hooray! It's fixed.

It was missing a:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

Thanks to everyone who helped troubleshoot this.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 38
• 16
• ### Forum Statistics

• Total Topics
634128
• Total Posts
3015693
×