正文

(OPENGL编程指南第四版)OPENGL范例程序7.52008-03-30 20:02:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yuqiexing/33686.html

分享到:

// 范例P195.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include <gl/glut.h>

#define PT 1          //一点
#define STROKE 2         //一画
#define END 3          //结束

// 字体结构体
typedef struct charpoint{
 GLfloat x,y;
 int type;
}CP;

//具体字体描述笔画

CP Adata[]={
 {0,0,PT},{0,9,PT},{1,10,PT},{4,10,PT},{5,9,PT},
 {5,0,STROKE},{0,5,PT},{5,5,END}
};

CP Cdata[]={
 {5,0,PT},{1,0,PT},{0,1,PT},{0,9,PT},{1,10,PT},{5,10,END}
};

CP Ddata[]={
 {0,0,PT},{0,10,PT},{3,10,PT},{4,8,PT},{4,2,PT},{3,0,PT},{0,0,END}
};

CP Edata[]={
 {5,0,PT},{0,0,PT},{0,10,PT},{5,10,STROKE},
 {0,5,PT},{4,5,END}
};

CP Pdata[]={
 {0,0,PT},{0,10,PT},{4,10,PT},{5,9,PT},{5,6,PT},
 {4,5,PT},{0,5,END}
};

CP Rdata[]={
 {0,0,PT},{0,10,PT},{4,10,PT},{5,9,PT},{5,6,PT},
 {4,5,PT},{0,5,STROKE},{3,5,PT},{5,0,END}
};

CP Sdata[]={
 {0,1,PT},{1,0,PT},{4,0,PT},{5,1,PT},{5,4,PT},
 {4,5,PT},{1,5,PT},{0,6,PT},{0,9,PT},{1,10,PT},
 {4,10,PT},{5,9,END}
};

static void drawletter(CP *l)
{
 glBegin(GL_LINE_STRIP);
 while(l){
  switch(l->type){
  case PT:
   glVertex2f(l->x,l->y);
   break;
  case STROKE:
   //结束一画,开始新的一画
   glVertex2f(l->x,l->y);
   glEnd();
   glBegin(GL_LINE_STRIP);
   break;
  case END:
   //结束一个字母,开始绘制另一个中间有间隔的字母
   glVertex2f(l->x,l->y);
   glEnd();
   glTranslatef(8.0,0.0,0.0);
   return;
  }
  l++;
 }
}


static void init ()
{
 GLuint base;
 glShadeModel(GL_FLAT);
 
 base=glGenLists(128);
 glListBase(base);
 glNewList(base+'A',GL_COMPILE);drawletter(Adata);glEndList();
 glNewList(base+'C',GL_COMPILE);drawletter(Cdata);glEndList();
 glNewList(base+'D',GL_COMPILE);drawletter(Ddata);glEndList();
 glNewList(base+'E',GL_COMPILE);drawletter(Edata);glEndList();
 glNewList(base+'P',GL_COMPILE);drawletter(Pdata);glEndList();
 glNewList(base+'R',GL_COMPILE);drawletter(Rdata);glEndList();
 glNewList(base+'S',GL_COMPILE);drawletter(Sdata);glEndList();
 glNewList(base+' ',GL_COMPILE);glTranslatef(8,0,0);glEndList();
}

char *test1="A CPARE DERAPE APPEASS AS";
char *test2="APES PREPARE RARE PEPPERS";

static void printStrokedString(char *s)
{
 GLsizei len=strlen(s);
 glCallLists(len,GL_BYTE,(GLbyte*)s);
}

void display()
{
 glClear(GL_COLOR_BUFFER_BIT);     //清除所有像素
 glColor3f(1.0,1.0,1.0);       //设置画笔白色
 /*在缓存中绘制4对顶点坐标的矩形*/
 
 glPushMatrix();
 glScalef(2.0,2.0,2.0);
 glTranslatef(10,30,0);
 printStrokedString(test1);
 glPopMatrix();
 
 glPushMatrix();
 //glScalef(2.0,2.0,2.0);
 glTranslatef(10,13,0);
 printStrokedString(test2);
 glPopMatrix();
 
 glFlush();          //立即显示出来
}

void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluOrtho2D(0,w,0,h);
}

int main(int argc,char ** argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(440,120);     //初始化窗口大小
 glutInitWindowPosition(0,0);     //初始化窗口位置
 glutCreateWindow("hello");      //创建标题为“hello”的窗口
 init();           //调用初始化函数
 glutDisplayFunc(display);      //注册回调函数
 glutReshapeFunc(reshape);
 glutMainLoop();         //进入主循环处理事件
 return 0;
}

 


阅读(3002) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册