~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to extern/bFTGL/demo/FTGLDemo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef __APPLE_CC__
 
2
        #include <GLUT/glut.h>
 
3
#else
 
4
        #include <GL/glut.h>
 
5
#endif
 
6
 
 
7
#include <stdlib.h>
 
8
#include <stdio.h>
 
9
 
 
10
#include "tb.h"
 
11
 
 
12
#include "FTGLExtrdFont.h"
 
13
#include "FTGLOutlineFont.h"
 
14
#include "FTGLPolygonFont.h"
 
15
#include "FTGLTextureFont.h"
 
16
#include "FTGLPixmapFont.h"
 
17
#include "FTGLBitmapFont.h"
 
18
 
 
19
// YOU'LL PROBABLY WANT TO CHANGE THESE
 
20
#ifdef __linux__
 
21
        #define FONT_FILE "/usr/share/fonts/truetype/arial.ttf"
 
22
#endif
 
23
#ifdef __APPLE_CC__
 
24
        #define FONT_FILE "/Users/henry/Development/PROJECTS/FTGL/test/font_pack/arial.ttf"
 
25
#endif
 
26
#ifdef WIN32
 
27
        #define FONT_FILE "C:\\WINNT\\Fonts\\arial.ttf"
 
28
#endif
 
29
#ifndef FONT_FILE
 
30
        #define FONT_FILE 0
 
31
#endif
 
32
 
 
33
#define EDITING 1
 
34
#define INTERACTIVE 2
 
35
 
 
36
#define FTGL_BITMAP 0
 
37
#define FTGL_PIXMAP 1
 
38
#define FTGL_OUTLINE 2
 
39
#define FTGL_POLYGON 3
 
40
#define FTGL_EXTRUDE 4
 
41
#define FTGL_TEXTURE 5
 
42
 
 
43
char* fontfile = FONT_FILE;
 
44
int current_font = FTGL_EXTRUDE;
 
45
 
 
46
GLint w_win = 640, h_win = 480;
 
47
int mode = INTERACTIVE;
 
48
int carat = 0;
 
49
 
 
50
//wchar_t myString[16] = { 0x6FB3, 0x9580};
 
51
wchar_t myString[16];
 
52
 
 
53
static FTFont* fonts[6];
 
54
static FTGLPixmapFont* infoFont;
 
55
 
 
56
void SetCamera(void);
 
57
 
 
58
void setUpLighting()
 
59
{
 
60
   // Set up lighting.
 
61
   float light1_ambient[4]  = { 1.0, 1.0, 1.0, 1.0 };
 
62
   float light1_diffuse[4]  = { 1.0, 0.9, 0.9, 1.0 };
 
63
   float light1_specular[4] = { 1.0, 0.7, 0.7, 1.0 };
 
64
   float light1_position[4] = { -1.0, 1.0, 1.0, 0.0 };
 
65
   glLightfv(GL_LIGHT1, GL_AMBIENT,  light1_ambient);
 
66
   glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_diffuse);
 
67
   glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
 
68
   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
 
69
   glEnable(GL_LIGHT1);
 
70
 
 
71
   float light2_ambient[4]  = { 0.2, 0.2, 0.2, 1.0 };
 
72
   float light2_diffuse[4]  = { 0.9, 0.9, 0.9, 1.0 };
 
73
   float light2_specular[4] = { 0.7, 0.7, 0.7, 1.0 };
 
74
   float light2_position[4] = { 1.0, -1.0, -1.0, 0.0 };
 
75
   glLightfv(GL_LIGHT2, GL_AMBIENT,  light2_ambient);
 
76
   glLightfv(GL_LIGHT2, GL_DIFFUSE,  light2_diffuse);
 
77
   glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular);
 
78
   glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
 
79
//   glEnable(GL_LIGHT2);
 
80
 
 
81
   float front_emission[4] = { 0.3, 0.2, 0.1, 0.0 };
 
82
   float front_ambient[4]  = { 0.2, 0.2, 0.2, 0.0 };
 
83
   float front_diffuse[4]  = { 0.95, 0.95, 0.8, 0.0 };
 
84
   float front_specular[4] = { 0.6, 0.6, 0.6, 0.0 };
 
85
   glMaterialfv(GL_FRONT, GL_EMISSION, front_emission);
 
86
   glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient);
 
87
   glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse);
 
88
   glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
 
89
   glMaterialf(GL_FRONT, GL_SHININESS, 16.0);
 
90
   glColor4fv(front_diffuse);
 
91
 
 
92
   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
 
93
   glEnable(GL_CULL_FACE);
 
94
   glColorMaterial(GL_FRONT, GL_DIFFUSE);
 
95
   glEnable(GL_COLOR_MATERIAL);
 
96
 
 
97
   glEnable(GL_LIGHTING);
 
98
   glShadeModel(GL_SMOOTH);
 
99
}
 
100
 
 
101
 
 
102
void setUpFonts( const char* fontfile)
 
103
{
 
104
        fonts[FTGL_BITMAP] = new FTGLBitmapFont( fontfile);
 
105
        fonts[FTGL_PIXMAP] = new FTGLPixmapFont( fontfile);
 
106
        fonts[FTGL_OUTLINE] = new FTGLOutlineFont( fontfile);
 
107
        fonts[FTGL_POLYGON] = new FTGLPolygonFont( fontfile);
 
108
        fonts[FTGL_EXTRUDE] = new FTGLExtrdFont( fontfile);
 
109
        fonts[FTGL_TEXTURE] = new FTGLTextureFont( fontfile);
 
110
 
 
111
        for( int x = 0; x < 6; ++x)
 
112
        {
 
113
                if( fonts[x]->Error())
 
114
                {
 
115
                        fprintf( stderr, "Failed to open font %s", fontfile);
 
116
                        exit(1);
 
117
                }
 
118
                
 
119
                if( !fonts[x]->FaceSize( 144))
 
120
                {
 
121
                        fprintf( stderr, "Failed to set size");
 
122
                        exit(1);
 
123
                }
 
124
        
 
125
                fonts[x]->Depth(20);
 
126
                
 
127
                fonts[x]->CharMap(ft_encoding_unicode);
 
128
        }
 
129
        
 
130
        infoFont = new FTGLPixmapFont( fontfile);
 
131
        
 
132
        if( infoFont->Error())
 
133
        {
 
134
                fprintf( stderr, "Failed to open font %s", fontfile);
 
135
                exit(1);
 
136
        }
 
137
        
 
138
        infoFont->FaceSize( 18);
 
139
 
 
140
        myString[0] = 65;
 
141
        myString[1] = 0;
 
142
}
 
143
 
 
144
 
 
145
void renderFontmetrics()
 
146
{
 
147
        float x1, y1, z1, x2, y2, z2;
 
148
        fonts[current_font]->BBox( myString, x1, y1, z1, x2, y2, z2);
 
149
        
 
150
        // Draw the bounding box
 
151
        glDisable( GL_LIGHTING);
 
152
        glDisable( GL_TEXTURE_2D);
 
153
                        glEnable( GL_LINE_SMOOTH);
 
154
                        glEnable(GL_BLEND);
 
155
                        glBlendFunc( GL_SRC_ALPHA, GL_ONE); // GL_ONE_MINUS_SRC_ALPHA
 
156
 
 
157
        glColor3f( 0.0, 1.0, 0.0);
 
158
        // Draw the front face
 
159
        glBegin( GL_LINE_LOOP);
 
160
                glVertex3f( x1, y1, z1);
 
161
                glVertex3f( x1, y2, z1);
 
162
                glVertex3f( x2, y2, z1);
 
163
                glVertex3f( x2, y1, z1);
 
164
        glEnd();
 
165
        // Draw the back face
 
166
        if( current_font == FTGL_EXTRUDE && z1 != z2)
 
167
        {
 
168
                glBegin( GL_LINE_LOOP);
 
169
                        glVertex3f( x1, y1, z2);
 
170
                        glVertex3f( x1, y2, z2);
 
171
                        glVertex3f( x2, y2, z2);
 
172
                        glVertex3f( x2, y1, z2);
 
173
                glEnd();
 
174
        // Join the faces
 
175
                glBegin( GL_LINES);
 
176
                        glVertex3f( x1, y1, z1);
 
177
                        glVertex3f( x1, y1, z2);
 
178
                        
 
179
                        glVertex3f( x1, y2, z1);
 
180
                        glVertex3f( x1, y2, z2);
 
181
                        
 
182
                        glVertex3f( x2, y2, z1);
 
183
                        glVertex3f( x2, y2, z2);
 
184
                        
 
185
                        glVertex3f( x2, y1, z1);
 
186
                        glVertex3f( x2, y1, z2);
 
187
                glEnd();
 
188
        }
 
189
                
 
190
                // Draw the baseline, Ascender and Descender
 
191
        glBegin( GL_LINES);
 
192
                glColor3f( 0.0, 0.0, 1.0);
 
193
                glVertex3f( 0.0, 0.0, 0.0);
 
194
                glVertex3f( fonts[current_font]->Advance( myString), 0.0, 0.0);
 
195
                glVertex3f( 0.0, fonts[current_font]->Ascender(), 0.0);
 
196
                glVertex3f( 0.0, fonts[current_font]->Descender(), 0.0);
 
197
                
 
198
        glEnd();
 
199
        
 
200
        // Draw the origin
 
201
        glColor3f( 1.0, 0.0, 0.0);
 
202
        glPointSize( 5.0);
 
203
        glBegin( GL_POINTS);
 
204
                glVertex3f( 0.0, 0.0, 0.0);
 
205
        glEnd();
 
206
}
 
207
 
 
208
 
 
209
void renderFontInfo()
 
210
{
 
211
    glMatrixMode( GL_PROJECTION);
 
212
    glLoadIdentity();
 
213
    gluOrtho2D(0, w_win, 0, h_win);
 
214
    glMatrixMode(GL_MODELVIEW);
 
215
    glLoadIdentity();
 
216
 
 
217
        // draw mode
 
218
        glColor3f( 1.0, 1.0, 1.0);
 
219
        glRasterPos2f( 20.0f , h_win - ( 20.0f + infoFont->Ascender()));
 
220
 
 
221
        switch( mode)
 
222
        {
 
223
                case EDITING:
 
224
                        infoFont->Render("Edit Mode");
 
225
                        break;
 
226
                case INTERACTIVE:
 
227
                        break;
 
228
        }
 
229
        
 
230
        // draw font type
 
231
        glRasterPos2i( 20 , 20);
 
232
        switch( current_font)
 
233
        {
 
234
                case FTGL_BITMAP:
 
235
                        infoFont->Render("Bitmap Font");
 
236
                        break;
 
237
                case FTGL_PIXMAP:
 
238
                        infoFont->Render("Pixmap Font");
 
239
                        break;
 
240
                case FTGL_OUTLINE:
 
241
                        infoFont->Render("Outline Font");
 
242
                        break;
 
243
                case FTGL_POLYGON:
 
244
                        infoFont->Render("Polygon Font");
 
245
                        break;
 
246
                case FTGL_EXTRUDE:
 
247
                        infoFont->Render("Extruded Font");
 
248
                        break;
 
249
                case FTGL_TEXTURE:
 
250
                        infoFont->Render("Texture Font");
 
251
                        break;
 
252
        }
 
253
        
 
254
        glRasterPos2f( 20.0f , 20.0f + infoFont->Ascender() - infoFont->Descender());
 
255
        infoFont->Render(fontfile);
 
256
}
 
257
 
 
258
void do_display (void)
 
259
{
 
260
        switch( current_font)
 
261
        {
 
262
                case FTGL_BITMAP:
 
263
                case FTGL_PIXMAP:
 
264
                case FTGL_OUTLINE:
 
265
                        break;
 
266
                case FTGL_POLYGON:
 
267
                        glDisable( GL_BLEND);
 
268
                        setUpLighting();
 
269
                        break;
 
270
                case FTGL_EXTRUDE:
 
271
                        glEnable( GL_DEPTH_TEST);
 
272
                        glDisable( GL_BLEND);
 
273
                        setUpLighting();
 
274
                        break;
 
275
                case FTGL_TEXTURE:
 
276
                        glEnable( GL_TEXTURE_2D);
 
277
                        glDisable( GL_DEPTH_TEST);
 
278
                        setUpLighting();
 
279
                        glNormal3f( 0.0, 0.0, 1.0);
 
280
                        break;
 
281
 
 
282
        }
 
283
 
 
284
        glColor3f( 1.0, 1.0, 1.0);
 
285
// If you do want to switch the color of bitmaps rendered with glBitmap,
 
286
// you will need to explicitly call glRasterPos3f (or its ilk) to lock
 
287
// in a changed current color.
 
288
 
 
289
        fonts[current_font]->Render( myString);
 
290
        renderFontmetrics();
 
291
        renderFontInfo();
 
292
}
 
293
 
 
294
 
 
295
void display(void)
 
296
{
 
297
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
298
 
 
299
        SetCamera();
 
300
        
 
301
        switch( current_font)
 
302
        {
 
303
                case FTGL_BITMAP:
 
304
                case FTGL_PIXMAP:
 
305
                        glRasterPos2i( w_win / 2, h_win / 2);
 
306
                        glTranslatef(  w_win / 2, h_win / 2, 0.0);
 
307
                        break;
 
308
                case FTGL_OUTLINE:
 
309
                case FTGL_POLYGON:
 
310
                case FTGL_EXTRUDE:
 
311
                case FTGL_TEXTURE:
 
312
                        tbMatrix();
 
313
                        break;
 
314
        }
 
315
        
 
316
        glPushMatrix();
 
317
 
 
318
        do_display();
 
319
 
 
320
        glPopMatrix();
 
321
        
 
322
    glutSwapBuffers();
 
323
}
 
324
 
 
325
 
 
326
void myinit( const char* fontfile)
 
327
{
 
328
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
329
        glClearColor( 0.13, 0.17, 0.32, 0.0);
 
330
        glColor3f( 1.0, 1.0, 1.0);
 
331
        
 
332
        glEnable( GL_CULL_FACE);
 
333
        glFrontFace( GL_CCW);
 
334
        
 
335
        glEnable( GL_DEPTH_TEST);
 
336
        
 
337
        glEnable( GL_POLYGON_OFFSET_LINE);
 
338
        glPolygonOffset( 1.0, 1.0); // ????
 
339
                
 
340
        SetCamera();
 
341
 
 
342
        tbInit(GLUT_LEFT_BUTTON);
 
343
        tbAnimate( GL_FALSE);
 
344
 
 
345
    setUpFonts( fontfile);
 
346
}
 
347
 
 
348
 
 
349
void parsekey(unsigned char key, int x, int y)
 
350
{
 
351
        switch (key)
 
352
        {
 
353
                case 27: exit(0); break;
 
354
                case 13:
 
355
                        if( mode == EDITING)
 
356
                        {
 
357
                                mode = INTERACTIVE;
 
358
                        }
 
359
                        else
 
360
                        {
 
361
                                mode = EDITING;
 
362
                                carat = 0;
 
363
                        }
 
364
                        break;
 
365
                case ' ':
 
366
                        current_font++;
 
367
                        if(current_font > 5)
 
368
                                current_font = 0;
 
369
                        break;
 
370
                default:
 
371
                        if( mode == INTERACTIVE)
 
372
                        {
 
373
                                myString[0] = key;
 
374
                                myString[1] = 0;
 
375
                                break;
 
376
                        }
 
377
                        else
 
378
                        {
 
379
                                myString[carat] = key;
 
380
                                myString[carat + 1] = 0;
 
381
                                carat = carat > 14 ? 14 : ++carat;
 
382
                        }
 
383
        }
 
384
        
 
385
        glutPostRedisplay();
 
386
 
 
387
}
 
388
 
 
389
 
 
390
void motion(int x, int y)
 
391
{
 
392
        tbMotion( x, y);
 
393
}
 
394
 
 
395
void mouse(int button, int state, int x, int y)
 
396
{
 
397
        tbMouse( button, state, x, y);
 
398
}
 
399
 
 
400
void myReshape(int w, int h)
 
401
{
 
402
        glMatrixMode (GL_MODELVIEW);
 
403
        glViewport (0, 0, w, h);
 
404
        glLoadIdentity();
 
405
                
 
406
        w_win = w;
 
407
        h_win = h;
 
408
        SetCamera();
 
409
        
 
410
        tbReshape(w_win, h_win);
 
411
}
 
412
 
 
413
void SetCamera(void)
 
414
{
 
415
        switch( current_font)
 
416
        {
 
417
                case FTGL_BITMAP:
 
418
                case FTGL_PIXMAP:
 
419
                        glMatrixMode( GL_PROJECTION);
 
420
                        glLoadIdentity();
 
421
                        gluOrtho2D(0, w_win, 0, h_win);
 
422
                        glMatrixMode(GL_MODELVIEW);
 
423
                        glLoadIdentity();
 
424
                        break;
 
425
                case FTGL_OUTLINE:
 
426
                case FTGL_POLYGON:
 
427
                case FTGL_EXTRUDE:
 
428
                case FTGL_TEXTURE:
 
429
                        glMatrixMode (GL_PROJECTION);
 
430
                        glLoadIdentity ();
 
431
                        gluPerspective( 90, (float)w_win / (float)h_win, 1, 1000);
 
432
                        glMatrixMode(GL_MODELVIEW);
 
433
                        glLoadIdentity();
 
434
                        gluLookAt( 0.0, 0.0, (float)h_win / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 
435
                        break;
 
436
        }       
 
437
}
 
438
 
 
439
 
 
440
int main(int argc, char *argv[])
 
441
{
 
442
#ifndef __APPLE_CC__ // Bloody finder args???
 
443
        if (argc == 2)
 
444
                fontfile = argv[1];
 
445
#endif
 
446
 
 
447
        if (!fontfile)
 
448
        {
 
449
                fprintf(stderr, "A font file must be specified on the command line\n");
 
450
                exit(1);
 
451
        }
 
452
 
 
453
        glutInit( &argc, argv);
 
454
    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
 
455
        glutInitWindowPosition(50, 50);
 
456
        glutInitWindowSize( w_win, h_win);
 
457
        glutCreateWindow("FTGL TEST");
 
458
        glutDisplayFunc(display);
 
459
        glutKeyboardFunc(parsekey);
 
460
        glutMouseFunc(mouse);
 
461
    glutMotionFunc(motion);
 
462
        glutReshapeFunc(myReshape);
 
463
        glutIdleFunc(display);
 
464
 
 
465
        myinit( fontfile);
 
466
 
 
467
        glutMainLoop();
 
468
 
 
469
        return 0;
 
470
}