-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
603 lines (510 loc) · 20.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/EngineDefs.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/Node.h>
#include <Urho3D/Scene/Component.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/StaticModelGroup.h>
#include <Urho3D/Graphics/Skybox.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Drawable.h>
#include <Urho3D/Graphics/OctreeQuery.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/Viewport.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/UI/Slider.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/Button.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/Graphics/Terrain.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Zone.h>
#include <Urho3D/Math/Random.h>
#include <Urho3D/Math/RandomEngine.h>
#include <Urho3D/Graphics/AnimatedModel.h>
#include <Urho3D/Graphics/AnimationController.h>
#include <cmath>
// This is probably always OK.
using namespace Urho3D;
class GrassStaticModelGroup : public StaticModelGroup
{
URHO3D_OBJECT(GrassStaticModelGroup, StaticModelGroup);
public:
explicit GrassStaticModelGroup(Context *context) : StaticModelGroup(context){}
~GrassStaticModelGroup() override;
static void RegisterObject(Context* context)
{
context->RegisterFactory<GrassStaticModelGroup>();
URHO3D_COPY_BASE_ATTRIBUTES(StaticModelGroup);
}
protected:
void OnWorldBoundingBoxUpdate() override
{
StaticModelGroup::OnWorldBoundingBoxUpdate();
worldBoundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
}
};
GrassStaticModelGroup::~GrassStaticModelGroup() = default;
struct SkyPreset
{
float Br_{0}, Bm_{0}, g_{0}, cirrus_{0}, cumulus_{0}, cumulusbrightness_{0};
SkyPreset Lerp(const SkyPreset &rhs, float t)
{
SkyPreset p;
p.Br_=Br_ + t * (rhs.Br_ - Br_);
p.Bm_=Bm_ + t * (rhs.Bm_ - Bm_);
p.g_=g_ + t * (rhs.g_ - g_);
p.cirrus_=cirrus_ + t * (rhs.cirrus_ - cirrus_);
p.cumulus_=cumulus_ + t * (rhs.cumulus_ - cumulus_);
p.cumulusbrightness_=cumulusbrightness_ + t * (rhs.cumulusbrightness_ - cumulusbrightness_);
return p;
}
};
struct SkyPresetTemplate
{
float Br_{0}, Bm_{0}, g_{0}, cumulusbrightness_{0};
float cirruslow_{0}, cirrushigh_{0}, cumuluslow_{0}, cumulushigh_{0};
SkyPreset Randomize()
{
SkyPreset p;
p.Br_=Br_;
p.Bm_=Bm_;
p.g_=g_;
p.cumulusbrightness_=cumulusbrightness_;
p.cirrus_ = cirruslow_ + RandStandardNormal() * (cirrushigh_ - cirruslow_);
p.cumulus_ = cumuluslow_ + RandStandardNormal() * (cumulushigh_ - cumuluslow_);
return p;
}
};
struct SunSettings
{
Vector3 sunpos_{0,0,0};
Color suncolor_{0,0,0};
Color fogcolor_{0,0,0};
};
Color CalculateSkyboxColor(float timeofday, const Vector3 &pos, const SkyPreset &env)
{
const Vector3 nitrogen(0.650, 0.570, 0.475);
auto vecpow=[](const Vector3 &vec, float p)->Vector3 {return Vector3(std::pow(vec.x_, p), std::pow(vec.y_, p), std::pow(vec.z_, p));};
auto vecdiv=[](float c, const Vector3 &vec)->Vector3 {return Vector3(c/vec.x_, c/vec.y_, c/vec.z_);};
auto vecexp=[](const Vector3 &vec)->Vector3 {return Vector3(std::exp(vec.x_), std::exp(vec.y_), std::exp(vec.z_));};
Vector3 fsun(std::cos(timeofday * 0.2617993875), std::sin(timeofday * 0.2617993875), 0);
float Br=env.Br_;
float Bm=env.Bm_;
float g=env.g_;
Vector3 Kr = vecdiv(Br, vecpow(nitrogen, 4.f));
Vector3 Km = vecdiv(Bm, vecpow(nitrogen, 0.84f));
float mu = pos.DotProduct(fsun);
float rayleigh = 3.f / (8.0f * 3.14f) * (1.0f + mu * mu);
Vector3 mie = (Kr + Km * (1.0f - g * g) / (2.0f + g * g) / pow(1.0f + g * g - 2.0f * g * mu, 1.5f)) / (Br + Bm);
Vector3 day_extinction=vecexp((Kr / Br)*-exp(-((pos.y_ + fsun.y_ * 4.0f) * (exp(-pos.y_ * 16.0f) + 0.1f) / 80.0f) / Br) * (exp(-pos.y_ * 16.0f) + 0.1f)) * exp(-pos.y_ * exp(-pos.y_ * 8.0f ) * 4.0f) * exp(-pos.y_ * 2.0f) * 4.0f;
float v=1.0f - std::exp(fsun.y_);
Vector3 night_extinction=Vector3(v,v,v)*0.2f;
Vector3 extinction=day_extinction.Lerp(night_extinction, -fsun.y_ * 0.2f + 0.5f);
Vector3 c=mie*extinction*rayleigh;
//c.Normalize();
//c=c*1.1f;
return Color(std::max(0.0f, std::min(1.0f, c.x_)), std::max(0.0f, std::min(1.0f, c.y_)), std::max(0.0f, std::min(1.0f, c.z_)));
}
SunSettings CalculateSunSettings(float timeofday, float abovehorizon, const SkyPreset &env)
{
SunSettings sun;
sun.sunpos_ = Vector3(std::cos(timeofday * 0.2617993875), std::sin(timeofday * 0.2617993875), 0);
Vector3 pos(1, abovehorizon, 0);
pos.Normalize();
if(pos.y_ < 0.f) pos.y_ = 0.f;
sun.fogcolor_=CalculateSkyboxColor(timeofday, pos, env);
sun.suncolor_=CalculateSkyboxColor(timeofday, sun.sunpos_, env);
sun.suncolor_.r_=std::max(0.01f, std::min(sun.suncolor_.r_, 1.f));
sun.suncolor_.g_=std::max(0.01f, std::min(sun.suncolor_.g_, 1.f));
sun.suncolor_.b_=std::max(0.01f, std::min(sun.suncolor_.b_, 1.f));
return sun;
}
class AtmosphereSettings
{
public:
AtmosphereSettings(){}
void AddPreset(const SkyPresetTemplate &t)
{
presets_.push_back(t);
currentpreset_=lastpreset_=RandomPreset();
time_=0.f;
}
void SetInterval(float i){interval_=i;}
SkyPreset Update(float dt)
{
time_ += dt;
if(time_>interval_)
{
// Roll a new preset
lastpreset_=currentpreset_;
currentpreset_=RandomPreset();
changetime_=5.f;
time_=0;
}
else
{
changetime_-=dt;
}
float factor=1.f-(std::max(0.0f, std::min(1.f, changetime_/5.f)));
return lastpreset_.Lerp(currentpreset_, factor);
}
protected:
ea::vector<SkyPresetTemplate> presets_;
unsigned int currentpresetindex_;
float changetime_{0};
float time_{0};
float interval_{5.f};
RandomEngine random_;
SkyPreset currentpreset_{0.001f, 0.0023f, 0.9599f, 2.1f, 1.4f, 1.1575f};
SkyPreset lastpreset_{0.001f, 0.0023f, 0.9599f, 2.1f, 1.4f, 1.1575f};
SkyPreset RandomPreset()
{
if(presets_.size()==0)
{
return SkyPreset{0.001f, 0.0023f, 0.9599f, 2.1f, 1.4f, 1.1575f};
}
unsigned int which=std::min(presets_.size()-1, random_.GetUInt(presets_.size()));
return presets_[which].Randomize();
}
};
class AwesomeGameApplication : public Application
{
// This macro defines some methods that every `Urho3D::Object` descendant should have.
URHO3D_OBJECT(AwesomeGameApplication, Application);
public:
// Likewise every `Urho3D::Object` descendant should implement constructor with single `Context*` parameter.
AwesomeGameApplication(Context* context)
: Application(context)
{
}
void Setup() override
{
// Engine is not initialized yet. Set up all the parameters now.
engineParameters_[EP_FULL_SCREEN] = false;
engineParameters_[EP_WINDOW_HEIGHT] = 768;
engineParameters_[EP_WINDOW_WIDTH] = 1024;
// Resource prefix path is a list of semicolon-separated paths which will be checked for containing resource directories. They are relative to application executable file.
engineParameters_[EP_RESOURCE_PREFIX_PATHS] = ".;..";
engineParameters_[EP_WINDOW_MAXIMIZE] = true;
engineParameters_[EP_WINDOW_RESIZABLE]=true;
}
void Start() override
{
GrassStaticModelGroup::RegisterObject(context_);
// At this point engine is initialized, but first frame was not rendered yet. Further setup should be done here. To make sample a little bit user friendly show mouse cursor here.
GetSubsystem<Input>()->SetMouseVisible(true);
scene_=new Scene(context_);
scene_->CreateComponent<Octree>();
cameraNode_=scene_->CreateChild();
camera_=cameraNode_->CreateComponent<Camera>();
camera_->SetFov(50.f);
camera_->SetViewMask(3);
cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
auto* renderer = GetSubsystem<Renderer>();
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
SharedPtr<Viewport> viewport(new Viewport(context_, scene_, camera_));
renderer->SetViewport(0, viewport);
// Setup a skybox
Node *n=scene_->CreateChild();
StaticModel *skybox=n->CreateComponent<Skybox>();
auto cache=GetSubsystem<ResourceCache>();
skybox->SetModel(cache->GetResource<Model>("Models/Icosphere.mdl"));
skyboxmaterial_=cache->GetResource<Material>("Materials/ProcSkybox.xml");
skybox->SetMaterial(skyboxmaterial_);
// Create a Zone component for ambient lighting & fog control
Node* zoneNode = scene_->CreateChild("Zone");
zone_ = zoneNode->CreateComponent<Zone>();
zone_->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
zone_->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
zone_->SetFogColor(Color(1.0f, 1.0f, 1.0f));
zone_->SetFogStart(200.0f);
zone_->SetFogEnd(350.0f);
// Create a directional light to the world. Enable cascaded shadows on it
lightNode_ = scene_->CreateChild("DirectionalLight");
lightNode_->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
light_ = lightNode_->CreateComponent<Light>();
light_->SetLightType(LIGHT_DIRECTIONAL);
light_->SetCastShadows(true);
light_->SetShadowBias(BiasParameters(0.0025f, 0.5f));
light_->SetShadowCascade(CascadeParameters(20.0f, 100.0f, 400.0f, 0.0f, 1.6f));
light_->SetSpecularIntensity(0.5f);
light_->SetColor(Color(1.1f, 1.1f, 1.0f));
backLightNode_ = scene_->CreateChild();
Light *backlight=backLightNode_->CreateComponent<Light>();
backlight->SetLightType(LIGHT_DIRECTIONAL);
backlight->SetCastShadows(false);
backlight->SetColor(Color(0.0f, 0.0f, 0.0f));
Node* terrainNode = scene_->CreateChild("Terrain");
terrainNode->SetPosition(Vector3(0.0f, 0.0f, 0.0f));
terrain_ = terrainNode->CreateComponent<Terrain>();
terrain_->SetPatchSize(64);
terrain_->SetSpacing(Vector3(2.0f, 0.5f, 2.0f)); // Spacing between vertices and vertical resolution of the height map
terrain_->SetSmoothing(true);
terrain_->SetHeightMap(cache->GetResource<Image>("Textures/elevation.png"));
terrain_->SetMaterial(cache->GetResource<Material>("Materials/Terrain4.xml"));
terrain_->SetViewMask(2);
terrainmaterial_=cache->GetResource<Material>("Materials/Terrain4.xml");
// The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all
// terrain patches and other objects behind it
//terrain_->SetOccluder(true);
terrain_->SetCastShadows(true);
Node *on=scene_->CreateChild();
StaticModel *om=on->CreateComponent<StaticModel>();
om->SetModel(cache->GetResource<Model>("Models/Blob.mdl"));
om->SetMaterial(cache->GetResource<Material>("Materials/TriplanarCliff4.xml"));
om->SetCastShadows(true);
om->SetViewMask(2);
grassTestNode_ = scene_->CreateChild();
StaticModelGroup *smg1=grassTestNode_->CreateComponent<GrassStaticModelGroup>();
smg1->SetModel(cache->GetResource<Model>("Models/GrassBunch3.mdl"));
smg1->SetMaterial(cache->GetResource<Material>("Materials/GrassTest.xml"));
smg1->SetCastShadows(false);
StaticModelGroup *smg2=grassTestNode_->CreateComponent<GrassStaticModelGroup>();
smg2->SetModel(cache->GetResource<Model>("Models/BlueFlower.mdl"));
smg2->SetMaterial(cache->GetResource<Material>("Materials/FlowerTest.xml"));
smg2->SetCastShadows(false);
int radius=90;
for(int x=0; x<radius*2; ++x)
{
for(int y=0; y<radius*2; ++y)
{
//if(x!=12 || y!=12)
float dx=(float)x - (float)radius;
float dy=(float)y - (float)radius;
float dist=std::sqrt(dx*dx+dy*dy);
if(dist<=(float)radius)
{
Node *ch=grassTestNode_->CreateChild();
ch->SetPosition(Vector3(((float)x-(float)radius+0.5f), 0, ((float)y-(float)radius+0.5f)));
smg1->AddInstanceNode(ch);
smg2->AddInstanceNode(ch);
}
}
}
Material *m=cache->GetResource<Material>("Materials/GrassTest.xml");
m->SetShaderParameter("HeightMapData", Variant(Vector4(terrain_->GetHeightMap()->GetWidth(), terrain_->GetHeightMap()->GetHeight(), terrain_->GetSpacing().x_, terrain_->GetSpacing().y_)));
m->SetShaderParameter("Radius", Variant(Vector2((float)radius*0.8f, (float)radius)));
m=cache->GetResource<Material>("Materials/FlowerTest.xml");
m->SetShaderParameter("HeightMapData", Variant(Vector4(terrain_->GetHeightMap()->GetWidth(), terrain_->GetHeightMap()->GetHeight(), terrain_->GetSpacing().x_, terrain_->GetSpacing().y_)));
m->SetShaderParameter("Radius", Variant(Vector2((float)radius*0.8f, (float)radius)));
auto ui=GetSubsystem<UI>();
auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
element_=ui->LoadLayout(cache->GetResource<XMLFile>("UI/sliders.xml"), style);
ui->GetRoot()->AddChild(element_);
element_->SetWidth(ui->GetRoot()->GetWidth());
element_->SetPosition(IntVector2(0, ui->GetRoot()->GetHeight()-element_->GetHeight()));
toggle_=ui->LoadLayout(cache->GetResource<XMLFile>("UI/ToggleButton.xml"), style);
ui->GetRoot()->AddChild(toggle_);
toggle_->SetPosition(IntVector2(0,0));
dynamic_cast<Slider *>(element_->GetChild("BrSlider", true))->SetValue(10);
dynamic_cast<Slider *>(element_->GetChild("BmSlider", true))->SetValue(25);
dynamic_cast<Slider *>(element_->GetChild("gSlider", true))->SetValue(75);
dynamic_cast<Slider *>(element_->GetChild("CirrusSlider", true))->SetValue(60);
dynamic_cast<Slider *>(element_->GetChild("CumulusSlider", true))->SetValue(40);
dynamic_cast<Slider *>(element_->GetChild("CumulusBrightnessSlider", true))->SetValue(33);
dynamic_cast<Slider *>(element_->GetChild("SunSlider", true))->SetValue(10);
SubscribeToEvent(StringHash("Update"), URHO3D_HANDLER(AwesomeGameApplication, HandleUpdate));
SubscribeToEvent(toggle_, StringHash("Pressed"), URHO3D_HANDLER(AwesomeGameApplication, HandleToggle));
SubscribeToEvent(element_->GetChild("AddPresetButton", true), StringHash("Pressed"), URHO3D_HANDLER(AwesomeGameApplication, HandleAddPreset));
}
void Stop() override
{
// This step is executed when application is closing. No more frames will be rendered after this method is invoked.
}
float GetSliderValue(const ea::string &name, float rangelow, float rangehigh)
{
Slider *s=dynamic_cast<Slider *>(element_->GetChild(name+"Slider", true));
if(s)
{
float v=(s->GetValue()/100.0f);
v=rangelow + v * (rangehigh - rangelow);
Text *t=dynamic_cast<Text *>(element_->GetChild(name+"Value", true));
if(t)
{
t->SetText(ToString("%.4f", v));
}
return v;
}
return 0;
}
void Update(float timeStep)
{
//float timeofday{0};
auto cache=GetSubsystem<ResourceCache>();
float speedmul=0.1f;
SkyPreset p;
if(manual_)
{
p.Br_=GetSliderValue("Br", 0.0001, 0.009);
p.Bm_=GetSliderValue("Bm", 0.0001, 0.009);
p.g_=GetSliderValue("g", 0.9, 1.0);
p.cirrus_=GetSliderValue("Cirrus", 0.0, 3.5);
p.cumulus_=GetSliderValue("Cumulus", 0.0, 3.5);
p.cumulusbrightness_=GetSliderValue("CumulusBrightness", 0.25, 3.0);
timeofday_=GetSliderValue("Sun", 0.0, 24.0);
speedmul=GetSliderValue("Speed", 0.1, 2.0);
}
else
{
p = atmosphere_.Update(timeStep);
timeofday_ += timeStep*0.125f;
}
while (timeofday_ >=24.f) timeofday_ -= 24.f;
skyboxmaterial_->SetShaderParameter("TimeOfDay", Variant(timeofday_));
skyboxmaterial_->SetShaderParameter("Br", Variant(p.Br_));
skyboxmaterial_->SetShaderParameter("Bm", Variant(p.Bm_));
skyboxmaterial_->SetShaderParameter("G", Variant(p.g_));
skyboxmaterial_->SetShaderParameter("Cirrus", Variant(p.cirrus_));
skyboxmaterial_->SetShaderParameter("Cumulus", Variant(p.cumulus_));
skyboxmaterial_->SetShaderParameter("CumulusBrightness", Variant(p.cumulusbrightness_));
time_ += timeStep*speedmul;
skyboxmaterial_->SetShaderParameter("CloudTime", Variant(time_));
terrainmaterial_->SetShaderParameter("TimeOfDay", Variant(timeofday_));
terrainmaterial_->SetShaderParameter("Br", Variant(p.Br_));
terrainmaterial_->SetShaderParameter("Bm", Variant(p.Bm_));
Material *cliffmat=cache->GetResource<Material>("Materials/TriplanarCliff4.xml");
cliffmat->SetShaderParameter("TimeOfDay", Variant(timeofday_));
cliffmat->SetShaderParameter("Br", Variant(p.Br_));
cliffmat->SetShaderParameter("Bm", Variant(p.Bm_));
auto input=GetSubsystem<Input>();
if(input->GetMouseButtonDown(MOUSEB_RIGHT))
{
input->SetMouseVisible(false);
}
else
{
input->SetMouseVisible(true);
}
SunSettings sun=CalculateSunSettings(timeofday_, 0.1f, p);
zone_->SetFogColor(sun.fogcolor_);
zone_->SetAmbientColor(Color(sun.fogcolor_.r_*0.4f+0.1f, sun.fogcolor_.g_*0.4f+0.1f, sun.fogcolor_.b_*0.4f+0.15f));
lightNode_->SetDirection(-sun.sunpos_);
backLightNode_->SetDirection(sun.sunpos_);
light_->SetColor(sun.suncolor_);
float px=cameraNode_->GetWorldPosition().x_;
float pz=cameraNode_->GetWorldPosition().z_;
Vector3 ps((float)(int)(px), 0, (float)(int)(pz));
grassTestNode_->SetPosition(ps);
//auto cache=GetSubsystem<ResourceCache>();
Material *m=cache->GetResource<Material>("Materials/GrassTest.xml");
m->SetShaderParameter("ActualCameraPos", Variant(cameraNode_->GetWorldPosition()));
m=cache->GetResource<Material>("Materials/FlowerTest.xml");
m->SetShaderParameter("ActualCameraPos", Variant(cameraNode_->GetWorldPosition()));
MoveCamera(timeStep);
}
void MoveCamera(float timeStep)
{
// Do not move if the UI has a focused element (the console)
if (GetSubsystem<UI>()->GetFocusElement())
return;
auto* input = GetSubsystem<Input>();
// Movement speed as world units per second
const float MOVE_SPEED = 30.0f;
// Mouse sensitivity as degrees per pixel
const float MOUSE_SENSITIVITY = 0.1f;
// Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
if(input->GetMouseButtonDown(MOUSEB_RIGHT))
{
IntVector2 mouseMove = input->GetMouseMove();
yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
pitch_ = Clamp(pitch_, -90.0f, 90.0f);
// Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
//URHO3D_LOGINFOF("Pitch: %.2f", pitch_);
}
// Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
if (input->GetKeyDown(KEY_W))
cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_S))
cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_A))
cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_D))
cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
Octree *octree=scene_->GetComponent<Octree>();
float hitpos=0;
Drawable *hitdrawable=nullptr;
Vector3 ground;
Vector3 pos=cameraNode_->GetPosition();
Vector3 raypos=pos+Vector3(0,100.f,0);
Ray ray(raypos, pos-raypos);
static ea::vector<RayQueryResult> result;
result.clear();
RayOctreeQuery query(result, ray, RAY_TRIANGLE, 300.f, DRAWABLE_GEOMETRY, 2);
octree->Raycast(query);
if(result.size()!=0)
{
for(unsigned int i=0; i<result.size(); ++i)
{
if(result[i].distance_>=0)
{
ground=ray.origin_+ray.direction_*result[i].distance_;
pos.y_=ground.y_+6.f;//terrain_->GetHeight(pos) + 12.0;
cameraNode_->SetPosition(pos);
return;
}
}
}
//Vector3 pos=cameraNode_->GetPosition();
pos.y_=terrain_->GetHeight(pos) + 6.0;
cameraNode_->SetPosition(pos);
}
protected:
SharedPtr<Scene> scene_;
Node *cameraNode_, *lightNode_, *backLightNode_;
Camera *camera_;
float yaw_{0}, pitch_{-20.7f};
float time_{0};
Material *skyboxmaterial_{nullptr}, *terrainmaterial_{nullptr};
SharedPtr<UIElement> element_;
Zone *zone_{nullptr};
Light *light_{nullptr};
Terrain *terrain_;
bool manual_{true};
float timeofday_{0.f};
Node *grassTestNode_{nullptr};
AtmosphereSettings atmosphere_;
SharedPtr<UIElement> toggle_;
void HandleUpdate(StringHash eventType, VariantMap &eventData)
{
float timeStep=eventData["TimeStep"].GetFloat();
Update(timeStep);
}
void HandleToggle(StringHash eventType, VariantMap &eventData)
{
manual_= !manual_;
element_->SetVisible(manual_);
Text *t=dynamic_cast<Text *>(toggle_->GetChild("ToggleText", true));
if(t)
{
if(manual_) t->SetText("Manual");
else t->SetText("Auto");
}
}
void HandleAddPreset(StringHash eventType, VariantMap &eventData)
{
SkyPresetTemplate p;
p.Br_=GetSliderValue("Br", 0.0001, 0.009);
p.Bm_=GetSliderValue("Bm", 0.0001, 0.009);
p.g_=GetSliderValue("g", 0.9, 0.9999);
p.cumulusbrightness_=GetSliderValue("CumulusBrightness", 0.25, 3.0);
float cirrus=GetSliderValue("Cirrus", 0.0, 3.5);
float cumulus=GetSliderValue("Cumulus", 0.0, 3.5);
p.cirruslow_=cirrus*0.8f;
p.cirrushigh_=cirrus;
p.cumuluslow_=cumulus*0.8f;
p.cumulushigh_=cumulus;
atmosphere_.AddPreset(p);
}
};
// A helper macro which defines main function. Forgetting it will result in linker errors complaining about missing `_main` or `_WinMain@16`.
URHO3D_DEFINE_APPLICATION_MAIN(AwesomeGameApplication);