OSG天空图代码

server/2024/9/22 13:51:32/

osgEarth

    // 创建天空选项osgEarth::Util::SkyOptions skyOptions;// 设置天空的坐标系统(可选)skyOptions.coordinateSystem() = osgEarth::Util::SkyOptions::COORDSYS_ECEF;// 设置一天中的小时数(可选)skyOptions.hours() = 12.0f; // 中午// 设置环境光级别(可选)skyOptions.ambient() = 0.5f; // 50%的环境光// 使用选项创建天空节点osg::ref_ptr<osgEarth::Util::SkyNode> skyNode = osgEarth::Util::SkyNode::create(skyOptions);// 设置天空节点的日期和时间osgEarth::DateTime dateTime(2024, 6, 5, 12); // 2024年6月5日中午skyNode->setDateTime(dateTime);osgEarth::Util::Ephemeris* ephemeris = new osgEarth::Util::Ephemeris;skyNode->setEphemeris(ephemeris);// 设置天空节点的参考点(对于投影地图)osgEarth::GeoPoint refPoint(getMapNode()->getMapSRS(), 0, 0, 0); // 地球中心skyNode->setReferencePoint(refPoint);skyNode->setLighting(true);_root->addChild(skyNode);skyNode->attach(_viewer,0);//运行了 osgEarth 中的着色器生成器,通过对 _root 中的节点进行分析和处理,生成对应的着色器代码。着色器生成器根据节点的材质、光照、纹理等属性,自动生成相应的顶点着色器和片段着色器代码,以实现渲染效果。osgEarth::Registry::shaderGenerator().run(_root.get());
//    对根节点 _root 及其子节点中的所有状态集合进行优化。这个优化过程尝试将相同的状态合并为一个,以减少OpenGL状态切换次数,从而提高渲染性能。StateSetCache *cache = osgEarth::Registry::stateSetCache();cache->optimize(_root.get());// 添加场景数据_viewer->setSceneData(_root.get());``````cpp
class MoveEarthySkyWithEyePointTransform : public osg::Transform {
public:virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const override {osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);if (cv) {osg::Vec3 eyePointLocal = cv->getEyeLocal();matrix.preMult(osg::Matrix::translate(eyePointLocal));}return true;}virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const override {osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);if (cv) {osg::Vec3 eyePointLocal = cv->getEyeLocal();matrix.postMult(osg::Matrix::translate(-eyePointLocal));}return true;}
};// Update texture matrix for cubemaps
struct TexMatCallback : public osg::NodeCallback {
public:TexMatCallback(osg::TexMat& tm) : _texMat(tm) {}virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) override {osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);if (cv) {const osg::Matrix& MV = *(cv->getModelViewMatrix());const osg::Matrix R = osg::Matrix::rotate(osg::DegreesToRadians(112.0f), 0.0f, 0.0f, 1.0f) *osg::Matrix::rotate(osg::DegreesToRadians(90.0f), 1.0f, 0.0f, 0.0f);osg::Quat q = MV.getRotate();const osg::Matrix C = osg::Matrix::rotate(q.inverse());_texMat.setMatrix(C * R);}traverse(node, nv);}osg::TexMat& _texMat;
};class CSkyLight {
public:osg::TextureCubeMap* readCubeMap(const std::string& path) {osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;osg::Image* images[6];
//        std::string faces[6] = {"front3", "back3", "right3", "left3", "bottom3", "top3"};
//        std::string faces[6] = {"front3", "back3", "top3", "bottom3", "left3", "right3"};std::string faces[6] = {"front3", "back3", "bottom3", "top3", "left3", "right3"};osg::TextureCubeMap::Face faceEnums[6] = {osg::TextureCubeMap::POSITIVE_X,osg::TextureCubeMap::NEGATIVE_X,osg::TextureCubeMap::POSITIVE_Y,osg::TextureCubeMap::NEGATIVE_Y,osg::TextureCubeMap::POSITIVE_Z,osg::TextureCubeMap::NEGATIVE_Z};for (int i = 0; i < 6; ++i) {images[i] = osgDB::readImageFile(path + faces[i] + ".png");if (!images[i]) return nullptr;cubemap->setImage(faceEnums[i], images[i]);}cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);cubemap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);cubemap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);return cubemap;}osg::Node* createSkyBox(const std::string& path) {osg::StateSet* stateset = new osg::StateSet();osg::TexEnv* te = new osg::TexEnv;te->setMode(osg::TexEnv::REPLACE);stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON);osg::TexGen* tg = new osg::TexGen;tg->setMode(osg::TexGen::NORMAL_MAP);stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON);osg::TexMat* tm = new osg::TexMat;stateset->setTextureAttribute(0, tm);osg::TextureCubeMap* skymap = readCubeMap(path);stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON);stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);stateset->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);osg::Depth* depth = new osg::Depth;depth->setFunction(osg::Depth::ALWAYS);depth->setRange(1.0, 1.0);stateset->setAttributeAndModes(depth, osg::StateAttribute::ON);stateset->setRenderBinDetails(-1, "RenderBin");osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 1));osg::Geode* geode = new osg::Geode;geode->setCullingActive(false);geode->setStateSet(stateset);geode->addDrawable(drawable);osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;transform->setCullingActive(false);transform->addChild(geode);osg::ClearNode* clearNode = new osg::ClearNode;clearNode->setCullCallback(new TexMatCallback(*tm));clearNode->addChild(transform);return clearNode;}
};
int main() {osgViewer::Viewer viewer;viewer.setUpViewInWindow(100, 100, 800, 600);osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("./osgb/Tractor.OSGB");if (!model){std::cout << "无法加载模型" << std::endl;return 1;}osg::ref_ptr<osg::Group> geode = new osg::Group;geode->addChild(model);CSkyLight c;geode->addChild(c.createSkyBox("./osgb/"));viewer.setSceneData(geode);

在这里插入图片描述


http://www.ppmy.cn/server/48050.html

相关文章

计算机毕业设计Python+Spark知识图谱课程推荐系统 课程预测系统 课程大数据 课程数据分析 课程大屏 mooc慕课推荐系统 大数据毕业设计

毕业设计&#xff08;论文&#xff09;开题报告表 课题名称 基于深度强化学习的课程推荐系统设计与实现 课题来源 自选项目 课题类型 工程设计 指导教师 赵彦锋 学生姓名 学 号 专 业 软件工程 一、课题意义 推荐系统是信息时代中解决信息过载问题的一种…

flutter 适配屏幕宽高工具

使用的是flutter插件flutter_screenutil flutter pub add flutter_screenutil 使用 void main() > runApp(MyApp());class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {//填入设计稿中设备的屏幕尺寸,单位dpreturn ScreenUtilInit(desi…

NoSQL实战(MongoDB搭建主从复制)

什么是复制集&#xff1f; MongoDB复制是将数据同步到多个服务器的过程&#xff1b; 复制集提供了数据的冗余备份并提高了数据的可用性&#xff0c;通常可以保证数据的安全性&#xff1b; 复制集还允许您从硬件故障和服务中断中恢复数据。 保障数据的安全性 数据高可用性 (2…

LlamaFactory-Ollama-Langchain大模型训练-部署一条龙

前言 近些日子&#xff0c;大模型火的一塌糊涂&#xff0c;那么现在就有义务要学习一套好用的、从 dataset --> train --> deploy 的一整套流程&#xff0c;好拿来装逼。话不多说&#xff0c;进入正题 Train 框架 目前好用的框架太多&#xff0c;如BELLE, ChatGLM等&a…

大模型日报2024-06-05

大模型日报 2024-06-05 大模型资讯 AI气象预测取得重大进展&#xff1a;单台桌面电脑即可运行全球天气模型 摘要: 一项新的人工智能天气预测模型已经取得重大进展&#xff0c;该模型能够在一台普通的桌面电脑上运行&#xff0c;预测全球天气。这意味着即使没有复杂的物理计算&a…

[Mac软件]Leech for Mac v3.2 - 轻量级mac下载工具

黑果魏叔推荐Leech是由Many Tricks开发的适用于Mac OS X的轻量级且功能强大的下载管理器。 Leech让您完全控制下载&#xff0c;并与浏览器完全集成。您可以将下载排队&#xff0c;暂停和恢复&#xff0c;从受密码保护的服务器下载&#xff0c;并将密码存储在系统范围的安全钥匙…

【Mac版】Java生成二维码

软件版本 IntelliJ IDEA&#xff1a;2023.2 JDK&#xff1a;17 Tomcat&#xff1a;10.1.11 Maven&#xff1a;3.9.3 技术栈 servlet谷歌的&#xff1a;zxing 生成普通的黑白二维码在二维码中间添加一个小图标 github开源项目&#xff1a;qrcode qrcode开源项目的内部是基于z…

【conda】解决conda activate无效的问题

conda activate 虚拟环境名字执行上述命令失效&#xff0c;提示如下信息&#xff1a; 经查询&#xff0c;原因是新版的 conda 不再使用 activate 来激活虚拟环境&#xff0c;而是改用&#xff1a; source activate 虚拟环境名退出虚拟环境还是&#xff1a; conda deactivate…