查看: 1755|回复: 1

求助,osgearth添加了星空背景和光照,在加入模型之后出现地球变黑问题

[复制链接]
  • TA的每日心情
    奋斗
    2019-11-7 20:26
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2020-9-24 21:36:18 | 显示全部楼层 |阅读模式
    osgearth添加了星空背景和光照,在加入模型之后,视角移动到某些位置会出现地球消失的问题
    代码如下,求大佬指点
    #include "stdafx.h"

    int main()
    {
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> root = new osg::Group;

    osg::ref_ptr<osg::Node> earthNode = osgDB::readNodeFile("gdal_tiff.earth");
    osg::ref_ptr<osgEarth::MapNode> mapNode = osgEarth::MapNode::findMapNode(earthNode);
    osg::ref_ptr<osgEarth::Util::EarthManipulator> em = new osgEarth::Util::EarthManipulator;

    em->getSettings()->setArcViewpointTransitions(true);
    viewer->setCameraManipulator(em);
    //root->addChild(mapNode); //同时加载mapNode和m_pSkyNode会出现瓦片闪烁
    if(mapNode.valid()) em->setNode(mapNode);

    //设置星空,添加光照
    {
      osgEarth::Config skyConf;
      double hours = skyConf.value("hours", 6.0);
      osgEarth:ateTime dateTime(2020, 8, 29, hours);
      osgEarth::Util::Ephemeris* ephemeris = new osgEarth::Util::Ephemeris;
      osgEarth::Util::SkyNode* m_pSkyNode = osgEarth::Util::SkyNode::create(mapNode);
      m_pSkyNode->setName("SkyNode");
      m_pSkyNode->setEphemeris(ephemeris);
      m_pSkyNode->setDateTime(dateTime);
      m_pSkyNode->attach(viewer, 1);
      m_pSkyNode->setLighting(true);
      m_pSkyNode->addChild(mapNode);
      root->addChild(m_pSkyNode);
    }

    //添加一个模型
    {
      const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
      osg::Node* model = osgDB::readNodeFile("cow.osg");
      model->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
      osg::Matrix Lmatrix;
      geoSRS->getEllipsoid()->computeLocalToWorldTransformFromLatLongHeight(
       osg::DegreesToRadians(40.0),
       osg::DegreesToRadians(116.0),
       100000.0, Lmatrix);
      Lmatrix.preMult(osg::Matrix::scale(osg::Vec3(10000, 10000, 10000)));
      osg::MatrixTransform* mt = new osg::MatrixTransform;
      mt->setMatrix(Lmatrix);
      mt->addChild(model);
      root->addChild(mt);
    }

    viewer->setSceneData(root.get());
    viewer->run();
    return 0;
    }


    /*************************************/
    // stdafx.h : 标准系统包含文件的包含文件,
    // 或是经常使用但不常更改的
    // 特定于项目的包含文件
    //

    #pragma once


    #include <stdio.h>
    #include <tchar.h>

    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <time.h>
    #include <Windows.h>

    #include <assert.h>


    // TODO: 在此处引用程序需要的其他头文件
    #include <osgViewer/Viewer>
    #include <osgViewer/CompositeViewer>
    #include <osg/Node>   
    #include <osg/Geode>   
    #include <osg/Group>
    #include <osg/MatrixTransform>
    #include <osg/CoordinateSystemNode>
    #include <osg/ComputeBoundsVisitor>

    #include <osgDB/ReadFile>
    #include <osgDB/writefile>

    #include <osgGA/CameraManipulator>
    #include <osgGA/GUIEventHandler>
    #include <osgGA/KeySwitchMatrixManipulator>
    #include <osgGA/StateSetManipulator>

    #include <osgUtil/LineSegmentIntersector>
    #include <osgUtil/optimizer>

    #include <osgEarth/Utils>
    #include <osgEarth/MapNode>
    #include <osgEarth/Viewpoint>
    #include <osgEarth/GeoData>
    #include <osgEarth/ImageLayer>
    #include <osgEarthUtil/Controls>
    #include <osgEarthAnnotation/PlaceNode>
    #include <osgEarthUtil/EarthManipulator>
    #include <osgEarthUtil/AutoClipPlaneHandler>        //解决裁剪面问题
    //#include <osgEarthUtil/SkyNode>        包含会出错,“osgEarth::Util::SkyNode”:“class”类型重定义,osgEarthUtil/Sky和osgEarthUtil/SkyNode
    #include <osgEarthUtil/ExampleResources>
    #include <osgEarthDrivers/model_simple/SimpleModelOptions>

    using namespace std;
    using namespace osgEarth;
    using namespace osgEarth::Util;

    该用户从未签到

    发表于 2023-9-18 10:14:49 | 显示全部楼层
    您好我遇到同样的问题,请问您解决了吗?
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

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

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

    联系我们

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