1 // Some needed include files
2 #ifdef OSG_BUILD_ACTIVE
5 #include <OSGGLUTWindow.h>
6 #include <OSGSimpleSceneManager.h>
7 #include <OSGSimpleTexturedMaterial.h>
8 #include <OSGSolidBackground.h>
9 #include <OSGTextTXFFace.h>
10 #include <OSGTextLayoutParam.h>
11 #include <OSGTextLayoutResult.h>
13 #include <OpenSG/OSGConfig.h>
14 #include <OpenSG/OSGGLUT.h>
15 #include <OpenSG/OSGGLUTWindow.h>
16 #include <OpenSG/OSGSimpleSceneManager.h>
17 #include <OpenSG/OSGSimpleTexturedMaterial.h>
18 #include <OpenSG/OSGSolidBackground.h>
19 #include <OpenSG/OSGTextTXFFace.h>
20 #include <OpenSG/OSGTextLayoutParam.h>
21 #include <OpenSG/OSGTextLayoutResult.h>
25 // The SimpleSceneManager is a little usefull class which helps us to
26 // manage little scenes. It will be discussed in detail later on
27 OSG::SimpleSceneManagerRefPtr mgr
;
29 // We have a forward declaration here, just to sort the code
30 int setupGLUT(int *argc
, char *argv
[]);
32 int main(int argc
, char *argv
[])
34 // Init the OpenSG subsystem
35 OSG::osgInit(argc
, argv
);
38 // We create a GLUT Window (that is almost the same for most applications)
39 int winid
= setupGLUT(&argc
, argv
);
40 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
41 gwin
->setGlutId(winid
);
45 std::string family
= "SANS";
46 OSG::TextFace::Style style
= OSG::TextFace::STYLE_PLAIN
;
47 OSG::TextTXFParam txfParam
;
50 txfParam
.setCharacters("Hello World!");
51 txfParam
.textureWidth
= 0;
52 OSG::TextTXFFaceRefPtr face
=
53 OSG::TextTXFFace::create(family
, style
, txfParam
);
56 std::cerr
<< "ERROR: Cannot create face object!" << std::endl
;
60 // Lay out one single line of text
61 std::string text
= "Hello World!"; // Use UTF-8 encoding!
62 OSG::TextLayoutParam layoutParam
;
63 layoutParam
.horizontal
= true;
64 layoutParam
.leftToRight
= true;
65 layoutParam
.topToBottom
= true;
66 layoutParam
.majorAlignment
= OSG::TextLayoutParam::ALIGN_FIRST
;
67 layoutParam
.minorAlignment
= OSG::TextLayoutParam::ALIGN_FIRST
;
68 layoutParam
.spacing
= 1.f
;
69 layoutParam
.length
.push_back(0.f
);
70 layoutParam
.maxExtend
= 0.f
;
71 OSG::TextLayoutResult layoutResult
;
72 face
->layout(text
, layoutParam
, layoutResult
);
74 // Create the text geometry
75 OSG::Real32 scale
= 1.f
;
76 OSG::NodeRecPtr scene
= face
->makeNode(layoutResult
, scale
);
78 // Get the texture that contains the characters of the font
79 OSG::ImageRecPtr image
= face
->getTexture();
81 // Create the texture that will hold the image
82 OSG::SimpleTexturedMaterialRecPtr tex
=
83 OSG::SimpleTexturedMaterial::create();
85 tex
->setEnvMode(GL_MODULATE
);
86 tex
->setDiffuse(OSG::Color3f(1, 0, 0));
88 // Assign the texture to the geometry
89 OSG::GeometryRecPtr geo
=
90 dynamic_cast<OSG::Geometry
*>(scene
->getCore());
91 geo
->setMaterial(tex
);
93 // Create and setup the SSM
94 mgr
= OSG::SimpleSceneManager::create();
98 // Create a blue background
99 OSG::SolidBackgroundRecPtr bg
= OSG::SolidBackground::create();
100 bg
->setColor(OSG::Color3f(0.1, 0.1, 0.5));
102 gwin
->getPort(0)->setBackground(bg
);
106 OSG::commitChanges();
109 // Give Control to the GLUT Main Loop
115 // react to size changes
116 void reshape(int w
, int h
)
122 // just redraw our scene if this GLUT callback is invoked
128 // react to mouse button presses
129 void mouse(int button
, int state
, int x
, int y
)
132 mgr
->mouseButtonRelease(button
, x
, y
);
134 mgr
->mouseButtonPress(button
, x
, y
);
139 // react to mouse motions with pressed buttons
140 void motion(int x
, int y
)
142 mgr
->mouseMove(x
, y
);
146 // The GLUT subsystem is set up here. This is very similar to other GLUT
147 // applications If you have worked with GLUT before, you may have the feeling
148 // of meeting old friends again, if you have not used GLUT before that is no
149 // problem. GLUT will be introduced briefly in the next section.
151 int setupGLUT(int *argc
, char *argv
[])
153 glutInit(argc
, argv
);
154 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
156 int winid
= glutCreateWindow("OpenSG TXF Text Example");
158 glutDisplayFunc(display
);
159 glutMouseFunc(mouse
);
160 glutMotionFunc(motion
);
161 glutReshapeFunc(reshape
);