查看: 1545|回复: 0

FlySky大师的代码示例理解不清,大家来“喷”我吧

[复制链接]

该用户从未签到

发表于 2014-11-13 11:00:07 | 显示全部楼层 |阅读模式
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Camera>
#include <osg/Matrix>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgText/Text>
#include <osgText/Font>
#include <osgUtil/Optimizer>
#include <iostream>
osg::ref_ptr<osg::Camera> createAllKindText()
{
        osg::ref_ptr<osg::Camera> camera = new osg::Camera() ;
        //设置投影矩阵
        camera->setProjectionMatrix(osg::Matrix:rtho2D(0,1280,0,800));
        //设置视图矩阵  
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setViewMatrix(osg::Matrix::identity());
        //清除深度缓存
        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
        //设置渲染顺寻,在主摄像机之前渲染
        camera->setRenderOrder(osg::Camera:OST_RENDER);
        //设置为不接收事件,始终不得到焦点
        camera->setAllowEventFocus(false);
        osg::ref_ptr<osgText::Font> font = osgText::readFontFile("fonts/arial.ttf");
        osg::ref_ptr<osg::Geode> geode  = new osg::Geode;
        osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
        //-----------------------------------------------//-----------------------------------------------
        //设置文字输出的格式
        //设置格式为LEFT_TO_RIGHT,从左到右
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(20.0f);
                text->setPosition(osg::Vec3(50.0f,750.0f,0.0f));
                text->setLayout(osgText::Text:EFT_TO_RIGHT);
                text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);");
                geode->addDrawable(text.get());
        }

        //设置格式为RIGHT_TO_LEFT,从右到左
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(30.0f);
                text->setPosition(osg::Vec3(1200.0f,750.0f,0.0f));
                text->setLayout(osgText::Text::RIGHT_TO_LEFT);
                text->setAlignment(osgText::Text::RIGHT_BASE_LINE);
                text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT);");
                geode->addDrawable(text.get());
        }

        //设置格式为VERTICAL,垂直
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(20.0f);
                text->setPosition(osg::Vec3(50.0f,750.0f,0.0f));
                text->setLayout(osgText::Text::VERTICAL);
                text->setText("text->setLayout(osgText::Text::VERTICAL);");
                geode->addDrawable(text.get());
        }

        //-----------------------------------------------//-----------------------------------------------
        //设置阴影
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(40.0f);
                text->setPosition(osg::Vec3(100.0f,650.0f,0.0f));

                //设置阴影为DROP_SHADOW_BOTTOM_RIGHT
                text->setBackdropType(osgText::Text:ROP_SHADOW_BOTTOM_RIGHT);
                text->setBackdropColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
                text->setBackdropOffset(0.1f,-0.1f);
                text->setText("text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);");
                geode->addDrawable(text.get());
        }

        //-----------------------------------------------//-----------------------------------------------
        //设置边框
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(30.0f);
                text->setPosition(osg::Vec3(100.0f,450.0f,0.0f));
                //设置边框对齐绘制
                text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX | osgText::Text::ALIGNMENT);
                text->setText("text->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX | osgText::Text::ALIGNMENT);");
                geode->addDrawable(text.get());
        }

        //-----------------------------------------------//-----------------------------------------------
        //设置颜色倾斜模式
        {
                osg::ref_ptr<osgText::Text> text = new osgText::Text;
                text->setFont(font.get());
                text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
                text->setCharacterSize(40.0f);
                text->setPosition(osg::Vec3(100.0f,250.0f,0.0f));
                //设置颜色倾斜模式为PER_CHARACTER
                text->setColorGradientMode(osgText::Text::PER_CHARACTER);
                //设置倾斜四个角落的颜色
                text->setColorGradientCorners(osg::Vec4(1.0f,0.0f,0.0f,1.0f),osg::Vec4(0.0f,1.0f,0.0f,1.0f),
                        osg::Vec4(0.0f,0.0f,1.0f,1.0f),osg::Vec4(1.0f,1.0f,1.0f,1.0f));
                text->setText("text->setColorGradientMode(osgText::Text::SOLID);");
                geode->addDrawable(text.get());
        }
        camera->addChild(geode.get());
        return camera.get() ;
}

int main()
{
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
        //创建各种文字效果
        osg::ref_ptr<osg::Camera> camera = createAllKindText();
        //添加到根节点
        osg::ref_ptr<osg::Group> root = new osg::Group() ;
        root->addChild(createAllKindText());
        //优化场景数据
        osgUtil::Optimizer optimizer ;
        optimizer.optimize(root.get()) ;
        viewer->setSceneData(root.get());
        viewer->realize();
        viewer->run();
        return 0 ;
}


有几点不明白,创建一个相机,是不是必须做如下几点操作:
//设置投影矩阵,设置投影矩阵有什么作用,我也大概知道是三维转二维,但是如果我自己写代码,我不会条件反射的一下子想到加上这行代码
        camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,800));
//设置视图矩阵  ,设置视图矩阵的作用?
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setViewMatrix(osg::Matrix::identity());
//清除深度缓存
        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表