Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: 2D text in a 3D world

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2000
    Posts
    5

    2D text in a 3D world

    I've tried to do 2D text in my 3D world, but I with no success so far.
    I've tried going to gluOrtho2D and then using glRasterPos2D to place the text, and then glutBitmapCharacter to actually draw the text.
    The problem is, I don't exactly know how to set up gluOrtho2D, and then what coordinates to put in glRasterPos2D (I've tried 0,1,0,1 in gluOrtho2D and then something like 0.4,0.4 in glRasterPos2D but that didn't work).
    If someone can please point me in the right direction, including what matrix operations I need to do (like glPushMatrix or glMatrixMode to go to the projection matrix maybe?).

    Thanks.

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    8

    Re: 2D text in a 3D world

    The glRasterPos2D function looks like this:

    glRasterPos2D(int x, int y);

    You are not allowed to put float-digits as parameters.

  3. #3
    Junior Member Regular Contributor
    Join Date
    May 2001
    Posts
    137

    Re: 2D text in a 3D world

    Hi Tidhar,

    if you switch from 3d to 2d you can use:

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho (0.0, g_width, g_height,0, -1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    This will give you a rectangle with range (0,0) to (g_width,g_height). You may set g_width and g_height to the size of your window.

    Then you can do all your 2d drawing and text output (BTW void glRasterPos2f(GLfloat x, GLfloat y); is perfectly legal) and then reset your matrices:

    glEnable(GL_LIGHTING);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    Please note that this restores your projection matrix but not your modelview matrix. But I guess that you'll update it each frame...

    Hope that helps,
    Stefan

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    5

    Re: 2D text in a 3D world

    Hi,

    I'm trying to do the same, raster a 2d text. It works fine, but after restoring the projection and modelview matrixs something doesn't work properly with selection. I always get a hit wherever I put the mouse pointer. It seems like something is wrong with the modelview restoration. I don't know what I'm missing.

    Thats's a code snippet of what I'm doing:


    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, (GLfloat)(viewport[2] - viewport[0]), 0.0, (GLfloat)(viewport[3] - viewport[1]), -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glPushAttrib(GL_LIST_BIT);
    glListBase(IDBASELISTAFUENTETEXTO);

    glRasterPos2d(...);
    glCallLists(...); //to draw the text

    glPopAttrib();


    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    I've even tried to use glPushAttrib(GL_ALL_ATTRIB_BITS) and glPopAttrib()...

    Thanks in advance,

    Ignasi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •