title: Drawing bitmap fonts with OpenGL, what does glRasterPos2i() do?
body: <p>This is another one of those &quot;I have a blank screen, please help me fix it&quot; moments.</p>
  <p>This example is from The OpenGL Programming Guide, Version 2.1, Page 311-312.</p>
  <p>The example is supposed to draw 2 lines of text on the screen.</p>
  <p>Past of the problem I think is that I don't understand how glRasterPos2i() works. Does it:
  A:) Set the position of bitmaps to be drawn in the 3D world in homogeneous / &quot;OpenGL coordinates&quot;
  B:) Set the position of bitmaps to be drawn on the screen in pixel coordinates</p>
  <p>Here is the code I have so far: You can pretty much ignore the first big lump which defines what the bitmaps are.</p>
  <pre><code>#include &lt;GL/glut.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  
  
  // This first bit is kind of irreverent, it sets up some fonts in memory as bitmaps
  GLubyte space[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  
  GLubyte letters[][13] = {
                          { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0xc3, 0x18 },
                          { 0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                          { 0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },
                          { 0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc },
                          { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff },
                          { 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff },
                          { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },
                          { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                          { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e },
                          { 0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06 },
                          { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6, 0xc3 },
                          { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0 },
                          { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xdb, 0xff, 0xff, 0xe7, 0xc3 },
                          { 0x00, 0x00, 0xc7, 0xc7, 0xcf, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3 },
                          { 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e },
                          { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc0, 0xf3, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                          { 0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c },
                          { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                          { 0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e },
                          { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff },
                          { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                          { 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                          { 0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                          { 0x00, 0x00, 0xc3, 0x66, 0x66, 0xc3, 0xc3, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },
                          { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },
                          { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff }
                          };
  
  
  // This is just copying from the book
  GLuint fontOffset;
  
  void makeRasterFont()
  {
      GLuint i, j;
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
      fontOffset = glGenLists(128);
      for(i = 0, j = 'A'; i &lt; 26; i ++, j ++)
      {
          glNewList(fontOffset + ' ', GL_COMPILE);
              glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
          glEndList();
      }
      glNewList(fontOffset + ' ', GL_COMPILE);
          glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
      glEndList();
  }
  
  
  void init()
  {
      glShadeModel(GL_FLAT);
      makeRasterFont();
  }
  
  
  void printString(char* s)
  {
      glPushAttrib(GL_LIST_BIT);
          glListBase(fontOffset);
          glCallLists(std::strlen(s), GL_UNSIGNED_BYTE, (GLubyte*)s);
      glPopAttrib();
  }
  
  
  void display()
  {
      GLfloat white[3] = {1.0, 1.0, 1.0 };
  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      glColor3fv(white);
  
      // Print some text on the screen at (20,60) and (20,40)
      glRasterPos2i(20, 60);
      printString(&quot;THE QUICK BROWN FOX JUMPS&quot;);
      glRasterPos2i(20, 40);
      printString(&quot;OVER A LAZY DOG&quot;);
  
      glFlush();
  }
  
  
  void reshape(int w, int h)
  {
      // Set the viewport
      glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  
      // Set viewing mode
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 100.0);
      glMatrixMode(GL_MODELVIEW);
  }
  
  
  
  int main(int argc, char** argv)
  {
  
      /* Init glut with a single buffer display mode,
       * window size, position and title */
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(500, 500);
      glutInitWindowPosition(100, 100);
      glutCreateWindow(argv[0]);
  
      // Call init routine to set OpenGL specific initialization values
      init();
  
      // Set callback function
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
  
      // Enter main loop
      glutMainLoop();
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p>Sorry for the type of question - I hate just asking &quot;please fix my code&quot;, because really I should be able to fix it myself. On this occasion I find myself, stuck, basically. Thanks for you time and help.</p>
  <h1>Solution:</h1>
  <p>For those interested, to &quot;get it to work&quot;, the changes made were:</p>
  <p>1: Change gluPerspective to gluOrtho2D(0, width, 0, height).</p>
  <p>2: Change glnewList(fontOffset + ' ', GL_COMPILE) to glnewList(fontOffset + j, GL_COMPILE) - not BOTH, just the FIRST ONE IN THE LOOP.</p>
  <p>3: Set the glRasterPos2i to be anywhere within the region specified by glOrtho2D. My width and height are both 500, so I used coordinates (20, 60) and then (20, 40).</p>
  <p>You could have just left it with gluPerspective, and used coordinates about (0,0) without specifying any transformations. However, since a bitmap is 2D I think this is less intuitive.</p>