Small size texture problem

Member
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

Code:
spriteTexture=0;
        width=-1;
        height=-1;
        
        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.
        
        if(spriteImage)
        {
            // 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.
            CGContextRelease(spriteContext);
            
            // 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
            free(spriteData);
        }



Then the rendering

glPushMatrix();
    //Set params needed for drawing textures
    // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // Enable use of the texture
    glEnable(GL_TEXTURE_2D);
    // Set a blending function to use
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // Enable blending
    glEnable(GL_BLEND);
    //Set the texture to use
    Texture *texture=[textures objectAtIndex:gameTick%frames];
    [texture bind];
    glColor4f(1,1,1,alpha);
    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);
    glEnableClientState(GL_VERTEX_ARRAY);
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    //Draw the texture
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();

Thanks if anyone has any ideas
Quote this message in a reply
Member
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: http://majicjungle.com
Quote this message in a reply
Member
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
Member
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 8,966 Aug 14, 2011 10:44 PM
Last Post: headkaze
  iPad window size problem the_wandering_monster 7 8,488 Oct 8, 2010 11:06 AM
Last Post: Rasterman
  SOIL image library problem or texture lib problem hippocoder 12 8,676 Mar 22, 2010 08:51 PM
Last Post: Frank C.
  Texture Size in Memory bmantzey 9 5,139 Feb 21, 2009 10:54 AM
Last Post: bmantzey
  Texture Path Problem Phr4nk 2 2,184 Dec 9, 2008 06:16 PM
Last Post: Phr4nk