osg之黑夜背景地月系显示

2023-11-09 23:45

本文主要是介绍osg之黑夜背景地月系显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

效果

 代码


效果

 代码


/**
* Lights test. This application is for testing the LightSource support in osgEarth.
* 灯光测试。此应用程序用于测试osgEarth中的光源支持。
*/
#include "stdafx.h"
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarth/Lighting>
#include <osgEarth/PhongLightingEffect>
#include <osgEarth/NodeUtils>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthUtil/Ephemeris>
#include <osgEarthUtil/Shadowing>#define LC "[lights] "using namespace osgEarth;
using namespace osgEarth::Util;int
usage(const char* name)
{OE_NOTICE<< "\nUsage: " << name << " file.earth" << std::endl<< MapNodeHelper().usage() << std::endl;return 0;
}// converts a double-precision Vec3d to an equivalent single-precision Vec4f position
// as needed for light positions.
// Vec3d转换为Vec4f ,根据光源位置的需要
osg::Vec4
worldToVec4(const osg::Vec3d& ecef)
{osg::Vec4 result(0.0f, 0.0f, 0.0f, 1.0f);osg::Vec3d d = ecef;while (d.length() > 1e6)// 避免光源位置太远??{d *= 0.1;result.w() *= 0.1;}return osg::Vec4(d.x(), d.y(), d.z(), result.w());
}// 生成随机颜色
osg::Vec4
randomColor()
{float r = (float)rand() / (float)RAND_MAX;float g = (float)rand() / (float)RAND_MAX;float b = (float)rand() / (float)RAND_MAX;return osg::Vec4(r, g, b, 1.0f);
}// 添加光源
osg::Group*
addLights(osg::View* view, osg::Node* root, int lightNum)
{// 获取地理坐标系MapNode* mapNode = MapNode::get(root);const SpatialReference* mapsrs = mapNode->getMapSRS();const SpatialReference* geosrs = mapsrs->getGeographicSRS();osg::Vec3d world;osg::Group* lights = new osg::Group();// Add a directional light that simulates the sun - but skip this if a sky// was already added in the earth file.// 添加模拟太阳的平行光// 但如果地球文件中已经添加了天空,则跳过此操作。if (lightNum == 0){// Ephemeris 星历表类,给出了自然发生的天体天体的位置;// 其中包括太阳和月亮。// 还包括一些相关的实用程序功能。Ephemeris e;DateTime dt(2016, 8, 10, 14.0);// 设置UTC时间CelestialBody sun = e.getSunPosition(dt); // 设置天体相对于地球的位置。world = sun.geocentric;// 太阳的地理位置// 定义太阳光osg::Light* sunLight = new osg::Light(lightNum++);world.normalize();// 归一化sunLight->setPosition(osg::Vec4d(world, 0.0));sunLight->setAmbient(osg::Vec4(0.2, 0.2, 0.2, 1.0));// 环境光照sunLight->setDiffuse(osg::Vec4(1.0, 1.0, 0.9, 1.0));// 漫反射光照// osg::LightSource 用于定义场景中的灯光的叶节点。osg::LightSource* sunLS = new osg::LightSource();sunLS->setLight(sunLight);lights->addChild(sunLS);// 为root节点 投射阴影ShadowCaster* caster = osgEarth::findTopMostNodeOfType<ShadowCaster>(root);if (caster){OE_INFO << "Found a shadow caster!\n";caster->setLight(sunLight);}std::cout << "because no skyNode,so create sunLS" << std::endl;}#if 1	// 这里主要是为测试加载其他光源// A red spot light. A spot light has a real position in space // and points in a specific direciton. The Cutoff and Exponent// properties control the cone angle and sharpness, respectively// 一束红光。拥有真实的位置和光方向。// “Cutoff”和“Exponent”属性分别控制圆锥体角度和锐度{// 定义光照射 地点GeoPoint p(geosrs, -121, 34, 5000000., ALTMODE_ABSOLUTE);p.toWorld(world);// 定义光osg::Light* spot = new osg::Light(lightNum++);spot->setPosition(worldToVec4(world));spot->setAmbient(osg::Vec4(0, 0.2, 0, 1));spot->setDiffuse(osg::Vec4(1, 0, 0, 1));spot->setSpotCutoff(20.0f);spot->setSpotExponent(100.0f);// point straight down at the map:直接指向地图world.normalize();spot->setDirection(-world);// 光源叶子节点osg::LightSource* spotLS = new osg::LightSource();spotLS->setLight(spot);lights->addChild(spotLS);}// A green point light. A Point light lives at a real location in // space and lights equally in all directions.// 绿灯。点光源位于空间中的真实位置,并在所有方向上均匀发光。{// 定义光照射 地点GeoPoint p(geosrs, -45, -35, 1000000., ALTMODE_ABSOLUTE);p.toWorld(world);// 定义光osg::Light* point = new osg::Light(lightNum++);point->setPosition(worldToVec4(world));point->setAmbient(osg::Vec4(0, 0, 0, 1));point->setDiffuse(osg::Vec4(1.0, 1.0, 0.0, 1));// 光源叶子节点osg::LightSource* pointLS = new osg::LightSource();pointLS->setLight(point);lights->addChild(pointLS);}
#endif// Generate the necessary uniforms for the shaders.// 为着色器生成必要的uniforms。// GenerateGL3LightingUniforms类的作用:遍历图形,查找灯光和材质,//		并为它们生成静态 Uniforms 或动态剔除回调,//		以便它们可以使用核心配置文件着色器。GenerateGL3LightingUniforms gen;lights->accept(gen);return lights;
}int
main(int argc, char** argv)
{osg::ArgumentParser arguments(&argc, argv);// help?if (arguments.read("--help"))return usage(argv[0]);// create a viewer:osgViewer::Viewer viewer(arguments);// Whether to test updating material// 是否测试更新材质bool update = arguments.read("--update");// Tell the database pager to not modify the unref settingsviewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true, false);// install our default manipulator (do this before calling load)viewer.setCameraManipulator(new EarthManipulator(arguments));// disable the small-feature cullingviewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);// 在添加光源之前,需要关闭viewer本身的光viewer.setLightingMode(viewer.NO_LIGHT);// load an earth file, and support all or our example command-line optionsosg::ref_ptr<osg::Node> node = MapNodeHelper().load(arguments, &viewer);if (node.valid()){MapNode* mapNode = MapNode::get(node.get());if (!mapNode)return -1;// Example of a custom material for the terrain.// 地形自定义材质示例。osg::ref_ptr< osg::Material > material = 0;if (update)// 开启update属性后,会创建material,进而调用回调方法,随机更改影像颜色{OE_NOTICE << "Custom material" << std::endl;material = new osg::Material;// 材质决定材质颜色material->setDiffuse(osg::Material::FRONT, osg::Vec4(1, 1, 1, 1));//漫反射光照    material->setAmbient(osg::Material::FRONT, osg::Vec4(1, 1, 1, 1));// 环境光照// Attach our StateAttributeCallback so that uniforms are updated.绑定材质回调material->setUpdateCallback(new MaterialCallback());mapNode->getOrCreateStateSet()->setAttributeAndModes(material);}// Does a Sky already exist (loaded from the earth file)?SkyNode* sky = osgEarth::findTopMostNodeOfType<SkyNode>(node.get());if (!sky)// 如果没有深空节点{std::cout << "no skyNode " << std::endl;// Add phong lighting.添加标签照明???PhongLightingEffect* phong = new PhongLightingEffect();phong->attach(node->getOrCreateStateSet());}// 添加光源. 当没有sky时,才会采用addLights中,创建光源的方式添加。osg::Group* lights = addLights(&viewer, node.get(), sky ? 1 : 0);mapNode->addChild(lights);viewer.setSceneData(node.get());while (!viewer.done()){if (viewer.getFrameStamp()->getFrameNumber() % 100 == 0){// 每100帧,随机生成一个颜色if (material){material->setDiffuse(osg::Material::FRONT, randomColor());}}viewer.frame();}return 0;}else{return usage(argv[0]);}
}

这篇关于osg之黑夜背景地月系显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/379167

相关文章

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

第49课 Scratch入门篇:骇客任务背景特效

骇客任务背景特效 故事背景:   骇客帝国特色背景在黑色中慢慢滚动着! 程序原理:  1 、 角色的设计技巧  2 、克隆体的应用及特效的使用 开始编程   1、使用 黑色的背景: ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/7d74c872f06b4d9fbc88aecee634b074.png#pic_center)   2

小程序button控件上下边框的显示和隐藏

问题 想使用button自带的loading图标功能,但又不需要button显示边框线 button控件有一条淡灰色的边框,在控件上了样式 border:none; 无法让button边框隐藏 代码如下: <button class="btn">.btn{border:none; /*一般使用这个就是可以去掉边框了*/} 解决方案 发现button控件有一个伪元素(::after

数据中台出现的背景

数据中台产生背景 数据建设中出现的问题 在企业数据建设过程中,都离不开大数据平台建设,大数据平台建设涉及数据采集、数据存储、数据仓库构建、数据处理分析、数据挖掘、数据可视化等一系列流程。 随着企业体量不断增大,一个企业可能有总公司及很多子公司,随着企业各类业务多元化和垂直业务发展,从全企业角度来看,每个子公司或者某些独立的业务部都在构建大数据分析平台,在企业内部形成了很多分散、烟囱式、独立的

MFC中Spin Control控件使用,同时数据在Edit Control中显示

实现mfc spin control 上下滚动,只需捕捉spin control 的 UDN_DELTAPOD 消息,如下:  OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {  LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR);  // TODO: 在此添加控件通知处理程序代码    if

PNG透明背景按钮的实现(MFC)

问题描述: 当前要在对话框上添加一个以两个PNG图片作为背景的按钮,PNG图的背景是透明的,按钮也要做出相同的透明效果。并且鼠标不在按钮上时,按钮显示"bg1.png";鼠标移动到按钮上时,按钮显示"bg2.png" 开发环境为VS2010。 解决办法: 使用GDI+库装载PNG图片,并使用MFC Button Control和CMFCButton类结合,调用CMFCButton