-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathOgre2DepthCamera.cc
More file actions
1276 lines (1113 loc) · 42.5 KB
/
Ogre2DepthCamera.cc
File metadata and controls
1276 lines (1113 loc) · 42.5 KB
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
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#if (_WIN32)
/* Needed for std::min */
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include <math.h>
#include <gz/math/Helpers.hh>
#include "gz/rendering/RenderTypes.hh"
#include "gz/rendering/ogre2/Ogre2Conversions.hh"
#include "gz/rendering/ogre2/Ogre2DepthCamera.hh"
#include "gz/rendering/ogre2/Ogre2GaussianNoisePass.hh"
#include "gz/rendering/ogre2/Ogre2Includes.hh"
#include "gz/rendering/ogre2/Ogre2ParticleEmitter.hh"
#include "gz/rendering/ogre2/Ogre2RenderEngine.hh"
#include "gz/rendering/ogre2/Ogre2RenderTarget.hh"
#include "gz/rendering/ogre2/Ogre2RenderTypes.hh"
#include "gz/rendering/ogre2/Ogre2Scene.hh"
#include "gz/rendering/ogre2/Ogre2Sensor.hh"
#include "Ogre2ParticleNoiseListener.hh"
namespace gz
{
namespace rendering
{
inline namespace GZ_RENDERING_VERSION_NAMESPACE {
//
/// \brief Gaussian noise render pass for depth cameras
/// The class implementation is very similar to Ogre2GaussianNoisePass but
/// uses a different shader material for apply noise to depth cameras
/// This class is added here since we can not modify Ogre2GaussianNoisePass
/// as it would break ABI
class Ogre2DepthGaussianNoisePass : public Ogre2GaussianNoisePass
{
/// \brief Constructor
public: Ogre2DepthGaussianNoisePass() {}
/// \brief Destructor
public: virtual ~Ogre2DepthGaussianNoisePass() {}
// Documentation inherited.
public: void PreRender() override;
// Documentation inherited.
public: void CreateRenderPass() override;
/// brief Pointer to the Gaussian noise ogre material
private: Ogre::Material *gaussianNoiseMat = nullptr;
};
}
}
}
/// \internal
/// \brief Private data for the Ogre2DepthCamera class
class gz::rendering::Ogre2DepthCameraPrivate
{
/// \brief The depth buffer
public: float *depthBuffer = nullptr;
/// \brief Outgoing depth data, used by newDepthFrame event.
public: float *depthImage = nullptr;
/// \brief Outgoing point cloud data, used by newRgbPointCloud event.
public: float *pointCloudImage = nullptr;
/// \brief maximum value used for data outside sensor range
public: float dataMaxVal = gz::math::INF_D;
/// \brief minimum value used for data outside sensor range
public: float dataMinVal = -gz::math::INF_D;
/// \brief 1st pass compositor workspace definition
public: std::string ogreCompositorWorkspaceDef;
/// \brief 1st pass compositor node definition
public: std::string ogreCompositorBaseNodeDef;
/// \brief Final pass compositor node definition
public: std::string ogreCompositorFinalNodeDef;
/// \brief Compositor workspace.
public: Ogre::CompositorWorkspace *ogreCompositorWorkspace = nullptr;
/// \brief Output texture with depth and color data
public: Ogre::TextureGpu *ogreDepthTexture[2];
/// \brief Dummy render texture for the depth data
public: RenderTexturePtr depthTexture;
/// \brief The depth material
public: Ogre::MaterialPtr depthMaterial;
/// \brief The depth material in final pass
public: Ogre::MaterialPtr depthFinalMaterial;
/// \brief A chain of render passes applied to the render target
public: std::vector<RenderPassPtr> renderPasses;
/// \brief Flag to indicate if render pass need to be rebuilt
public: bool renderPassDirty = false;
/// \brief Event used to signal rgb point cloud data
public: gz::common::EventT<void(const float *,
unsigned int, unsigned int, unsigned int,
const std::string &)> newRgbPointCloud;
/// \brief Event used to signal depth data
public: gz::common::EventT<void(const float *,
unsigned int, unsigned int, unsigned int,
const std::string &)> newDepthFrame;
/// \brief standard deviation of particle noise
public: double particleStddev = 0.01;
/// \brief Listener for setting particle noise value based on particle
/// emitter region
public: std::unique_ptr<Ogre2ParticleNoiseListener> particleNoiseListener;
/// \brief Particle scatter ratio. This is used to determine the ratio of
/// particles that will detected by the depth camera
public: double particleScatterRatio = 0.1;
/// \brief Name of sky box material
public: const std::string kSkyboxMaterialName = "SkyBox";
/// \brief Name of shadow compositor node
public: const std::string kShadowNodeName = "PbsMaterialsShadowNode";
};
using namespace gz;
using namespace rendering;
//////////////////////////////////////////////////
void Ogre2DepthGaussianNoisePass::PreRender()
{
// This function is similar to Ogre2GaussianNoisePass but duplicated here
// for Ogre2DepthCamera
if (!this->gaussianNoiseMat)
return;
if (!this->enabled)
return;
Ogre::Vector3 offsets(math::Rand::DblUniform(0.0, 1.0),
math::Rand::DblUniform(0.0, 1.0),
math::Rand::DblUniform(0.0, 1.0));
Ogre::Pass *pass = this->gaussianNoiseMat->getTechnique(0)->getPass(0);
Ogre::GpuProgramParametersSharedPtr psParams =
pass->getFragmentProgramParameters();
psParams->setNamedConstant("offsets", offsets);
psParams->setNamedConstant("mean", static_cast<Ogre::Real>(this->mean));
psParams->setNamedConstant("stddev",
static_cast<Ogre::Real>(this->stdDev));
}
//////////////////////////////////////////////////
void Ogre2DepthGaussianNoisePass::CreateRenderPass()
{
// This function is similar to Ogre2GaussianNoisePass but duplicated here
// for Ogre2DepthCamera.
static int gaussianDepthNodeCounter = 0;
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Ogre::CompositorManager2 *ogreCompMgr = ogreRoot->getCompositorManager2();
std::string nodeDefName = "GaussianDepthNoiseNode_"
+ std::to_string(gaussianDepthNodeCounter);
if (ogreCompMgr->hasNodeDefinition(nodeDefName))
return;
// The GaussianNoise material is defined in script (gaussian_noise.material).
// clone the material
std::string matName = "GaussianNoiseDepth";
Ogre::MaterialPtr ogreMat =
Ogre::MaterialManager::getSingleton().getByName(matName);
if (!ogreMat)
{
gzerr << "Gaussian noise material not found: '" << matName << "'"
<< std::endl;
return;
}
if (!ogreMat->isLoaded())
ogreMat->load();
std::string materialName = matName + "_" +
std::to_string(gaussianDepthNodeCounter);
this->gaussianNoiseMat = ogreMat->clone(materialName).get();
this->ogreCompositorNodeDefName = nodeDefName;
gaussianDepthNodeCounter++;
Ogre::CompositorNodeDef *nodeDef =
ogreCompMgr->addNodeDefinition(nodeDefName);
// Input texture
nodeDef->addTextureSourceName("rt_input", 0,
Ogre::TextureDefinitionBase::TEXTURE_INPUT);
nodeDef->addTextureSourceName("rt_output", 1,
Ogre::TextureDefinitionBase::TEXTURE_INPUT);
// rt_input target
nodeDef->setNumTargetPass(1);
Ogre::CompositorTargetDef *inputTargetDef =
nodeDef->addTargetPass("rt_output");
inputTargetDef->setNumPasses(1);
{
// quad pass
Ogre::CompositorPassQuadDef *passQuad =
static_cast<Ogre::CompositorPassQuadDef *>(
inputTargetDef->addPass(Ogre::PASS_QUAD));
passQuad->setAllLoadActions(Ogre::LoadAction::Clear);
passQuad->mMaterialName = materialName;
passQuad->addQuadTextureSource(0, "rt_input");
}
nodeDef->mapOutputChannel(0, "rt_output");
nodeDef->mapOutputChannel(1, "rt_input");
}
//////////////////////////////////////////////////
Ogre2DepthCamera::Ogre2DepthCamera()
: dataPtr(new Ogre2DepthCameraPrivate())
{
this->dataPtr->ogreCompositorWorkspace = nullptr;
}
//////////////////////////////////////////////////
Ogre2DepthCamera::~Ogre2DepthCamera()
{
this->Destroy();
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::Init()
{
BaseDepthCamera::Init();
// create internal camera
this->CreateCamera();
// create dummy render texture
this->CreateRenderTexture();
this->Reset();
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::Destroy()
{
this->RemoveAllRenderPasses();
if (this->dataPtr->depthBuffer)
{
delete [] this->dataPtr->depthBuffer;
this->dataPtr->depthBuffer = nullptr;
}
if (this->dataPtr->depthImage)
{
delete [] this->dataPtr->depthImage;
this->dataPtr->depthImage = nullptr;
}
if (this->dataPtr->pointCloudImage)
{
delete [] this->dataPtr->pointCloudImage;
this->dataPtr->pointCloudImage = nullptr;
}
if (!this->ogreCamera)
return;
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Ogre::CompositorManager2 *ogreCompMgr = ogreRoot->getCompositorManager2();
// remove depth texture, material, compositor
for (size_t i = 0u; i < 2u; ++i)
{
if (this->dataPtr->ogreDepthTexture[i])
{
ogreRoot->getRenderSystem()->getTextureGpuManager()->destroyTexture(
this->dataPtr->ogreDepthTexture[i]);
this->dataPtr->ogreDepthTexture[i] = nullptr;
}
}
if (this->dataPtr->ogreCompositorWorkspace)
{
ogreCompMgr->removeWorkspace(
this->dataPtr->ogreCompositorWorkspace);
}
if (this->dataPtr->depthMaterial)
{
Ogre::MaterialManager::getSingleton().remove(
this->dataPtr->depthMaterial->getName());
this->dataPtr->depthMaterial.setNull();
}
if (this->dataPtr->depthFinalMaterial)
{
Ogre::MaterialManager::getSingleton().remove(
this->dataPtr->depthFinalMaterial->getName());
this->dataPtr->depthFinalMaterial.setNull();
}
if (!this->dataPtr->ogreCompositorWorkspaceDef.empty())
{
ogreCompMgr->removeWorkspaceDefinition(
this->dataPtr->ogreCompositorWorkspaceDef);
ogreCompMgr->removeNodeDefinition(
this->dataPtr->ogreCompositorBaseNodeDef);
ogreCompMgr->removeNodeDefinition(
this->dataPtr->ogreCompositorFinalNodeDef);
}
if (this->dataPtr->particleNoiseListener)
{
this->ogreCamera->removeListener(
this->dataPtr->particleNoiseListener.get());
this->dataPtr->particleNoiseListener.reset();
}
Ogre::SceneManager *ogreSceneManager;
ogreSceneManager = this->scene->OgreSceneManager();
if (ogreSceneManager == nullptr)
{
gzerr << "Scene manager cannot be obtained" << std::endl;
}
else
{
if (ogreSceneManager->findCameraNoThrow(this->name) != nullptr)
{
ogreSceneManager->destroyCamera(this->ogreCamera);
this->ogreCamera = nullptr;
}
}
BaseDepthCamera::Destroy();
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::CreateCamera()
{
// create ogre camera object
Ogre::SceneManager *ogreSceneManager;
ogreSceneManager = this->scene->OgreSceneManager();
if (ogreSceneManager == nullptr)
{
gzerr << "Scene manager cannot be obtained" << std::endl;
return;
}
this->ogreCamera = ogreSceneManager->createCamera(this->name);
if (this->ogreCamera == nullptr)
{
gzerr << "Ogre camera cannot be created" << std::endl;
return;
}
// by default, ogre2 cameras are attached to root scene node
this->ogreCamera->detachFromParent();
this->ogreNode->attachObject(this->ogreCamera);
// rotate to Gazebo coordinate system
this->ogreCamera->yaw(Ogre::Degree(-90.0));
this->ogreCamera->roll(Ogre::Degree(-90.0));
this->ogreCamera->setFixedYawAxis(false);
// TODO(anyone): provide api access
this->ogreCamera->setRenderingDistance(100);
this->ogreCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
this->ogreCamera->setCustomProjectionMatrix(false);
}
/////////////////////////////////////////////////
void Ogre2DepthCamera::CreateRenderTexture()
{
RenderTexturePtr base = this->scene->CreateRenderTexture();
this->dataPtr->depthTexture =
std::dynamic_pointer_cast<Ogre2RenderTexture>(base);
this->dataPtr->depthTexture->SetWidth(1);
this->dataPtr->depthTexture->SetHeight(1);
}
/////////////////////////////////////////////////////////
void Ogre2DepthCamera::CreateDepthTexture()
{
// set aspect ratio and fov
const double aspectRatio = this->AspectRatio();
const double angle = this->HFOV().Radian();
const double vfov =
this->LimitFOV(2.0 * atan(tan(angle / 2.0) / aspectRatio));
this->ogreCamera->setFOVy(Ogre::Radian((Ogre::Real)vfov));
this->ogreCamera->setAspectRatio((Ogre::Real)aspectRatio);
// Load depth material
// The DepthCamera material is defined in script (depth_camera.material).
// We need to clone it since we are going to modify its uniform variables
std::string matDepthName = "DepthCamera";
Ogre::MaterialPtr matDepth =
Ogre::MaterialManager::getSingleton().getByName(matDepthName);
this->dataPtr->depthMaterial = matDepth->clone(
this->Name() + "_" + matDepthName);
this->dataPtr->depthMaterial->load();
Ogre::Pass *pass = this->dataPtr->depthMaterial->getTechnique(0)->getPass(0);
Ogre::GpuProgramParametersSharedPtr psParams =
pass->getFragmentProgramParameters();
// Configure camera behaviour.
// Make the clipping plane dist large and handle near clamping in shaders
double nearPlane = this->NearClipPlane() * 0.9;
double farPlane = this->FarClipPlane() * 1.1;
this->ogreCamera->setNearClipDistance(nearPlane);
this->ogreCamera->setFarClipDistance(farPlane);
// Set the uniform variables (depth_camera_fs.glsl).
// The projectParams is used to linearize depth buffer data
// The other params are used to clamp the range output
// Use the 'real' clip distance here so depth can be
// linearized correctly
Ogre::Vector2 projectionAB = this->ogreCamera->getProjectionParamsAB();
double projectionA = projectionAB.x;
double projectionB = projectionAB.y;
projectionB /= farPlane;
psParams->setNamedConstant("projectionParams",
Ogre::Vector2(projectionA, projectionB));
psParams->setNamedConstant("near",
static_cast<float>(this->NearClipPlane()));
psParams->setNamedConstant("far",
static_cast<float>(this->FarClipPlane()));
psParams->setNamedConstant("max",
static_cast<float>(this->dataPtr->dataMaxVal));
psParams->setNamedConstant("min",
static_cast<float>(this->dataPtr->dataMinVal));
Ogre::Vector3 bg(this->Scene()->BackgroundColor().R(),
this->Scene()->BackgroundColor().G(),
this->Scene()->BackgroundColor().B());
psParams->setNamedConstant("backgroundColor", bg);
psParams->setNamedConstant("particleStddev",
static_cast<float>(this->dataPtr->particleStddev));
std::string matDepthFinalName = "DepthCameraFinal";
Ogre::MaterialPtr matDepthFinal =
Ogre::MaterialManager::getSingleton().getByName(matDepthFinalName);
this->dataPtr->depthFinalMaterial = matDepthFinal->clone(
this->Name() + "_" + matDepthFinalName);
this->dataPtr->depthFinalMaterial->load();
Ogre::Pass *passFinal =
this->dataPtr->depthFinalMaterial->getTechnique(0)->getPass(0);
Ogre::GpuProgramParametersSharedPtr psParamsFinal =
passFinal->getFragmentProgramParameters();
psParamsFinal->setNamedConstant("near",
static_cast<float>(this->NearClipPlane()));
psParamsFinal->setNamedConstant("far",
static_cast<float>(this->FarClipPlane()));
psParamsFinal->setNamedConstant("max",
static_cast<float>(this->dataPtr->dataMaxVal));
psParamsFinal->setNamedConstant("min",
static_cast<float>(this->dataPtr->dataMinVal));
// create background material is specified
MaterialPtr backgroundMaterial = this->Scene()->BackgroundMaterial();
bool validBackground = backgroundMaterial &&
!backgroundMaterial->EnvironmentMap().empty();
// let depth camera shader know if there is background material
// This is needed for manual clipping of color pixel values.
psParams->setNamedConstant("hasBackground",
static_cast<int>(validBackground));
if (validBackground)
{
Ogre::MaterialManager &matManager = Ogre::MaterialManager::getSingleton();
std::string skyMatName = this->dataPtr->kSkyboxMaterialName + "_"
+ this->Name();
auto mat = matManager.getByName(skyMatName);
if (!mat)
{
auto skyboxMat = matManager.getByName(this->dataPtr->kSkyboxMaterialName);
if (!skyboxMat)
{
gzerr << "Unable to find skybox material" << std::endl;
return;
}
mat = skyboxMat->clone(skyMatName);
}
Ogre::TextureUnitState *texUnit =
mat->getTechnique(0u)->getPass(0u)->getTextureUnitState(0u);
texUnit->setTextureName(backgroundMaterial->EnvironmentMap(),
Ogre::TextureTypes::TypeCube);
}
// Create depth camera compositor
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Ogre::CompositorManager2 *ogreCompMgr = ogreRoot->getCompositorManager2();
std::string wsDefName = "DepthCameraWorkspace_" + this->Name();
this->dataPtr->ogreCompositorWorkspaceDef = wsDefName;
if (!ogreCompMgr->hasWorkspaceDefinition(wsDefName))
{
// The depth camera compositor does a few passes in order to simulate
// particles effects in depth / point cloud image data
//
// render scene (color) with particles, c1
// render scene (depth) without particles, d1
// render scene (grayscale) with particles only, g2
// render scene (depth) with particles only, d2
//
// if g2 is non-zero // pixel with particle
// if d2 < d1 // particle is in view
// apply noise and scatterbility to d2
// set depth data to d2
// else
// set depth data to d1
// set color data to c1
// We need to programmatically create the compositor because we need to
// configure it to use the cloned depth material created earlier.
// The compositor node definition is equivalent to the following:
//
// compositor_node DepthCamera
// {
// texture rt0 target_width target_height PF_FLOAT32_RGBA
// texture rt1 target_width target_height PF_FLOAT32_RGBA
// texture colorTexture target_width target_height PF_R8G8B8
// depth_texture depth_format PF_D32_FLOAT
// texture depthTexture target_width target_height PF_D32_FLOAT
// texture particleTexture target_width target_height PFG_R8_UNORM
// // particleDepthTexture shares same depth buffer as particleTexture
// texture particleDepthTexture target_width target_height PFG_D32_FLOAT
//
// rtv particleTexture
// {
// depth particleDepthTexture
// }
//
// target colorTexture
// {
// pass clear
// {
// colour_value 0.0 0.0 0.0 1.0
// }
// pass render_scene
// {
// }
// }
// target depthTexture
// {
// pass clear
// {
// colour_value 0.0 0.0 0.0 1.0
// }
// pass render_scene
// {
// visibility_mask 0x11011111
// }
// }
// target particleTexture
// {
// pass clear
// {
// }
// pass render_scene
// {
// visibility_mask 0x00100000
// }
// }
// target rt0
// {
// pass clear
// {
// }
// pass render_quad
// {
// material DepthCamera // Use copy instead of original
// input 0 depthTexture
// input 1 colorTexture
// quad_normals camera_far_corners_view_space
// }
// }
// out 0 rt0
// out 1 rt1
// }
std::string baseNodeDefName = wsDefName + "/BaseNode";
this->dataPtr->ogreCompositorBaseNodeDef = baseNodeDefName;
Ogre::CompositorNodeDef *baseNodeDef =
ogreCompMgr->addNodeDefinition(baseNodeDefName);
baseNodeDef->addTextureSourceName(
"rt0", 0u, Ogre::TextureDefinitionBase::TEXTURE_INPUT);
baseNodeDef->addTextureSourceName(
"rt1", 1u, Ogre::TextureDefinitionBase::TEXTURE_INPUT);
Ogre::TextureDefinitionBase::TextureDefinition *depthTexDef =
baseNodeDef->addTextureDefinition("depthTexture");
depthTexDef->textureType = Ogre::TextureTypes::Type2D;
depthTexDef->width = 0;
depthTexDef->height = 0;
depthTexDef->depthOrSlices = 1;
depthTexDef->numMipmaps = 0;
depthTexDef->widthFactor = 1;
depthTexDef->heightFactor = 1;
depthTexDef->format = Ogre::PFG_D32_FLOAT;
depthTexDef->textureFlags &= ~Ogre::TextureFlags::Uav;
depthTexDef->depthBufferId = Ogre::DepthBuffer::POOL_DEFAULT;
depthTexDef->depthBufferFormat = Ogre::PFG_UNKNOWN;
depthTexDef->fsaa = "0";
Ogre::RenderTargetViewDef *rtvDepth =
baseNodeDef->addRenderTextureView("depthTexture");
rtvDepth->setForTextureDefinition("depthTexture", depthTexDef );
Ogre::TextureDefinitionBase::TextureDefinition *colorTexDef =
baseNodeDef->addTextureDefinition("colorTexture");
colorTexDef->textureType = Ogre::TextureTypes::Type2D;
colorTexDef->width = 0;
colorTexDef->height = 0;
colorTexDef->depthOrSlices = 1;
colorTexDef->numMipmaps = 0;
colorTexDef->widthFactor = 1;
colorTexDef->heightFactor = 1;
colorTexDef->format = Ogre::PFG_RGBA8_UNORM_SRGB;
// Note we are using low level materials in quad pass so also had to perform
// gamma correction in the fragment shaders (depth_camera_fs.glsl)
colorTexDef->textureFlags &= ~Ogre::TextureFlags::Uav;
colorTexDef->depthBufferId = Ogre::DepthBuffer::POOL_DEFAULT;
colorTexDef->depthBufferFormat = Ogre::PFG_D32_FLOAT;
colorTexDef->preferDepthTexture = true;
colorTexDef->fsaa = "0";
Ogre::RenderTargetViewDef *rtvColor =
baseNodeDef->addRenderTextureView("colorTexture");
rtvColor->setForTextureDefinition("colorTexture", colorTexDef);
Ogre::TextureDefinitionBase::TextureDefinition *particleTexDef =
baseNodeDef->addTextureDefinition("particleTexture");
particleTexDef->textureType = Ogre::TextureTypes::Type2D;
particleTexDef->width = 0;
particleTexDef->height = 0;
particleTexDef->depthOrSlices = 1;
particleTexDef->numMipmaps = 0;
particleTexDef->widthFactor = 0.5;
particleTexDef->heightFactor = 0.5;
particleTexDef->format = Ogre::PFG_R8_UNORM;
particleTexDef->textureFlags &= ~Ogre::TextureFlags::Uav;
particleTexDef->depthBufferId = Ogre::DepthBuffer::POOL_DEFAULT;
particleTexDef->depthBufferFormat = Ogre::PFG_UNKNOWN;
particleTexDef->preferDepthTexture = false;
particleTexDef->fsaa = "0";
Ogre::TextureDefinitionBase::TextureDefinition *particleDepthTexDef =
baseNodeDef->addTextureDefinition("particleDepthTexture");
particleDepthTexDef->textureType = Ogre::TextureTypes::Type2D;
particleDepthTexDef->width = 0;
particleDepthTexDef->height = 0;
particleDepthTexDef->depthOrSlices = 1;
particleDepthTexDef->numMipmaps = 0;
particleDepthTexDef->widthFactor = 0.5;
particleDepthTexDef->heightFactor = 0.5;
particleDepthTexDef->format = Ogre::PFG_D32_FLOAT;
particleDepthTexDef->depthBufferId = Ogre::DepthBuffer::POOL_NON_SHAREABLE;
particleDepthTexDef->depthBufferFormat = Ogre::PFG_UNKNOWN;
particleDepthTexDef->fsaa = "0";
particleDepthTexDef->textureFlags &= ~Ogre::TextureFlags::Uav;
// Auto setup the RTV then manually override the depth buffer so
// it uses the one we created (and thus we can sample from it later)
Ogre::RenderTargetViewDef *rtvParticleTexture =
baseNodeDef->addRenderTextureView("particleTexture");
rtvParticleTexture->setForTextureDefinition("particleTexture",
particleTexDef);
rtvParticleTexture->depthAttachment.textureName = "particleDepthTexture";
baseNodeDef->setNumTargetPass(4);
Ogre::CompositorTargetDef *colorTargetDef =
baseNodeDef->addTargetPass("colorTexture");
if (validBackground)
colorTargetDef->setNumPasses(3);
else
colorTargetDef->setNumPasses(2);
{
// scene pass - opaque
{
Ogre::CompositorPassSceneDef *passScene =
static_cast<Ogre::CompositorPassSceneDef *>(
colorTargetDef->addPass(Ogre::PASS_SCENE));
passScene->mShadowNode = this->dataPtr->kShadowNodeName;
passScene->setVisibilityMask(GZ_VISIBILITY_ALL);
passScene->mIncludeOverlays = false;
passScene->mFirstRQ = 0u;
passScene->mLastRQ = 2u;
if (validBackground)
{
passScene->setAllLoadActions(Ogre::LoadAction::DontCare);
passScene->mLoadActionDepth = Ogre::LoadAction::Clear;
passScene->mLoadActionStencil = Ogre::LoadAction::Clear;
}
else
{
passScene->setAllLoadActions(Ogre::LoadAction::Clear);
passScene->setAllClearColours(
Ogre2Conversions::Convert(this->Scene()->BackgroundColor()));
}
}
// render background, e.g. sky, after opaque stuff
if (validBackground)
{
// quad pass
Ogre::CompositorPassQuadDef *passQuad =
static_cast<Ogre::CompositorPassQuadDef *>(
colorTargetDef->addPass(Ogre::PASS_QUAD));
passQuad->mMaterialName = this->dataPtr->kSkyboxMaterialName + "_"
+ this->Name();
passQuad->mFrustumCorners =
Ogre::CompositorPassQuadDef::CAMERA_DIRECTION;
}
// scene pass - transparent stuff
{
Ogre::CompositorPassSceneDef *passScene =
static_cast<Ogre::CompositorPassSceneDef *>(
colorTargetDef->addPass(Ogre::PASS_SCENE));
passScene->setVisibilityMask(GZ_VISIBILITY_ALL);
// todo(anyone) PbsMaterialsShadowNode is hardcoded.
// Although this may be just fine
passScene->mShadowNode = this->dataPtr->kShadowNodeName;
passScene->mFirstRQ = 2u;
}
}
Ogre::CompositorTargetDef *depthTargetDef =
baseNodeDef->addTargetPass("depthTexture");
depthTargetDef->setNumPasses(1);
{
// scene pass
Ogre::CompositorPassSceneDef *passScene =
static_cast<Ogre::CompositorPassSceneDef *>(
depthTargetDef->addPass(Ogre::PASS_SCENE));
passScene->setAllLoadActions(Ogre::LoadAction::Clear);
passScene->setAllClearColours(Ogre::ColourValue(
this->FarClipPlane(),
this->FarClipPlane(),
this->FarClipPlane()));
// depth texute does not contain particles
passScene->setVisibilityMask(
GZ_VISIBILITY_ALL & ~Ogre2ParticleEmitter::kParticleVisibilityFlags);
}
Ogre::CompositorTargetDef *particleTargetDef =
baseNodeDef->addTargetPass("particleTexture");
particleTargetDef->setNumPasses(1);
{
// scene pass
Ogre::CompositorPassSceneDef *passScene =
static_cast<Ogre::CompositorPassSceneDef *>(
particleTargetDef->addPass(Ogre::PASS_SCENE));
passScene->setAllLoadActions(Ogre::LoadAction::Clear);
passScene->setAllClearColours(Ogre::ColourValue::Black);
passScene->setVisibilityMask(
Ogre2ParticleEmitter::kParticleVisibilityFlags);
}
// rt0 target - converts depth to xyz
Ogre::CompositorTargetDef *inTargetDef =
baseNodeDef->addTargetPass("rt0");
inTargetDef->setNumPasses(1);
{
// quad pass
Ogre::CompositorPassQuadDef *passQuad =
static_cast<Ogre::CompositorPassQuadDef *>(
inTargetDef->addPass(Ogre::PASS_QUAD));
passQuad->setAllLoadActions(Ogre::LoadAction::Clear);
passQuad->setAllClearColours(Ogre::ColourValue(
this->FarClipPlane(),
this->FarClipPlane(),
this->FarClipPlane()));
passQuad->mMaterialName = this->dataPtr->depthMaterial->getName();
passQuad->addQuadTextureSource(0, "depthTexture");
passQuad->addQuadTextureSource(1, "colorTexture");
passQuad->addQuadTextureSource(2, "particleTexture");
passQuad->addQuadTextureSource(3, "particleDepthTexture");
passQuad->mFrustumCorners =
Ogre::CompositorPassQuadDef::VIEW_SPACE_CORNERS;
}
baseNodeDef->mapOutputChannel(0, "rt0");
baseNodeDef->mapOutputChannel(1, "rt1");
// Programmatically create the final pass node and use the cloned final
// depth material created earlier.
// The compositor node definition is equivalent to the following:
//
// compositor_node DepthCameraFinal
// {
// in 0 rt_output
// in 1 rt_input
//
// target rt_output
// {
// pass clear
// {
// }
// pass render_quad
// {
// material DepthCameraFinal // Use copy instead of original
// input 0 rt_input
// }
// }
// }
std::string finalNodeDefName = wsDefName + "/FinalNode";
this->dataPtr->ogreCompositorFinalNodeDef = finalNodeDefName;
Ogre::CompositorNodeDef *finalNodeDef =
ogreCompMgr->addNodeDefinition(finalNodeDefName);
finalNodeDef->addTextureSourceName("rt_input", 0,
Ogre::TextureDefinitionBase::TEXTURE_INPUT);
// output texture
finalNodeDef->addTextureSourceName("rt_output", 1,
Ogre::TextureDefinitionBase::TEXTURE_INPUT);
finalNodeDef->setNumTargetPass(1);
// rt_output target - converts depth to xyz
Ogre::CompositorTargetDef *outputTargetDef =
finalNodeDef->addTargetPass("rt_output");
outputTargetDef->setNumPasses(1);
{
// quad pass
Ogre::CompositorPassQuadDef *passQuad =
static_cast<Ogre::CompositorPassQuadDef *>(
outputTargetDef->addPass(Ogre::PASS_QUAD));
passQuad->setAllLoadActions(Ogre::LoadAction::Clear);
passQuad->setAllClearColours(Ogre::ColourValue(
this->FarClipPlane(),
this->FarClipPlane(),
this->FarClipPlane()));
passQuad->mMaterialName = this->dataPtr->depthFinalMaterial->getName();
passQuad->addQuadTextureSource(0, "rt_input");
}
finalNodeDef->mapOutputChannel(0, "rt_output");
// Finally create the workspace.
// The compositor workspace definition is equivalent to the following:
//
// workspace DepthCameraWorkspace
// {
// connect_output DepthCameraFinal 0
// connect DepthCamera 0 DepthCameraFinal 1
// }
Ogre::CompositorWorkspaceDef *workDef =
ogreCompMgr->addWorkspaceDefinition(wsDefName);
workDef->connectExternal(0, baseNodeDefName, 0);
workDef->connectExternal(1, baseNodeDefName, 1);
workDef->connect(baseNodeDefName, finalNodeDefName);
}
Ogre::CompositorWorkspaceDef *wsDef =
ogreCompMgr->getWorkspaceDefinition(wsDefName);
if (!wsDef)
{
gzerr << "Unable to add workspace definition [" << wsDefName << "] "
<< " for " << this->Name();
}
Ogre::TextureGpuManager *textureMgr =
ogreRoot->getRenderSystem()->getTextureGpuManager();
// create render texture - these textures pack the range data
for (size_t i = 0u; i < 2u; ++i)
{
this->dataPtr->ogreDepthTexture[i] =
textureMgr->createTexture(
this->Name() + "_depth" + std::to_string(i),
Ogre::GpuPageOutStrategy::SaveToSystemRam,
Ogre::TextureFlags::RenderToTexture,
Ogre::TextureTypes::Type2D);
this->dataPtr->ogreDepthTexture[i]->setResolution(
this->ImageWidth(), this->ImageHeight());
this->dataPtr->ogreDepthTexture[i]->setNumMipmaps(1u);
this->dataPtr->ogreDepthTexture[i]->setPixelFormat(
Ogre::PFG_RGBA32_UINT);
this->dataPtr->ogreDepthTexture[i]->scheduleTransitionTo(
Ogre::GpuResidency::Resident);
}
CreateWorkspaceInstance();
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::CreateWorkspaceInstance()
{
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Ogre::CompositorManager2 *ogreCompMgr = ogreRoot->getCompositorManager2();
Ogre::CompositorChannelVec externalTargets(2u);
externalTargets[0] = this->dataPtr->ogreDepthTexture[0];
externalTargets[1] = this->dataPtr->ogreDepthTexture[1];
// create compositor worksspace
this->dataPtr->ogreCompositorWorkspace =
ogreCompMgr->addWorkspace(
this->scene->OgreSceneManager(),
externalTargets,
this->ogreCamera,
this->dataPtr->ogreCompositorWorkspaceDef,
false);
this->dataPtr->ogreCompositorWorkspace->addListener(
engine->TerraWorkspaceListener());
// add the listener
Ogre::CompositorNode *node =
this->dataPtr->ogreCompositorWorkspace->getNodeSequence()[0];
auto channelsTex = node->getLocalTextures();
for (auto c : channelsTex)
{
if (c->getPixelFormat() == Ogre::PFG_R8_UNORM)
{
// add particle noise / scatter effects listener so we can set the
// amount of noise based on size of emitter
this->dataPtr->particleNoiseListener.reset(
new Ogre2ParticleNoiseListener(this->scene,
this->dataPtr->depthMaterial));
this->ogreCamera->addListener(
this->dataPtr->particleNoiseListener.get());
break;
}
}
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::Render()
{
// Our shaders rely on clamped values so enable it for this sensor
//
// TODO(anyone): Matias N. Goldberg (dark_sylinc) insists this is a hack
// and something is wrong. We should not need depth clamp. Depth clamp is
// just masking a bug
const bool bOldDepthClamp = this->ogreCamera->getNeedsDepthClamp();
this->ogreCamera->_setNeedsDepthClamp(true);
this->scene->StartRendering(this->ogreCamera);
// update the compositors
this->dataPtr->ogreCompositorWorkspace->_validateFinalTarget();
this->dataPtr->ogreCompositorWorkspace->_beginUpdate(false);
this->dataPtr->ogreCompositorWorkspace->_update();
this->dataPtr->ogreCompositorWorkspace->_endUpdate(false);
Ogre::vector<Ogre::TextureGpu*>::type swappedTargets;
swappedTargets.reserve(2u);
this->dataPtr->ogreCompositorWorkspace->_swapFinalTarget(swappedTargets);
this->scene->FlushGpuCommandsAndStartNewFrame(1u, false);
this->ogreCamera->_setNeedsDepthClamp(bOldDepthClamp);
}
//////////////////////////////////////////////////
void Ogre2DepthCamera::PreRender()
{