|
- // num30_r.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "stdafx.h"
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osgText/Text>
- #include <osgViewer/Viewer>
- #include <locale.h>
- #include <osgDB/ReadFile>
- #include <osg/Shader>
- #include <osg/LineWidth>
- #include <osg/StateSet>
- #include <osgDB/ConvertUTF>
- #include <osg/Camera>
- #include <osg/MatrixTransform>
- #pragma comment(lib,"OpenThreadsd.lib")
- #pragma comment(lib,"osgd.lib")
- #pragma comment(lib,"osgDBd.lib")
- #pragma comment(lib,"osgAPExd.lib")
- #pragma comment(lib,"osgGAd.lib")
- #pragma comment(lib,"osgUtild.lib")
- #pragma comment(lib,"osgTextd.lib")
- #pragma comment(lib,"osgSimd.lib")
- #pragma comment(lib,"osgViewerd.lib")
- static const char vertSource10[] =
- "varying vec2 texcoord; \n"
- "void main() \n"
- "{ \n"
- " texcoord = gl_MultiTexCoord0.st ;\n"
- " gl_Position = ftransform(); \n"
- "} \n";
- static const char fragSource10[] =
- "uniform vec4 color; \n"
- "varying vec2 texcoord; \n"
- "uniform sampler2D sampler0; \n"
- "void main() { \n"
- " gl_FragColor = color; \n"
- "} \n";
-
- osg::ref_ptr<osg::Node> createHUD()
- {
- //文字
- osgText::Text* text = new osgText::Text;
- //设置字体
- std::string caiyun("simhei.ttf");//此处设置的是汉字字体
- text->setFont(caiyun);
- //设置文字显示的位置
- osg::Vec3 position(0.5f,63.5f,0.0f);
- text->setPosition(position);
- text->setText(L"osg中国官网网站");//设置显示的文字
- //几何体节点
- osg::Geode* geode = new osg::Geode();
- geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中
- osg::Drawable* geom3 = geode->getDrawable(0);
- osg::StateSet * ss3 = geom3->getOrCreateStateSet();//->getStateSet();
- osg::Program * program3 = new osg::Program;
- program3->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource10));
- program3->addShader(new osg::Shader(osg::Shader::VERTEX,vertSource10));
- osg::ref_ptr<osg::Uniform> textColor = new osg::Uniform("color",osg::Vec4(1,0,0,1));
- osg::ref_ptr<osg::Uniform> textTexture = new osg::Uniform("sampler0",0);
- ss3->addUniform(textColor);
- ss3->addUniform(textTexture);
- ss3->setAttribute(program3);
- //文字
- osgText::Text* text2 = new osgText::Text;
- //设置字体
- std::string caiyun2("simhei.ttf");//此处设置的是汉字字体
- text2->setFont(caiyun2);
- //设置文字显示的位置
- osg::Vec3 position2(-0.5f,-63.5f,0.0f);
- text2->setPosition(position2);
- text2->setText(L"外国官网网站");//设置显示的文字
- //几何体节点
- //osg::Geode* geode2 = new osg::Geode();
- geode->addDrawable( text2 );//将文字Text作这drawable加入到Geode节点中
- osg::Drawable* geom4 = geode->getDrawable(1);
- osg::StateSet * ss4 = geom4->getOrCreateStateSet();//->getStateSet();
- osg::Program * program4 = new osg::Program;
- program4->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource10));
- program4->addShader(new osg::Shader(osg::Shader::VERTEX,vertSource10));
- osg::ref_ptr<osg::Uniform> textColor2 = new osg::Uniform("color",osg::Vec4(1,1,0,1));
- osg::ref_ptr<osg::Uniform> textTexture2 = new osg::Uniform("sampler0",0);
- ss4->addUniform(textColor);
- ss4->addUniform(textTexture);
- ss4->setAttribute(program4);
-
- osg::ref_ptr<osg::MatrixTransform> pMT = new osg::MatrixTransform;
- osg::Matrixf m;
- m.setRotate(osg::Quat(90*osg::PI/180,osg::Vec3(1,0,0)));
- pMT->setMatrix(m);
- pMT->addChild(geode);
- return pMT;
- };
- int main( int argc, char **argv){
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Group> root= new osg::Group;
- root->addChild(createHUD());
- viewer.setSceneData(root.get());
- viewer.realize();
- viewer.run();
- return 0;
- }
复制代码 |
|