OSG文字-各种文字效果(边框、阴影及颜色倾斜)示例(2)

  1. 各种文字效果(边框、阴影及颜色倾斜)示例

        各种文字效果(边框、阴影及颜色倾斜)示例的代码如程序清单9-2所示:

1.	/* 各种文字效果(边框、阴影及颜色倾斜)示例 */  
2.	osg::ref_ptr createAllKindText(const string &strDataFolder)  
3.	{  
4.	    osg::ref_ptr camera = new osg::Camera();  
5.	  
6.	    // 设置投影矩阵  
7.	    camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1280, 0, 800));  
8.	  
9.	    // 设置视图矩阵  
10.	    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);  
11.	    camera->setViewMatrix(osg::Matrix::identity());  
12.	  
13.	    // 清除深度缓存  
14.	    camera->setClearMask(GL_DEPTH_BUFFER_BIT);  
15.	  
16.	    // 设置渲染顺序,在主摄像机之前渲染  
17.	    camera->setRenderOrder(osg::Camera::POST_RENDER);  
18.	  
19.	    // 设置为不接收事件,始终不得到焦点  
20.	    camera->setAllowEventFocus(false);  
21.	  
22.	    string strFontPath = strDataFolder + "fonts\\arial.ttf";  
23.	    osg::ref_ptr font = osgText::readFontFile(strFontPath);  
24.	    osg::ref_ptr geode = new osg::Geode;  
25.	  
26.	    osg::ref_ptr stateset = geode->getOrCreateStateSet();  
27.	    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
28.	  
29.	    //--------------------------------------------------------------------  
30.	    // 设置文字的输出的格式  
31.	      
32.	    // 设置格式为LEFT_TO_RIGHT,从左到右  
33.	    {  
34.	        osg::ref_ptr text = new osgText::Text;  
35.	        text->setFont(font.get());  
36.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
37.	        text->setCharacterSize(20.0);  
38.	        text->setPosition(osg::Vec3(50.0, 750.0, 0.0));  
39.	        text->setLayout(osgText::Text::LEFT_TO_RIGHT);  
40.	        text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);");  
41.	        geode->addDrawable(text.get());  
42.	    }  
43.	    // 设置格式为RIGHT_TO_LEFT,从右到左  
44.	    {  
45.	        osg::ref_ptr text = new osgText::Text;  
46.	        text->setFont(font.get());  
47.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
48.	        text->setCharacterSize(30.0);  
49.	        text->setPosition(osg::Vec3(1200.0, 750.0, 0.0));  
50.	  
51.	        text->setLayout(osgText::Text::RIGHT_TO_LEFT);   
52.	        text->setAlignment(osgText::Text::RIGHT_BASE_LINE);  
53.	  
54.	        text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT); ");  
55.	  
56.	        geode->addDrawable(text.get());  
57.	    }  
58.	    // 设置格式为VERTICAL,垂直  
59.	    {  
60.	        osg::ref_ptr text = new osgText::Text;  
61.	        text->setFont(font.get());  
62.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
63.	        text->setCharacterSize(20.0);  
64.	        text->setPosition(osg::Vec3(50.0, 750.0, 0.0));  
65.	        text->setLayout(osgText::Text::VERTICAL);  
66.	        text->setText("text->setLayout(osgText::Text::VERTICAL);");  
67.	        geode->addDrawable(text.get());  
68.	    }  
69.	    //--------------------------------------------------------------------  
70.	    // 设置阴影  
71.	    {  
72.	        osg::ref_ptr text = new osgText::Text;  
73.	        text->setFont(font.get());  
74.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
75.	        text->setCharacterSize(40.0);  
76.	        text->setPosition(osg::Vec3(100.0, 650.0, 0.0));  
77.	  
78.	        // 设置阴影为DROP_SHADOW_BOTTOM_RIGHT  
79.	        text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);  
80.	        text->setBackdropColor(osg::Vec4(0.0, 1.0, 0.0, 1.0));  
81.	        text->setBackdropOffset(0.1, -0.1);  
82.	        text->setText("text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);");  
83.	        geode->addDrawable(text.get());  
84.	    }  
85.	  
86.	    //--------------------------------------------------------------------  
87.	    // 设置边框  
88.	    {  
89.	        osg::ref_ptr text = new osgText::Text;  
90.	        text->setFont(font.get());  
91.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
92.	        text->setCharacterSize(30.0);  
93.	        text->setPosition(osg::Vec3(100.0, 450.0, 0.0));  
94.	  
95.	        // 设置边框对齐绘制  
96.	        text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX|osgText::Text::ALIGNMENT);  
97.	        text->setText("text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX|osgText::Text::ALIGNMENT);");  
98.	        geode->addDrawable(text.get());  
99.	    }  
100.	  
101.	    //-------------------------------------------------------------------
102.	    // 设置颜色倾斜模式  
103.	    {  
104.	        osg::ref_ptr text = new osgText::Text;  
105.	        text->setFont(font.get());  
106.	        text->setColor(osg::Vec4(1.0, 1.0, 0.0, 1.0));  
107.	        text->setCharacterSize(40.0);  
108.	        text->setPosition(osg::Vec3(100.0, 250.0, 0.0));  
109.	  
110.	        // 设置颜色倾斜模式为:PER_CHARCTER  
111.	        text->setColorGradientMode(osgText::Text::PER_CHARACTER);  
112.	          
113.	        // 设置倾斜4个角落的颜色  
114.	        text->setColorGradientCorners(osg::Vec4(1.0,0.0,0.0,1.0),osg::Vec4(0.0,1.0,0.0,1.0),  
115.	            osg::Vec4(0.0,0.0,1.0,1.0),osg::Vec4(1.0,1.0,1.0,1.0));  
116.	        text->setText("text->setColorGradientMode(osgText::Text::SOLID);");  
117.	        geode->addDrawable(text.get());  
118.	    }  
119.	  
120.	    camera->addChild(geode.get());  
121.	  
122.	    return camera.get();  
123.	}  
124.	  
125.	void osgText_9_2(const string &strDataFolder)  
126.	{  
127.	    osg::ref_ptr viewer = new osgViewer::Viewer();  
128.	    osg::ref_ptr traits = new osg::GraphicsContext::Traits;  
129.	    traits->x = 40;  
130.	    traits->y = 40;  
131.	    traits->width = 600;  
132.	    traits->height = 480;  
133.	    traits->windowDecoration = true;  
134.	    traits->doubleBuffer = true;  
135.	    traits->sharedContext = 0;  
136.	  
137.	    osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
138.	  
139.	    //osg::ref_ptr camera = new osg::Camera;  
140.	  
141.	  
142.	    // 创建各种文字效果  
143.	    osg::ref_ptr camera = createAllKindText(strDataFolder);  
144.	    camera->setGraphicsContext(gc.get());  
145.	    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
146.	    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
147.	    camera->setDrawBuffer(buffer);  
148.	    camera->setReadBuffer(buffer);  
149.	  
150.	    viewer->addSlave(camera.get());  
151.	  
152.	    // 添加到根节点  
153.	    osg::ref_ptr root = new osg::Group();  
154.	    root->addChild(createAllKindText(strDataFolder));  
155.	  
156.	    // 优化场景数据  
157.	    osgUtil::Optimizer optimizer;  
158.	    optimizer.optimize(root.get());  
159.	  
160.	    viewer->setSceneData(root.get());  
161.	  
162.	    viewer->realize();  
163.	  
164.	    viewer->run();  
165.	}  

        运行程序,截图如图9-4所示

OSG文字-各种文字效果(边框、阴影及颜色倾斜)示例(2)_第1张图片

图9-4 各种文字效果(边框、阴影及颜色倾斜)示例截图

你可能感兴趣的:(OSG,c++,3d,图形渲染,交互)