OpenGL Texture problems

Discussion in 'iOS Programming' started by seventoes, Aug 7, 2008.

  1. seventoes macrumors newbie

    Joined:
    Feb 5, 2008
    #1
    This is probably something simple, but I cant seem to spot whats wrong with it.

    http://img386.imageshack.us/my.php?image=picture4gk6.png

    :confused:

    I load the image just like the GLSprite example,
    Code:
    tempImage = [UIImage imageNamed:@"BackgroundGrid.png"].CGImage;
    width = next_power_of_2(CGImageGetWidth(tempImage));
    height = next_power_of_2(CGImageGetHeight(tempImage));
    		
    	// Allocate memory for bitmap context
    	tempData = (GLubyte *) malloc(width * height * 4);
    	tempContext = CGBitmapContextCreate(tempData, width, height, 8, width*4, CGImageGetColorSpace(tempImage), kCGImageAlphaPremultipliedLast);
    	// Draw sprite on the context
    	CGContextDrawImage(tempContext, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), tempImage);
    
    // etc...
    
    My power of 2 function is working fine, but it looks like some size is being messed up somewhere. I'm thinking it has something to do with the arrays im using for glTexturePointer and glTexCoordPointer, since i still dont fully get what those 2 are for. I tried to go through the Texture2D code to figure out what values to use, and i came up with this:
    Code:
    // Sets up an array of values to use as the sprite vertices.
    const GLfloat spriteVertices[] = {
    0, 0, 1,
    320, 0, 1,
    0, 480, 1,
    320, 480, 1,
    };
    
    // Sets up an array of values for the texture coordinates.
    const GLshort spriteTexcoords[] = {
    0, 1,
    1, 1,
    0, 0,
    1, 0,
    };
    Drawing with:
    Code:
    glBindTexture(GL_TEXTURE_2D, sprites[eTexBackground].tex);
    glVertexPointer(3, GL_FLOAT, 0, spriteVertices);
    glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    Also the texture randomly flashes on and off the screen :-/

    Anyone tell me what noob mistake I'm making?
     
  2. DipDog3 macrumors 65816

    DipDog3

    Joined:
    Sep 20, 2002
    #2
    Try:

     
  3. seventoes thread starter macrumors newbie

    Joined:
    Feb 5, 2008
    #3
    No luck, same result :(
     
  4. DipDog3 macrumors 65816

    DipDog3

    Joined:
    Sep 20, 2002
    #4
    Try

    Changed the width*4 to width for bytesPerRow.
     
  5. seventoes thread starter macrumors newbie

    Joined:
    Feb 5, 2008
    #5
    Nope,
     
  6. DipDog3 macrumors 65816

    DipDog3

    Joined:
    Sep 20, 2002
    #6
    That sucks.

    Try resizing your image to 256x256 and try applying it.
     
  7. seventoes thread starter macrumors newbie

    Joined:
    Feb 5, 2008
    #7
    K, resized it and changed:
    Code:
    const GLfloat spriteVertices[] = {
    0, 0, 1,
    256, 0, 1,
    0, 256, 1,
    256, 256, 1,
    };
    Now i get this:
    http://img371.imageshack.us/my.php?image=picture4aa4.png
     
  8. ThaBunny macrumors newbie

    Joined:
    Aug 2, 2008
    #8
    You're passing your texcoords as FLOAT, but they're SHORTs

    Change the texcoord array to

    const float spriteTexcoords[] = {
    0.0f, 1.0f,
    1.0f, 1.0f,
    0.0f, 0.0f,
    1.0f, 0.0f,
    };
     
  9. seventoes thread starter macrumors newbie

    Joined:
    Feb 5, 2008
    #9
    That was it!! I knew it was something stupid. :p Thanks so much!

    Now the texture is showing up just fine, but its randomly flashing :(

    EDIT: Nevermind on that one, my erase function was swapping the buffers :|

    Thanks for the help!
     

Share This Page