首页 > 分享 > osgearth3.2设置光源位置及颜色

osgearth3.2设置光源位置及颜色

可以自定义光源位置,颜色,距离,角度,范围 

  

#include <Windows.h>

#include <osgDB/ReadFile>

#include <osgViewer/Viewer>

#include <osgEarth/Notify>

#include <osgEarth/Lighting>

#include <osgEarth/PhongLightingEffect>

#include <osgEarth/NodeUtils>

#include <osgEarth/EarthManipulator>

#include <osgEarth/ExampleResources>

#include <osgEarth/Ephemeris>

#include <osgEarth/Shadowing>

#ifdef _DEBUG

#pragma comment(lib, "osgd.lib")

#pragma comment(lib, "osgDBd.lib")

#pragma comment(lib, "osgFXd.lib")

#pragma comment(lib, "osgViewerd.lib")

#pragma comment(lib, "osgVolumed.lib")

#pragma comment(lib, "OpenThreadsd.lib")

#pragma comment(lib, "osgGAd.lib")

#pragma comment(lib, "osgUtild.lib")

#pragma comment(lib, "osgManipulatord.lib")

#pragma comment(lib, "osgTextd.lib")

#pragma comment(lib, "osgSimd.lib")

#pragma comment(lib, "osgTerraind.lib")

#pragma comment(lib, "osgWidgetd.lib")

#pragma comment(lib, "osgUId.lib")

#pragma comment(lib, "osgShadowd.lib")

#pragma comment(lib, "osgAnimationd.lib")

#pragma comment(lib, "osgParticled.lib")

#pragma comment(lib, "osgPresentationd.lib")

#pragma comment(lib, "osgQOpenGLd.lib")

#pragma comment(lib, "osgEarthd.lib")

#pragma comment(lib, "glu32.lib")

#pragma comment(lib, "opengl32.lib")

#else

#pragma comment(lib, "osg.lib")

#pragma comment(lib, "osgDB.lib")

#pragma comment(lib, "osgFX.lib")

#pragma comment(lib, "osgViewer.lib")

#pragma comment(lib, "osgVolume.lib")

#pragma comment(lib, "OpenThreads.lib")

#pragma comment(lib, "osgGA.lib")

#pragma comment(lib, "osgUtil.lib")

#pragma comment(lib, "osgManipulator.lib")

#pragma comment(lib, "osgText.lib")

#pragma comment(lib, "osgSim.lib")

#pragma comment(lib, "osgTerrain.lib")

#pragma comment(lib, "osgWidget.lib")

#pragma comment(lib, "osgUI.lib")

#pragma comment(lib, "osgShadow.lib")

#pragma comment(lib, "osgAnimation.lib")

#pragma comment(lib, "osgParticle.lib")

#pragma comment(lib, "osgPresentation.lib")

#pragma comment(lib, "osgQOpenGL.lib")

#pragma comment(lib, "osgEarth.lib")

#pragma comment(lib, "glu32.lib")

#pragma comment(lib, "opengl32.lib")

#endif;

using namespace osgEarth;

using namespace osgEarth::Util;

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();

if (lightNum == 0)

{

Ephemeris e;

DateTime dt(2022, 9, 10, 10.0);

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, 0.0, 0.0, 1.0));

osg::LightSource* sunLS = new osg::LightSource();

sunLS->setLight(sunLight);

lights->addChild(sunLS);

ShadowCaster* caster = osgEarth::findTopMostNodeOfType<ShadowCaster>(root);

if (caster)

{

OE_INFO << "Found a shadow caster!n";

caster->setLight(sunLight);

}

}

#if 1

{

GeoPoint p(geosrs, 116.0, 40.0, 50000., ALTMODE_ABSOLUTE);

p.toWorld(world);

std::cout << "word:" << world.x() << " " << world.y() << " " << world.z();

osg::Light* spot = new osg::Light(lightNum++);

spot->setPosition(worldToVec4(world));

spot->setAmbient(osg::Vec4(0, 0.2, 0, 1));

spot->setDiffuse(osg::Vec4(0, 0, 1, 1));

spot->setSpotCutoff(200.0f);

spot->setSpotExponent(10.0f);

world.normalize();

spot->setDirection(-world);

osg::LightSource* spotLS = new osg::LightSource();

spotLS->setLight(spot);

lights->addChild(spotLS);

}

#endif

GenerateGL3LightingUniforms gen;

lights->accept(gen);

return lights;

}

int main(int argc, char** argv)

{

osgEarth::initialize();

osgViewer::Viewer viewer;

viewer.setCameraManipulator(new EarthManipulator());

viewer.setLightingMode(viewer.NO_LIGHT);

osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("simple.earth");

if (node.valid())

{

MapNode* mapNode = MapNode::get(node.get());

if (!mapNode)

return -1;

osg::ref_ptr< osg::Material > material = 0;

{

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));

material->setUpdateCallback(new MaterialCallback());

mapNode->getOrCreateStateSet()->setAttributeAndModes(material);

}

SkyNode* sky = osgEarth::findTopMostNodeOfType<SkyNode>(node.get());

if (!sky)

{

PhongLightingEffect* phong = new PhongLightingEffect();

phong->attach(node->getOrCreateStateSet());

}

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)

{

if (material)

{

}

}

viewer.frame();

}

return 0;

}

else

{

}

}

相关知识

固体照明光源
颜色的调节及涂色技巧探究论文
花坛的设置
佳能怎么设置对焦模式
花卉摄影时如何设置相机?必备色彩知识
如何建设培养架光源气候室?了解它的特点,功能,优势,价格
基于LED光源优势的植物工厂光照策略探讨.pdf
拍花技巧:拍花想要颜色更艳丽,相机如何设置?
一种基于周期发光的动植物照明系统及方法
《照明设计——从传统光源到LED》书讯

网址: osgearth3.2设置光源位置及颜色 https://m.huajiangbk.com/newsview587156.html

所属分类:花卉
上一篇: UE4场景基础、建模、光照设置
下一篇: Unity Shader 之 环