/
GfxActor.cpp
3230 lines (2919 loc) · 127 KB
/
GfxActor.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
Copyright 2016-2020 Petr Ohlidal
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GfxActor.h"
#include "ApproxMath.h"
#include "AirBrake.h"
#include "Actor.h"
#include "Collisions.h"
#include "DustPool.h" // General particle gfx
#include "EngineSim.h"
#include "GameContext.h"
#include "GfxScene.h"
#include "GUIManager.h"
#include "GUIUtils.h"
#include "HydraxWater.h"
#include "FlexAirfoil.h"
#include "FlexBody.h"
#include "FlexMeshWheel.h"
#include "FlexObj.h"
#include "InputEngine.h" // TODO: Keys shouldn't be queried from here, but buffered in sim. loop ~ only_a_ptr, 06/2018
#include "MeshObject.h"
#include "MovableText.h"
#include "OgreImGui.h"
#include "Renderdash.h" // classic 'renderdash' material
#include "ActorSpawner.h"
#include "SlideNode.h"
#include "SkyManager.h"
#include "SoundScriptManager.h"
#include "Terrain.h"
#include "TurboJet.h"
#include "TurboProp.h"
#include "Utils.h"
#include "imgui_internal.h"
#include <Ogre.h>
using namespace RoR;
RoR::GfxActor::GfxActor(ActorPtr actor, ActorSpawner* spawner, std::string ogre_resource_group,
RoR::Renderdash* renderdash):
m_actor(actor),
m_custom_resource_group(ogre_resource_group),
m_renderdash(renderdash)
{
// Setup particles
m_particles_drip = App::GetGfxScene()->GetDustPool("drip");
m_particles_dust = App::GetGfxScene()->GetDustPool("dust"); // Dust, water vapour, tyre smoke
m_particles_splash = App::GetGfxScene()->GetDustPool("splash");
m_particles_ripple = App::GetGfxScene()->GetDustPool("ripple");
m_particles_sparks = App::GetGfxScene()->GetDustPool("sparks");
m_particles_clump = App::GetGfxScene()->GetDustPool("clump");
}
RoR::GfxActor::~GfxActor()
{
// Dispose videocameras
this->SetVideoCamState(VideoCamState::VCSTATE_DISABLED);
while (!m_videocameras.empty())
{
VideoCamera& vcam = m_videocameras.back();
Ogre::TextureManager::getSingleton().remove(vcam.vcam_render_tex->getHandle());
vcam.vcam_render_tex.setNull();
vcam.vcam_render_target = nullptr; // Invalidated with parent texture
App::GetGfxScene()->GetSceneManager()->destroyCamera(vcam.vcam_ogre_camera);
m_videocameras.pop_back();
}
// Dispose rods
if (m_gfx_beams_parent_scenenode != nullptr)
{
for (BeamGfx& rod: m_gfx_beams)
{
Ogre::MovableObject* ogre_object = rod.rod_scenenode->getAttachedObject(0);
rod.rod_scenenode->detachAllObjects();
App::GetGfxScene()->GetSceneManager()->destroyEntity(static_cast<Ogre::Entity*>(ogre_object));
}
m_gfx_beams.clear();
m_gfx_beams_parent_scenenode->removeAndDestroyAllChildren();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(m_gfx_beams_parent_scenenode);
m_gfx_beams_parent_scenenode = nullptr;
}
// delete meshwheels
for (size_t i = 0; i < m_wheels.size(); i++)
{
if (m_wheels[i].wx_flex_mesh != nullptr)
{
delete m_wheels[i].wx_flex_mesh;
}
if (m_wheels[i].wx_scenenode != nullptr)
{
m_wheels[i].wx_scenenode->removeAndDestroyAllChildren();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(m_wheels[i].wx_scenenode);
}
}
// delete airbrakes
for (AirbrakeGfx& abx: m_gfx_airbrakes)
{
// scene node
abx.abx_scenenode->detachAllObjects();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(abx.abx_scenenode);
// entity
App::GetGfxScene()->GetSceneManager()->destroyEntity(abx.abx_entity);
// mesh
Ogre::MeshManager::getSingleton().remove(abx.abx_mesh);
}
m_gfx_airbrakes.clear();
// Delete props
for (Prop & prop: m_props)
{
for (int k = 0; k < 4; ++k)
{
if (prop.pp_beacon_scene_node[k])
{
Ogre::SceneNode* scene_node = prop.pp_beacon_scene_node[k];
scene_node->removeAndDestroyAllChildren();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(scene_node);
}
if (prop.pp_beacon_light[k])
{
App::GetGfxScene()->GetSceneManager()->destroyLight(prop.pp_beacon_light[k]);
}
}
if (prop.pp_scene_node)
{
prop.pp_scene_node->removeAndDestroyAllChildren();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(prop.pp_scene_node);
}
if (prop.pp_wheel_scene_node)
{
prop.pp_wheel_scene_node->removeAndDestroyAllChildren();
App::GetGfxScene()->GetSceneManager()->destroySceneNode(prop.pp_wheel_scene_node);
}
if (prop.pp_mesh_obj)
{
delete prop.pp_mesh_obj;
}
if (prop.pp_wheel_mesh_obj)
{
delete prop.pp_wheel_mesh_obj;
}
}
m_props.clear();
// Delete flexbodies
for (FlexBody* fb: m_flexbodies)
{
delete fb;
}
// Delete old cab mesh
if (m_cab_mesh != nullptr)
{
m_cab_scene_node->detachAllObjects();
m_cab_scene_node->getParentSceneNode()->removeAndDestroyChild(m_cab_scene_node);
m_cab_scene_node = nullptr;
m_cab_entity->_getManager()->destroyEntity(m_cab_entity);
m_cab_entity = nullptr;
delete m_cab_mesh; // Unloads the ManualMesh resource; do this last
m_cab_mesh = nullptr;
}
// Delete old dashboard RTT
if (m_renderdash != nullptr)
{
delete m_renderdash;
}
}
ActorPtr RoR::GfxActor::GetActor()
{
return m_actor;
}
void RoR::GfxActor::SetMaterialFlareOn(int flare_index, bool state_on)
{
for (FlareMaterial& entry: m_flare_materials)
{
if (entry.flare_index != flare_index)
{
continue;
}
const int num_techniques = static_cast<int>(entry.mat_instance->getNumTechniques());
for (int i = 0; i < num_techniques; i++)
{
Ogre::Technique* tech = entry.mat_instance->getTechnique(i);
if (!tech)
continue;
if (tech->getSchemeName() == "glow")
{
// glowing technique
// set the ambient value as glow amount
Ogre::Pass* p = tech->getPass(0);
if (!p)
continue;
if (state_on)
{
p->setSelfIllumination(entry.emissive_color);
p->setAmbient(Ogre::ColourValue::White);
p->setDiffuse(Ogre::ColourValue::White);
}
else
{
p->setSelfIllumination(Ogre::ColourValue::ZERO);
p->setAmbient(Ogre::ColourValue::Black);
p->setDiffuse(Ogre::ColourValue::Black);
}
}
else
{
// normal technique
Ogre::Pass* p = tech->getPass(0);
if (!p)
continue;
Ogre::TextureUnitState* tus = p->getTextureUnitState(0);
if (!tus)
continue;
if (tus->getNumFrames() < 2)
continue;
int frame = state_on ? 1 : 0;
tus->setCurrentFrame(frame);
if (state_on)
p->setSelfIllumination(entry.emissive_color);
else
p->setSelfIllumination(Ogre::ColourValue::ZERO);
}
} // for each technique
}
}
void RoR::GfxActor::RegisterCabMaterial(Ogre::MaterialPtr mat, Ogre::MaterialPtr mat_trans)
{
// Material instances of this actor
m_cab_mat_visual = mat;
m_cab_mat_visual_trans = mat_trans;
if (mat->getTechnique(0)->getNumPasses() == 1)
return; // No emissive pass -> nothing to do.
m_cab_mat_template_emissive = mat->clone("CabMaterialEmissive-" + mat->getName(), true, m_custom_resource_group);
m_cab_mat_template_plain = mat->clone("CabMaterialPlain-" + mat->getName(), true, m_custom_resource_group);
m_cab_mat_template_plain->getTechnique(0)->removePass(1);
m_cab_mat_template_plain->compile();
}
void RoR::GfxActor::SetCabLightsActive(bool state_on)
{
if (m_cab_mat_template_emissive.isNull()) // Both this and '_plain' are only set when emissive pass is present.
return;
// NOTE: Updating material in-place like this is probably inefficient,
// but in order to maintain all the existing material features working together,
// we need to avoid any material swapping on runtime. ~ only_a_ptr, 05/2017
Ogre::MaterialPtr template_mat = (state_on) ? m_cab_mat_template_emissive : m_cab_mat_template_plain;
Ogre::Technique* dest_tech = m_cab_mat_visual->getTechnique(0);
Ogre::Technique* templ_tech = template_mat->getTechnique(0);
dest_tech->removeAllPasses();
for (unsigned short i = 0; i < templ_tech->getNumPasses(); ++i)
{
Ogre::Pass* templ_pass = templ_tech->getPass(i);
Ogre::Pass* dest_pass = dest_tech->createPass();
*dest_pass = *templ_pass; // Copy the pass! Reference: http://www.ogre3d.org/forums/viewtopic.php?f=2&t=83453
}
m_cab_mat_visual->compile();
}
void RoR::GfxActor::SetVideoCamState(VideoCamState state)
{
if (state == m_vidcam_state)
{
return; // Nothing to do.
}
const bool enable = (state == VideoCamState::VCSTATE_ENABLED_ONLINE);
for (VideoCamera vidcam: m_videocameras)
{
if (vidcam.vcam_render_target != nullptr)
{
vidcam.vcam_render_target->setActive(enable);
if (enable)
vidcam.vcam_material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(vidcam.vcam_render_tex->getName());
else
vidcam.vcam_material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(vidcam.vcam_off_tex_name);
continue;
}
if (vidcam.vcam_render_window != nullptr)
{
vidcam.vcam_render_window->setActive(enable);
}
}
m_vidcam_state = state;
}
void RoR::GfxActor::UpdateVideoCameras(float dt_sec)
{
if (m_vidcam_state != VideoCamState::VCSTATE_ENABLED_ONLINE)
return;
for (VideoCamera& vidcam: m_videocameras)
{
#ifdef USE_CAELUM
// caelum needs to know that we changed the cameras
SkyManager* sky = App::GetGameContext()->GetTerrain()->getSkyManager();
if ((sky != nullptr) && (RoR::App::app_state->getEnum<AppState>() == RoR::AppState::SIMULATION))
{
sky->NotifySkyCameraChanged(vidcam.vcam_ogre_camera);
}
#endif // USE_CAELUM
if ((vidcam.vcam_type == VCTYPE_MIRROR_PROP_LEFT)
|| (vidcam.vcam_type == VCTYPE_MIRROR_PROP_RIGHT))
{
// Mirror prop - special processing.
float mirror_angle = 0.f;
Ogre::Vector3 offset(Ogre::Vector3::ZERO);
if (vidcam.vcam_type == VCTYPE_MIRROR_PROP_LEFT)
{
mirror_angle = m_actor->ar_left_mirror_angle;
offset = Ogre::Vector3(0.07f, -0.22f, 0);
}
else
{
mirror_angle = m_actor->ar_right_mirror_angle;
offset = Ogre::Vector3(0.07f, +0.22f, 0);
}
Ogre::Vector3 normal = vidcam.vcam_prop_scenenode->getOrientation()
* Ogre::Vector3(cos(mirror_angle), sin(mirror_angle), 0.0f);
Ogre::Vector3 center = vidcam.vcam_prop_scenenode->getPosition()
+ vidcam.vcam_prop_scenenode->getOrientation() * offset;
Ogre::Radian roll = Ogre::Degree(360)
- Ogre::Radian(asin(m_actor->getDirection().dotProduct(Ogre::Vector3::UNIT_Y)));
Ogre::Plane plane = Ogre::Plane(normal, center);
Ogre::Vector3 project = plane.projectVector(App::GetCameraManager()->GetCameraNode()->getPosition() - center);
vidcam.vcam_ogre_camera->setPosition(center);
vidcam.vcam_ogre_camera->lookAt(App::GetCameraManager()->GetCameraNode()->getPosition() - 2.0f * project);
vidcam.vcam_ogre_camera->roll(roll);
vidcam.vcam_ogre_camera->setNearClipDistance(1); // fixes Caelum sky rendered black on mirrors
continue; // Done processing mirror prop.
}
// update the texture now, otherwise shuttering
if (vidcam.vcam_render_target != nullptr)
vidcam.vcam_render_target->update();
if (vidcam.vcam_render_window != nullptr)
vidcam.vcam_render_window->update();
// get the normal of the camera plane now
const Ogre::Vector3 abs_pos_center = m_simbuf.simbuf_nodes[vidcam.vcam_node_center].AbsPosition;
const Ogre::Vector3 abs_pos_z = m_simbuf.simbuf_nodes[vidcam.vcam_node_dir_z].AbsPosition;
const Ogre::Vector3 abs_pos_y = m_simbuf.simbuf_nodes[vidcam.vcam_node_dir_y].AbsPosition;
Ogre::Vector3 normal = (-(abs_pos_center - abs_pos_z)).crossProduct(-(abs_pos_center - abs_pos_y));
normal.normalise();
// add user set offset
Ogre::Vector3 pos = m_simbuf.simbuf_nodes[vidcam.vcam_node_alt_pos].AbsPosition +
(vidcam.vcam_pos_offset.x * normal) +
(vidcam.vcam_pos_offset.y * (abs_pos_center - abs_pos_y)) +
(vidcam.vcam_pos_offset.z * (abs_pos_center - abs_pos_z));
//avoid the camera roll
// camup orientates to frustrum of world by default -> rotating the cam related to trucks yaw, lets bind cam rotation videocamera base (nref,ny,nz) as frustum
// could this be done faster&better with a plane setFrustumExtents ?
Ogre::Vector3 frustumUP = abs_pos_center - abs_pos_y;
frustumUP.normalise();
vidcam.vcam_ogre_camera->setFixedYawAxis(true, frustumUP);
if (vidcam.vcam_type == VCTYPE_MIRROR)
{
//rotate the normal of the mirror by user rotation setting so it reflects correct
normal = vidcam.vcam_rotation * normal;
// merge camera direction and reflect it on our plane
vidcam.vcam_ogre_camera->setDirection((pos - App::GetCameraManager()->GetCameraNode()->getPosition()).reflect(normal));
}
else if (vidcam.vcam_type == VCTYPE_VIDEOCAM)
{
// rotate the camera according to the nodes orientation and user rotation
Ogre::Vector3 refx = abs_pos_z - abs_pos_center;
refx.normalise();
Ogre::Vector3 refy = abs_pos_center - abs_pos_y;
refy.normalise();
Ogre::Quaternion rot = Ogre::Quaternion(-refx, -refy, -normal);
vidcam.vcam_ogre_camera->setOrientation(rot * vidcam.vcam_rotation); // rotate the camera orientation towards the calculated cam direction plus user rotation
}
else if (vidcam.vcam_type == VCTYPE_TRACKING_VIDEOCAM)
{
normal = m_simbuf.simbuf_nodes[vidcam.vcam_node_lookat].AbsPosition - pos;
normal.normalise();
Ogre::Vector3 refx = abs_pos_z - abs_pos_center;
refx.normalise();
// why does this flip ~2-3� around zero orientation and only with trackercam. back to slower crossproduct calc, a bit slower but better .. sigh
// Ogre::Vector3 refy = abs_pos_center - abs_pos_y;
Ogre::Vector3 refy = refx.crossProduct(normal);
refy.normalise();
Ogre::Quaternion rot = Ogre::Quaternion(-refx, -refy, -normal);
vidcam.vcam_ogre_camera->setOrientation(rot * vidcam.vcam_rotation); // rotate the camera orientation towards the calculated cam direction plus user rotation
}
if (vidcam.vcam_debug_node != nullptr)
{
vidcam.vcam_debug_node->setPosition(pos);
vidcam.vcam_debug_node->setOrientation(vidcam.vcam_ogre_camera->getOrientation());
}
// set the new position
vidcam.vcam_ogre_camera->setPosition(pos);
}
}
void RoR::GfxActor::UpdateParticles(float dt_sec)
{
float water_height = 0.f; // Unused if terrain has no water
if (App::GetGameContext()->GetTerrain()->getWater() != nullptr)
{
water_height = App::GetGameContext()->GetTerrain()->getWater()->GetStaticWaterHeight();
}
for (NodeGfx& nfx: m_gfx_nodes)
{
const node_t& n = m_actor->ar_nodes[nfx.nx_node_idx];
// 'Wet' effects - water dripping and vapour
if (nfx.nx_may_get_wet && !nfx.nx_no_particles)
{
// Getting out of water?
if (!n.nd_under_water && nfx.nx_under_water_prev)
{
nfx.nx_wet_time_sec = 0.f;
}
// Dripping water?
if (nfx.nx_wet_time_sec != -1)
{
nfx.nx_wet_time_sec += dt_sec;
if (nfx.nx_wet_time_sec > 5.f) // Dries off in 5 sec
{
nfx.nx_wet_time_sec = -1.f;
}
else if (nfx.nx_may_get_wet)
{
if (m_particles_drip != nullptr)
{
m_particles_drip->allocDrip(n.AbsPosition, n.Velocity, nfx.nx_wet_time_sec); // Dripping water particles
}
if (nfx.nx_is_hot && m_particles_dust != nullptr)
{
m_particles_dust->allocVapour(n.AbsPosition, n.Velocity, nfx.nx_wet_time_sec); // Water vapour particles
}
}
}
}
// Water splash and ripple
if (n.nd_under_water && !nfx.nx_no_particles)
{
if ((water_height - n.AbsPosition.y < 0.2f) && (n.Velocity.squaredLength() > 4.f))
{
if (m_particles_splash)
{
m_particles_splash->allocSplash(n.AbsPosition, n.Velocity);
}
if (m_particles_ripple)
{
m_particles_ripple->allocRipple(n.AbsPosition, n.Velocity);
}
}
}
// Ground collision (dust, sparks, tyre smoke, clumps...)
if (!nfx.nx_no_particles && n.nd_has_ground_contact && n.nd_last_collision_gm != nullptr)
{
switch (n.nd_last_collision_gm->fx_type)
{
case Collisions::FX_DUSTY:
if (m_particles_dust != nullptr)
{
m_particles_dust->malloc(n.AbsPosition, n.Velocity / 2.0, n.nd_last_collision_gm->fx_colour);
}
break;
case Collisions::FX_CLUMPY:
if (m_particles_clump != nullptr && n.Velocity.squaredLength() > 1.f)
{
m_particles_clump->allocClump(n.AbsPosition, n.Velocity / 2.0, n.nd_last_collision_gm->fx_colour);
}
break;
case Collisions::FX_HARD:
if (n.nd_tyre_node) // skidmarks and tyre smoke
{
const float SMOKE_THRESHOLD = 8.f;
const float SCREECH_THRESHOLD = 5.f;
const float slipv = n.nd_last_collision_slip.length();
const float screech = std::min(slipv, n.nd_avg_collision_slip) - SCREECH_THRESHOLD;
if (screech > 0.0f)
{
SOUND_MODULATE(m_actor, SS_MOD_SCREETCH, screech / SCREECH_THRESHOLD);
SOUND_PLAY_ONCE(m_actor, SS_TRIG_SCREETCH);
}
if (m_particles_dust != nullptr && n.nd_avg_collision_slip > SMOKE_THRESHOLD)
{
m_particles_dust->allocSmoke(n.AbsPosition, n.Velocity);
}
}
else if (!nfx.nx_no_sparks) // Not a wheel => sparks
{
if (m_particles_sparks != nullptr && n.nd_avg_collision_slip > 5.f)
{
if (n.nd_last_collision_slip.squaredLength() > 25.f)
{
m_particles_sparks->allocSparks(n.AbsPosition, n.Velocity);
}
}
}
break;
default:;
}
}
nfx.nx_under_water_prev = n.nd_under_water;
}
}
const ImU32 BEAM_COLOR (0xff556633); // All colors are in ABGR format (alpha, blue, green, red)
const float BEAM_THICKNESS (1.2f);
const ImU32 BEAM_BROKEN_COLOR (0xff4466dd);
const float BEAM_BROKEN_THICKNESS (1.8f);
const ImU32 BEAM_HYDRO_COLOR (0xff55a3e0);
const float BEAM_HYDRO_THICKNESS (1.4f);
const ImU32 BEAM_STRENGTH_TEXT_COLOR (0xffcfd0cc);
const ImU32 BEAM_STRESS_TEXT_COLOR (0xff58bbfc);
// TODO: commands cannot be distinguished on runtime
const ImU32 NODE_COLOR (0xff44ddff);
const float NODE_RADIUS (2.f);
const ImU32 NODE_TEXT_COLOR (0xffcccccf); // node ID text color
const ImU32 NODE_MASS_TEXT_COLOR (0xff77bb66);
const ImU32 NODE_IMMOVABLE_COLOR (0xff0033ff);
const float NODE_IMMOVABLE_RADIUS (2.8f);
void RoR::GfxActor::UpdateDebugView()
{
if (m_debug_view == DebugViewType::DEBUGVIEW_NONE && !m_actor->ar_physics_paused)
{
return; // Nothing to do
}
// Var
ImVec2 screen_size = ImGui::GetIO().DisplaySize;
World2ScreenConverter world2screen(
App::GetCameraManager()->GetCamera()->getViewMatrix(true), App::GetCameraManager()->GetCamera()->getProjectionMatrix(), Ogre::Vector2(screen_size.x, screen_size.y));
ImDrawList* drawlist = GetImDummyFullscreenWindow();
if (m_actor->ar_physics_paused && !App::GetGuiManager()->IsGuiHidden())
{
// Should we replace this circle with a proper bounding box?
Ogre::Vector3 pos_xyz = world2screen.Convert(m_actor->getPosition());
if (pos_xyz.z < 0.f)
{
ImVec2 pos(pos_xyz.x, pos_xyz.y);
float radius = 0.0f;
for (int i = 0; i < m_actor->ar_num_nodes; ++i)
{
radius = std::max(radius, pos_xyz.distance(world2screen.Convert(m_actor->ar_nodes[i].AbsPosition)));
}
drawlist->AddCircleFilled(pos, radius * 1.05f, 0x22222222, 36);
}
}
// Skeleton display. NOTE: Order matters, it determines Z-ordering on render
if ((m_debug_view == DebugViewType::DEBUGVIEW_SKELETON) ||
(m_debug_view == DebugViewType::DEBUGVIEW_NODES) ||
(m_debug_view == DebugViewType::DEBUGVIEW_BEAMS))
{
// Beams
const beam_t* beams = m_actor->ar_beams;
const size_t num_beams = static_cast<size_t>(m_actor->ar_num_beams);
for (size_t i = 0; i < num_beams; ++i)
{
if (App::diag_hide_wheels->getBool() &&
(beams[i].p1->nd_tyre_node || beams[i].p1->nd_rim_node ||
beams[i].p2->nd_tyre_node || beams[i].p2->nd_rim_node))
continue;
Ogre::Vector3 pos1 = world2screen.Convert(beams[i].p1->AbsPosition);
Ogre::Vector3 pos2 = world2screen.Convert(beams[i].p2->AbsPosition);
if ((pos1.z < 0.f) && (pos2.z < 0.f))
{
ImVec2 pos1xy(pos1.x, pos1.y);
ImVec2 pos2xy(pos2.x, pos2.y);
if (beams[i].bm_broken)
{
if (!App::diag_hide_broken_beams->getBool())
{
drawlist->AddLine(pos1xy, pos2xy, BEAM_BROKEN_COLOR, BEAM_BROKEN_THICKNESS);
}
}
else if (beams[i].bm_type == BEAM_HYDRO)
{
if (!beams[i].bm_disabled)
{
drawlist->AddLine(pos1xy, pos2xy, BEAM_HYDRO_COLOR, BEAM_HYDRO_THICKNESS);
}
}
else
{
ImU32 color = BEAM_COLOR;
if (!App::diag_hide_beam_stress->getBool())
{
if (beams[i].stress > 0)
{
float stress_ratio = pow(beams[i].stress / beams[i].maxposstress, 2.0f);
float s = std::min(stress_ratio, 1.0f);
color = Ogre::ColourValue(0.2f * (1 + 2.0f * s), 0.4f * (1.0f - s), 0.33f, 1.0f).getAsABGR();
}
else if (beams[i].stress < 0)
{
float stress_ratio = pow(beams[i].stress / beams[i].maxnegstress, 2.0f);
float s = std::min(stress_ratio, 1.0f);
color = Ogre::ColourValue(0.2f, 0.4f * (1.0f - s), 0.33f * (1 + 1.0f * s), 1.0f).getAsABGR();
}
}
drawlist->AddLine(pos1xy, pos2xy, color, BEAM_THICKNESS);
}
}
}
if (!App::diag_hide_nodes->getBool())
{
// Nodes
const node_t* nodes = m_actor->ar_nodes;
const size_t num_nodes = static_cast<size_t>(m_actor->ar_num_nodes);
for (size_t i = 0; i < num_nodes; ++i)
{
if (App::diag_hide_wheels->getBool() && (nodes[i].nd_tyre_node || nodes[i].nd_rim_node))
continue;
Ogre::Vector3 pos_xyz = world2screen.Convert(nodes[i].AbsPosition);
if (pos_xyz.z < 0.f)
{
ImVec2 pos(pos_xyz.x, pos_xyz.y);
if (nodes[i].nd_immovable)
{
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, NODE_IMMOVABLE_COLOR);
}
else
{
drawlist->AddCircleFilled(pos, NODE_RADIUS, NODE_COLOR);
}
}
}
// Node info; drawn after nodes to have higher Z-order
if ((m_debug_view == DebugViewType::DEBUGVIEW_NODES) || (m_debug_view == DebugViewType::DEBUGVIEW_BEAMS))
{
for (size_t i = 0; i < num_nodes; ++i)
{
if ((App::diag_hide_wheels->getBool() || App::diag_hide_wheel_info->getBool()) &&
(nodes[i].nd_tyre_node || nodes[i].nd_rim_node))
continue;
Ogre::Vector3 pos = world2screen.Convert(nodes[i].AbsPosition);
if (pos.z < 0.f)
{
ImVec2 pos_xy(pos.x, pos.y);
Str<25> id_buf;
id_buf << nodes[i].pos;
drawlist->AddText(pos_xy, NODE_TEXT_COLOR, id_buf.ToCStr());
if (m_debug_view != DebugViewType::DEBUGVIEW_BEAMS)
{
char mass_buf[50];
snprintf(mass_buf, 50, "|%.1fKg", nodes[i].mass);
ImVec2 offset = ImGui::CalcTextSize(id_buf.ToCStr());
drawlist->AddText(ImVec2(pos.x + offset.x, pos.y), NODE_MASS_TEXT_COLOR, mass_buf);
}
}
}
}
}
// Beam-info: drawn after beams to have higher Z-order
if (m_debug_view == DebugViewType::DEBUGVIEW_BEAMS)
{
for (size_t i = 0; i < num_beams; ++i)
{
if ((App::diag_hide_wheels->getBool() || App::diag_hide_wheel_info->getBool()) &&
(beams[i].p1->nd_tyre_node || beams[i].p1->nd_rim_node ||
beams[i].p2->nd_tyre_node || beams[i].p2->nd_rim_node))
continue;
// Position
Ogre::Vector3 world_pos = (beams[i].p1->AbsPosition + beams[i].p2->AbsPosition) / 2.f;
Ogre::Vector3 pos_xyz = world2screen.Convert(world_pos);
if (pos_xyz.z >= 0.f)
{
continue; // Behind the camera
}
ImVec2 pos(pos_xyz.x, pos_xyz.y);
// Strength is usually in thousands or millions - we shorten it.
const size_t BUF_LEN = 50;
char buf[BUF_LEN];
if (beams[i].strength >= 1000000000000.f)
{
snprintf(buf, BUF_LEN, "%.1fT", (beams[i].strength / 1000000000000.f));
}
else if (beams[i].strength >= 1000000000.f)
{
snprintf(buf, BUF_LEN, "%.1fG", (beams[i].strength / 1000000000.f));
}
else if (beams[i].strength >= 1000000.f)
{
snprintf(buf, BUF_LEN, "%.1fM", (beams[i].strength / 1000000.f));
}
else if (beams[i].strength >= 1000.f)
{
snprintf(buf, BUF_LEN, "%.1fK", (beams[i].strength / 1000.f));
}
else
{
snprintf(buf, BUF_LEN, "%.1f", beams[i].strength);
}
const ImVec2 stren_text_size = ImGui::CalcTextSize(buf);
drawlist->AddText(ImVec2(pos.x - stren_text_size.x, pos.y), BEAM_STRENGTH_TEXT_COLOR, buf);
// Stress
snprintf(buf, BUF_LEN, "|%.1f", beams[i].stress);
drawlist->AddText(pos, BEAM_STRESS_TEXT_COLOR, buf);
}
}
} else if (m_debug_view == DebugViewType::DEBUGVIEW_WHEELS)
{
// Wheels
const wheel_t* wheels = m_actor->ar_wheels;
const size_t num_wheels = static_cast<size_t>(m_actor->ar_num_wheels);
for (int i = 0; i < num_wheels; i++)
{
Ogre::Vector3 axis = wheels[i].wh_axis_node_1->RelPosition - wheels[i].wh_axis_node_0->RelPosition;
axis.normalise();
// Wheel axle
{
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_axis_node_1->AbsPosition);
if (pos1_xyz.z < 0.f)
{
ImVec2 pos(pos1_xyz.x, pos1_xyz.y);
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, NODE_COLOR);
}
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_axis_node_0->AbsPosition);
if (pos2_xyz.z < 0.f)
{
ImVec2 pos(pos2_xyz.x, pos2_xyz.y);
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, NODE_COLOR);
}
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, BEAM_COLOR, BEAM_BROKEN_THICKNESS);
}
// Id, Speed, Torque
Ogre::Vector3 pos_xyz = pos1_xyz.midPoint(pos2_xyz);
if (pos_xyz.z < 0.f)
{
float v = ImGui::GetTextLineHeightWithSpacing();
ImVec2 pos(pos_xyz.x, pos_xyz.y);
Str<25> wheel_id_buf;
wheel_id_buf << "Id: " << (i + 1);
float h1 = ImGui::CalcTextSize(wheel_id_buf.ToCStr()).x / 2.0f;
drawlist->AddText(ImVec2(pos.x - h1, pos.y), NODE_TEXT_COLOR, wheel_id_buf.ToCStr());
Str<25> rpm_buf;
rpm_buf << "Speed: " << static_cast<int>(Round(wheels[i].debug_rpm)) << " rpm";
float h2 = ImGui::CalcTextSize(rpm_buf.ToCStr()).x / 2.0f;
drawlist->AddText(ImVec2(pos.x - h2, pos.y + v), NODE_TEXT_COLOR, rpm_buf.ToCStr());
Str<25> torque_buf;
torque_buf << "Torque: " << static_cast<int>(Round(wheels[i].debug_torque)) << " Nm";
float h3 = ImGui::CalcTextSize(torque_buf.ToCStr()).x / 2.0f;
drawlist->AddText(ImVec2(pos.x - h3, pos.y + v + v), NODE_TEXT_COLOR, torque_buf.ToCStr());
}
}
Ogre::Vector3 rradius = wheels[i].wh_arm_node->RelPosition - wheels[i].wh_near_attach_node->RelPosition;
// Reference arm
{
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_arm_node->AbsPosition);
if (pos1_xyz.z < 0.f)
{
ImVec2 pos(pos1_xyz.x, pos1_xyz.y);
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, NODE_IMMOVABLE_COLOR);
}
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition);
if (pos2_xyz.z < 0.f)
{
ImVec2 pos(pos2_xyz.x, pos2_xyz.y);
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, NODE_COLOR);
}
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, BEAM_BROKEN_COLOR, BEAM_BROKEN_THICKNESS);
}
}
Ogre::Vector3 radius = Ogre::Plane(axis, wheels[i].wh_near_attach_node->RelPosition).projectVector(rradius);
// Projection plane
#if 0
{
Ogre::Vector3 up = axis.crossProduct(radius);
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition + radius - up);
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition + radius + up);
Ogre::Vector3 pos3_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition - radius + up);
Ogre::Vector3 pos4_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition - radius - up);
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f) && (pos3_xyz.z < 0.f) && (pos4_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
ImVec2 pos3xy(pos3_xyz.x, pos3_xyz.y);
ImVec2 pos4xy(pos4_xyz.x, pos4_xyz.y);
drawlist->AddQuadFilled(pos1xy, pos2xy, pos3xy, pos4xy, 0x22888888);
}
}
#endif
// Projected reference arm & error arm
{
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition);
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition + radius);
Ogre::Vector3 pos3_xyz = world2screen.Convert(wheels[i].wh_arm_node->AbsPosition);
if (pos2_xyz.z < 0.f)
{
ImVec2 pos(pos2_xyz.x, pos2_xyz.y);
drawlist->AddCircleFilled(pos, NODE_IMMOVABLE_RADIUS, 0x660033ff);
}
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, 0x664466dd, BEAM_BROKEN_THICKNESS);
}
if ((pos2_xyz.z < 0.f) && (pos3_xyz.z < 0.f))
{
ImVec2 pos1xy(pos2_xyz.x, pos2_xyz.y);
ImVec2 pos2xy(pos3_xyz.x, pos3_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, 0x99555555, BEAM_BROKEN_THICKNESS);
}
}
// Reaction torque
{
Ogre::Vector3 cforce = wheels[i].debug_scaled_cforce;
{
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_arm_node->AbsPosition);
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_arm_node->AbsPosition - cforce);
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, 0xffcc4444, BEAM_BROKEN_THICKNESS);
}
}
{
Ogre::Vector3 pos1_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition);
Ogre::Vector3 pos2_xyz = world2screen.Convert(wheels[i].wh_near_attach_node->AbsPosition + cforce);
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, 0xffcc4444, BEAM_BROKEN_THICKNESS);
}
}
}
// Wheel slip
{
Ogre::Vector3 m = wheels[i].wh_axis_node_0->AbsPosition.midPoint(wheels[i].wh_axis_node_1->AbsPosition);
Ogre::Real w = wheels[i].wh_axis_node_0->AbsPosition.distance(m);
Ogre::Vector3 u = - axis.crossProduct(m_simbuf.simbuf_direction);
if (!wheels[i].debug_force.isZeroLength())
{
u = - wheels[i].debug_force.normalisedCopy();
}
Ogre::Vector3 f = axis.crossProduct(u);
Ogre::Vector3 a = - axis * w + f * std::max(w, wheels[i].wh_radius * 0.5f);
Ogre::Vector3 b = + axis * w + f * std::max(w, wheels[i].wh_radius * 0.5f);
Ogre::Vector3 c = + axis * w - f * std::max(w, wheels[i].wh_radius * 0.5f);
Ogre::Vector3 d = - axis * w - f * std::max(w, wheels[i].wh_radius * 0.5f);
Ogre::Quaternion r = Ogre::Quaternion::IDENTITY;
if (wheels[i].debug_vel.length() > 1.0f)
{
r = Ogre::Quaternion(f.angleBetween(wheels[i].debug_vel), u);
}
Ogre::Vector3 pos1_xyz = world2screen.Convert(m - u * wheels[i].wh_radius + r * a);
Ogre::Vector3 pos2_xyz = world2screen.Convert(m - u * wheels[i].wh_radius + r * b);
Ogre::Vector3 pos3_xyz = world2screen.Convert(m - u * wheels[i].wh_radius + r * c);
Ogre::Vector3 pos4_xyz = world2screen.Convert(m - u * wheels[i].wh_radius + r * d);
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f) && (pos3_xyz.z < 0.f) && (pos4_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
ImVec2 pos3xy(pos3_xyz.x, pos3_xyz.y);
ImVec2 pos4xy(pos4_xyz.x, pos4_xyz.y);
if (!wheels[i].debug_force.isZeroLength())
{
float slipv = wheels[i].debug_slip.length();
float wheelv = wheels[i].debug_vel.length();
float slip_ratio = std::min(slipv, wheelv) / std::max(1.0f, wheelv);
float scale = pow(slip_ratio, 2);
ImU32 col = Ogre::ColourValue(scale, 1.0f - scale, 0.0f, 0.2f).getAsABGR();
drawlist->AddQuadFilled(pos1xy, pos2xy, pos3xy, pos4xy, col);
}
else
{
drawlist->AddQuadFilled(pos1xy, pos2xy, pos3xy, pos4xy, 0x55555555);
}
}
}
// Slip vector
if (!wheels[i].debug_vel.isZeroLength())
{
Ogre::Vector3 m = wheels[i].wh_axis_node_0->AbsPosition.midPoint(wheels[i].wh_axis_node_1->AbsPosition);
Ogre::Real w = wheels[i].wh_axis_node_0->AbsPosition.distance(m);
Ogre::Vector3 d = axis.crossProduct(m_simbuf.simbuf_direction) * wheels[i].wh_radius;
Ogre::Real slipv = wheels[i].debug_slip.length();
Ogre::Real wheelv = wheels[i].debug_vel.length();
Ogre::Vector3 s = wheels[i].debug_slip * (std::min(slipv, wheelv) / std::max(1.0f, wheelv)) / slipv;
Ogre::Vector3 pos1_xyz = world2screen.Convert(m + d);
Ogre::Vector3 pos2_xyz = world2screen.Convert(m + d + s * std::max(w, wheels[i].wh_radius * 0.5f));
if ((pos1_xyz.z < 0.f) && (pos2_xyz.z < 0.f))
{
ImVec2 pos1xy(pos1_xyz.x, pos1_xyz.y);
ImVec2 pos2xy(pos2_xyz.x, pos2_xyz.y);
drawlist->AddLine(pos1xy, pos2xy, 0xbb4466dd, BEAM_BROKEN_THICKNESS);
}
}
// Down force
{
Ogre::Real f = wheels[i].debug_force.length();
Ogre::Real mass = m_actor->getTotalMass(false) * num_wheels;
Ogre::Vector3 normalised_force = wheels[i].debug_force.normalisedCopy() * std::min(f / mass, 1.0f);
Ogre::Vector3 m = wheels[i].wh_axis_node_0->AbsPosition.midPoint(wheels[i].wh_axis_node_1->AbsPosition);
Ogre::Vector3 pos5_xyz = world2screen.Convert(m);
Ogre::Vector3 pos6_xyz = world2screen.Convert(m + normalised_force * wheels[i].wh_radius);
if ((pos5_xyz.z < 0.f) && (pos6_xyz.z < 0.f))
{
ImVec2 pos1xy(pos5_xyz.x, pos5_xyz.y);
ImVec2 pos2xy(pos6_xyz.x, pos6_xyz.y);