Выбрать главу

   glTexCoord2f(cx, 1-cy-0.0625f); // Texture Coord (Bottom Left)

   glVertex2i(0,0); // Vertex Coord (Bottom Left)

The next texture coordinate is now 1/16th to the right of the last texture coordinate (exactly one character wide). So this would be the bottom right texture point.

   glTexCoord2f(cx+0.0625f, 1-cy-0.0625f); // Texture Coord (Bottom Right)

   glVertex2i(16,0); // Vertex Coord (Bottom Right)

The third texture coordinate stays at the far right of our character, but moves up 1/16th of our texture (exactly the height of one character). This will be the top right point of an individual character.

   glTexCoord2f(cx+0.0625f, 1-cy); // Texture Coord (Top Right)

   glVertex2i(16,16); // Vertex Coord (Top Right)

Finally we move left to set our last texture coordinate at the top left of our character.

   glTexCoord2f(cx,1-cy); // Texture Coord (Top Left)

   glVertex2i(0,16); // Vertex Coord (Top Left)

  glEnd(); // Done Building Our Quad (Character)

Finally, we translate 10 pixels to the right, placing us to the right of our texture. If we didn't translate, the letters would all be drawn on top of eachother. Because our font is so narrow, we don't want to move 16 pixels to the right. If we did, there would be big spaces between each letter. Moving by just 10 pixels eliminates the spaces.

  glTranslated(10,0,0); // Move To The Right Of The Character

  glEndList(); // Done Building The Display List

 } // Loop Until All 256 Are Built

}

The following section of code is the same code we used in our other font tutorials to free the display list before our program quits. All 256 display lists starting at base will be deleted. (good thing to do!).

GLvoid KillFont(GLvoid) // Delete The Font From Memory

{

 glDeleteLists(base,256); // Delete All 256 Display Lists

}

The next section of code is where all of our drawing is done. Everything is fairly new so I'll try to explain each line in great detail. Just a small note: Alot can be added to this code, such as variable support, character sizing, spacing, and alot of checking to restore things to how they were before we decided to print.

glPrint() takes three parameters. The first is the x position on the screen (the position from left to right). Next is the y position on the screen (up and down… 0 at the bottom, bigger numbers at the top). Then we have our actual string (the text we want to print), and finally a variable called set. If you have a look at the bitmap that Giuseppe D'Agata has made, you'll notice there are two different character sets. The first character set is normal, and the second character set is italicized. If set is 0, the first character set is selected. If set is 1 or greater the second character set is selected.

GLvoid glPrint(GLint x, GLint y, char *string, int set) // Where The Printing Happens

{

The first thing we do is make sure that set is either 0 or 1. If set is greater than 1, we'll make it equal to 1.

 if (set>1) // Is set Greater Than One?

 {

  set=1; // If So, Make Set Equal One

 }

Now we select our Font texture. We do this just in case a different texture was selected before we decided to print something to the screen.

 glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Font Texture

Now we disable depth testing. The reason I do this is so that blending works nicely. If you don't disable depth testing, the text may end up going behind something, or blending may not look right. If you have no plan to blend the text onto the screen (so that black spaces do not show up around our letters) you can leave depth testing on.

 glDisable(GL_DEPTH_TEST); // Disables Depth Testing

The next few lines are VERY important! We select our Projection Matrix. Right after that, we use a command called glPushMatrix(). glPushMatrix stores the current matrix (projection). Kind of like the memory button on a calculator.

 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

 glPushMatrix(); // Store The Projection Matrix

Now that our projection matrix has been stored, we reset the matrix and set up our Ortho screen. The first and third numbers (0) represent the bottom left of the screen. We could make the left side of the screen equal –640 if we want, but why would we work with negatives if we don't need to. The second and fourth numbers represent the top right of the screen. It's wise to set these values to match the resolution you are currently in. There is no depth so we set the z values to –1 & 1.

 glLoadIdentity(); // Reset The Projection Matrix

 glOrtho(0, 640, 0, 480, -1, 1); // Set Up An Ortho Screen

Now we select our modelview matrix, and store it's current settings using glPushMatrix(). We then reset the modelview matrix so we can work with it using our Ortho view.

 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

 glPushMatrix(); // Store The Modelview Matrix

 glLoadIdentity(); // Reset The Modelview Matrix

With our perspective settings saved, and our Ortho screen set up, we can now draw our text. We start by translating to the position on the screen that we want to draw our text at. We use glTranslated() instead of glTranslatef() because we are working with actual pixels, so floating point values are not important. After all, you can't have half a pixel :)

 glTranslated(x,y,0); // Position The Text (0,0 – Bottom Left)

The line below will select which font set we want to use. If we want to use the second font set we add 128 to the current base display list (128 is half of our 256 characters). By adding 128 we skip over the first 128 characters.

 glListBase(base-32+(128*set)); // Choose The Font Set (0 or 1)

Now all that's left for us to do is draw the letters to the screen. We do this exactly the same as we did in all the other font tutorials. We use glCallLists(). strlen(string) is the length of our string (how many characters we want to draw), GL_BYTE means that each character is represented by a byte (a byte is any value from 0 to 255). Finally, string holds the actual text we want to print to the screen.

 glCallLists(strlen(string), GL_BYTE, string); // Write The Text To The Screen

All we have to do now is restore our perspective view. We select the projection matrix and use glPopMatrix() to recall the settings we previously stored with glPushMatrix(). It's important to restore things in the opposite order you stored them in.

 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

 glPopMatrix(); // Restore The Old Projection Matrix

Now we select the modelview matrix, and do the same thing. We use glPopMatrix() to restore our modelview matrix to what it was before we set up our Ortho display.

 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

 glPopMatrix(); // Restore The Old Projection Matrix

Finally, we enable depth testing. If you didn't disable depth testing in the code above, you don't need this line.