多次渲染不同的物体

Nehe教程17

字体是正交投影,物体是透视投影,绘制字体时关闭深度,入栈矩阵即可:



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.

?
1
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.

?
1
2
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.

?
1
2
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.

?
1
2
3
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 :)

?
1
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.

?
1
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_UNSIGNED_BYTE means that each character is represented by an unsigned byte (a byte is any value from 0 to 255). Finally, string holds the actual text we want to print to the screen.

?
1
glCallLists( strlen (string),GL_UNSIGNED_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.

?
1
2
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.

?
1
2
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.

?
1
2
     glEnable(GL_DEPTH_TEST);    

你可能感兴趣的:(String,character,byte,Matrix,testing,Numbers)