| 1 |
|
|---|
| 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 |
|
|---|
| 14 |
|
|---|
| 15 |
OSG_USING_NAMESPACE |
|---|
| 16 |
using namespace std; |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
SimpleSceneManager *mgr; |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
int setupGLUT(int *argc, char *argv[]); |
|---|
| 24 |
|
|---|
| 25 |
int main(int argc, char *argv[]) |
|---|
| 26 |
{ |
|---|
| 27 |
|
|---|
| 28 |
osgInit(argc, argv); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
int winid = setupGLUT(&argc, argv); |
|---|
| 32 |
GLUTWindowPtr gwin = GLUTWindow::create(); |
|---|
| 33 |
gwin->setId(winid); |
|---|
| 34 |
gwin->init(); |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 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 |
|
|---|
| 52 |
addRefP(face); |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
string text = "Hello World!"; |
|---|
| 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 |
|
|---|
| 69 |
Real32 scale = 1.f; |
|---|
| 70 |
NodePtr scene = face->makeNode(layoutResult, scale); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
ImagePtr image = face->getTexture(); |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 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 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
subRefP(face); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
GeometryPtr geo = GeometryPtr::dcast(scene->getCore()); |
|---|
| 90 |
beginEditCP(geo, Geometry::MaterialFieldMask); |
|---|
| 91 |
geo->setMaterial(tex); |
|---|
| 92 |
endEditCP(geo, Geometry::MaterialFieldMask); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
mgr = new SimpleSceneManager; |
|---|
| 96 |
mgr->setWindow(gwin); |
|---|
| 97 |
mgr->setRoot(scene); |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 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 |
|
|---|
| 111 |
glutMainLoop(); |
|---|
| 112 |
|
|---|
| 113 |
return 0; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
void reshape(int w, int h) |
|---|
| 118 |
{ |
|---|
| 119 |
mgr->resize(w, h); |
|---|
| 120 |
glutPostRedisplay(); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
void display() |
|---|
| 125 |
{ |
|---|
| 126 |
mgr->redraw(); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 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 |
|
|---|
| 141 |
void motion(int x, int y) |
|---|
| 142 |
{ |
|---|
| 143 |
mgr->mouseMove(x, y); |
|---|
| 144 |
glutPostRedisplay(); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 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 |
} |
|---|