Tutorial/OpenSG1/Text: 16text_txf.cpp

File 16text_txf.cpp, 4.6 kB (added by PatrickDaehne, 7 months ago)
Line 
1 // Some needed include files
2 #include <OpenSG/OSGConfig.h>
3 #include <OpenSG/OSGGLUT.h>
4 #include <OpenSG/OSGGLUTWindow.h>
5 #include <OpenSG/OSGSimpleSceneManager.h>
6 #include <OpenSG/OSGSimpleTexturedMaterial.h>
7 #include <OpenSG/OSGSolidBackground.h>
8 #include <OpenSG/OSGTextTXFFace.h>
9 #include <OpenSG/OSGTextLayoutParam.h>
10 #include <OpenSG/OSGTextLayoutResult.h>
11 #include <iostream>
12
13 // In most cases it is useful to add this line, else every OpenSG command
14 // must be preceeded by an extra OSG::
15 OSG_USING_NAMESPACE
16 using namespace std;
17
18 // The SimpleSceneManager is a little usefull class which helps us to
19 // manage little scenes. It will be discussed in detail later on
20 SimpleSceneManager *mgr;
21
22 // We have a forward declaration here, just to sort the code
23 int setupGLUT(int *argc, char *argv[]);
24
25 int main(int argc, char *argv[])
26 {
27     // Init the OpenSG subsystem
28     osgInit(argc, argv);
29
30     // We create a GLUT Window (that is almost the same for most applications)
31     int winid = setupGLUT(&argc, argv);
32     GLUTWindowPtr gwin = GLUTWindow::create();
33     gwin->setId(winid);
34     gwin->init();
35
36     // Create the face
37     string family = "SANS";
38     TextFace::Style style = TextFace::STYLE_PLAIN;
39     TextTXFParam txfParam;
40     txfParam.size = 46;
41     txfParam.gap = 1;
42     txfParam.setCharacters("Hello World!");
43     txfParam.textureWidth = 0;
44     TextTXFFace *face = TextTXFFace::create(family, style, txfParam);
45     if (face == 0)
46     {
47         cerr << "ERROR: Cannot create face object!" << endl;
48         return -1;
49     }
50
51     // Do not forget to increment the reference counter!
52     addRefP(face);
53
54     // Lay out one single line of text
55     string text = "Hello World!"; // Use UTF-8 encoding!
56     TextLayoutParam layoutParam;
57     layoutParam.horizontal = true;
58     layoutParam.leftToRight = true;
59     layoutParam.topToBottom = true;
60     layoutParam.majorAlignment = TextLayoutParam::ALIGN_FIRST;
61     layoutParam.minorAlignment = TextLayoutParam::ALIGN_FIRST;
62     layoutParam.spacing = 1.f;
63     layoutParam.length.push_back(0.f);
64     layoutParam.maxExtend = 0.f;
65     TextLayoutResult layoutResult;
66     face->layout(text, layoutParam, layoutResult);
67
68     // Create the text geometry
69     Real32 scale = 1.f;
70     NodePtr scene = face->makeNode(layoutResult, scale);
71
72     // Get the texture that contains the characters of the font
73     ImagePtr image = face->getTexture();
74
75     // Create the texture that will hold the image
76     SimpleTexturedMaterialPtr tex = SimpleTexturedMaterial::create();
77     beginEditCP(tex);
78         tex->setImage(image);
79         tex->setEnvMode(GL_MODULATE);
80         tex->setDiffuse(Color3f(1, 0, 0));
81     endEditCP(tex);
82
83     // We do not need the face any more, so we decrement the
84     // reference counter to release it. Do not use the face
85     // object beyond this point!
86     subRefP(face);
87
88     // Assign the texture to the geometry
89     GeometryPtr geo = GeometryPtr::dcast(scene->getCore());
90     beginEditCP(geo, Geometry::MaterialFieldMask);
91         geo->setMaterial(tex);
92     endEditCP(geo, Geometry::MaterialFieldMask);
93
94     // Create and setup the SSM
95     mgr = new SimpleSceneManager;
96     mgr->setWindow(gwin);
97     mgr->setRoot(scene);
98
99     // Create a blue background
100     SolidBackgroundPtr bg = SolidBackground::create();
101     beginEditCP(bg);
102         bg->setColor(Color3f(0.1, 0.1, 0.5));
103     endEditCP(bg);
104     beginEditCP(gwin->getPort(0));
105         gwin->getPort(0)->setBackground(bg);
106     endEditCP(gwin->getPort(0));
107
108     mgr->showAll();
109
110     // Give Control to the GLUT Main Loop
111     glutMainLoop();
112
113     return 0;
114 }
115
116 // react to size changes
117 void reshape(int w, int h)
118 {
119     mgr->resize(w, h);
120     glutPostRedisplay();
121 }
122
123 // just redraw our scene if this GLUT callback is invoked
124 void display()
125 {
126     mgr->redraw();
127 }
128
129 // react to mouse button presses
130 void mouse(int button, int state, int x, int y)
131 {
132     if (state)
133         mgr->mouseButtonRelease(button, x, y);
134     else
135         mgr->mouseButtonPress(button, x, y);
136
137     glutPostRedisplay();
138 }
139
140 // react to mouse motions with pressed buttons
141 void motion(int x, int y)
142 {
143     mgr->mouseMove(x, y);
144     glutPostRedisplay();
145 }
146
147 // The GLUT subsystem is set up here. This is very similar to other GLUT
148 // applications If you have worked with GLUT before, you may have the feeling
149 // of meeting old friends again, if you have not used GLUT before that is no
150 // problem. GLUT will be introduced briefly in the next section.
151
152 int setupGLUT(int *argc, char *argv[])
153 {
154     glutInit(argc, argv);
155     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
156
157     int winid = glutCreateWindow("OpenSG TXF Text Example");
158
159     glutDisplayFunc(display);
160     glutMouseFunc(mouse);
161     glutMotionFunc(motion);
162     glutReshapeFunc(reshape);
163
164     return winid;
165 }