iDevGames Forums
Opengl/Cocoa text rendering - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Opengl/Cocoa text rendering (/thread-9289.html)

Pages: 1 2


Opengl/Cocoa text rendering - tesil - Aug 20, 2011 11:25 AM

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?


RE: Opengl/Cocoa text rendering - OneSadCookie - Aug 20, 2011 01:39 PM

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?


RE: Opengl/Cocoa text rendering - tesil - Aug 20, 2011 05:32 PM

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).


RE: Opengl/Cocoa text rendering - OneSadCookie - Aug 20, 2011 06:14 PM

have you got blending turned on? perhaps the transparent pixels are not black?


RE: Opengl/Cocoa text rendering - tesil - Aug 20, 2011 06:27 PM

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.


RE: Opengl/Cocoa text rendering - OneSadCookie - Aug 20, 2011 09:29 PM

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?


RE: Opengl/Cocoa text rendering - tesil - Aug 21, 2011 10:36 AM

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.


RE: Opengl/Cocoa text rendering - OneSadCookie - Aug 21, 2011 05:48 PM

Code:
svn checkout http://onesadcookie.com/svn/NSViewTexture

It seems to work in 64-bit when I make that change here...


RE: Opengl/Cocoa text rendering - tesil - Aug 23, 2011 11:22 AM

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);
}



RE: Opengl/Cocoa text rendering - tesil - Aug 24, 2011 10:24 AM

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?


RE: Opengl/Cocoa text rendering - OneSadCookie - Aug 28, 2011 09:21 AM

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.


RE: Opengl/Cocoa text rendering - tesil - Feb 25, 2012 08:51 AM

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?


RE: Opengl/Cocoa text rendering - SethWillits - Feb 25, 2012 12:21 PM

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.


RE: Opengl/Cocoa text rendering - tesil - Feb 27, 2012 08:08 AM

(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.


RE: Opengl/Cocoa text rendering - tesil - Mar 20, 2012 07:39 AM

By the way, will this code work on iOS?