Texture2d rainbow effect on flip

Apprentice
Posts: 7
Joined: 2009.03
Post: #1
Hi everyone.

I've been using OpenGL ES on the iPhone for a while. My purpose is to draw charts. But when it comes to displaying numbers things become tough Sad I use Texture2D class to draw numbers but when one texture draws on top of another I get rainbow effect when I flip my view Sad

Here's my code.

Code:
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        for(int i = 0; i != 2; i++)
        {
            Texture2D* testTex = [[Texture2D alloc] initWithString:@"Hello" dimensions:CGSizeMake(20, 20) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:14];
            [testTex drawAtPoint:CGPointMake(200.0f, 200.0f)];
            [testTex release];
        }
        glDisable(GL_BLEND);

[Image: RmcCI3wvWQ.jpg]

Any clues?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
First off, for debugging graphics issues, screenshots are worth more than a thousand words. Wink

Are you sure that you can delete textures before the rendering is complete? I would expect that it would be fine, but I've never actually tried it before.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #3
I've made a screenshot but I forgot to paste it Blush

Here it is

[Image: RmcCI3wvWQ.jpg]
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #4
your texture is 20 * 20 pixels right? GL_TEXTURE_2D requires your textures to have dimensions of powers of 2.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #5
Hog Wrote:your texture is 20 * 20 pixels right? GL_TEXTURE_2D requires your textures to have dimensions of powers of 2.
I've set dimensions to 32x32 but it works the same Sad

I suppose that Texture2D rounds dimensions to be pow of 2 but I've never heard of such rules. It works with my size fineAnnoyed
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #6
Texture2D uses malloc when loading images and should use calloc instead to avoid getting garbarge in images less that 64 x64. You'll have to go in there and mod it yourself.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #7
Does it have anything to do with "rainbow" effect?
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
According to your screenshots, yes it [might].
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #9
Cant see where a memory leak might be...
Each malloc call is followed by free call. Why is there a need to use calloc? Memory is allocated for one object not for an array Annoyed
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #10
Using calloc has nothing to do with memory leaks. It has to do with the way Core Graphics fills in the buffer. If you're just using the string function of Texture2D you might be unaffected. If you're loading images though, just change them to calloc and save yourself the grief.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #11
Blush Sorry don't understand what should I do.
Could you point me in the code?
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #12
Try this: In Texture2D, do a search and replace of malloc with calloc. Then when you build it'll say there are too few arguments to calloc. So highlight each line where the error occurs and add , 1 to the parameter list. So something that used to say:

data = malloc(height * width * 4);

Should now be:

data = calloc(height * width * 4, 1);
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #13
The issue is that malloc gives you memory with some random junk in it and calloc makes sure that all of the memory is set to 0's before giving you the pointer.

When you draw the texture into that memory, it draws over the random garbage leaving you with the rainbow speckles. If the memory was set to 0's first it would have been drawn over white and would have looked normal.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #14
Right. Apparently, Core Graphics overwrites everything in the buffer if over 64x64, but doesn't for buffers less than that, which is why calloc is needed to clear the buffer first.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2009.03
Post: #15
Thanks for the tip but it didn't remove that "rainbow" Sad
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Texture2d crash lander ajrs84 0 2,882 Apr 1, 2012 08:54 AM
Last Post: ajrs84
  Water effect markhula 9 8,525 May 10, 2011 12:03 AM
Last Post: markhula
  Problem using Texture2D sub-section bendell 0 2,526 Mar 20, 2010 02:06 AM
Last Post: bendell
  Create a texture2d with contents of other textures godexsoft 6 4,793 Nov 12, 2009 10:24 PM
Last Post: godexsoft
  Creating a water effect iamflimflam1 0 2,544 Aug 4, 2009 06:46 AM
Last Post: iamflimflam1