Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problems with switching of textures


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 asdraban   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 December 2011 - 07:58 AM

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!

Sponsor:

#2 Kobo   Members   -  Reputation: 128

Like
0Likes
Like

Posted 21 December 2011 - 11:48 AM

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

#3 mhagain   Crossbones+   -  Reputation: 7979

Like
1Likes
Like

Posted 21 December 2011 - 01:25 PM

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).

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 asdraban   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 December 2011 - 01:51 PM

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).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS