| 1 |
/* |
| 2 |
* simple.cpp - simple demo for FTGL, the OpenGL font library |
| 3 |
* |
| 4 |
* Copyright (c) 2008 Sam Hocevar <[email protected]> |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
* a copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be |
| 15 |
* included in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <math.h> // sin(), cos() |
| 29 |
#include <stdlib.h> // exit() |
| 30 |
|
| 31 |
#if defined HAVE_GL_GLUT_H |
| 32 |
# include <GL/glut.h> |
| 33 |
#elif defined HAVE_GLUT_GLUT_H |
| 34 |
# include <GLUT/glut.h> |
| 35 |
#else |
| 36 |
# error GLUT headers not present |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <FTGL/ftgl.h> |
| 40 |
|
| 41 |
static FTFont *font[3]; |
| 42 |
static int fontindex = 0; |
| 43 |
static int lastfps = 0; |
| 44 |
static int frames = 0; |
| 45 |
|
| 46 |
// |
| 47 |
// FTHaloGlyph is a derivation of FTPolygonGlyph that also displays a |
| 48 |
// halo of FTOutlineGlyph objects at varying positions and with varying |
| 49 |
// outset values. |
| 50 |
// |
| 51 |
class FTHaloGlyph : public FTPolygonGlyph |
| 52 |
{ |
| 53 |
public: |
| 54 |
FTHaloGlyph(FT_GlyphSlot glyph) : FTPolygonGlyph(glyph, 0, true) |
| 55 |
{ |
| 56 |
for(int i = 0; i < 5; i++) |
| 57 |
{ |
| 58 |
subglyph[i] = new FTOutlineGlyph(glyph, i, true); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private: |
| 63 |
const FTPoint& Render(const FTPoint& pen, int renderMode) |
| 64 |
{ |
| 65 |
glPushMatrix(); |
| 66 |
for(int i = 0; i < 5; i++) |
| 67 |
{ |
| 68 |
glTranslatef(0.0, 0.0, -2.0); |
| 69 |
subglyph[i]->Render(pen, renderMode); |
| 70 |
} |
| 71 |
glPopMatrix(); |
| 72 |
|
| 73 |
return FTPolygonGlyph::Render(pen, renderMode); |
| 74 |
} |
| 75 |
|
| 76 |
FTGlyph *subglyph[5]; |
| 77 |
}; |
| 78 |
|
| 79 |
// |
| 80 |
// FTHaloFont is a simple FTFont derivation that builds FTHaloGlyph |
| 81 |
// objects. |
| 82 |
// |
| 83 |
class FTHaloFont : public FTFont |
| 84 |
{ |
| 85 |
public: |
| 86 |
FTHaloFont(char const *fontFilePath) : FTFont(fontFilePath) {} |
| 87 |
|
| 88 |
private: |
| 89 |
virtual FTGlyph* MakeGlyph(FT_GlyphSlot slot) |
| 90 |
{ |
| 91 |
return new FTHaloGlyph(slot); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
// |
| 96 |
// Main OpenGL loop: set up lights, apply a few rotation effects, and |
| 97 |
// render text using the current FTGL object. |
| 98 |
// |
| 99 |
static void RenderScene(void) |
| 100 |
{ |
| 101 |
int now = glutGet(GLUT_ELAPSED_TIME); |
| 102 |
|
| 103 |
float n = (float)now / 20.; |
| 104 |
float t1 = sin(n / 80); |
| 105 |
float t2 = sin(n / 50 + 1); |
| 106 |
float t3 = sin(n / 30 + 2); |
| 107 |
|
| 108 |
float ambient[4] = { (t1 + 2.0f) / 3, |
| 109 |
(t2 + 2.0f) / 3, |
| 110 |
(t3 + 2.0f) / 3, 0.3 }; |
| 111 |
float diffuse[4] = { 1.0, 0.9, 0.9, 1.0 }; |
| 112 |
float specular[4] = { 1.0, 0.7, 0.7, 1.0 }; |
| 113 |
float position[4] = { 100.0, 100.0, 0.0, 1.0 }; |
| 114 |
|
| 115 |
float front_ambient[4] = { 0.7, 0.7, 0.7, 0.0 }; |
| 116 |
|
| 117 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 118 |
|
| 119 |
glEnable(GL_LIGHTING); |
| 120 |
glEnable(GL_DEPTH_TEST); |
| 121 |
glEnable(GL_LINE_SMOOTH); |
| 122 |
glEnable(GL_BLEND); |
| 123 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
| 124 |
|
| 125 |
glPushMatrix(); |
| 126 |
glTranslatef(-0.9, -0.2, -10.0); |
| 127 |
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); |
| 128 |
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); |
| 129 |
glLightfv(GL_LIGHT1, GL_SPECULAR, specular); |
| 130 |
glLightfv(GL_LIGHT1, GL_POSITION, position); |
| 131 |
glEnable(GL_LIGHT1); |
| 132 |
glPopMatrix(); |
| 133 |
|
| 134 |
glPushMatrix(); |
| 135 |
glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient); |
| 136 |
glColorMaterial(GL_FRONT, GL_DIFFUSE); |
| 137 |
glTranslatef(0.0, 0.0, 20.0); |
| 138 |
glRotatef(n / 1.11, 0.0, 1.0, 0.0); |
| 139 |
glRotatef(n / 2.23, 1.0, 0.0, 0.0); |
| 140 |
glRotatef(n / 3.17, 0.0, 0.0, 1.0); |
| 141 |
glTranslatef(-260.0, -0.2, 0.0); |
| 142 |
glColor3f(1.0, 1.0, 1.0); |
| 143 |
font[fontindex]->Render("Hello FTGL!"); |
| 144 |
glPopMatrix(); |
| 145 |
|
| 146 |
glutSwapBuffers(); |
| 147 |
|
| 148 |
frames++; |
| 149 |
|
| 150 |
if(now - lastfps > 5000) |
| 151 |
{ |
| 152 |
fprintf(stderr, "%i frames in 5.0 seconds = %g FPS\n", |
| 153 |
frames, frames * 1000. / (now - lastfps)); |
| 154 |
lastfps += 5000; |
| 155 |
frames = 0; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// |
| 160 |
// GLUT key processing function: <esc> quits, <tab> cycles across fonts. |
| 161 |
// |
| 162 |
static void ProcessKeys(unsigned char key, int, int) |
| 163 |
{ |
| 164 |
switch(key) |
| 165 |
{ |
| 166 |
case 27: |
| 167 |
delete font[0]; |
| 168 |
delete font[1]; |
| 169 |
delete font[2]; |
| 170 |
exit(EXIT_SUCCESS); |
| 171 |
break; |
| 172 |
case '\t': |
| 173 |
fontindex = (fontindex + 1) % 3; |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// |
| 179 |
// Main program entry point: set up GLUT window, load fonts, run GLUT loop. |
| 180 |
// |
| 181 |
int main(int argc, char **argv) |
| 182 |
{ |
| 183 |
char const *file = NULL; |
| 184 |
|
| 185 |
#ifdef FONT_FILE |
| 186 |
file = FONT_FILE; |
| 187 |
#else |
| 188 |
if(argc < 2) |
| 189 |
{ |
| 190 |
fprintf(stderr, "Usage: %s <font_name.ttf>\n", argv[0]); |
| 191 |
return EXIT_FAILURE; |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
if(argc > 1) |
| 196 |
{ |
| 197 |
file = argv[1]; |
| 198 |
} |
| 199 |
|
| 200 |
// Initialise GLUT stuff |
| 201 |
glutInit(&argc, argv); |
| 202 |
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); |
| 203 |
glutInitWindowPosition(100, 100); |
| 204 |
glutInitWindowSize(640, 480); |
| 205 |
glutCreateWindow("simple FTGL C++ demo"); |
| 206 |
|
| 207 |
glutDisplayFunc(RenderScene); |
| 208 |
glutIdleFunc(RenderScene); |
| 209 |
glutKeyboardFunc(ProcessKeys); |
| 210 |
|
| 211 |
glMatrixMode(GL_PROJECTION); |
| 212 |
glLoadIdentity(); |
| 213 |
gluPerspective(90, 640.0f / 480.0f, 1, 1000); |
| 214 |
glMatrixMode(GL_MODELVIEW); |
| 215 |
glLoadIdentity(); |
| 216 |
gluLookAt(0.0, 0.0, 640.0f / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
| 217 |
|
| 218 |
// Initialise FTGL stuff |
| 219 |
font[0] = new FTExtrudeFont(file); |
| 220 |
font[1] = new FTBufferFont(file); |
| 221 |
font[2] = new FTHaloFont(file); |
| 222 |
|
| 223 |
if(font[0]->Error() || font[1]->Error() || font[2]->Error()) |
| 224 |
{ |
| 225 |
fprintf(stderr, "%s: could not load font `%s'\n", argv[0], file); |
| 226 |
return EXIT_FAILURE; |
| 227 |
} |
| 228 |
|
| 229 |
font[0]->FaceSize(80); |
| 230 |
font[0]->Depth(10); |
| 231 |
font[0]->Outset(0, 3); |
| 232 |
font[0]->CharMap(ft_encoding_unicode); |
| 233 |
|
| 234 |
font[1]->FaceSize(80); |
| 235 |
font[1]->CharMap(ft_encoding_unicode); |
| 236 |
|
| 237 |
font[2]->FaceSize(80); |
| 238 |
font[2]->CharMap(ft_encoding_unicode); |
| 239 |
|
| 240 |
fprintf(stderr, "Using FTGL version %s\n", |
| 241 |
FTGL::GetString(FTGL::CONFIG_VERSION)); |
| 242 |
|
| 243 |
// Run GLUT loop |
| 244 |
glutMainLoop(); |
| 245 |
|
| 246 |
return EXIT_SUCCESS; |
| 247 |
} |
| 248 |
|