【OSG】osgText::Text 研究

由于需要在3D坐标轴上显示刻度值,所以要用到osgText::Text,这里简单记录一下其常见用法。

 

一、基本知识

 

  • 常见设置

设置字体:setFont

设置内容:setText,这里输入参数需要是osgText::String,但它的构造函数可以接收std::string类型的字符串。

设置坐标位置:setPosition

设置文字尺寸:setCharacterSize

设置恒定尺寸还是随视角缩放(与现实一致):恒定为setCharacterSizeMode( osgText::Text::SCREEN_COORDS );

设置字体分辨率:setFontResolution

设置颜色:setColor

 

  • 文字方向

setAxisAlignment(),着重介绍一下其枚举含义:

osgText::Text::SCREEN : 一直朝向镜头,所以,文字不会随着物体的旋转而旋转,永远都是水平的。

osgText::Text::XY_PLANE : 沿着轴对齐,文字处于XY决定的平面上,朝着Z轴正方向。

osgText::Text::REVERSED_XY_PLANE : 和上条的唯一区别是,朝着Z轴负方向。

(YZ, XZ同理)

 

  • 对齐方式(图片摘自《OpenSceneGraph Quick Start Guide》)

各枚举效果:

RIGHT_BOTTOM                         CENTER_BOTTOM                        LEFT_BOTTOM

RIGHT_BOTTOM_BASE_LINE    CENTER_BOTTOM_BASE_LINE    LEFT_BOTTOM_BASE_LINE

RIGHT_BASE_LINE                      CENTER_BASE_LINE                      LEFT_BASE_LINE

RIGHT_CENTER                           CENTER_CENTER                           LEFT_CENTER

RIGHT_TOP                                  CENTER_TOP                                 LEFT_TOP

 

 

文字与图片中的效果一一对应,注意,绿点代表Position的坐标。理解好啥是BASE_LINE就差不多了。

 


 

二、示例代码

如图所示,osgText::Text 继承自osgText::TextBase,更远的继承自 osg::Drawable。所以对于osgText::TextBase 来说,是与 osg::Geometry 一个层次的。

 

实例:为飞机添加一段描述:

#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgText/Font>
#include <osgText/Text>
#include <osgViewer/Viewer>

osg::ref_ptr<osgText::Font> g_font = osgText::readFontFile("fonts/arial.ttf");

osg::Camera* createHUDCamera( double left, double right, double bottom, double top )
{
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
    camera->setClearMask( GL_DEPTH_BUFFER_BIT );
    camera->setRenderOrder( osg::Camera::POST_RENDER );
    camera->setAllowEventFocus( false );
    camera->setProjectionMatrix(
        osg::Matrix::ortho2D(left, right, bottom, top) );
    return camera.release();
}

osgText::Text* createText( const osg::Vec3& pos, const std::string& content, float size )
{
    osg::ref_ptr<osgText::Text> text = new osgText::Text;
    text->setFont( g_font.get() );
    text->setCharacterSize( size );
    text->setAxisAlignment( osgText::TextBase::XY_PLANE );
    text->setPosition( pos );
    text->setText( content );
    return text.release();
}

int main()
{
    osg::ref_ptr<osg::Geode> textGeode = new osg::Geode;
    textGeode->addDrawable( createText(
        osg::Vec3(150.0f, 500.0f, 0.0f),
        "The Cessna monoplane",
        20.0f)
        );
    textGeode->addDrawable( createText(
        osg::Vec3(150.0f, 450.0f, 0.0f),
        "Six-seat, low-wing and twin-engined",
        15.0f)
        );

    osg::Camera* camera = createHUDCamera(0, 1024, 0, 768);
    camera->addChild( textGeode.get() );
    camera->getOrCreateStateSet()->setMode(
        GL_LIGHTING, osg::StateAttribute::OFF );

    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( osgDB::readNodeFile("cessna.osg") );
    root->addChild( camera );
    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow(40, 40, 800, 600);
    viewer.setSceneData( root.get() );
    return viewer.run();
}

【OSG】osgText::Text 研究

时间: 2024-08-09 01:32:45

【OSG】osgText::Text 研究的相关文章

osgText::Text osg字体

#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgViewer/CompositeViewer> #include <osgDB/ReadFile> #include <osg

osgText::Text简介

整理自<OpenSceneGraph三维渲染引擎编程指南> 在OSG中,为了显示高质量的文字,专门定义了一个新的名字空间来管理场景中的文字渲染,这个名字空间中的类主要用于加载字体和控制文字渲染的各种属性,该空间主要包括下面几个类. 1 class FadeText //渐变文字 2 class Font //字体 3 class Font3D //3D字体 4 class VectorUInt //向量单元 5 class String //字符串——用于多字节字符和各类文字编码 6 clas

OSG的HUD抬头文字显示

原文:http://blog.csdn.net/tmljs1988/article/details/7562926 可以运行 1.       HUD流程图: 完整源代码如下: /*OSG中的HUD,文字总是显示在最前面*/ #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osg/Geode> #include <osg/Depth> #include <osg/Camer

OSG开源教程(转)

整理:荣明.王伟 北 京 2008年4月 序 第一次接触OSG是在2001年,当时开源社区刚刚兴起,还没有现在这么火.下载了OSG源码,但是在看了几个Demo之后,感觉没有什么特别之处.时隔七年之后,我再次将目光投向OSG,发现OSG确实有其独到之处,很多3D效果已经不弱于甚至超过商业软件,有感于开源力量的巨大.但是,与当前主流3D商业软件如Vega.VegaPrime.VTree.Performer等相比,开源软件的缺点也很明显,其中文档缺乏可谓其致命弱点之一.开发者只能从浩瀚的源码中,进行编

OSG程序设计之Hello World 4.0

代码如下: //需要添加两个库:osgUtild.lib.osgTextd.lib #include <osgDB/ReadFile> #include <osgUtil/Optimizer> #include <osg/CoordinateSystemNode> #include <osg/Switch> #include <osgText/Text> #include <osgViewer/Viewer> #include <

OSG中的DataVariance

场景数据动态更新的核心就是设置数据变度属性DataVariance,它决定了OSG在多线程渲染的过程中的执行策略:只有所有DYNAMIC属性的对象被渲染完毕之后,OSG才会开始执行下一帧的用户更新操作:这样有效地可以避免数据的过快更新造成当前的渲染动作出错,以致系统崩溃. 所有派生自osg::Object的对象都可以设置数据变度的属性,通常设置的时机在新建对象之时,例如:osg::ref_ptr<osg::Group> node = new osg::Group;node->setDat

OSG显示文字——自定义显示文字函数

#include <Windows.h> #include <osg/Geode> #include <osg/Geometry> #include <osgText/Text> #include <osgViewer/Viewer> #include <locale.h> #include <osgDB/ReadFile> void setupProperties(osgText::Text& textObjec

OSG项目经验2&lt;在场景中添加文字面版&gt;

添加文字版需要用到osg的三个名字空间: osgText::Text,这个类用来添加文字和设置文字的一些属性: osg::Geometry,这个类用来画常见的图形: osg/ShapeDrawable,通过这个类也可以画一些常见的图形,并且可以吧Geometry加进osg::Geode节点中: 下面先简要介绍一下这三个命名空间: 1.osgText::Text:见http://www.cnblogs.com/xiaocainiao2hao/p/4544273.html 2.OSG::Gemote

OSG中的示例程序简介

OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示了路径动画的使用(AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera.CameraView.MatrixTransform.PositionAttitudeTransform等四种类型的节点上.二)演示了osgSim::OverlayNode