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

GLvoid BuildFont(GLvoid) // Build Our Font Display List

{

 base=glGenLists(256); // Creating 256 Display Lists

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

 for (loop1=0; loop1<256; loop1++) // Loop Through All 256 Lists

 {

  float cx=float(loop1%16)/16.0f; // X Position Of Current Character

  float cy=float(loop1/16)/16.0f; // Y Position Of Current Character

  glNewList(base+loop1,GL_COMPILE); // Start Building A List

  glBegin(GL_QUADS); // Use A Quad For Each Character

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

   glVertex2d(0,16); // Vertex Coord (Bottom Left)

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

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

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

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

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

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

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

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

  glEndList(); // Done Building The Display List

 } // Loop Until All 256 Are Built

}

It's a good idea to destroy the font display list when you're done with it, so I've added the following section of code. Again, nothing new.

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

{

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

}

The glPrint() code hasn't changed that much. The only difference from the tutorial on bitmap font textures is that I have added the ability to print the value of variables. The only reason I've written this section of code out is so that you can see the changes. The print statement will position the text at the x and y position that you specify. You can pick one of 2 character sets, and the value of variables will be written to the screen. This allows us to display the current level and stage on the screen.

Notice that I enable texture mapping, reset the view and then translate to the proper x / y position. Also notice that if character set 0 is selected, the font is enlarged one and half times width wise, and double it's original size up and down. I did this so that I could write the title of the game in big letters. After the text has been drawn, I disable texture mapping.

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

{

 char text[256]; // Holds Our String

 va_list ap; // Pointer To List Of Arguments

 if (fmt == NULL) // If There's No Text

  return; // Do Nothing

 va_start(ap, fmt); // Parses The String For Variables

 vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers

 va_end(ap); // Results Are Stored In Text

 if (set>1) // Did User Choose An Invalid Character Set?

 {

  set=1; // If So, Select Set 1 (Italic)

 }

 glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

 glLoadIdentity(); // Reset The Modelview Matrix

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

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

 if (set==0) // If Set 0 Is Being Used Enlarge Font

 {

  glScalef(1.5f,2.0f,1.0f); // Enlarge Font Width And Height

 }

 glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen

 glDisable(GL_TEXTURE_2D); // Disable Texture Mapping

}

The resize code is NEW :) Instead of using a perspective view I'm using an ortho view for this tutorial. That means that objects don't get smaller as they move away from the viewer. The z-axis is pretty much useless in this tutorial.

We start off by setting up the view port. We do this the same way we'd do it if we were setting up a perspective view. We make the viewport equal to the width of our window.

Then we select the projection matrix (thing movie projector, it information on how to display our image), and reset it.

Immediately after we reset the projection matrix, we set up our ortho view. I'll explain the command in detaiclass="underline"

The first parameter (0.0f) is the value that we want for the far left side of the screen. You wanted to know how to use actual pixel values, so instead of using a negative number for far left, I've set the value to 0. The second parameter is the value for the far right side of the screen. If our window is 640×480, the value stored in width will be 640. So the far right side of the screen effectively becomes 640. Therefore our screen runs from 0 to 640 on the x-axis.

The third parameter (height) would normally be our negative y-axis value (bottom of the screen). But because we want exact pixels, we wont have a negative value. Instead we will make the bottom of the screen equal the height of our window. If our window is 640×480, height will be equal to 480. So the bottom of our screen will be 480. The fourth parameter would normally be the positive value for the top of our screen. We want the top of the screen to be 0 (good old fashioned screen coordinates) so we just set the fourth parameter to 0. This gives us from 0 to 480 on the y-axis.

The last two parameters are for the z-axis. We don't really care about the z-axis so we'll set the range from –1.0f to 1.0f. Just enough that we can see anything drawn at 0.0f on the z-axis.

After we've set up the ortho view, we select the modelview matrix (object information… location, etc) and reset it.

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window

{

 if (height==0) // Prevent A Divide By Zero By

 {

  height=1; // Making Height Equal One

 }

 glViewport(0, 0, width, height); // Reset The Current Viewport

 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

 glLoadIdentity(); // Reset The Projection Matrix

 glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f); // Create Ortho 640x480 View (0,0 At Top Left)

 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

 glLoadIdentity(); // Reset The Modelview Matrix

}

The init code has a few new commands. We start off by loading our textures. If they didn't load properly, the program will quit with an error message. After we have built the textures, we build our font set. I don't bother error checking but you can if you want.

After the font has been built, we set things up. We enable smooth shading, set our clear color to black and set depth clearing to 1.0f. After that is a new line of code.