Jump to content
  • Advertisement
Sign in to follow this  
MassimoNicolardi

Problems with switching of textures

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

Hello,

First of all I am using mac with Xcode.

I have written a class method to load a texture. It reads the png file, copy it in the video memory and returns the texture id.
I use this method to load 2 different textures.

However when i use the first or the second I just see the last loaded...

Perhaps I'm missing something about how to switch between different textures...

Here is the class that loads the texture:


+ (GLuint) loadTexture: (NSString *) fileName {

NSImage *baseImage = [NSImage imageNamed:fileName];

// We have to recreate the image flipped right-side up (bleh)

NSImage *image = [[NSImage alloc] initWithSize:[baseImage size]];

NSRect r = NSZeroRect;

r.size = [baseImage size];

[image lockFocus];

NSAffineTransform *t = [NSAffineTransform transform];

[t translateXBy:0 yBy:r.size.height];

[t scaleXBy:1 yBy:-1];

[t concat];

[baseImage drawAtPoint:NSZeroPoint fromRect:r operation:NSCompositeCopy fraction:1];

[image unlockFocus];

NSBitmapImageRep *bitmap=[[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];

GLubyte *sourcePic;

GLenum imageFormat = GL_RGBA;

long sourceRowBytes, pixelSize;



// Check if it has alpha (transparency)

// We have to know how many numbers for each pixel.

// A pixel can have just a white value (grayscale) or red,green,and blue values (color). It has a 4th value if it's transparent like a png.

if ([bitmap hasAlpha] == YES) {

imageFormat = GL_RGBA;

pixelSize = 4;

} else {

imageFormat = GL_RGB;

pixelSize = 3;

}



// NOW we actually get the data

NSSize imageSize = [bitmap size];

sourceRowBytes = [bitmap bytesPerRow];



// This SHOULD be enough but nope

sourcePic = (GLubyte*) [bitmap bitmapData];



// We have to copy that raw data one row at a time....yay

GLubyte *pic = (GLubyte *) malloc( imageSize.height * sourceRowBytes );

GLuint i;

for (i = 0; i < imageSize.height; i++) {

memcpy ( pic + (i * sourceRowBytes),

sourcePic + ((((int)imageSize.height - i) - 1) * sourceRowBytes),

sourceRowBytes);

}

sourcePic = pic;

GLuint textureID; //This is the 'id' we reference when we want to use it.



// Set the right 'state' to deal with textures

glEnable(GL_TEXTURE_2D);

glEnable(GL_COLOR_MATERIAL);



// Generate a 2D Texture buffer

// Note : There is such a thing as a 1D texture in OpenG which is ssentially just an image 1 pixel high

// Seems odd but pretty useful for things like simple gradients.

glGenTextures (1, &textureID);

glBindTexture (GL_TEXTURE_2D, textureID);

glPixelStorei(GL_UNPACK_ALIGNMENT,1);



// Here we set the basic properties such as how it looks when resized and if it's bordered

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BORDER, 0);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



// Not really necessary all the time but we can also define if it can be tiled (repeating)

// Here GL_TEXTURE_WRAP_S = horizontal and GL_TEXTURE_WRAP_T = vertical

// This defaults to GL_REPEAT so we're going to prevent that by using GL_CLAMP

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);



// And.....CREATE

glTexImage2D(GL_TEXTURE_2D, 0, imageFormat, r.size.width, r.size.height, 0, imageFormat, GL_UNSIGNED_BYTE, sourcePic);



NSLog(@"TEXTURE ID: %d",textureID);

// Get out of the texture 'state'

glDisable(GL_TEXTURE_2D);

glDisable(GL_COLOR_MATERIAL);

return textureID;

}





Then I load the textures using...


textures[0] = [TextureClass loadTexture: @"01.png"];

textures[1] = [TextureClass loadTexture: @"02.png"];




In the drawing section I have:


glEnable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE0);




//NSLog(@"Texture bindata: %d",textid);

glBindTexture(GL_TEXTURE0,textid);

//glColor3f(0,0.7,0);



// SOPRA

glBegin(GL_QUADS);

glTexCoord2f(texture_offset_u+0+U_FRACT,texture_offset_v+U_FRACT);

glVertex3f(-PAGE_WIDTH,-PAGE_WIDTH,0);



glTexCoord2f(texture_offset_u+0+U_FRACT,texture_offset_v+0);

glVertex3f(-PAGE_WIDTH,PAGE_WIDTH,0);



glTexCoord2f(texture_offset_u+U_FRACT+U_FRACT,texture_offset_v+0);

glVertex3f( PAGE_WIDTH,PAGE_WIDTH,0);



glTexCoord2f(texture_offset_u+U_FRACT+U_FRACT,texture_offset_v+U_FRACT);

glVertex3f(PAGE_WIDTH,-PAGE_WIDTH,0);

glEnd();




glDisable(GL_TEXTURE_2D);

glDisable(GL_TEXTURE0);






Changing the textid between textures[0] or textures[1] I have the same result... just the last texture is displayed on screen...

Can you find out where's my error?

Thank you!

Share this post


Link to post
Share on other sites
Advertisement
I'm not familiar with Xcode but it looks like you need to make calls to glActiveTexture and send it the gluint id of whichever texture you are going to use next

Share this post


Link to post
Share on other sites
These lines in your drawing code are the trouble makers:glEnable(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0,textid);

There is no glEnable (GL_TEXTURE0); I guess that maybe you wanted to use glActiveTexture (GL_TEXTURE0) instead but you don't really need to in this case as GL_TEXTURE0 is active by default and will always be active unless you explicitly change it yourself.

For glBindTexture the target param should be GL_TEXTURE_2D, not GL_TEXTURE0. This glBindTexture call is actually failing on you, which is causing the last texture bound to stay bound, which will be the last one you've created (where you've got your bind call right).

Share this post


Link to post
Share on other sites
Thanks a lot!!! it worked perfectly!

These lines in your drawing code are the trouble makers:glEnable(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0,textid);

There is no glEnable (GL_TEXTURE0); I guess that maybe you wanted to use glActiveTexture (GL_TEXTURE0) instead but you don't really need to in this case as GL_TEXTURE0 is active by default and will always be active unless you explicitly change it yourself.

For glBindTexture the target param should be GL_TEXTURE_2D, not GL_TEXTURE0. This glBindTexture call is actually failing on you, which is causing the last texture bound to stay bound, which will be the last one you've created (where you've got your bind call right).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!