Small size texture problem

Posts: 306
Joined: 2009.03
Post: #1
I need to move to a texture atlas, but i was hoping to delay that until my next project.
Everything is rendering fine in my app except I tried to add a small mute button in the corner. This is the first time i have loaded a texture in the 16x16 range. The texture shows on the screen but it has random color pixelation(as if it was using random memory) along side the actual image from the file. I looked over my texture loading code and rendering code and none of it seems to have anything that would care the image size. If i take the same image and enlarge it to 256x256 it works fine. Does anyone know if there is a lower limit on texture sizes?

code below in case im doing something wrong

        CGImageRef spriteImage;
        CGContextRef spriteContext;
        GLubyte *spriteData=NULL;
        // Creates a Core Graphics image from an image file
        spriteImage = [UIImage imageNamed:filename].CGImage;
        // Get the width and height of the image
        width = CGImageGetWidth(spriteImage);
        height = CGImageGetHeight(spriteImage);
        // Texture dimensions must be a power of 2.
            // Allocated memory needed for the bitmap context
            spriteData = (GLubyte *) malloc(width * height * 4);
            // Uses the bitmatp creation function provided by the Core Graphics framework.
            spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
            // After you create the context, you can draw the sprite image to the context.
            CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (GLfloat)width, (GLfloat)height), spriteImage);
            // You don't need the context at this point, so you need to release it to avoid memory leaks.
            // Use OpenGL ES to generate a name for the texture.
            glGenTextures(1, &spriteTexture);
            // Bind the texture name.
            glBindTexture(GL_TEXTURE_2D, spriteTexture);
            // Speidfy a 2D texture image, provideing the a pointer to the image data in memory
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
            // Release the image data

Then the rendering

    //Set params needed for drawing textures
    // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
    // Enable use of the texture
    // Set a blending function to use
    // Enable blending
    //Set the texture to use
    Texture *texture=[textures objectAtIndex:gameTick%frames];
    [texture bind];
    glTranslatef(centerX, centerY, 0);
    float halfWidth=texture.width*scale/2.0f;
    float halfHeight=texture.height*scale/2.0f;
    // Sets up pointers and enables states needed for using vertex arrays and textures
    const GLfloat spriteVertices[] = {
        -halfWidth, -halfHeight,
        halfWidth, -halfHeight,
        -halfWidth,  halfHeight,
        halfWidth,  halfHeight,

    glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
    //Draw the texture
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Thanks if anyone has any ideas
Quote this message in a reply
Posts: 320
Joined: 2003.06
Post: #2
I'm not 100% sure this is the problem, but try clearing your CGContext before drawing the image. CGContextClearRect. If you have any 100% transparent parts of your image your current code could well be causing random garbage to appear.

Chopper, iSight Screensavers, DuckDuckDuck:
Quote this message in a reply
Posts: 34
Joined: 2009.01
Post: #3
I believe that if you replace the malloc with calloc, this issue may go away.
Quote this message in a reply
Posts: 306
Joined: 2009.03
Post: #4
Thanks guys, Calloc did the trick. This is strange that big textures didn't show this problem. Maybe its because they need so much memory it was a higher likely hood that it would be unused memory. Anyhow thanks a ton. I have another question tonight but I will start a new thread for it heh.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  opengl es strange 16bit texture scale problem spuckfunkel 6 12,506 Aug 14, 2011 10:44 PM
Last Post: headkaze
  iPad window size problem the_wandering_monster 7 10,707 Oct 8, 2010 11:06 AM
Last Post: Rasterman
  SOIL image library problem or texture lib problem hippocoder 12 11,708 Mar 22, 2010 08:51 PM
Last Post: Frank C.
  Texture Size in Memory bmantzey 9 6,268 Feb 21, 2009 10:54 AM
Last Post: bmantzey
  Texture Path Problem Phr4nk 2 2,869 Dec 9, 2008 06:16 PM
Last Post: Phr4nk