tags:

views:

272

answers:

1

I have the following code to render text in my app, first i get the mouse coordinates in the world, then use those coordinates to place my text in the world, so it will follow my mouse position:

Edit: added buildfont() function in code example:

GLvoid BuildFont(GLvoid)                                // Build Our Bitmap Font
{
    HFONT   font;                                       // Windows Font ID
    HFONT   oldfont;                                    // Used For Good House Keeping

    base = glGenLists(96);                              // Storage For 96 Characters

    font = CreateFont(  -12,                            // Height Of Font
                        0,                              // Width Of Font
                        0,                              // Angle Of Escapement
                        0,                              // Orientation Angle
                        FW_NORMAL,                      // Font Weight
                        FALSE,                          // Italic
                        FALSE,                          // Underline
                        FALSE,                          // Strikeout
                        ANSI_CHARSET,                   // Character Set Identifier
                        OUT_TT_PRECIS,                  // Output Precision
                        CLIP_DEFAULT_PRECIS,            // Clipping Precision
                        ANTIALIASED_QUALITY,            // Output Quality
                        FF_DONTCARE|DEFAULT_PITCH,      // Family And Pitch
                        "Verdana");                     // Font Name (if not found, its using some other font)

    oldfont = (HFONT)SelectObject(hDC, font);           // Selects The Font We Want
    wglUseFontBitmaps(hDC, 32, 96, base);               // Builds 96 Characters Starting At Character 32
    SelectObject(hDC, oldfont);                         // Selects The Font We Want
    DeleteObject(font);                                 // Delete The Font
}

GLvoid glPrint(const char *fmt, ...){
    char text[256];
    va_list ap;
    if (fmt == NULL) return;
    va_start(ap, fmt);
        vsprintf(text, fmt, ap);
    va_end(ap);
    glPushAttrib(GL_LIST_BIT);  
    glListBase(base - 32);  
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
    glPopAttrib();
}

...

glPushMatrix();
    glColor4f(0,0,0,1);
    // X-1 wont work because these are the world coordinates:
    glRasterPos2d(MousePosX-1, MousePosY);
    glPrint("TEST");

    glColor4f(1,1,0,1);
    glRasterPos2d(MousePosX, MousePosY);
    glPrint("TEST");
glPopMatrix();

But i want to render multiline texts, or texts with "borders" (like in above code i tried) or text with background (so i could distinguish them better from the background) So how i do this?

I just need to know how i can move it in pixels, so i could precisely modify the position on my screen WITHOUT using 2d projection view on top of my 3d render projection... i just want to make it as simple as possible.

I tried to draw a quad under the text, but of course it doesnt work since its still using the world coordinates... so when i rotate my camera the text doesnt move along the background of the text... i am afraid the only solution is to create another projection on top of the 3d projection...

+1  A: 

Here's a small snippet of code that I use to render some debug text in a small application:

void
renderText(float x, float y, const char* text) {
    int viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(viewport[0], viewport[2], viewport[1], viewport[3], -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRasterPos2f(x, viewport[3] - y);
    const int length = (int)strlen(text);
    for (int i = 0; i < length; ++i) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, text[i]);
    }
    glMatrixMode( GL_PROJECTION );
    glPopMatrix();
    glMatrixMode( GL_MODELVIEW );   
    glPopMatrix();
}

xand y is the desired window coordinates of the string. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ...) is just a utility function from GLUT that renders a 9 x 15 pixels large bitmap character.

Andreas Brinck
well that is basically same as what im using now: another layer of "window" with 2d projection mode on top of the 3d rendering mode.
@Newbie Is there any particular reason why you don't want to have a separate projection for the text?
Andreas Brinck
i thought there could be some other way... no problem, i will just use 2d projection on top of 3d :)