查看: 1589|回复: 2

outline 与 天空盒 同时使用 outline 显示不全 求解

[复制链接]

该用户从未签到

发表于 2019-1-29 17:21:08 | 显示全部楼层 |阅读模式
本帖最后由 koxter 于 2019-1-29 17:24 编辑

将例子 Examples osgvertexprogram 中的天空盒 与 OsgFx::outline 同时使用
随着相机旋转 outline不显示 或 变的显示不全
求解

开启天空盒

开启天空盒

不开天空盒

不开天空盒

该用户从未签到

 楼主| 发表于 2019-1-29 17:22:20 | 显示全部楼层
本帖最后由 koxter 于 2019-1-29 17:26 编辑

二楼是代码
  1. #include <osgViewer/Viewer>
  2. #include <osg/Group>
  3. #include <osg/Geode>
  4. #include <osgGA/GUIEventHandler>
  5. #include <osgFX/Outline>
  6. #include <osg/ShapeDrawable>
  7. #include <osg/Stencil>
  8. #include <osg/CullFace>
  9. #include <osg/PolygonMode>
  10. #include <osg/LineWidth>
  11. #include <osg/Material>
  12. #include <osgFX/Outline>
  13. #include <osgFX/BumpMapping>
  14. #include <osg/Depth>
  15. #include <osg/CullFace>
  16. #include <osg/PolygonOffset>
  17. #include <osg/PolygonMode>
  18. #include <osg/Material>
  19. #include <osgDB/ReadFile>
  20. #include <osgGA/OrbitManipulator>
  21. #include <osg/TexMat>
  22. #include <osg/TexGen>
  23. #include <osg/TexEnv>
  24. #include <osg/TexEnvCombine>
  25. #include <osg/TextureCubeMap>



  26. osg::TextureCubeMap* readCubeMap()
  27. {
  28.         osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;
  29.         //#define CUBEMAP_FILENAME(face) "nvlobby_" #face ".png"
  30.         //#define CUBEMAP_FILENAME(face) "Cubemap_axis/" #face ".png"
  31. #define CUBEMAP_FILENAME(face) "Cubemap_snow/" #face ".jpg"

  32.         osg::Image* imagePosX = osgDB::readImageFile(CUBEMAP_FILENAME(posx));
  33.         osg::Image* imageNegX = osgDB::readImageFile(CUBEMAP_FILENAME(negx));
  34.         osg::Image* imagePosY = osgDB::readImageFile(CUBEMAP_FILENAME(posy));
  35.         osg::Image* imageNegY = osgDB::readImageFile(CUBEMAP_FILENAME(negy));
  36.         osg::Image* imagePosZ = osgDB::readImageFile(CUBEMAP_FILENAME(posz));
  37.         osg::Image* imageNegZ = osgDB::readImageFile(CUBEMAP_FILENAME(negz));

  38.         if (imagePosX && imageNegX && imagePosY && imageNegY && imagePosZ && imageNegZ)
  39.         {
  40.                 cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
  41.                 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
  42.                 cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
  43.                 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
  44.                 cubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
  45.                 cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);

  46.                 cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
  47.                 cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
  48.                 cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

  49.                 cubemap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
  50.                 cubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
  51.         }

  52.         return cubemap;
  53. }


  54. // Update texture matrix for cubemaps
  55. struct TexMatCallback : public osg::NodeCallback
  56. {
  57. public:

  58.         TexMatCallback(osg::TexMat& tm) :
  59.                 _texMat(tm)
  60.         {
  61.         }

  62.         virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
  63.         {
  64.                 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
  65.                 if (cv)
  66.                 {
  67.                         const osg::Matrix& MV = *(cv->getModelViewMatrix());
  68.                         const osg::Matrix R = osg::Matrix::rotate(osg::DegreesToRadians(112.0f), 0.0f, 0.0f, 1.0f)*
  69.                                 osg::Matrix::rotate(osg::DegreesToRadians(90.0f), 1.0f, 0.0f, 0.0f);

  70.                         osg::Quat q = MV.getRotate();
  71.                         const osg::Matrix C = osg::Matrix::rotate(q.inverse());

  72.                         _texMat.setMatrix(C*R);
  73.                 }

  74.                 traverse(node, nv);
  75.         }

  76.         osg::TexMat& _texMat;
  77. };


  78. class MoveEarthySkyWithEyePointTransform : public osg::Transform
  79. {
  80. public:
  81.         /** Get the transformation matrix which moves from local coords to world coords.*/
  82.         virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const
  83.         {
  84.                 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
  85.                 if (cv)
  86.                 {
  87.                         osg::Vec3 eyePointLocal = cv->getEyeLocal();
  88.                         matrix.preMultTranslate(eyePointLocal);
  89.                 }
  90.                 return true;
  91.         }

  92.         /** Get the transformation matrix which moves from world coords to local coords.*/
  93.         virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const
  94.         {
  95.                 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
  96.                 if (cv)
  97.                 {
  98.                         osg::Vec3 eyePointLocal = cv->getEyeLocal();
  99.                         matrix.postMultTranslate(-eyePointLocal);
  100.                 }
  101.                 return true;
  102.         }
  103. };


  104. osg::Node* createSkyBox()
  105. {

  106.         osg::StateSet* stateset = new osg::StateSet();

  107.         osg::TexEnv* te = new osg::TexEnv;
  108.         te->setMode(osg::TexEnv::REPLACE);
  109.         stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON);

  110.         osg::TexGen *tg = new osg::TexGen;
  111.         tg->setMode(osg::TexGen::NORMAL_MAP);
  112.         stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON);

  113.         osg::TexMat *tm = new osg::TexMat;
  114.         stateset->setTextureAttribute(0, tm);

  115.         osg::TextureCubeMap* skymap = readCubeMap();
  116.         stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON);

  117.         stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
  118.         stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);

  119.         // clear the depth to the far plane.
  120.         osg::Depth* depth = new osg::Depth;
  121.         depth->setFunction(osg::Depth::ALWAYS);
  122.         depth->setRange(1.0, 1.0);
  123.         stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);

  124.         stateset->setRenderBinDetails(-1, "RenderBin");

  125.         osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 1));

  126.         osg::Geode* geode = new osg::Geode;
  127.         geode->setCullingActive(false);
  128.         geode->setStateSet(stateset);
  129.         geode->addDrawable(drawable);


  130.         osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;
  131.         transform->setCullingActive(false);
  132.         transform->addChild(geode);

  133.         osg::ClearNode* clearNode = new osg::ClearNode;
  134.         //clearNode->setRequiresClear(false);
  135.         clearNode->setCullCallback(new TexMatCallback(*tm));
  136.         clearNode->addChild(transform);

  137.         return clearNode;
  138. }


  139. int main(int argc, char **argv)
  140. {

  141.         // must have stencil buffer...
  142.         osg::DisplaySettings::instance()->setMinimumNumStencilBits(1);

  143.         // construct the viewer
  144.         osgViewer::Viewer viewer;
  145.         viewer.setUpViewInWindow(100, 100, 800, 600);


  146.         osg::ref_ptr<osg::Group> root = new osg::Group;

  147.         // read the scene from the list of file specified commandline args.
  148.         osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osgt");
  149.         osgFX::Outline* pOutLine = new osgFX::Outline;
  150.         pOutLine->setName("OutLineEffect");
  151.         pOutLine->setColor(osg::Vec4(1.0, 0.0f, 0.0f, 1.0f));
  152.         pOutLine->setWidth(10.0f);
  153.         pOutLine->addChild(loadedModel);
  154.         root->addChild(pOutLine);
  155.         root->addChild(createSkyBox());

  156.         //////////////////////////////////////////////////////////////////////////

  157.         // construct the viewer
  158.         viewer.setSceneData(root.get());

  159.         // must clear stencil buffer...
  160.         unsigned int clearMask = viewer.getCamera()->getClearMask();
  161.         viewer.getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  162.         return viewer.run();

  163. }
复制代码

该用户从未签到

 楼主| 发表于 2019-1-29 18:43:00 | 显示全部楼层
找到原因了, 是天空盒ClearNode节点的原因
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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