fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Tutorial / 16text_vector.cpp
blob2049abb6c104b99cc42ac751750a5cf5b95d60f4
1 // Some needed include files
2 #ifdef OSG_BUILD_ACTIVE
3 #include <OSGConfig.h>
4 #include <OSGGLUT.h>
5 #include <OSGGLUTWindow.h>
6 #include <OSGSimpleSceneManager.h>
7 #include <OSGSolidBackground.h>
8 #include <OSGTextVectorFace.h>
9 #include <OSGTextLayoutParam.h>
10 #include <OSGTextLayoutResult.h>
11 #else
12 #include <OpenSG/OSGConfig.h>
13 #include <OpenSG/OSGGLUT.h>
14 #include <OpenSG/OSGGLUTWindow.h>
15 #include <OpenSG/OSGSimpleSceneManager.h>
16 #include <OpenSG/OSGSolidBackground.h>
17 #include <OpenSG/OSGTextVectorFace.h>
18 #include <OpenSG/OSGTextLayoutParam.h>
19 #include <OpenSG/OSGTextLayoutResult.h>
20 #endif
21 #include <iostream>
23 // The SimpleSceneManager is a little usefull class which helps us to
24 // manage little scenes. It will be discussed in detail later on
25 OSG::SimpleSceneManagerRefPtr mgr;
27 // We have a forward declaration here, just to sort the code
28 int setupGLUT(int *argc, char *argv[]);
30 int main(int argc, char *argv[])
32 // Init the OpenSG subsystem
33 OSG::osgInit(argc, argv);
36 // We create a GLUT Window (that is almost the same for most applications)
37 int winid = setupGLUT(&argc, argv);
38 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
39 gwin->setGlutId(winid);
40 gwin->init();
42 // Create the face object
43 std::string family = "SANS";
44 OSG::TextFace::Style style = OSG::TextFace::STYLE_PLAIN;
45 OSG::TextVectorFaceRefPtr face =
46 OSG::TextVectorFace::create(family, style);
47 if (face == 0)
49 std::cerr << "ERROR: Cannot create face object!" << std::endl;
50 return -1;
53 // Lay out one single line of text
54 std::string text = "Hello World!"; // Use UTF-8 encoding!
55 OSG::TextLayoutParam layoutParam;
56 layoutParam.horizontal = true;
57 layoutParam.leftToRight = true;
58 layoutParam.topToBottom = true;
59 layoutParam.majorAlignment = OSG::TextLayoutParam::ALIGN_FIRST;
60 layoutParam.minorAlignment = OSG::TextLayoutParam::ALIGN_FIRST;
61 layoutParam.spacing = 1.f;
62 layoutParam.length.push_back(0.f);
63 layoutParam.maxExtend = 0.f;
64 OSG::TextLayoutResult layoutResult;
65 face->layout(text, layoutParam, layoutResult);
67 // Create the text geometry
68 OSG::Real32 scale = 1.f;
69 OSG::Real32 depth = 0.2f;
70 OSG::NodeRecPtr scene = face->makeNode(layoutResult, scale, depth);
72 // Create a simple red material for our text geometry
73 OSG::SimpleMaterialRecPtr material = OSG::SimpleMaterial::create();
74 material->setDiffuse(OSG::Color3f(1, 0, 0));
75 material->setLit(true);
77 // Assign the texture to the geometry
78 OSG::GeometryRecPtr geo =
79 dynamic_cast<OSG::Geometry *>(scene->getCore());
80 geo->setMaterial(material);
82 // Create and setup the SSM
83 mgr = OSG::SimpleSceneManager::create();
84 mgr->setWindow(gwin);
85 mgr->setRoot(scene);
87 // Create a blue background
88 OSG::SolidBackgroundRecPtr bg = OSG::SolidBackground::create();
89 bg->setColor(OSG::Color3f(0.1, 0.1, 0.5));
90 gwin->getPort(0)->setBackground(bg);
92 mgr->showAll();
95 // Give Control to the GLUT Main Loop
96 glutMainLoop();
98 return 0;
101 // react to size changes
102 void reshape(int w, int h)
104 mgr->resize(w, h);
105 glutPostRedisplay();
108 // just redraw our scene if this GLUT callback is invoked
109 void display()
111 mgr->redraw();
114 // react to mouse button presses
115 void mouse(int button, int state, int x, int y)
117 if (state)
118 mgr->mouseButtonRelease(button, x, y);
119 else
120 mgr->mouseButtonPress(button, x, y);
122 glutPostRedisplay();
125 // react to mouse motions with pressed buttons
126 void motion(int x, int y)
128 mgr->mouseMove(x, y);
129 glutPostRedisplay();
132 // The GLUT subsystem is set up here. This is very similar to other GLUT
133 // applications If you have worked with GLUT before, you may have the feeling
134 // of meeting old friends again, if you have not used GLUT before that is no
135 // problem. GLUT will be introduced briefly in the next section.
137 int setupGLUT(int *argc, char *argv[])
139 glutInit(argc, argv);
140 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
142 int winid = glutCreateWindow("OpenSG Vector Text Example");
144 glutDisplayFunc(display);
145 glutMouseFunc(mouse);
146 glutMotionFunc(motion);
147 glutReshapeFunc(reshape);
149 return winid;