Debian Codegraph @

demo/FTGLDemo.cpp

raw
1 /* -*- coding: utf-8 -*-
2 * FTGLDemo - simple demo for FTGL, the OpenGL font library
3 *
4 * Copyright (c) 2001-2004 Henry Maddocks <[email protected]>
5 * 2008 Sam Hocevar <[email protected]>
6 * 2008 Éric Beets <[email protected]>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include "config.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <iostream>
34 #include <sstream>
35
36 #if defined HAVE_GL_GLUT_H
37 # include <GL/glut.h>
38 #elif defined HAVE_GLUT_GLUT_H
39 # include <GLUT/glut.h>
40 #else
41 # error GLUT headers not present
42 #endif
43
44 #include <FTGL/ftgl.h>
45
46 #include "tb.h"
47
48 #if !defined FONT_FILE
49 // Put your font file here if configure did not find it.
50 # define FONT_FILE 0
51 #endif
52
53 #define EDITING 1
54 #define INTERACTIVE 2
55
56 #define FTGL_BITMAP 0
57 #define FTGL_PIXMAP 1
58 #define FTGL_OUTLINE 2
59 #define FTGL_POLYGON 3
60 #define FTGL_EXTRUDE 4
61 #define FTGL_TEXTURE 5
62 #define FTGL_BUFFER 6
63
64 char const* fontfile = FONT_FILE;
65 int current_font = FTGL_EXTRUDE;
66
67 GLint w_win = 640, h_win = 480;
68 int mode = INTERACTIVE;
69 int carat = 0;
70
71 FTSimpleLayout simpleLayout;
72 FTLayout *layouts[] = { &simpleLayout, NULL };
73 int currentLayout = 0;
74 const int NumLayouts = 2;
75
76 const float InitialLineLength = 600.0f;
77
78 const float OX = -300;
79 const float OY = 170;
80
81 //wchar_t myString[16] = { 0x6FB3, 0x9580};
82 char myString[4096];
83
84 static int const FTGL_NUM_FONTS = 7;
85 static FTFont* fonts[FTGL_NUM_FONTS];
86 static FTPixmapFont* infoFont;
87
88 static float textures[][48] =
89 {
90 {
91 1.0, 1.0, 1.0, 0.7, 0.7, 0.7, 1.0, 1.0, 1.0, 0.7, 0.7, 0.7,
92 0.7, 0.7, 0.7, 0.4, 0.4, 0.4, 0.7, 0.7, 0.7, 0.4, 0.4, 0.4,
93 1.0, 1.0, 1.0, 0.7, 0.7, 0.7, 1.0, 1.0, 1.0, 0.7, 0.7, 0.7,
94 0.7, 0.7, 0.7, 0.4, 0.4, 0.4, 0.7, 0.7, 0.7, 0.4, 0.4, 0.4,
95 },
96 {
97 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
98 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,
99 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
100 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3,
101 }
102 };
103
104 static GLuint textureID[2];
105
106 void SetCamera(void);
107
108 void setUpLighting()
109 {
110 // Set up lighting.
111 float light1_ambient[4] = { 0.5, 0.5, 0.5, 1.0 };
112 float light1_diffuse[4] = { 1.0, 0.9, 0.9, 1.0 };
113 float light1_specular[4] = { 1.0, 0.7, 0.7, 1.0 };
114 float light1_position[4] = { 400.0, 400.0, 100.0, 1.0 };
115 glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
116 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
117 glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
118 glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
119 glEnable(GL_LIGHT1);
120
121 float front_emission[4] = { 0.5, 0.4, 0.3, 0.0 };
122 float front_ambient[4] = { 0.4, 0.4, 0.4, 0.0 };
123 float front_diffuse[4] = { 0.95, 0.95, 0.8, 0.0 };
124 float front_specular[4] = { 0.8, 0.8, 0.8, 0.0 };
125 glMaterialfv(GL_FRONT, GL_EMISSION, front_emission);
126 glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient);
127 glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
128 glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
129 glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
130 glColor4fv(front_diffuse);
131 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
132
133 glColorMaterial(GL_FRONT, GL_DIFFUSE);
134 glEnable(GL_COLOR_MATERIAL);
135
136 glEnable(GL_LIGHTING);
137 }
138
139
140 void setUpFonts(const char* file)
141 {
142 fonts[FTGL_BITMAP] = new FTBitmapFont(file);
143 fonts[FTGL_PIXMAP] = new FTPixmapFont(file);
144 fonts[FTGL_OUTLINE] = new FTOutlineFont(file);
145 fonts[FTGL_POLYGON] = new FTPolygonFont(file);
146 fonts[FTGL_EXTRUDE] = new FTExtrudeFont(file);
147 fonts[FTGL_TEXTURE] = new FTTextureFont(file);
148 fonts[FTGL_BUFFER] = new FTBufferFont(file);
149
150 for(int x = 0; x < FTGL_NUM_FONTS; ++x)
151 {
152 if(fonts[x]->Error())
153 {
154 fprintf(stderr, "Failed to open font %s", file);
155 exit(1);
156 }
157
158 if(!fonts[x]->FaceSize(30))
159 {
160 fprintf(stderr, "Failed to set size");
161 exit(1);
162 }
163
164 fonts[x]->Depth(3.);
165 fonts[x]->Outset(-.5, 1.5);
166
167 fonts[x]->CharMap(ft_encoding_unicode);
168 }
169
170 infoFont = new FTPixmapFont(file);
171
172 if(infoFont->Error())
173 {
174 fprintf(stderr, "Failed to open font %s", file);
175 exit(1);
176 }
177
178 infoFont->FaceSize(18);
179 #if 1
180 strcpy(myString, "OpenGL is a powerful software interface for graphics "
181 "hardware that allows graphics programmers to produce high-quality "
182 "color images of 3D objects.\nabc def ghij klm nop qrs tuv wxyz "
183 "ABC DEF GHIJ KLM NOP QRS TUV WXYZ 01 23 45 67 89");
184 #elif 0
185 strcpy(myString, "OpenGL (Open Graphics Library — открытая графическая "
186 "библиотека) — спецификация, определяющая независимый от языка "
187 "программирования кросс-платформенный программный интерфейс "
188 "для написания приложений, использующих двумерную и трехмерную "
189 "компьютерную графику.");
190 #else
191 strcpy(myString, "OpenGL™ 是行业领域中最为广泛接纳的 2D/3D 图形 API, "
192 "其自诞生至今已催生了各种计算机平台及设备上的数千优秀应用程序。"
193 "OpenGL™ 是独立于视窗操作系统或其它操作系统的,亦是网络透明的。"
194 "在包含CAD、内容创作、能源、娱乐、游戏开发、制造业、制药业及虚拟"
195 "现实等行业领域中, OpenGL™ 帮助程序员实现在 PC、工作站、超级计算"
196 "机等硬件设备上的高性能、极具冲击力的高视觉表现力图形处理软件的开"
197 "发。");
198 #endif
199 }
200
201
202 void renderFontmetrics()
203 {
204 FTBBox bbox;
205 float x1, y1, z1, x2, y2, z2;
206
207 // If there is a layout, use it to compute the bbox, otherwise query as
208 // a string.
209 if(layouts[currentLayout])
210 bbox = layouts[currentLayout]->BBox(myString);
211 else
212 bbox = fonts[current_font]->BBox(myString);
213
214 x1 = bbox.Lower().Xf(); y1 = bbox.Lower().Yf(); z1 = bbox.Lower().Zf();
215 x2 = bbox.Upper().Xf(); y2 = bbox.Upper().Yf(); z2 = bbox.Upper().Zf();
216
217 // Draw the bounding box
218 glDisable(GL_LIGHTING);
219 glEnable(GL_LINE_SMOOTH);
220 glEnable(GL_BLEND);
221 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // GL_ONE_MINUS_SRC_ALPHA
222
223 glColor3f(0.0, 1.0, 0.0);
224 // Draw the front face
225 glBegin(GL_LINE_LOOP);
226 glVertex3f(x1, y1, z1);
227 glVertex3f(x1, y2, z1);
228 glVertex3f(x2, y2, z1);
229 glVertex3f(x2, y1, z1);
230 glEnd();
231 // Draw the back face
232 #pragma GCC diagnostic push
233 #pragma GCC diagnostic ignored "-Wfloat-equal"
234 bool zd = z1 != z2;
235 #pragma GCC diagnostic pop
236 if(current_font == FTGL_EXTRUDE && zd)
237 {
238 glBegin(GL_LINE_LOOP);
239 glVertex3f(x1, y1, z2);
240 glVertex3f(x1, y2, z2);
241 glVertex3f(x2, y2, z2);
242 glVertex3f(x2, y1, z2);
243 glEnd();
244 // Join the faces
245 glBegin(GL_LINES);
246 glVertex3f(x1, y1, z1);
247 glVertex3f(x1, y1, z2);
248
249 glVertex3f(x1, y2, z1);
250 glVertex3f(x1, y2, z2);
251
252 glVertex3f(x2, y2, z1);
253 glVertex3f(x2, y2, z2);
254
255 glVertex3f(x2, y1, z1);
256 glVertex3f(x2, y1, z2);
257 glEnd();
258 }
259
260 // Render layout-specific metrics
261 if(!layouts[currentLayout])
262 {
263 // There is no layout. Draw the baseline, Ascender and Descender
264 glBegin(GL_LINES);
265 glColor3f(0.0, 0.0, 1.0);
266 glVertex3f(0.0, 0.0, 0.0);
267 glVertex3f(fonts[current_font]->Advance(myString), 0.0, 0.0);
268 glVertex3f(0.0, fonts[current_font]->Ascender(), 0.0);
269 glVertex3f(0.0, fonts[current_font]->Descender(), 0.0);
270 glEnd();
271 }
272 else if(layouts[currentLayout]
273 && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout])))
274 {
275 float lineWidth = ((FTSimpleLayout *)layouts[currentLayout])->GetLineLength();
276
277 // The layout is a SimpleLayout. Render guides that mark the edges
278 // of the wrap region.
279 glColor3f(0.5, 1.0, 1.0);
280 glBegin(GL_LINES);
281 glVertex3f(0, 10000, 0);
282 glVertex3f(0, -10000, 0);
283 glVertex3f(lineWidth, 10000, 0);
284 glVertex3f(lineWidth, -10000, 0);
285 glEnd();
286 }
287
288 // Draw the origin
289 glTranslatef(-OX, -OY,0);
290 glColor3f(1.0, 0.0, 0.0);
291 glPointSize(5.0);
292 glBegin(GL_POINTS);
293 glVertex3f(0.0, 0.0, 0.0);
294 glEnd();
295 // Draw the axis
296 glColor3f(1, 0, 0);
297 glBegin(GL_LINES);
298 glVertex3f(0,0,0);
299 glVertex3f(100,0,0);
300 glEnd();
301 glColor3f(0, 1, 0);
302 glBegin(GL_LINES);
303 glVertex3f(0,0,0);
304 glVertex3f(0,100,0);
305 glEnd();
306 glColor3f(0, 0, 1);
307 glBegin(GL_LINES);
308 glVertex3f(0,0,0);
309 glVertex3f(0,0,100);
310 glEnd();
311 }
312
313
314 void renderFontInfo()
315 {
316 glMatrixMode(GL_PROJECTION);
317 glLoadIdentity();
318 gluOrtho2D(0, w_win, 0, h_win);
319 glMatrixMode(GL_MODELVIEW);
320 glLoadIdentity();
321
322 // draw mode
323 glColor3f(1.0, 1.0, 1.0);
324 glRasterPos2f(20.0f , h_win - (20.0f + infoFont->Ascender()));
325
326 switch(mode)
327 {
328 case EDITING:
329 infoFont->Render("Edit Mode");
330 break;
331 case INTERACTIVE:
332 break;
333 }
334
335 // draw font type
336 glRasterPos2i(20 , 20);
337 switch(current_font)
338 {
339 case FTGL_BITMAP:
340 infoFont->Render("Bitmap Font");
341 break;
342 case FTGL_PIXMAP:
343 infoFont->Render("Pixmap Font");
344 break;
345 case FTGL_OUTLINE:
346 infoFont->Render("Outline Font");
347 break;
348 case FTGL_POLYGON:
349 infoFont->Render("Polygon Font");
350 break;
351 case FTGL_EXTRUDE:
352 infoFont->Render("Extruded Font");
353 break;
354 case FTGL_TEXTURE:
355 infoFont->Render("Texture Font");
356 break;
357 case FTGL_BUFFER:
358 infoFont->Render("Buffer Font");
359 break;
360 }
361
362 glRasterPos2f(20.0f , 20.0f + infoFont->LineHeight());
363 std::stringstream tmpbuf;
364 tmpbuf << fontfile << " size: " << fonts[current_font]->FaceSize();
365 infoFont->Render(tmpbuf.str().c_str());
366
367 // If the current layout is a SimpleLayout, output the alignemnt mode
368 if(layouts[currentLayout]
369 && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout])))
370 {
371 glRasterPos2f(20.0f , 20.0f + 2*(infoFont->Ascender() - infoFont->Descender()));
372 // Output the alignment mode of the layout
373 switch (((FTSimpleLayout *)layouts[currentLayout])->GetAlignment())
374 {
375 case FTGL::ALIGN_LEFT:
376 infoFont->Render("Align Left");
377 break;
378 case FTGL::ALIGN_RIGHT:
379 infoFont->Render("Align Right");
380 break;
381 case FTGL::ALIGN_CENTER:
382 infoFont->Render("Align Center");
383 break;
384 case FTGL::ALIGN_JUSTIFY:
385 infoFont->Render("Align Justified");
386 break;
387 }
388 }
389 }
390
391
392 void do_display (void)
393 {
394 switch(current_font)
395 {
396 case FTGL_BITMAP:
397 case FTGL_PIXMAP:
398 case FTGL_OUTLINE:
399 glDisable(GL_TEXTURE_2D);
400 break;
401 case FTGL_POLYGON:
402 glDisable(GL_TEXTURE_2D);
403 setUpLighting();
404 break;
405 case FTGL_EXTRUDE:
406 glEnable(GL_DEPTH_TEST);
407 glDisable(GL_BLEND);
408 glEnable(GL_TEXTURE_2D);
409 setUpLighting();
410 glBindTexture(GL_TEXTURE_2D, textureID[0]);
411 break;
412 case FTGL_TEXTURE:
413 case FTGL_BUFFER:
414 glEnable(GL_TEXTURE_2D);
415 glDisable(GL_DEPTH_TEST);
416 setUpLighting();
417 glNormal3f(0.0, 0.0, 1.0);
418 break;
419 }
420 glTranslatef(OX, OY,0);
421
422 // If you do want to switch the color of bitmaps rendered with glBitmap,
423 // you will need to explicitly call glRasterPos (or its ilk) to lock
424 // in a changed current color.
425
426 glPushMatrix();
427 glColor3f(1.0, 1.0, 1.0);
428 int renderMode = FTGL::RENDER_FRONT | FTGL::RENDER_BACK;
429 if(layouts[currentLayout])
430 layouts[currentLayout]->Render(myString, -1,
431 FTPoint(), renderMode);
432 else
433 fonts[current_font]->Render(myString, -1,
434 FTPoint(), FTPoint(), renderMode);
435
436 if(current_font == FTGL_EXTRUDE)
437 {
438 glBindTexture(GL_TEXTURE_2D, textureID[1]);
439 renderMode = FTGL::RENDER_SIDE;
440 if(layouts[currentLayout])
441 layouts[currentLayout]->Render(myString, -1,
442 FTPoint(), renderMode);
443 else
444 fonts[current_font]->Render(myString, -1,
445 FTPoint(), FTPoint(), renderMode);
446 }
447 glPopMatrix();
448
449 glPushMatrix();
450 renderFontmetrics();
451 glPopMatrix();
452
453 renderFontInfo();
454 }
455
456
457 void display(void)
458 {
459 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
460
461 SetCamera();
462
463 switch(current_font)
464 {
465 case FTGL_BITMAP:
466 case FTGL_PIXMAP:
467 glRasterPos2i((long)(w_win / 2 + OX), (long)(h_win / 2 + OY));
468 glTranslatef(w_win / 2, h_win / 2, 0.0);
469 break;
470 case FTGL_OUTLINE:
471 case FTGL_POLYGON:
472 case FTGL_EXTRUDE:
473 case FTGL_TEXTURE:
474 case FTGL_BUFFER:
475 tbMatrix();
476 break;
477 }
478
479 glPushMatrix();
480
481 do_display();
482
483 glPopMatrix();
484
485 glutSwapBuffers();
486 }
487
488
489 void myinit(const char* file)
490 {
491 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
492 glClearColor(0.5, 0.5, 0.7, 0.0);
493 glColor3f(1.0, 1.0, 1.0);
494
495 glEnable(GL_CULL_FACE);
496 glFrontFace(GL_CCW);
497
498 glEnable(GL_DEPTH_TEST);
499 glEnable(GL_CULL_FACE);
500 glShadeModel(GL_SMOOTH);
501
502 glEnable(GL_POLYGON_OFFSET_LINE);
503 glPolygonOffset(1.0, 1.0); // ????
504
505 SetCamera();
506
507 tbInit(GLUT_LEFT_BUTTON);
508 tbAnimate(GL_FALSE);
509
510 setUpFonts(file);
511
512 // Configure the SimpleLayout
513 simpleLayout.SetLineLength(InitialLineLength);
514 simpleLayout.SetFont(fonts[current_font]);
515
516 glGenTextures(2, textureID);
517
518 for(int i = 0; i < 2; i++)
519 {
520 glBindTexture(GL_TEXTURE_2D, textureID[i]);
521 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
525 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_FLOAT,
526 textures[i]);
527 }
528 }
529
530
531 void parsekey(unsigned char key, int, int)
532 {
533 switch (key)
534 {
535 case 27:
536 exit(0);
537 break;
538 case 13:
539 if(mode == EDITING)
540 {
541 mode = INTERACTIVE;
542 }
543 else
544 {
545 mode = EDITING;
546 carat = 0;
547 }
548 break;
549 case '\t':
550 // If current layout is a SimpleLayout, change its alignment properties
551 if(layouts[currentLayout]
552 && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout])))
553 {
554 FTSimpleLayout *l = (FTSimpleLayout *)layouts[currentLayout];
555 // Decrement the layout
556 switch (l->GetAlignment())
557 {
558 case FTGL::ALIGN_LEFT:
559 l->SetAlignment(FTGL::ALIGN_RIGHT);
560 break;
561 case FTGL::ALIGN_RIGHT:
562 l->SetAlignment(FTGL::ALIGN_CENTER);
563 break;
564 case FTGL::ALIGN_CENTER:
565 l->SetAlignment(FTGL::ALIGN_JUSTIFY);
566 break;
567 case FTGL::ALIGN_JUSTIFY:
568 l->SetAlignment(FTGL::ALIGN_LEFT);
569 break;
570 }
571 }
572 break;
573 default:
574 if(mode == INTERACTIVE)
575 {
576 myString[0] = key;
577 myString[1] = 0;
578 }
579 else
580 {
581 myString[carat] = key;
582 myString[carat + 1] = 0;
583 carat = carat > 2000 ? 2000 : carat + 1;
584 }
585 break;
586 }
587
588 glutPostRedisplay();
589 }
590
591
592 void parseSpecialKey(int key, int, int)
593 {
594 FTSimpleLayout *l = NULL;
595 unsigned int s;
596
597 // If the currentLayout is a SimpleLayout store a pointer in l
598 if(layouts[currentLayout]
599 && (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout])))
600 {
601 l = (FTSimpleLayout *)layouts[currentLayout];
602 }
603
604 switch (key)
605 {
606 case GLUT_KEY_UP:
607 current_font = (current_font + 1) % FTGL_NUM_FONTS;
608 break;
609 case GLUT_KEY_DOWN:
610 current_font = (current_font + FTGL_NUM_FONTS - 1) % FTGL_NUM_FONTS;
611 break;
612 case GLUT_KEY_PAGE_UP:
613 currentLayout = (currentLayout + 1) % NumLayouts;
614 break;
615 case GLUT_KEY_PAGE_DOWN:
616 currentLayout = (currentLayout + NumLayouts - 1) % NumLayouts;
617 break;
618 case GLUT_KEY_HOME:
619 /* If the current layout is simple decrement its line length */
620 if (l) l->SetLineLength(l->GetLineLength() - 10.0f);
621 break;
622 case GLUT_KEY_END:
623 /* If the current layout is simple increment its line length */
624 if (l) l->SetLineLength(l->GetLineLength() + 10.0f);
625 break;
626 case GLUT_KEY_LEFT:
627 s = fonts[current_font]->FaceSize();
628 if(s >= 2)
629 fonts[current_font]->FaceSize(s - 1);
630 break;
631 case GLUT_KEY_RIGHT:
632 fonts[current_font]->FaceSize(fonts[current_font]->FaceSize() + 1);
633 break;
634 }
635
636 // If the current layout is a SimpleLayout, update its font.
637 if(l)
638 {
639 l->SetFont(fonts[current_font]);
640 }
641
642 glutPostRedisplay();
643 }
644
645
646 void motion(int x, int y)
647 {
648 tbMotion(x, y);
649 }
650
651 void mouse(int button, int state, int x, int y)
652 {
653 tbMouse(button, state, x, y);
654 }
655
656 void myReshape(int w, int h)
657 {
658 glMatrixMode (GL_MODELVIEW);
659 glViewport (0, 0, w, h);
660 glLoadIdentity();
661
662 w_win = w;
663 h_win = h;
664 SetCamera();
665
666 tbReshape(w_win, h_win);
667 }
668
669 void SetCamera(void)
670 {
671 switch(current_font)
672 {
673 case FTGL_BITMAP:
674 case FTGL_PIXMAP:
675 glMatrixMode(GL_PROJECTION);
676 glLoadIdentity();
677 gluOrtho2D(0, w_win, 0, h_win);
678 glMatrixMode(GL_MODELVIEW);
679 glLoadIdentity();
680 break;
681 case FTGL_OUTLINE:
682 case FTGL_POLYGON:
683 case FTGL_EXTRUDE:
684 case FTGL_TEXTURE:
685 case FTGL_BUFFER:
686 glMatrixMode (GL_PROJECTION);
687 glLoadIdentity ();
688 gluPerspective(90, (float)w_win / (float)h_win, 1, 1000);
689 glMatrixMode(GL_MODELVIEW);
690 glLoadIdentity();
691 gluLookAt(0.0, 0.0, (float)h_win / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
692 break;
693 }
694 }
695
696
697 int main(int argc, char *argv[])
698 {
699 #ifndef __APPLE_CC__ // Bloody finder args???
700 if (argc == 2)
701 fontfile = argv[1];
702 #endif
703
704 if (!fontfile)
705 {
706 fprintf(stderr, "A font file must be specified on the command line\n");
707 exit(1);
708 }
709
710 std::cout
711 << "The following interactive commands are available:" << std::endl
712 << "\tTAB: change the alignment of the text" << std::endl
713 << "\tENTER: toggle interactive mode" << std::endl
714 << std::endl
715 << "\tARROW-UP: cycle the font type up" << std::endl
716 << "\tARROW-DOWN: cycle the font type down" << std::endl
717 << "\tARROW-LEFT: decrease the font size" << std::endl
718 << "\tARROW-RIGHT: increase the font size" << std::endl
719 << std::endl
720 << "\tPAGE-UP: cycle the layout up" << std::endl
721 << "\tPAGE-DOWN: cycle the layout down" << std::endl
722 << "\tHOME: decrease line length" << std::endl
723 << "\tEND: increase line length" << std::endl
724 << std::endl
725 << "\tESC: quit" << std::endl;
726
727 glutInit(&argc, argv);
728 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
729 glutInitWindowPosition(50, 50);
730 glutInitWindowSize(w_win, h_win);
731 glutCreateWindow("FTGL TEST");
732 glutDisplayFunc(display);
733 glutKeyboardFunc(parsekey);
734 glutSpecialFunc(parseSpecialKey);
735 glutMouseFunc(mouse);
736 glutMotionFunc(motion);
737 glutReshapeFunc(myReshape);
738 glutIdleFunc(display);
739
740 myinit(fontfile);
741
742 glutMainLoop();
743
744 return 0;
745 }
746