Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2717751
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-01-22 12:52:29

针对下面不同的风格:
    fonts[0] = new FTGLOutlineFont(fontfile);
    fonts[1] = new FTGLPolygonFont(fontfile);
    fonts[2] = new FTGLTextureFont(fontfile);
    fonts[3] = new FTGLBitmapFont(fontfile);
    fonts[4] = new FTGLPixmapFont(fontfile);
渲染字体效果:

//#include // exit()
//
//#include
//#include
#include "config.h"
#include
#include
#if defined HAVE_GL_GLUT_H
#   include
#elif defined HAVE_GLUT_GLUT_H
#   include
#else
#   error GLUT headers not present
#endif
#include "FTGL/FTGLOutlineFont.h"
#include "FTGL/FTGLPolygonFont.h"
#include "FTGL/FTGLBitmapFont.h"
#include "FTGL/FTGLTextureFont.h"
#include "FTGL/FTGLPixmapFont.h"
static FTFont* fonts[5];
static int width;
static int height;
using namespace std;
void
my_init( const char* font_filename )
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 char *fontfile="c:\\windows\\fonts\\simhei.ttf";
    fonts[0] = new FTGLOutlineFont(fontfile);
    fonts[1] = new FTGLPolygonFont(fontfile);
    fonts[2] = new FTGLTextureFont(fontfile);
    fonts[3] = new FTGLBitmapFont(fontfile);
    fonts[4] = new FTGLPixmapFont(fontfile);
    for (int i=0; i< 5; i++) {
        if (fonts[i]->Error()) {
            cerr << "ERROR: Unable to open file " << font_filename << "\n";
        }
        else {
            int point_size = 12;
            if (!fonts[i]->FaceSize(point_size)) {
                cerr << "ERROR: Unable to set font face size " << point_size << "\n";
            }
        }
    }
}
static void do_ortho()
{
  int w;
  int h;
  GLdouble size;
  GLdouble aspect;
  w = width;
  h = height;
  aspect = (GLdouble)w / (GLdouble)h;
  // Use the whole window.
  glViewport(0, 0, w, h);
  // We are going to do some 2-D orthographic drawing.
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  size = (GLdouble)((w >= h) ? w : h) / 2.0;
  if (w <= h) {
    aspect = (GLdouble)h/(GLdouble)w;
    glOrtho(-size, size, -size*aspect, size*aspect,
            -100000.0, 100000.0);
  }
  else {
    aspect = (GLdouble)w/(GLdouble)h;
    glOrtho(-size*aspect, size*aspect, -size, size,
            -100000.0, 100000.0);
  }
  // Make the world and window coordinates coincide so that 1.0 in
  // model space equals one pixel in window space.
  glScaled(aspect, aspect, 1.0);
   // Now determine where to draw things.
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void my_reshape(int w, int h)
{
  width = w;
  height = h;
}
void my_handle_key(unsigned char key, int x, int y)
{
   switch (key) {
   case 27:    // Esc - Quits the program.
       for (int i=0; i<5; i++) {
           if (fonts[i]) {
               delete fonts[i];
               fonts[i] = 0;
           }
       }
      exit(1);
      break;
   default:
      break;
   }
}
void draw_scene()
{
   /* Set up some strings with the characters to draw. */
   unsigned int count = 0;
   char string[8][256];
   int i;
   for (i=1; i < 32; i++) { /* Skip zero - it's the null terminator! */
      string[0][count] = i;
      count++;
   }
   string[0][count] = '\0';
   count = 0;
   for (i=32; i < 64; i++) {
      string[1][count] = i;
      count++;
   }
   string[1][count] = '\0';
   count = 0;
   for (i=64; i < 96; i++) {
      string[2][count] = i;
      count++;
   }
   string[2][count] = '\0';
   count = 0;
   for (i=96; i < 128; i++) {
      string[3][count] = i;
      count++;
   }
   string[3][count] = '\0';
   count = 0;
   for (i=128; i < 160; i++) {
      string[4][count] = i;
      count++;
   }
   string[4][count] = '\0';
   count = 0;
   for (i=160; i < 192; i++) {
      string[5][count] = i;
      count++;
   }
   string[5][count] = '\0';
   count = 0;
   for (i=192; i < 224; i++) {
      string[6][count] = i;
      count++;
   }
   string[6][count] = '\0';
   count = 0;
   for (i=224; i < 256; i++) {
      string[7][count] = i;
      count++;
   }
   string[7][count] = '\0';

   glColor3f(1.0, 1.0, 1.0);
   for (int font = 0; font < 5; font++) {
       GLfloat x = -250.0;
       GLfloat y;
       GLfloat yild = 20.0;
       for (int j=0; j<4; j++) {
           y = 275.0-font*120.0-j*yild;
           if (font >= 3) {
               glRasterPos2f(x, y);
               fonts[font]->Render(string[j]);
           }
           else {
               if (font == 2) {
                   glEnable(GL_TEXTURE_2D);
                   glEnable(GL_BLEND);
                   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
               }
               glPushMatrix(); {
                   glTranslatef(x, y, 0.0);
                   fonts[font]->Render(string[j]);
               } glPopMatrix();
               if (font == 2) {
                   glDisable(GL_TEXTURE_2D);
                   glDisable(GL_BLEND);
               }
           }
       }
   }
}

void my_display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    do_ortho();
    draw_scene();
    glutSwapBuffers();
}
int main(int argc, char **argv)
{
   glutInitWindowSize(600, 600);
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
   glutCreateWindow("FTGL demo");
   int result = 1;
   if ( argc != 2 ) {
       //cerr << "usage: " << argv[0] << " font_filename.ttf\n";
       result = 0;
   }
   else {
       my_init(argv[1]);
       glutDisplayFunc(my_display);
       glutReshapeFunc(my_reshape);
       glutKeyboardFunc(my_handle_key);
       glutMainLoop();
   }
   exit(result);
}
 

 
阅读(2872) | 评论(0) | 转发(0) |
0

上一篇:QT4中文乱码

下一篇:对字体渲染的思考

给主人留下些什么吧!~~