Opengl/Cocoa text rendering

Apprentice
Posts: 11
Joined: 2011.02
Post: #1
So for a couple years I'd been using the code from OSC's text tutorial to render fast text into opengl views. I just got lion and tried running a project that previously worked fine in 10.6 and now i get static and strange/seemingly random artifacts within the text's bounding rect. Anyone ideas or similar experiences?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
http://onesadcookie.com/svn/NSViewTexture/ broke at 10.6 (which I fixed in r2691), but I just checked and it still seems to work on Lion?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #3
Hmmm, that's odd. I just tried it again in a new empty project and got the same effect, static fill the quad behind the text (the text does display).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
have you got blending turned on? perhaps the transparent pixels are not black?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #5
Nope, I copied you're code exactly into an empty project. Could it be some Xcode thing? ...Because as far as I can see that's the only difference aside from lion.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Well, does it work if you actually use my Xcode project?

The project I have is probably set up to build against the 10.6 SDK... does it work if you change that setting?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #7
I didn't see a way to download the actual project so I just copied the NSViewTexture class files into the project, then I copied all of the code from NSViewTextureTest.cpp into my projects main file.

So I tried changing both the base sdk and the deployment target but neither made a difference. Then I switched the architecture from the default 64 bit to 32 and it worked (regardless of the sdk), though I have not the slightest idea of why.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Code:
svn checkout http://onesadcookie.com/svn/NSViewTexture

It seems to work in 64-bit when I make that change here...
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #9
Okay, so I think I've got the first part taken care of. But there's still one issue. Using the code from the tutorial I created a routine to create textures on the fly which are deleted upon use, but the problem is that the text sitting in the supposedly deleted textures shows up in all subsequent textures.

Code:
id_type view;
GLuint texture;

void fast_text(float x,
                    float y,
                    float w,
                    float h,
                    const char *str)
{
    view = ConvenienceCreateNSTextView(W,
                                       h,
                                       "Apple Chancery",
                                       24.0,
                                       2,
                                       str);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    TexImageNSView(GL_TEXTURE_2D, view);
    
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, texture);
    glEnable(GL_TEXTURE_2D);
    
    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(x, y);
        glTexCoord2f(1.0f, 1.0f);
        glVertex2f( x+w, y);
        glTexCoord2f(1.0f, 0.0f);
        glVertex2f( x+w,  y+h);
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(x,  y+h);
    glEnd();
    
    glDeleteTextures(1, &texture);
    ConvenienceReleaseNSTextView(view);
}
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #10
Basically each time I call fast_text, the next text is textured over whatever text was drawn into the view from the previous call; it's as if the view itself had never been deallocated however (and thus accumulates whatever is drawn into it), from my understanding of the code, ConvenienceReleaseNSTextView() should delete the view and make is okay for me to call ConvenienceCreateNSTextView() for 'view' once again, right?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
The code was using malloc to allocate memory for the bitmap, assuming Cocoa would clear it, but running afoul of it being coincidentally clear the first allocation.

I've fixed the code to use calloc instead, and updated the sample app to a case that hit the problem.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #12
Two questions...

One - I've been using this code for quite some time to render text in an app I'm working on. Recently I enabled distributed objects logging via the product->edit_scheme menu as part of an unrelated debugging effort and I noticed a lot of logging activity - which for the life of me I'm unable to understand - going on as the result of the above mentioned text drawing code. Here's a sample:

Code:
conv 0x0 sequence 6
2012-02-25 10:43:29.531 rpg[1363:ac07] awaiting sequence 6 on 51715
2012-02-25 10:43:29.533 rpg[1363:ac07] NSConnection got REPLY sequence 6 from <NSMachPort: 0x6205ffc0> on <NSMachPort: 0x65880fc0>
2012-02-25 10:43:29.534 rpg[1363:ac07] seq 6 in coder 0x67801fd0 being stored in hash table
2012-02-25 10:43:29.535 rpg[1363:ac07] Deleting sequence 6 in extractSequence on thread 0x62809fc0
2012-02-25 10:43:29.536 rpg[1363:ac07] got reply sequence 6 in 0x67801fd0
2012-02-25 10:43:29.537 rpg[1363:ac07] in forwardInvocation selName=_dataFromCheckingString:offset:types:options:orthography:learnedDictiona​ries:wordCount: received answer
2012-02-25 10:43:29.538 rpg[1363:ac07] exiting sendInvocation - [0x658a4fb0 _dataFromCheckingString:offset:types:options:orthography:learnedDictionaries:wor​dCount:...] conn:0x658a4fb0 exception: (null)
2012-02-25 10:43:29.541 rpg[1363:ac07] *** -[NSDistantObject release]: 0x66138fe0 6 1 (remote)
2012-02-25 10:43:29.542 rpg[1363:2b03] *** -[NSDistantObject methodSignatureForSelector:]: _dataFromCheckingString:offset:types:options:orthography:learnedDictionaries:wor​dCount:
2012-02-25 10:43:29.543 rpg[1363:2b03] *** -[NSDistantObject forwardInvocation:]: _dataFromCheckingString:offset:types:options:orthography:learnedDictionaries:wor​dCount: (remote)
2012-02-25 10:43:29.544 rpg[1363:2b03] +++ lookUpConnectionForProxy: returning 0x658a4fb0 for proxy 0x66138fe0
2012-02-25 10:43:29.545 rpg[1363:2b03] entered sendInvocation [0x... <NSMethodSignature: 0x63243fe0>...]
2012-02-25 10:43:29.546 rpg[1363:2b03] Reserving sequence 7 on thread 0x65be8fc0
2012-02-25 10:43:29.548 rpg[1363:2b03] +++ lookUpConnectionForProxy: returning 0x658a4fb0 for proxy 0x66138fe0
2012-02-25 10:43:29.549 rpg[1363:2b03] +++ lookUpWireIDForProxy: returning 32 for proxy 0x66138fe0
2012-02-25 10:43:29.551 rpg[1363:2b03] in forwardInvocation - made packet [0x658a4fb0 _dataFromCheckingString:offset:types:options:orthography:learnedDictionaries:wor​dCount:...] conn:0x658a4fb0 conv 0x0 sequence 7
2012-02-25 10:43:29.553 rpg[1363:2b03] awaiting sequence 7 on 51715
2012-02-25 10:43:29.554 rpg[1363:2b03] NSConnection got REPLY sequence 7 from <NSMachPort: 0x6205ffc0> on <NSMachPort: 0x65880fc0>
2012-02-25 10:43:29.555 rpg[1363:2b03] seq 7 in coder 0x6835bfd0 being stored in hash table
2012-02-25 10:43:29.556 rpg[1363:2b03] Deleting sequence 7 in extractSequence on thread 0x65be8fc0
2012-02-25 10:43:29.558 rpg[1363:2b03] got reply sequence 7 in 0x6835bfd0
2012-02-25 10:43:29.559 rpg[1363:2b03] in forwardInvocation selName=_dataFromCheckingString:offset:types:options:orthography:learnedDictiona​ries:wordCount: received answer
2012-02-25 10:43:29.560 rpg[1363:2b03] exiting sendInvocation - [0x658a4fb0 _dataFromCheckingString:offset:types:options:orthography:learnedDictionaries:wor​dCount:...] conn:0x658a4fb0 exception: (null)


Is this normal or something I should be worried about?



And two - I was fooling around with apples code for doing opengl text (GLString) and was unable to find a way in which to constrain the text so as to keep it within the bounds of a predefined rectangle. Anyone know how to do this?
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #13
That's logging the chatter that DO uses. I don't see how this at all relates to your text rendering or how it's a problem.

As for bounding the text, I assume you mean you want to be able to create a really long string, but have it wrap, or truncate?

It looks like if you use useStaticFrame: then it'll draw the string into a frame no larger than that given size, but the text will be single-line only and cut off rather than nicely truncated.

If you want wrapping or truncation, you'll need to modify GLString. In genTexture it's using [string drawAtPoint:...]; You'll want to use drawWithRect:options: with the option NSStringDrawingUsesLineFragmentOrigin I believe (for wrapping), and setup an NSMutableParagraph style with a line break mode for truncation.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #14
(Feb 25, 2012 12:21 PM)SethWillits Wrote:  I don't see how this at all relates to your text rendering or how it's a problem.

The text rendering is what seems to be generating the logs. When I turn it off, the logs disappear - in fact, I get no logs whatsoever when not rendering text.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2011.02
Post: #15
By the way, will this code work on iOS?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Fast rendering with OpenGL 1.1 dotbianry 4 3,660 Dec 18, 2012 03:58 AM
Last Post: dotbianry
  SDL/OpenGL fullscreen text rendering StealthyCoin 2 6,523 Mar 26, 2009 09:47 AM
Last Post: StealthyCoin
  OpenGL Text Rendering (in Cocoa) daveh84 5 7,893 Feb 19, 2009 12:44 PM
Last Post: TomorrowPlusX
  OpenGL &amp; Cocoa - Improving frame rate daveh84 4 5,550 Feb 2, 2009 06:53 AM
Last Post: backslash
  bad depth sorting in Cocoa OpenGL aldermoore 2 4,643 Dec 30, 2008 03:07 PM
Last Post: ThemsAllTook