高效学习OpenGL之执行多个显示列表glListBase(),glCallLists()

来源:互联网 发布:plc编程自制功能块 编辑:程序博客网 时间:2024/05/17 22:47
#include <GL/glut.h>
002.#include <stdlib.h>
003.#include <string.h>
004. 
005.#define PT 1
006.#define STROKE 2
007.#define END 3
008. 
009.typedef struct charpoint {
010.GLfloat   x, y;
011.int    type;
012.} CP;
013. 
014.CP Adata[] = {
015.00, PT}, {09, PT}, {110, PT}, {410, PT},
016.{59, PT}, {50, STROKE}, {05, PT}, {55, END}
017.};
018. 
019.CP Edata[] = {
020.{50, PT}, {00, PT}, {010, PT}, {510, STROKE},
021.{05, PT}, {45, END}
022.};
023. 
024.CP Pdata[] = {
025.{00, PT}, {010, PT},  {410, PT}, {59, PT}, {56, PT},
026.{45, PT}, {05, END}
027.};
028. 
029.CP Rdata[] = {
030.{00, PT}, {010, PT},  {410, PT}, {59, PT}, {56, PT},
031.{45, PT}, {05, STROKE}, {35, PT}, {50, END}
032.};
033. 
034.CP Sdata[] = {
035.{01, PT}, {10, PT}, {40, PT}, {51, PT}, {54, PT},
036.{45, PT}, {15, PT}, {06, PT}, {09, PT}, {110, PT},
037.{410, PT}, {59, END}
038.};
039. 
040./*  drawLetter() interprets the instructions from the array
041.*  for that letter and renders the letter with line segments.
042.*/
043.static void drawLetter(CP *l)
044.{
045.glBegin(GL_LINE_STRIP);
046.while (1) {
047.switch (l->type) {
048.case PT:
049.glVertex2fv(&l->x);
050.break;
051.case STROKE:
052.glVertex2fv(&l->x);
053.glEnd();
054.glBegin(GL_LINE_STRIP);
055.break;
056.case END:
057.glVertex2fv(&l->x);
058.glEnd();
059.glTranslatef(8.00.00.0);
060.return;
061.}
062.l++;
063.}
064.}
065. 
066./*  Create a display list for each of 6 characters  */
067.static void init (void)
068.{
069.GLuint base;
070. 
071.glShadeModel (GL_FLAT);
072. 
073.base = glGenLists (128);
074.glListBase(base);//指定一个偏移量,它将与glCallListserv()函数中的
075.//显示列表索引相加,以获取最终的显示列表索引
076.glNewList(base+'A', GL_COMPILE); drawLetter(Adata); glEndList();
077.glNewList(base+'E', GL_COMPILE); drawLetter(Edata); glEndList();
078.glNewList(base+'P', GL_COMPILE); drawLetter(Pdata); glEndList();
079.glNewList(base+'R', GL_COMPILE); drawLetter(Rdata); glEndList();
080.glNewList(base+'S', GL_COMPILE); drawLetter(Sdata); glEndList();
081.glNewList(base+' ', GL_COMPILE); glTranslatef(8.00.00.0); glEndList();
082.}
083. 
084.char *test1 = "A SPARE SERAPE APPEARS AS";
085.char *test2 = "APES PREPARE RARE PEPPERS";
086. 
087.static void printStrokedString(char *s)
088.{
089.GLsizei len = strlen(s);
090.glCallLists(len, GL_BYTE, (GLbyte *)s);
091.//执行len个显示列表,被执行的显示列表的索引是通过把当前显示列表基址表示的偏移值(由glListBase()指定)
092.//与s指向的数组中的有符号整型值相加得到的
093.}
094. 
095.void display(void)
096.{
097.glClear(GL_COLOR_BUFFER_BIT);
098.glColor3f(1.01.01.0);
099.glPushMatrix();
100.glScalef(2.02.02.0);
101.glTranslatef(10.030.00.0);
102.printStrokedString(test1);
103.glPopMatrix();
104.glPushMatrix();
105.glScalef(2.02.02.0);
106.glTranslatef(10.013.00.0);
107.printStrokedString(test2);
108.glPopMatrix();
109.glFlush();
110.}
111. 
112.void reshape(int w, int h)
113.{
114.glViewport(00, (GLsizei) w, (GLsizei) h);
115.glMatrixMode (GL_PROJECTION);
116.glLoadIdentity ();
117.gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
118.}
119. 
120.void keyboard(unsigned char key, int x, int y)
121.{
122.switch (key) {
123.case ' ':
124.glutPostRedisplay();
125.break;
126.case 27:
127.exit(0);
128.}
129.}
130. 
131./*  Main Loop
132.*  Open window with initial window size, title bar,
133.*  RGBA display mode, and handle input events.
134.*/
135.int main(int argc, char** argv)
136.{
137.glutInit(&argc, argv);
138.glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
139.glutInitWindowSize (440120);
140.glutCreateWindow ("stroke");
141.init ();
142.glutReshapeFunc(reshape);
143.glutKeyboardFunc(keyboard);
144.glutDisplayFunc(display);
145.glutMainLoop();
146.return 0;
147.}
0 0
原创粉丝点击