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

news/2023/11/30 8:16:29
  1. 各种文字效果(边框、阴影及颜色倾斜)示例

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

1.	/* 各种文字效果(边框、阴影及颜色倾斜)示例 */  
2.	osg::ref_ptr<osg::Camera> createAllKindText(const string &strDataFolder)  
3.	{  
4.	    osg::ref_ptr<osg::Camera> 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<osgText::Font> font = osgText::readFontFile(strFontPath);  
24.	    osg::ref_ptr<osg::Geode> geode = new osg::Geode;  
25.	  
26.	    osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();  
27.	    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
28.	  
29.	    //--------------------------------------------------------------------  
30.	    // 设置文字的输出的格式  
31.	      
32.	    // 设置格式为LEFT_TO_RIGHT,从左到右  
33.	    {  
34.	        osg::ref_ptr<osgText::Text> 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<osgText::Text> 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<osgText::Text> 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<osgText::Text> 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<osgText::Text> 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<osgText::Text> 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<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
128.	    osg::ref_ptr<osg::GraphicsContext::Traits> 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<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
138.	  
139.	    //osg::ref_ptr<osg::Camera> camera = new osg::Camera;  
140.	  
141.	  
142.	    // 创建各种文字效果  
143.	    osg::ref_ptr<osg::Camera> 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<osg::Group> 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所示

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


https://www.xjx100.cn/news/3092710.html

相关文章

maven打包可执行jar含依赖lib

修改pom.xml <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!-- jdk8可用&#xff0c;其他jdk版本可能需改插件版本 --><version>2.3.7.RE…

黑盒子测试

黑盒子测试的步骤包括&#xff1a; 测试计划&#xff1a;根据用户需求报告中关于功能要求和性能指标的规格说明书&#xff0c;定义相应的测试需求报告&#xff0c;制订黑盒测试的最高标准。同时适当选择测试内容&#xff0c;合理安排测试人员、测试时间及测试资源等。测试设计…

Linux 安全 - 扩展属性xattr

文章目录 前言一、简介二、扩展属性命名空间2.1 简介2.2 security扩展属性2.3 System扩展属性2.4 Trusted扩展属性2.5 User扩展属性 三、用户空间使用3.1 setfattr/getfattr3.2 setxattr/getxattr/listxattr 参考资料 前言 一、简介 xattr - Extended attributes扩展属性是与…

数字化转型导师坚鹏:数字化时代银行网点厅堂营销5大特点分析

数字化时代银行网点厅堂营销存在以下5大特点&#xff1a; 1、产品多样化&#xff1a;在数字化时代&#xff0c;银行的产品和服务变得更加多样化。除了传统的存款、贷款、理财等金融服务外&#xff0c;还新增了各种创新产品&#xff0c;如网上银行、移动支付、投资咨询、保险、…

Docker 安装 Oracle Database 23c

目录 访问 Oracle 官方网站 使用 Docker 运行 Oracle Database 23c 免费容器映像 创建并运行 Oracle Database 23c 容器 查看已下载的镜像 列出正在运行的容器 进入容器 sqlplus 命令 访问 Oracle 官方网站 Database Software Downloads | Oracle 中国 使用 Docker 运行…

nvm切换node后,没有npm

当我们想要在不同的 Node.js 版本之间切换的时候&#xff0c;通常会使用 nvm&#xff08;Node Version Manager&#xff09; 来完成。但是&#xff0c;当我们在使用 nvm 切换 Node.js 版本的时候&#xff0c;可能会遇到没有 npm 的情况。这种情况通常发生在我们在新环境或者重新…

(C)一些题2

1.在 C 语言中&#xff08;以 16位 PC 机为例&#xff09;,5种基本数据类型的存储空间长度的顺序为&#xff08;&#xff09; A . char < int < long int <float < double B . char int < long int<float <double C . char < int < long int …

外部 prometheus监控k8s集群资源

prometheus监控k8s集群资源 一&#xff0c;通过CADvisior 监控pod的资源状态1.1 授权外边用户可以访问prometheus接口。1.2 获取token保存1.3 配置prometheus.yml 启动并查看状态1.4 Grafana 导入仪表盘 二&#xff0c;通过kube-state-metrics 监控k8s资源状态2.1 部署 kube-st…