Radium Engine  1.5.20
Loading...
Searching...
No Matches
GeometryComponent.hpp
1#pragma once
2
3#include <Core/Asset/GeometryData.hpp>
4#include <Core/Asset/VolumeData.hpp>
5#include <Core/Containers/MakeShared.hpp>
6#include <Core/Geometry/TriangleMesh.hpp>
7#include <Core/Geometry/Volume.hpp>
8#include <Engine/Data/BlinnPhongMaterial.hpp>
9#include <Engine/Data/Material.hpp>
10#include <Engine/Data/MaterialConverters.hpp>
11#include <Engine/Data/Mesh.hpp>
12#include <Engine/Rendering/RenderObject.hpp>
13#include <Engine/Scene/Component.hpp>
14#include <Engine/Scene/ComponentMessenger.hpp>
15#include <Engine/Scene/Entity.hpp>
16
17namespace Ra {
18namespace Engine {
19namespace Data {
20class VolumeObject;
21} // namespace Data
22
23namespace Scene {
24
26class RA_ENGINE_API GeometryComponent : public Component
27{
28 public:
29 GeometryComponent( const std::string& name, Entity* entity ) :
30 Component( name, entity ), m_contentName( name ) {}
31 ~GeometryComponent() override {}
32
33 void initialize() override {}
34
35 // public:
36 // Component communication management
37 virtual void setupIO( const std::string& id );
38 void setContentName( const std::string& name ) { m_contentName = name; }
39 // void setDeformable( bool b );
40
42 // Ra::Core::Utils::Index getRenderObjectIndex() const;
43 protected:
44 private:
45 const Ra::Core::Utils::Index* roIndexRead() const;
46
47 protected:
48 // the index of the renderObject
49 Ra::Core::Utils::Index m_roIndex {};
50 std::string m_contentName {};
51};
52
62template <typename CoreMeshType>
64{
65 using base = GeometryComponent;
66
67 public:
68 using RenderMeshType = typename Data::RenderMeshType::getType<CoreMeshType>::Type;
69
70 inline SurfaceMeshComponent( const std::string& name,
71 Entity* entity,
73 inline SurfaceMeshComponent( const std::string& name,
74 Entity* entity,
76
81 inline SurfaceMeshComponent( const std::string& name,
82 Entity* entity,
83 CoreMeshType&& mesh,
84 Core::Asset::MaterialData* mat = nullptr );
85 inline SurfaceMeshComponent( const std::string& name,
86 Entity* entity,
87 CoreMeshType&& mesh,
89 ~SurfaceMeshComponent() override = default;
90
92 inline const CoreMeshType& getCoreGeometry() const;
93 inline RenderMeshType* getDisplayable();
94
95 // Component communication management
96 inline void setupIO( const std::string& id ) override;
97 inline void setDeformable( bool b );
98
99 private:
100 inline void generateMesh( const Ra::Core::Asset::GeometryData* data );
101
103 convertMatdataToMaterial( const Core::Asset::MaterialData* data );
104
105 inline void finalizeROFromGeometry( std::shared_ptr<Data::Material> roMaterial,
106 Core::Transform transform );
107
108 // Give access to the mesh and (if deformable) to update it
109 inline const CoreMeshType* getMeshOutput() const;
110 inline CoreMeshType* getMeshRw();
111
112 private:
113 // directly hold a reference to the displayMesh to simplify accesses in handlers
114 std::shared_ptr<RenderMeshType> m_displayMesh { nullptr };
115};
116
121
124class RA_ENGINE_API PointCloudComponent : public GeometryComponent
125{
126 using base = GeometryComponent;
127
128 public:
129 PointCloudComponent( const std::string& name,
130 Entity* entity,
131 const Ra::Core::Asset::GeometryData* data );
132
137 PointCloudComponent( const std::string& name,
138 Entity* entity,
139 Core::Geometry::PointCloud&& mesh,
140 Core::Asset::MaterialData* mat = nullptr );
141
143
144 void initialize() override;
145
147 const Ra::Core::Geometry::PointCloud& getCoreGeometry() const;
148 Data::PointCloud* getGeometry();
149
151 inline void setSplatSize( float s ) { m_splatSize = s; }
152
154 inline float getSplatSize() const { return m_splatSize; }
155
156 public:
157 // Component communication management
158 void setupIO( const std::string& id ) override;
159 void setDeformable( bool b );
160
161 private:
162 void generatePointCloud( const Ra::Core::Asset::GeometryData* data );
163
164 void finalizeROFromGeometry( const Core::Asset::MaterialData* data, Core::Transform transform );
165
166 // Give access to the mesh and (if deformable) to update it
167 const Ra::Core::Geometry::PointCloud* getMeshOutput() const;
168 Ra::Core::Geometry::PointCloud* getPointCloudRw();
169
170 private:
171 // directly hold a reference to the displayMesh to simplify accesses in handlers
172 std::shared_ptr<Data::PointCloud> m_displayMesh { nullptr };
173 // The diameter of the splat when rendered
174 float m_splatSize { 0.0025f };
175};
176
177/*-----------------------------------------------------------------------------------------------*/
178
185class RA_ENGINE_API VolumeComponent : public Component
186{
187 public:
188 VolumeComponent( const std::string& name,
189 Entity* entity,
190 const Ra::Core::Asset::VolumeData* data );
191 ~VolumeComponent() override;
192
193 void initialize() override;
194
195 public:
196 // Component communication management
197 void setupIO( const std::string& id );
198 void setContentName( const std::string& name );
199
201 Ra::Core::Utils::Index getRenderObjectIndex() const;
202 Data::VolumeObject* getDisplayVolume();
203
204 private:
205 void generateVolumeRender( const Ra::Core::Asset::VolumeData* data );
206
207 const Ra::Core::Geometry::AbstractVolume* getVolumeOutput() const;
209
210 const Ra::Core::Utils::Index* roIndexRead() const;
211
212 private:
213 Ra::Core::Utils::Index m_volumeIndex {};
214 std::string m_contentName {};
215 std::shared_ptr<Data::VolumeObject> m_displayVolume { nullptr };
216};
217
218template <typename CoreMeshType>
220 const std::string& name,
221 Entity* entity,
222 const Ra::Core::Asset::GeometryData* data ) :
223 GeometryComponent( name, entity ), m_displayMesh( nullptr ) {
224 generateMesh( data );
225}
226
227template <typename CoreMeshType>
228SurfaceMeshComponent<CoreMeshType>::SurfaceMeshComponent( const std::string& name,
229 Entity* entity,
231 GeometryComponent( name, entity ), m_displayMesh( data ) {
232 finalizeROFromGeometry( convertMatdataToMaterial( nullptr ), Core::Transform::Identity() );
233}
234
235template <typename CoreMeshType>
237 Entity* entity,
238 CoreMeshType&& mesh,
240 GeometryComponent( name, entity ),
241 m_displayMesh( new RenderMeshType( name, std::move( mesh ) ) ) {
242 setContentName( name );
243 finalizeROFromGeometry( convertMatdataToMaterial( mat ), Core::Transform::Identity() );
244}
245
246template <typename CoreMeshType>
248 const std::string& name,
249 Entity* entity,
250 CoreMeshType&& mesh,
252 GeometryComponent( name, entity ),
253 m_displayMesh( new RenderMeshType( name, std::move( mesh ) ) ) {
254 setContentName( name );
255 finalizeROFromGeometry( mat, Core::Transform::Identity() );
256}
257template <typename CoreMeshType>
258void SurfaceMeshComponent<CoreMeshType>::generateMesh( const Ra::Core::Asset::GeometryData* data ) {
259 m_contentName = data->getName();
260 m_displayMesh = Ra::Core::make_shared<RenderMeshType>( m_contentName );
261 CoreMeshType mesh = Data::createCoreMeshFromGeometryData<CoreMeshType>( data );
262
263 m_displayMesh->loadGeometry( std::move( mesh ) );
264
265 finalizeROFromGeometry(
266 convertMatdataToMaterial( data->hasMaterial() ? &( data->getMaterial() ) : nullptr ),
267 data->getFrame() );
268}
269
270template <typename CoreMeshType>
271std::shared_ptr<Data::Material> SurfaceMeshComponent<CoreMeshType>::convertMatdataToMaterial(
272 const Core::Asset::MaterialData* data ) {
273 // The technique for rendering this component
275 // First extract the material from asset or create a default one
276 if ( data != nullptr ) {
278 auto mat = converter.second( data );
279 roMaterial.reset( mat );
280 }
281 else {
282 auto mat = new Data::BlinnPhongMaterial( m_contentName + "_DefaultBPMaterial" );
283 mat->setRenderAsSplat( m_displayMesh->getNumFaces() == 0 );
284 mat->setColoredByVertexAttrib( m_displayMesh->getCoreGeometry().hasAttrib(
285 Ra::Core::Geometry::getAttribName( Ra::Core::Geometry::VERTEX_COLOR ) ) );
286 roMaterial.reset( mat );
287 }
288 return roMaterial;
289}
290
291template <typename CoreMeshType>
292void SurfaceMeshComponent<CoreMeshType>::finalizeROFromGeometry(
294 Core::Transform transform ) {
295
296 // initialize with a default rendertechique that draws nothing
297 std::string roName( m_name + "_" + m_contentName + "_RO" );
299 this,
301 m_displayMesh,
302 Rendering::RenderTechnique {} );
303 ro->setTransparent( roMaterial->isTransparent() );
304 ro->setMaterial( roMaterial );
305 setupIO( m_contentName );
306 ro->setLocalTransform( transform );
307 m_roIndex = addRenderObject( ro );
308}
309
310#ifndef CHECK_MESH_NOT_NULL
311# define CHECK_MESH_NOT_NULL \
312 \
313 CORE_ASSERT( m_displayMesh != nullptr, \
314 "DisplayMesh should exist while component is alive" );
315# define CHECK_MESH_NOT_NULL_UNDEF
316#endif
317
318template <typename CoreMeshType>
320 CHECK_MESH_NOT_NULL;
321 return m_displayMesh->getCoreGeometry();
322}
323
324template <typename CoreMeshType>
325typename SurfaceMeshComponent<CoreMeshType>::RenderMeshType*
327 CHECK_MESH_NOT_NULL;
328 return m_displayMesh.get();
329}
330
331template <typename CoreMeshType>
332void SurfaceMeshComponent<CoreMeshType>::setupIO( const std::string& id ) {
333 CHECK_MESH_NOT_NULL;
334
335 const auto& cm = ComponentMessenger::getInstance();
336 auto cbOut = std::bind( &SurfaceMeshComponent<CoreMeshType>::getMeshOutput, this );
337 auto cbRw = std::bind( &SurfaceMeshComponent<CoreMeshType>::getMeshRw, this );
338
339 cm->registerOutput<CoreMeshType>( getEntity(), this, id, cbOut );
340 cm->registerReadWrite<CoreMeshType>( getEntity(), this, id, cbRw );
341
342 base::setupIO( id );
343}
344
345template <typename CoreMeshType>
346const CoreMeshType* SurfaceMeshComponent<CoreMeshType>::getMeshOutput() const {
347 CHECK_MESH_NOT_NULL;
348 return &m_displayMesh->getCoreGeometry();
349}
350
351template <typename CoreMeshType>
352CoreMeshType* SurfaceMeshComponent<CoreMeshType>::getMeshRw() {
353 CHECK_MESH_NOT_NULL;
354 return &( m_displayMesh->getCoreGeometry() );
355}
356
357#ifdef CHECK_MESH_NOT_NULL_UNDEF
358# undef CHECK_MESH_NOT_NULL
359#endif
360
361} // namespace Scene
362} // namespace Engine
363} // namespace Ra
T bind(T... args)
virtual const std::string & getName() const
Acces to the name of the asset.
Definition AssetData.hpp:29
const MaterialData & getMaterial() const
Return the MaterialData associated to the objet.
Transform getFrame() const
Return the Transform of the object.
bool hasMaterial() const
Return true if the object has MaterialData.
represent material data loaded by a file loader. Material data must be identified by a unique name....
std::string getType() const
TYPE.
A PointCloud without indices.
Definition Mesh.hpp:311
static RenderObject * createRenderObject(const std::string &name, Scene::Component *comp, const RenderObjectType &type, std::shared_ptr< Data::Displayable > mesh, const RenderTechnique &techniqueConfig=RenderTechnique::createDefaultRenderTechnique())
A component is an element that can be updated by a system. It is also linked to some other components...
Definition Component.hpp:31
An entity is an scene element. It ties together components with a transform.
Definition Entity.hpp:23
Abstract interface of a geometric compoennet in the Engine.
void initialize() override
Pure virtual method to be overridden by any component. When this method is called you are guaranteed ...
void setSplatSize(float s)
set the splat size for rendering
float getSplatSize() const
get the splat size for rendering
Main class to convert Ra::Core::Asset::GeometryData to Ra::Engine::Mesh.
const CoreMeshType & getCoreGeometry() const
Returns the current display geometry.
Main class to convert Ra::Core::Asset::VolumeData to Ra::Engine::VolumeObject.
T move(T... args)
std::shared_ptr< T > make_shared(Args &&... args)
std::pair< bool, ConverterFunction > getMaterialConverter(const std::string &name)
CoreMeshType createCoreMeshFromGeometryData(const Ra::Core::Asset::GeometryData *data)
Definition Mesh.hpp:461
@ Geometry
"Geometry" render objects are those loaded using Radium::IO and generated by GeometrySystem
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
STL namespace.
T reset(T... args)