查看: 1120|回复: 0

shader渲染俩个字体全是方块格子呢???

[复制链接]

该用户从未签到

发表于 2014-11-30 16:27:11 | 显示全部楼层 |阅读模式
  1. // num30_r.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include "stdafx.h"
  5. #include <osg/Geode>
  6. #include <osg/Geometry>
  7. #include <osgText/Text>
  8. #include <osgViewer/Viewer>
  9. #include <locale.h>
  10. #include <osgDB/ReadFile>
  11. #include <osg/Shader>  
  12. #include <osg/LineWidth>
  13. #include <osg/StateSet>
  14. #include <osgDB/ConvertUTF>
  15. #include <osg/Camera>
  16. #include <osg/MatrixTransform>
  17. #pragma comment(lib,"OpenThreadsd.lib")
  18. #pragma comment(lib,"osgd.lib")
  19. #pragma comment(lib,"osgDBd.lib")
  20. #pragma comment(lib,"osgAPExd.lib")
  21. #pragma comment(lib,"osgGAd.lib")
  22. #pragma comment(lib,"osgUtild.lib")
  23. #pragma comment(lib,"osgTextd.lib")
  24. #pragma comment(lib,"osgSimd.lib")
  25. #pragma comment(lib,"osgViewerd.lib")

  26. static const char vertSource10[] =
  27. "varying vec2 texcoord;             \n"
  28. "void main()                                                \n"
  29. "{                                                                        \n"
  30. "        texcoord = gl_MultiTexCoord0.st ;\n"
  31. "        gl_Position = ftransform();            \n"
  32. "}                                                                        \n";



  33. static  const char fragSource10[] =
  34. "uniform  vec4 color;                      \n"
  35. "varying  vec2 texcoord;                  \n"
  36. "uniform sampler2D sampler0;                                       \n"
  37. "void main() {                                    \n"
  38. "  gl_FragColor = color;  \n"
  39. "}                                              \n";

  40.                                     

  41. osg::ref_ptr<osg::Node> createHUD()
  42. {
  43.         //文字
  44.         osgText::Text* text = new osgText::Text;
  45.         //设置字体
  46.         std::string caiyun("simhei.ttf");//此处设置的是汉字字体
  47.         text->setFont(caiyun);

  48.         //设置文字显示的位置
  49.         osg::Vec3 position(0.5f,63.5f,0.0f);
  50.         text->setPosition(position);   
  51.         text->setText(L"osg中国官网网站");//设置显示的文字
  52.         //几何体节点
  53.         osg::Geode* geode = new osg::Geode();
  54.         geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中

  55.         osg::Drawable* geom3 = geode->getDrawable(0);
  56.         osg::StateSet * ss3 = geom3->getOrCreateStateSet();//->getStateSet();
  57.         osg::Program * program3 = new osg::Program;
  58.         program3->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource10));
  59.         program3->addShader(new osg::Shader(osg::Shader::VERTEX,vertSource10));
  60.         osg::ref_ptr<osg::Uniform> textColor = new osg::Uniform("color",osg::Vec4(1,0,0,1));
  61.         osg::ref_ptr<osg::Uniform> textTexture = new osg::Uniform("sampler0",0);
  62.         ss3->addUniform(textColor);
  63.         ss3->addUniform(textTexture);
  64.         ss3->setAttribute(program3);

  65.         //文字
  66.         osgText::Text* text2 = new osgText::Text;
  67.         //设置字体
  68.         std::string caiyun2("simhei.ttf");//此处设置的是汉字字体
  69.         text2->setFont(caiyun2);

  70.         //设置文字显示的位置
  71.         osg::Vec3 position2(-0.5f,-63.5f,0.0f);
  72.         text2->setPosition(position2);   
  73.         text2->setText(L"外国官网网站");//设置显示的文字
  74.         //几何体节点
  75.         //osg::Geode* geode2 = new osg::Geode();
  76.         geode->addDrawable( text2 );//将文字Text作这drawable加入到Geode节点中

  77.         osg::Drawable* geom4 = geode->getDrawable(1);
  78.         osg::StateSet * ss4 = geom4->getOrCreateStateSet();//->getStateSet();
  79.         osg::Program * program4 = new osg::Program;
  80.         program4->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource10));
  81.         program4->addShader(new osg::Shader(osg::Shader::VERTEX,vertSource10));
  82.         osg::ref_ptr<osg::Uniform> textColor2 = new osg::Uniform("color",osg::Vec4(1,1,0,1));
  83.         osg::ref_ptr<osg::Uniform> textTexture2 = new osg::Uniform("sampler0",0);
  84.         ss4->addUniform(textColor);
  85.         ss4->addUniform(textTexture);
  86.         ss4->setAttribute(program4);

  87.        

  88.         osg::ref_ptr<osg::MatrixTransform> pMT = new osg::MatrixTransform;
  89.         osg::Matrixf m;
  90.         m.setRotate(osg::Quat(90*osg::PI/180,osg::Vec3(1,0,0)));
  91.         pMT->setMatrix(m);
  92.         pMT->addChild(geode);
  93.         return pMT;
  94. };



  95. int main( int argc, char **argv){
  96.         osgViewer::Viewer viewer;
  97.         osg::ref_ptr<osg::Group> root= new osg::Group;
  98.         root->addChild(createHUD());
  99.         viewer.setSceneData(root.get());
  100.         viewer.realize();
  101.         viewer.run();  
  102.         return 0;
  103. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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