0

I have an OpenGL application that displays a 2D grid. I want to be able to label each cell in the grid with a letter.

Right now I am drawing text on each cell using the code based on this answer:

void drawLetter(double x, double y, double win_w, double win_h, char c) {
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  gluOrtho2D(0.0, win_w, 0.0, win_h);

  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  glColor3f(1.0, 1.0, 0.0); // Green

  glRasterPos2i(x, y);

  void * font = GLUT_BITMAP_8_BY_13;
  glutBitmapCharacter(font, c);

  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
}

The problem I have is that the cells in the grid are much bigger than this font. I want the letters to be scaled to fill the entire cell. Ideally, I'd like to specify x, y, width, and height in window units, and scale the character to be those dimensions. However, I don't really know how to accomplish this. I've found some tutorials on the internet but they all seem to rely on external libraries; for various reasons I cannot use any external libraries.

Is there an easy way to accomplish this? I know there are also stroke fonts available but I still don't know how to accomplish the appropriate scaling.

Community
  • 1
  • 1
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • Why you can't use any extra libraries? – HolyBlackCat Sep 23 '16 at 18:34
  • Use external libraries. Font rendering is complex, libraries like FreeType make it simple. – Robert Rouhani Sep 23 '16 at 19:00
  • This is an isolated system that is not connected to the internet. However it seems like freetype2 is installed on this system. – rlbond Sep 23 '16 at 19:56
  • have you tried `glTranslatef` and `glScalef` rather than `glRasterPos2i`? (that is, translate to x,y and scale to the desired size) – Phildo Sep 23 '16 at 20:20
  • if that doesn't work, then I'd assume `glutBitmapCharacter` is strictly a 1-to-1 blitting operator (which I can't see for certain here... https://www.opengl.org/resources/libraries/glut/spec3/node76.html ), in which case you'll have to take a texture based approach (where you can render each char to its own quad, setting its uvs as subregions of the font texture). – Phildo Sep 23 '16 at 20:26

0 Answers0