如何开始使用 SilverLining
本文为如何开始使用SilverLining(中文教程一):演示示例,继续查看>>
如何开始使用SilverLining(中文教程二):渲染场景
如何开始使用SilverLining(中文教程三):资源内存管理
如何开始使用SilverLining(中文教程四):集成技巧
如何开始使用SilverLining(中文教程五):调整外观
一个简单的例子
初始化
在SDK的SampleCode/OpenGLExample、SampleCode/DirectX9Example、SampleCode/DirectX10Example和SampleCode/DirectX11Example目录中可以找到OpenGL和Direct3D的完整示例代码(Linux SDK只包括OpenGL)。下面是对每个目录中的一些关键代码的概述。
当你的应用程序启动时,你要初始化OpenGL或Direct3D,并创建和初始化一个Atmosphere对象。如果你想让你的云层在你的应用程序每次运行时都是不同的,那么一定要把随机数发生器也作为种子。
在DirectX9下,建议你在创建IDirect3DDevice对象时不要使用D3DCREATE_PUREDEVICE标志。Silverlining需要在你的设备上调用GetTransform和GetRenderState,以便在我们完成绘图后把东西放回原来的样子。我们还建议在创建设备时使用D3DPRESENTFLAG_LOCKABLE_BACKBUFFER标志,这将允许你更灵活地配置SilverLining如何进行光照(特别是,这将允许你在SilverLining.config中把render-offscreen设置为 "no"。)
如果你的大气层初始化成功,你就可以开始配置它,添加你的云层,并指定你希望模拟的时间和地点。我们接下来会讨论这个问题。如果它不能成功地初始化,很可能是用户需要更新图形驱动。SilverLining对于大多数可能存在兼容性问题的情况都有回退案例,但是一些老的驱动程序甚至在标准功能上也有问题。
// Initialize your Direct3D device object, glut, or wgl as appropriate
InitializeGraphicsSubsystem();
// Instantiate an Atmosphere object. Substitute your own purchased license name and code here.
atm = new Atmosphere("Your Company Name", "Your License Code");
// Tell SilverLining we're rendering in OpenGL, the Resources directory is 2 directories
// above the working directory, and we're using a right-handed coordinate system.
if (atm->Initialize(Atmosphere::OPENGL, "..\\..\\Resources\\", true, 0) == Atmosphere::E_NOERROR)
{
// If you want different clouds to be generated every time, remember to seed the
// random number generator.
atm->GetRandomNumberGenerator()->Seed(time(NULL));
// Tell SilverLining what your axis conventions are.
atm->SetUpVector(0.0, 1.0, 0.0);
atm->SetRightVector(1.0, 0.0, 0.0);
// Set up all the clouds
SetupAtmosphericConditions();
// Configure where and when we want to be
SetTimeAndLocation();
// Start rendering.
glutMainLoop();
}
让我们充实一下上面的SetupAtmosphericConditions()。一旦你初始化了一个大气对象,你就可以访问它的AtmosphericConditions对象来做一些事情,比如添加云层,改变一天中的时间,以及模拟风。为了快速启动和运行,你可以用这样的一行代码来设置云层条件:
atm->GetConditions()->SetPresetConditions(AtmosphericConditions::MOSTLY_CLOUDY, *atm);
但是,你可能想对云层进行更精细的控制。让我们写一段代码,添加一个卷云层,一个积云层,并让它成为一个大风天。这段代码是不言自明的:
static void SetupCirrusClouds()
{
CloudLayer *cirrusCloudLayer;
cirrusCloudLayer = CloudLayerFactory::Create(CIRRUS_FIBRATUS, *atm);
cirrusCloudLayer->SetBaseAltitude(8000);
cirrusCloudLayer->SetThickness(500);
cirrusCloudLayer->SetBaseLength(100000);
cirrusCloudLayer->SetBaseWidth(100000);
cirrusCloudLayer->SetLayerPosition(0, 0);
cirrusCloudLayer->SeedClouds(*atm);
atm->GetConditions()->AddCloudLayer(cirrusCloudLayer);
}
上述程序将在8000米的高度和100公里的范围内创建一个Cirrus云(高而飘渺的那种)。它以相机的位置为中心。
当你创建一个新的CloudLayer时,你必须首先
使用CloudLayerFactory类工厂对其进行实例化。
设置它的大小、覆盖范围和位置参数
调用CloudLayer::SeedClouds()来为该层填充云层。
通过AtmosphericConditions::AddCloudLayer()将该层添加到你的大气中。
同样地,让我们来设置一个积云堆积甲板:
// Add a cumulus congestus deck with 40% sky coverage, which stays centered around the camera position.
static void SetupCumulusCongestusClouds()
{
CloudLayer *cumulusCongestusLayer;
cumulusCongestusLayer = CloudLayerFactory::Create(CUMULUS_CONGESTUS, *atm);
cumulusCongestusLayer->SetIsInfinite(true);
cumulusCongestusLayer->SetBaseAltitude(1500);
cumulusCongestusLayer->SetThickness(100);
cumulusCongestusLayer->SetBaseLength(30000);
cumulusCongestusLayer->SetBaseWidth(30000);
cumulusCongestusLayer->SetDensity(0.4);
cumulusCongestusLayer->SetLayerPosition(0, 0);
cumulusCongestusLayer->SetFadeTowardEdges(true);
cumulusCongestusLayer->SetAlpha(0.8);
// Enable convection effects, but not growth:
cumulusCongestusLayer->SetCloudAnimationEffects(0.1, false, 0);
cumulusCongestusLayer->SeedClouds(*atm);
atm->GetConditions()->AddCloudLayer(cumulusCongestusLayer);
}
我们的SetupAtmosphericConditions函数将设置模拟风,调用上述两个函数来创建卷云和积云的甲板,最后设置模拟能见度--这将影响云层本身的雾化效果:
// Configure SilverLining for the desired wind, clouds, and visibility.
static void SetupAtmosphericConditions()
{
// Set up wind blowing south at 50 meters/sec
WindVolume wv;
wv.SetDirection(180);
wv.SetMinAltitude(0);
wv.SetMaxAltitude(10000);
wv.SetWindSpeed(50);
atm->GetConditions()->SetWind(wv);
// Set up the desired cloud types.
SetupCirrusClouds();
SetupCumulusCongestusClouds();
// Set visibility in meters
atm->GetConditions()->SetVisibility(100000);
}
如果你希望模拟一个特定的地点和时间,你也应该在初始化中进行设置。你也可以在应用程序运行时随时改变这一点。请确保你在LocalTime对象中指定的时区与你在Location对象中指定的经度一致,否则你会对结果感到非常困惑!(如果你想持续使用UTC时间而不是本地时间,将所有时间指定为GMT时区也是可以的)。
// Sets the simulated location and local time.
// Note, it's important that your longitude in the Location agrees with
// the time zone in the LocalTime.
static void SetTimeAndLocation()
{
Location loc;
loc.SetLatitude(45);
loc.SetLongitude(-122);
LocalTime tm;
tm.SetYear(1971);
tm.SetMonth(8);
tm.SetDay(5);
tm.SetHour(14);
tm.SetMinutes(0);
tm.SetSeconds(0);
tm.SetObservingDaylightSavingsTime(true);
tm.SetTimeZone(PST);
atm->GetConditions()->SetTime(tm);
atm->GetConditions()->SetLocation(loc);
}
无限云层
可以通过使用CloudLayer::SetIsInfinite()方法来修改Cumulus congestus、Cumulus mediocris、stratus、cirrus、stratocumulus和 cirrocumulus云层。
无限云层将保持在相机位置的中心;当相机移动时,离开云层的长度和宽度所定义的边界区域的云层将被重新定位,以出现在相机移动的地方。同样,如果风把云吹到了云层之外,它们会环绕到云层的另一边。
无限云层让你不必担心云层的定位,或设置多个云层来覆盖大面积的区域。
这给你带来了 "无限 "云层的效果,云层永远不会被吹走,你也无法将相机从云层中移开。你创建的云层的长度和宽度越大,当云层被重新定位时,爆裂就越不明显--尤其是当云层在远处被雾化时,通过设置AtmosphericConditions::SetVisibility()到一个与云层尺寸相似的值。你也可以使用CloudLayer::SetFadeTowardEdges()来在云层到达边缘之前将其淡出,确保弹出的云层永不可见。
CloudLayer::SetIsInfinite()应该在初始化云层时被调用;默认情况下,云层不是无限的。积雨云层不受SetIsInfinite()的影响,因为它们只包含一片云。
云的动画效果
积云云层支持可选的动画效果,以便在运行时模拟云层的对流和增长。使用CloudLayer::SetCloudAnimationEffects()方法来控制这些效果。
第一个参数控制单个云团是否会以随机速度旋转,使云团具有对流效果。该值是以弧度每秒为单位的最大旋转。这种效果没有性能成本。
第二个参数控制真正的动态云层生长模拟,由细胞自动机驱动。这确实会对CPU的性能产生影响,但在大多数情况下是可以忽略不计的。如果你启用了生长效应,第三个参数可以让你控制云在启动时的 "进化 "程度。如果把它设置为零,则开始时云会完全长大,逐渐改变其形状;如果设置为一个较小的值,则可以观察云的实时形成。第四个参数控制细胞自动机的迭代间隔秒数。将其设置为较长的值以获得较慢的生长效果,或者通过0来使用默认值。
当与延时效果一起使用时(通过使用一个自定义的MillisecondTimer类),可以实现云层随时间变化和生长的戏剧性效果。
如果你有任何问题,请随时联系我们 https://www.dhorde.com/ 在线客服,电话 023-62585653,或邮箱 sales@dhorde.com。
本文为如何开始使用SilverLining(中文教程一):演示示例,继续查看>>
如何开始使用SilverLining(中文教程二):渲染场景
如何开始使用SilverLining(中文教程三):资源内存管理
如何开始使用SilverLining(中文教程四):集成技巧
如何开始使用SilverLining(中文教程五):调整外观
渝公网安备50010702505508