/**********************************************************
*Write by FlySky
*zzuxp@163.com http://www.OsgChina.org
**********************************************************/ #include <osgViewer/Viewer> #include <osg/Node>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Group>
#include <osg/AutoTransform> #include <osgDB/ReadFile>
#include <osgDB/WriteFile> #include <osgText/Text> #include <osgUtil/Optimizer> #include <iostream> //创建自动变换节点
osg::ref_ptr<osg::Node> createAutoTransform(osg::Vec3& position, float size, std::string& label,
osg::AutoTransform::AutoRotateMode autoMode, osgText::Text::AxisAlignment axisAlignment)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode(); //字体
std::string font("fonts/cour.ttf"); //创建Text对象
osg::ref_ptr<osgText::Text> text = new osgText::Text();
geode->addDrawable(text.get()); //设置字体
text->setFont(font);
//设置字体的分辨率,默认为32*32
text->setFontResolution(128.0f, 128.0f);
//设置字体的大小
text->setCharacterSize(size);
//设置对齐方式
text->setAlignment(osgText::Text::CENTER_CENTER);
//设置方向
text->setAxisAlignment(axisAlignment);
//设置文字
text->setText(label); //关闭光照
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); //创建自动变换节点
osg::ref_ptr<osg::AutoTransform> at = new osg::AutoTransform();
//添加子节点
at->addChild(geode.get()); //设置自动变换方式
at->setAutoRotateMode(autoMode);
//根据屏幕大小来缩放节点,默认为false,设置为true时,节点无法缩放
at->setAutoScaleToScreen(false);
//at->setAutoScaleToScreen(true) ;
//设置缩放的最大和最小比例
at->setMinimumScale(0.0f);
at->setMaximumScale(5.0f);
//设置位置
at->setPosition(position); return at.get();
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); osg::ref_ptr<osg::Group> root = new osg::Group(); std::string text("Fly To Sky"); /*
三种变换模式:
ROTATE_TO_SCREEN  自动朝向屏幕
ROTATE_TO_CAMERA 自动朝向相机
NO_ROTATION 无
*/
//添加ROTATE_TO_SCEREEN模式变换节点
root->addChild(createAutoTransform(osg::Vec3(0.0f, 0.0f, 0.0f), 60.0f, text,
osg::AutoTransform::ROTATE_TO_SCREEN, osgText::Text::XY_PLANE)); //添加NO_ROTATION模式变换节点
root->addChild(createAutoTransform(osg::Vec3(0.0f, 0.0f, 0.0f), 60.0f, text,
osg::AutoTransform::NO_ROTATION, osgText::Text::YZ_PLANE)); //添加ROTATE_TO_CAMERA模式变换节点
//root->addChild(createAutoTransform(osg::Vec3(0.0f,0.0f,0.0f),60.0f,text,
// osg::AutoTransform::ROTATE_TO_CAMERA,osgText::Text::XY_PLANE)) ; //优化场景数据
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get()); viewer->setSceneData(root.get()); viewer->realize(); viewer->run(); return 0;
}

OSG学习:自动对齐节点示例的更多相关文章

  1. OSG学习:矩阵变换节点示例

    #include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...

  2. osg学习示例之遇到问题四骨骼动画编译osgCal

    osg学习示例之遇到问题四骨骼动画编译osgCal 转自:http://blog.csdn.net/wuwangrun/article/details/8239451 今天学到书<OpenSce ...

  3. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  4. OSG学习:位置变换节点示例

    osg::PositionAttitudeTransform节点. #include <osgViewer\Viewer> #include <osg\Node> #inclu ...

  5. OSG学习:阴影代码示例

    效果图: 代码示例: #include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #i ...

  6. OSG学习:响应键盘鼠标示例

    示例功能:示例里面有两个模型,一个是牛,另一个是飞机.鼠标右键时牛和飞机都隐藏,鼠标左键双击时牛和飞机都显示,按键盘上面的LEFT键,显示牛,按键盘上面的RIGHT键显示飞机.其中显示与隐藏节点使用的 ...

  7. OSG学习:转动的小汽车示例

    由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成. 代码如下: #include <osg\ShapeDrawable> #include <osg\Animat ...

  8. OSG学习:使用已有回调示例

    回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情.这个函数就叫做回 ...

  9. OSG学习:基本几何体绘制示例

    绘制并渲染几何体主要有如下3大步骤: 1.创建各种向量数据,如顶点.纹理坐标.颜色和法线等.需要注意的是,添加顶点数据时主要按照逆时针顺序添加, 以确保背面剔除的正确. 2.实例化一个几何体对象(os ...

随机推荐

  1. delphi 2010以上 安装 第三方控件

    delphi-“can't be installed because it is not a design time package. 一定要先装 dcl*.dpk ----------------- ...

  2. VSCode 配置 Python

    假设 Anaconda 和 VSCode 都安装好了. 安装插件 VSCode 自带的 python 高亮不是很好,这里我们用 One Dark Pro 插件, 以及安装 Python 插件. 设定 ...

  3. ubuntu16.04 64位安装tftp服务器

    1.安装tftpd-hpa和xinetd sudo apt-get install tftpd-hpa xined2.针对64位操作系统安装openbsd-inetd apt-get install ...

  4. [NOIP2017]逛公园(DP)

    先spfa一遍处理出d[]数组,(从n开始bfs一遍标记可以达到n的点) 题意即,在走最短路的基础上,可以最多多走K长度的路径, 考虑DP,每次剩余可走的长度会因决策而改变,所以考虑dp[i][j]为 ...

  5. c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承

    公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...

  6. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  7. journalctl 日志查看方法

    1  概述 日志管理工具journalctl是centos7上专有的日志管理工具,该工具是从message这个文件里读取信息.Systemd统一管理所有Unit的启动日志.带来的好处就是,可以只用jo ...

  8. 【费元星】crt 无法上传文件,总是显示盾牌表示-完美解决

    将如下内容保存到文件中,已.bat 结尾 taskkill /f /im explorer.exeattrib -s -r -h "%userprofile%\AppData\Local\i ...

  9. leetcode笔记9 Move Zeroes

    题目要求: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  10. 一个体验好的Windows 任务栏缩略图开发心得

    本文来自网易云社区 作者:孙有军 前言: 对于一个追求极致体验的软件来说,利用好系统的每一点优秀的特性,将会大大提高软件的品质. Windows vista以来任务栏缩略图,及Win + TAB的程序 ...