Jump to content
  • Advertisement
Sign in to follow this  
tobiasknight

Odd texture loading problem (for me)

This topic is 4861 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

(Note: the following code is in Objective-C) This one is a silly one.. i might be over looking something here.. and i might not.. So now.. i ask for help. I seem to be having odd problems when loading my textures. The following are the two functions that kinda replaces using just glBindTexture before drawing a poly or something.. anyway i use my method bindTexture: (NSString) filename to load and bind a texture.. it checks if the tex has been loaded before and if so, just binds that one.. if not.. then it loads the png file (going through and flipping it from bitmap data first) sets all my texparams and such then binds it. This should work (and infact worked before but now.. well now it doesn't) but it just maps this funky little texture. (note: I don't expect someone to "do my work for me" and i'm not asking for that, i'd just like some insight please =) ) and here is the code <code> -(BOOL) bindTexture:(NSString *)filename { BOOL result; result = NO; if([textureDict objectForKey:filename] != nil) { textureName = [[textureDict objectForKey:filename] unsignedIntValue]; glBindTexture( GL_TEXTURE_2D, textureName); result = YES; } else { if( [ self loadPNG: [NSString stringWithFormat:@"%@/%@", [[ NSBundle mainBundle ] resourcePath ], filename]] ) { glGenTextures(1, &textureName); [textureDict setObject:[NSNumber numberWithUnsignedInt: textureName] forKey:filename]; glBindTexture( GL_TEXTURE_2D, textureName); glTexImage2D( GL_TEXTURE_2D, 0, 3, textureSize.width, textureSize.height,0, textureFormat, GL_UNSIGNED_BYTE, &textureData ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glBindTexture( GL_TEXTURE_2D, textureName); //free(&textureData); result = YES; } } return result; } -(BOOL) loadPNG:(NSString *) filename // actually loads the PNG and flips it { BOOL result = NO; NSBitmapImageRep *image; int bitsPerPixel, bytesPerRow; unsigned char *imageData; int rowNum, destRowNum; image = [NSBitmapImageRep imageRepWithContentsOfFile:filename]; if(image != nil) { bitsPerPixel = [ image bitsPerPixel ]; bytesPerRow = [ image bytesPerRow ]; if( bitsPerPixel <= 24) textureFormat = GL_RGB; if( bitsPerPixel == 32) textureFormat = GL_RGBA; textureSize.width = [ image pixelsWide ]; textureSize.height = [ image pixelsHigh ]; textureData = malloc( bytesPerRow * textureSize.height); if(textureData != NULL) { result = YES; imageData = [ image bitmapData ]; destRowNum = 0; for( rowNum = textureSize.height-1; rowNum >= 0; rowNum--,destRowNum++) { memcpy( textureData + ( destRowNum * bytesPerRow ), imageData + ( rowNum * bytesPerRow), bytesPerRow); } } } return result; } </code> I've been puzzling over this for a few days now..so might anyone have a bit of insight as to my problem? Anyone familiar enough with this network might recognized the texture loading code as a Obj-C/Cocoa port of Nehe's texture loading tutorial. =) ::blush:: I've had this (modified) working before.. but now.. well now it seems to hate me. Thanks in advance -Xell

Share this post


Link to post
Share on other sites
Advertisement
I don't know what that code is but it is usually a problem of memory allocation, bitsperpixel, images size, format (the inteerplay between these parameters). I would say, check all the texture related parameters before and after glTexImage2D();

Share this post


Link to post
Share on other sites
The only potential problem I see is where you pass textureData to glTexImage2D. I'm not familiar with Objective-C, but if textureData is a pointer, then you probably shouldn't be passing it's address, you should just pass textureData.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!