|
楼主 |
发表于 2014-11-27 13:04:18
|
显示全部楼层
哥们为什么字体俩个不同shader渲染 会是最后一次shader渲染的颜色呢???
- #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>
- #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 texcoord1; \n"
- "varying vec4 VertexColor; \n"
- "uniform vec4 color; \n"
- "void main() \n"
- "{ \n"
- " VertexColor = color; \n"
- " texcoord1 = gl_MultiTexCoord0.st ;\n"
- " gl_Position = ftransform(); \n"
- "} \n";
- static const char fragSource10[] =
- "varying vec4 VertexColor; \n"
- "uniform vec4 color; \n"
- "varying vec2 texcoord1; \n"
- "uniform sampler2D sampler1; \n"
- "void main() { \n"
- " gl_FragColor = VertexColor *texture2D(sampler1,texcoord1).aaaa; \n"
- "} \n";
- static const char vertSource11[] =
- "varying vec2 texcoord1; \n"
- "void main() \n"
- "{ \n"
- " texcoord1 = gl_MultiTexCoord0.st ;\n"
- " gl_Position = ftransform(); \n"
- "} \n";
- static const char fragSource11[] =
- "varying vec4 VertexColor; \n"
- "uniform vec4 color2; \n"
- "varying vec2 texcoord1; \n"
- "uniform sampler2D sampler1; \n"
- "void main() { \n"
- " gl_FragColor = color2 *texture2D(sampler1,texcoord1).aaaa; \n"
- "} \n";
- osg::Node* createHUD()
- {
- //文字
- osgText::Text* text = new osgText::Text;
- //设置字体
- std::string caiyun("simhei.ttf");//此处设置的是汉字字体
- text->setFont(caiyun);
- //设置文字显示的位置
- osg::Vec3 position(150.0f,500.0f,0.0f);
- text->setPosition(position);
- //text->setColor( osg::Vec4( 1, 0, 0, 1));
- text->setText(L"osg中国官网网站");//设置显示的文字
- osgText::Text* text2 = new osgText::Text;
- //设置字体
- std::string caiyun2("simhei.ttf");//此处设置的是汉字字体
- text2->setFont(caiyun2);
- //设置文字显示的位置
- osg::Vec3 position2(150.0f,300.0f,0.0f);
- text2->setPosition(position2);
- //text->setColor( osg::Vec4( 1, 0, 0, 1));
- text2->setText(L"osg外国官网网站");//设置显示的文字
- //几何体节点
- osg::Geode* geode = new osg::Geode();
- osg::Geode* geode2 = new osg::Geode();
- geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中
- geode2->addDrawable( text2 );//将文字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,1,1));
- osg::ref_ptr<osg::Uniform> textTexture = new osg::Uniform("sampler1",0);
- ss3->addUniform(textColor);
- ss3->addUniform(textTexture);
- ss3->setAttribute(program3);
- osg::Drawable* geom = geode2->getDrawable(0);
- osg::StateSet * ss = geom->getOrCreateStateSet();//->getStateSet();
- osg::Program * program = new osg::Program;
- program->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource11));
- program->addShader(new osg::Shader(osg::Shader::VERTEX,vertSource11));
- osg::ref_ptr<osg::Uniform> textColor2 = new osg::Uniform("color2",osg::Vec4(0,0,1,1));
- osg::ref_ptr<osg::Uniform> textTexture2 = new osg::Uniform("sampler1",0);
- ss->addUniform(textColor2);
- ss->addUniform(textTexture2);
- ss->setAttribute(program);
- //相机
- osg::Camera *hud = new osg::Camera();
- if (hud)
- {
- hud->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::PROTECTED|osg::StateAttribute::OFF);
- hud->setProjectionMatrix(osg::Matrix::ortho2D(0,600,0,600));
- hud->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- hud->setViewMatrix(osg::Matrix::identity());
- hud->setClearMask(GL_DEPTH_BUFFER_BIT);
- hud->setRenderOrder(osg::Camera::POST_RENDER);
- hud->setDataVariance(osg::Object::DYNAMIC);
- hud->setAllowEventFocus(false);
- hud->addChild(geode);
- hud->addChild(geode2);
- }
- return hud;
- };
- int main( int argc, char **argv){
- osgViewer::Viewer viewer;
- //osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("fountain.osg");
- osg::ref_ptr<osg::Group> root= new osg::Group;
- //root->addChild( model.get());//加入某个模型
- root->addChild(createHUD());//把HUD文字的相机加入到根节点下
- viewer.setSceneData( root.get());
- viewer.realize();
- viewer.run() ;
- return 0;
- }
复制代码 |
|