查看: 2203|回复: 3

osg color颜色值 与 windows 颜色编辑对话框不同

[复制链接]

该用户从未签到

发表于 2014-7-16 11:37:49 | 显示全部楼层 |阅读模式
本帖最后由 rubby 于 2014-7-16 13:55 编辑

各位大哥:

我在编写程序的时候发现:

osg color颜色值 与 windows 颜色编辑对话框明显不同。

比如:
在windows 颜色编辑对话框中,RGB值分别是 255,128,0 的颜色如附件(windows颜色对话框.jpg)
中所示。

而osg中osg::vec4 color(255,128,0,255) 颜色 如附件 (osg color.jpg)所示,两者颜色明显不一样。

两个颜色:一个是桔红色,一个是偏黄色。

我的测试程序如下,在osg中将颜色设置为 osg::vec4 color(255,128,0,255) :
  1. #include <osgViewer/ViewerEventHandlers>
  2. #include <osgGA/StateSetManipulator>
  3. #include<osgDB/ReadFile>
  4. #include <osgDB/Registry>
  5. #include <osgDB/WriteFile>
  6. #include <osg/MatrixTransform>
  7. #include <osg/StateSet>
  8. #include <osg/StateAttribute>
  9. #include <osg/ShadeModel>
  10. #include<osg/CullFace>
  11. #include <osg/PolygonMode>
  12. #include <osg/LineWidth>
  13. #include <osg/Material>
  14. #include <osg/Light>
  15. #include <osg/NodeCallback>
  16. #include <osg/LightSource>
  17. #include <osg/Notify>
  18. #include <osg/CullFace>
  19. #include <osg/Point>
  20. #include<osgText/Font>
  21. #include <osgText/Text>
  22. #include <osg/AlphaFunc>
  23. #include<osg/BlendFunc>
  24. #include<osg/Texture>
  25. #include <osgutil/SmoothingVisitor>
  26. #include <osgViewer/CompositeViewer>
  27. #include <osg/ImageSequence>
  28. #include <osg/PositionAttitudeTransform>
  29. #include <osg/AutoTransform>
  30. #include <osg/LOD>
  31. #include <osg/DrawPixels>

  32. void setupTextPro2(osgText::Text &textObject, osgText::Font* font, float size, const osg::Vec3& vec3){
  33.         textObject.setFont(font);
  34.         textObject.setCharacterSize(size);
  35.         textObject.setPosition(vec3);
  36.         textObject.setColor(osg::Vec4(0.0,0.0,1.0,1.0));
  37.         textObject.setAlignment(osgText::Text::CENTER_BOTTOM);
  38.         textObject.setAxisAlignment(osgText::Text::XZ_PLANE);
  39. }

  40. void createText2(osgText::Text&textObject, const char* string){
  41.         int requiredSize = mbstowcs(NULL,string,0);
  42.         wchar_t* wtext = new wchar_t[requiredSize+1];

  43.         mbstowcs(wtext,string,requiredSize+1);
  44.         textObject.setText(wtext);
  45.         delete wtext;
  46. }

  47. /************************************************************************/
  48. /* 测试RGB颜色在OSG中的显示                                                                     */
  49. /************************************************************************/
  50. int main(){
  51.         setlocale(LC_ALL,".936");
  52.         const char* titleString ="Test Color";
  53.         const char* textString ={"3D"};

  54.         osgText::Font* fontHei= osgText::readFontFile("Fonts/simhei.ttf");
  55.         osgText::Font* fontKai =  osgText::readFontFile("Fonts/simkai.ttf");

  56.         osg::ref_ptr<osgText::Text> title = new osgText::Text;
  57.         setupTextPro2(*title, fontHei, 20.0f, osg::Vec3(0.0,0.0,0.0));
  58.         createText2(*title,titleString);

  59.         osg::ref_ptr<osgText::Text> text = new osgText::Text;
  60.         setupTextPro2(*text, fontKai, 15.0f, osg::Vec3(0.0,0.0,-80.0));
  61.         createText2(*text,textString);

  62.         osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  63.         osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-150.0,0.0,-100.0), //左下角,不是corner
  64.                 osg::Vec3(300.0,0.0,0.0),
  65.                 osg::Vec3(0.0,0.0,200.0), 1.0,1.0);

  66.         osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
  67.         color->push_back(osg::Vec4(255,128,0,255)); //=========================此处为我设置的颜色
  68.         geom->setColorArray(color.get());
  69.         geom->setColorBinding(osg::Geometry::BIND_OVERALL);

  70.         geode->addDrawable(geom);

  71.         geode->addDrawable(title.get());
  72.         geode->addDrawable(text.get()); //不增加这个,否则导致
  73.         //无光照
  74.         osg::StateSet* state = geode->getOrCreateStateSet();
  75.         state->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
  76.         state->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//关闭深度测试

  77.         //打开GL_BLEND混合模式(以保证Alpha纹理正确)
  78.         state->setMode(GL_BLEND,osg::StateAttribute::ON);

  79.        
  80.         osgViewer::Viewer viewer;
  81.         viewer.addEventHandler(new osgViewer::WindowSizeHandler);
  82.         viewer.setSceneData(geode.get());
  83.         return viewer.run();
  84. }
复制代码



哪位大哥帮我看下,这是什么原因,用什么方式可以是他们保持一致呢?

osg color.jpg
windows颜色对话框.jpg

该用户从未签到

发表于 2014-7-16 15:15:59 | 显示全部楼层
混合确实会有影响,忽然发现楼主的RGB竟然是255、128,狂汗
害人不浅
float传入rgba请确保范围在0~1(255 / 255.f,128 / 255.f,0)

该用户从未签到

 楼主| 发表于 2014-7-17 09:22:26 | 显示全部楼层
本帖最后由 rubby 于 2014-7-17 15:59 编辑
gis_wudi 发表于 2014-7-16 15:15
混合确实会有影响,忽然发现楼主的RGB竟然是255、128,狂汗
害人不浅
float传入rgba请确保范围在0~1(25 ...


多谢 gis_wudi. 果真如你所说。

再请教一个问题:

如果我将设置颜色数组该改为:

        osg::ref_ptr<osg::Vec4ubArray> color = new osg::Vec4ubArray;
        color->push_back(osg::Vec4ub(255,255,0,255));

颜色仍然不对。

1. 是 255 这种值必须用 unsigned byte 表示吗? 请问后缀应该是什么。
比如float类型,我知道可以表示为 255.0f. 255的后缀应该是什么? 告诉编译器这是unsigned byte.

2.
是否color 必须设置为0.0~1.0 之间。
0~255这种表示方式是不可以的?


多谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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