Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New proposal for Engine::Displayable #802

Open
wants to merge 28 commits into
base: release-candidate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5fb8ebc
[core] Fix IndexedGeometry.
dlyr Jul 20, 2022
8de3a11
[engine] Prepare API of GeometryDisplayable
nmellado Jul 19, 2021
6689dde
[engine] add GeometryDisplayableComponent
dlyr Mar 31, 2022
1a86723
[tests] Use GeometryDisplayableComponent in HelloRadium (nothing draw…
dlyr Mar 31, 2022
ec4057a
[core] Switch struct -> class for AbstractGeometry.
dlyr Jul 20, 2022
d020cea
[core] Use macro for clone implementation.
dlyr Jul 20, 2022
7ed8468
[engine] double wireframe (ad hoc for quad mesh).
dlyr Jul 20, 2022
509d68b
[core][engine] Use demangleType.
dlyr Sep 15, 2022
a5aa861
[engine] user BijectiveAssociation for translation table.
dlyr Sep 18, 2022
df8275e
[core][engine] Move triangulate to core.
dlyr Sep 18, 2022
0a6f878
[engine] Add empty ctor.
dlyr Sep 18, 2022
2a1e9a4
[engine] use geometrydisplayable for components.
dlyr Sep 18, 2022
6f05270
[engine] rename correspondance -> matching
dlyr Sep 18, 2022
9d879cf
[engine] GeometrySystem use GeometryDisplayable comp.
dlyr Sep 18, 2022
eced115
[examples] Start using GeometryDisplayable in draw prim.
dlyr Sep 18, 2022
b58b111
[engine] GeometryComponent clean include.
dlyr Sep 19, 2022
85ea8dd
[engine] Renderer remove debug print.
dlyr Sep 19, 2022
c5b1275
[engine] handle line index layer picking render mode.
dlyr Sep 19, 2022
0c04358
[core] MeshPrimitives remove debug print.
dlyr Sep 19, 2022
eb21bbd
[core] IndexedGeometry remove debug print.
dlyr Sep 19, 2022
a8168ef
[examples] HelloRadium remove debug print.
dlyr Sep 19, 2022
ba7e3ce
[engine] Remove unused LineMeshComponent.
dlyr Oct 27, 2022
8304bdb
[examples] add fixed light in skinning example.
dlyr Dec 8, 2022
d61b0cc
[core] use inverse transpose for normal lbs.
dlyr Dec 8, 2022
ade232c
[core] comment typo in trianglemesh
dlyr Dec 8, 2022
c4647b2
[core][engine] use AttribArrayGeometry as skinning data.
dlyr Dec 8, 2022
e9f3e16
[core] register rw on AttribArrayGeometry for SurfaceMeshComponent.
dlyr Dec 8, 2022
076ec5e
remove temporary non working code
dlyr May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Shaders/Lines/Wireframe.geom.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in vec4 vPosition[2];
out vec4 gColor;
out float pixelWidthDiv2;
uniform vec2 viewport;
uniform float pixelWidth;
vec4 vColor[2];

void main() {
Expand All @@ -34,7 +35,6 @@ void main() {
vec4 scss1 = css1 * css0.w;

vec3 dir = ( scss1 - scss0 ).xyz;
float pixelWidth = 1.8;
const float border = 4.;
vec3 slope = normalize( vec3( -dir.y, dir.x, 0 ) );
vec4 n = vec4( vec3( pixelWidth + border ) / vp * slope, 0 );
Expand Down
15 changes: 8 additions & 7 deletions examples/DrawPrimitives/AllPrimitivesComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ void AllPrimitivesComponent::initialize() {

//// CUBES ////
if ( ENABLE_CUBES ) {
std::shared_ptr<Mesh> cube1( new Mesh( "Cube" ) );
std::shared_ptr<GeometryDisplayable> cube1( new GeometryDisplayable( "Cube" ) );
auto coord = cellSize / 16_ra;
cube1->loadGeometry(
Geometry::makeSharpBox( Vector3 { coord, coord, coord }, Color::Green() ) );
Geometry::makeSharpBox2( Vector3 { coord, coord, coord }, Color::Green() ) );
auto renderObject1 = RenderObject::createRenderObject(
"Cube1", this, RenderObjectType::Geometry, cube1, {} );
renderObject1->setLocalTransform( Transform { Translation( cellCorner ) } );
renderObject1->setMaterial( blinnPhongMaterial );
addRenderObject( renderObject1 );

std::shared_ptr<Mesh> texCube( new Mesh( "Cube" ) );
texCube->loadGeometry( Geometry::makeSharpBox(
std::shared_ptr<GeometryDisplayable> texCube( new GeometryDisplayable( "Cube" ) );
texCube->loadGeometry( Geometry::makeSharpBox2(
Vector3 { 1.2_ra * coord, 1.2_ra * coord, 1.2_ra * coord }, Color::White(), true ) );
auto renderObjectTexCube = RenderObject::createRenderObject(
"TexCube", this, RenderObjectType::Geometry, texCube, {} );
Expand All @@ -153,14 +153,15 @@ void AllPrimitivesComponent::initialize() {
addRenderObject( renderObjectTexCube );

// another cube
std::shared_ptr<Mesh> cube2( new Mesh( "Cube" ) );
std::shared_ptr<GeometryDisplayable> cube2( new GeometryDisplayable( "Cube" ) );
coord = cellSize / 4_ra;
cube2->loadGeometry( Geometry::makeSharpBox( Vector3 { coord, coord, coord } ) );
cube2->loadGeometry( Geometry::makeSharpBox2( Vector3 { coord, coord, coord } ) );

const std::string myColourName { "colour" };
cube2->getCoreGeometry().addAttrib(
myColourName, Vector4Array { cube2->getNumVertices(), Color::Red() } );

cube2->setAttribNameCorrespondance(
cube2->setAttribNameMatching(
myColourName, Ra::Core::Geometry::getAttribName( Ra::Core::Geometry::VERTEX_COLOR ) );
auto renderObject2 = RenderObject::createRenderObject(
"CubeRO_2", this, RenderObjectType::Geometry, cube2, {} );
Expand Down
6 changes: 3 additions & 3 deletions examples/HelloRadium/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main( int argc, char* argv[] ) {
//! [Verifying the OpenGL version available to the engine]

//! [Creating the cube]
auto cube = Ra::Core::Geometry::makeSharpBox( { 0.1f, 0.1f, 0.1f } );
auto cube { std::move( Ra::Core::Geometry::makeSharpBox2( { 0.1f, 0.1f, 0.1f } ) ) };
//! [Creating the cube]

//! [Colorize the Cube]
Expand All @@ -41,8 +41,8 @@ int main( int argc, char* argv[] ) {
//! [Create the engine entity for the cube]

//! [Create a geometry component with the cube]
auto c =
new Ra::Engine::Scene::TriangleMeshComponent( "Cube Mesh", e, std::move( cube ), nullptr );
auto c = new Ra::Engine::Scene::GeometryDisplayableComponent(
"Cube Mesh", e, std::move( cube ), nullptr );
//! [Create a geometry component with the cube]

//! [Register the entity/component association to the geometry system ]
Expand Down
10 changes: 10 additions & 0 deletions examples/SimpleSkinning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
#include <Engine/FrameInfo.hpp>
#include <Engine/RadiumEngine.hpp>
#include <Engine/Scene/CameraManager.hpp>
#include <Engine/Scene/DefaultLightManager.hpp>
#include <Engine/Scene/DirLight.hpp>
#include <Engine/Scene/EntityManager.hpp>
#include <Engine/Scene/GeometryComponent.hpp>
#include <Engine/Scene/SkeletonBasedAnimationSystem.hpp>
#include <Engine/Scene/SkeletonComponent.hpp>
#include <Engine/Scene/SkinningComponent.hpp>
#include <Engine/Scene/System.hpp>
#include <Engine/Scene/SystemDisplay.hpp>
#include <Gui/BaseApplication.hpp>
#include <Gui/RadiumWindow/SimpleWindowFactory.hpp>
#include <Gui/Viewer/Viewer.hpp>
Expand Down Expand Up @@ -71,6 +74,13 @@ class SkinningSystem : public Scene::System
};

void setupScene( Ra::Engine::RadiumEngine* engine ) {

DefaultLightManager* lightManager =
static_cast<DefaultLightManager*>( engine->getSystem( "DefaultLightManager" ) );
auto light = new Engine::Scene::DirectionalLight(
Ra::Engine::Scene::SystemEntity::getInstance(), "light" );
lightManager->addLight( light );

auto animationSystem = new SkinningSystem;
engine->registerSystem( "Simple animation system", animationSystem );

Expand Down
4 changes: 3 additions & 1 deletion src/Core/Animation/LinearBlendSkinning.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Core/Types.hpp"
#include <Core/Animation/LinearBlendSkinning.hpp>

#include <Core/Animation/SkinningData.hpp>
Expand Down Expand Up @@ -33,9 +34,10 @@ void linearBlendSkinning( const SkinningRefData& refData,
const Scalar w = it.value();
// prepare the pose w.r.t. the bind matrix and the mesh transform
const Transform M = refData.m_meshTransformInverse * pose[j] * bindMatrix[j];
const Matrix3 N = M.matrix().inverse().transpose().block<3, 3>( 0, 0 );
// apply LBS
frameData.m_currentPosition[i] += w * ( M * vertices[i] );
frameData.m_currentNormal[i] += w * ( M.linear() * normals[i] );
frameData.m_currentNormal[i] += w * ( N * normals[i] );
frameData.m_currentTangent[i] += w * ( M.linear() * tangents[i] );
frameData.m_currentBitangent[i] += w * ( M.linear() * bitangents[i] );
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Animation/SkinningData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Animation {
/// \brief Skinning data that get set at startup including the "reference state".
struct SkinningRefData {
/// The mesh in reference position.
Geometry::TriangleMesh m_referenceMesh;
Geometry::AttribArrayGeometry m_referenceMesh;

/// The inverse of the mesh's transform.
Transform m_meshTransformInverse;
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Geometry/AbstractGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace Geometry {
/// \warning: Part of the current proposal for a modular implementation of
/// displayable objects.
///
/// \note We use a struct because all members are public anyway
struct RA_CORE_API AbstractGeometry {
class RA_CORE_API AbstractGeometry
{
public:
/*
* Note: Explicitly defaulted virtual destructor, copy/move constructors,
* copy/move assignment operators
Expand Down
9 changes: 5 additions & 4 deletions src/Core/Geometry/IndexedGeometry.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Core/Geometry/IndexedGeometry.hpp>

#include <iterator>

namespace Ra {
Expand Down Expand Up @@ -215,16 +216,16 @@ MultiIndexedGeometry::addLayer( std::unique_ptr<GeometryIndexLayerBase>&& layer,
const bool withLock,
const std::string& layerName ) {
LayerKeyType key { layer->semantics(), layerName };
std::pair<LayerKeyType, EntryType> elt { key, std::make_pair( false, std::move( layer ) ) };
auto elt = std::make_pair( key, std::make_pair( false, std::move( layer ) ) );
auto [pos, inserted] = m_indices.insert( std::move( elt ) );
notify();

if ( withLock ) {
CORE_ASSERT( !pos->second.first, "try to get already locked layer" );
pos->second.first = true;
}
/// If not inserted, the pointer is deleted. So the caller must ensure this possible deletion
/// is safe before calling this method.
/// If not inserted, the pointer is deleted. So the caller must ensure this possible
/// deletion is safe before calling this method.

return { inserted, *( pos->second.second ) };
}
Expand All @@ -245,7 +246,7 @@ void MultiIndexedGeometry::deepClear() {
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

std::size_t MultiIndexedGeometry::KeyHash::operator()( const LayerKeyType& k ) const {
std::size_t MultiIndexedGeometry::LayerKeyHash::operator()( const LayerKeyType& k ) const {
// Mix semantic collection into a single identifier string
std::ostringstream stream;
std::copy( k.first.begin(), k.first.end(), std::ostream_iterator<std::string>( stream, "" ) );
Expand Down
66 changes: 62 additions & 4 deletions src/Core/Geometry/IndexedGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@ namespace Ra {
namespace Core {
namespace Geometry {

template <typename T>
VectorArray<Vector3ui> triangulate( const VectorArray<T>& in ) {
VectorArray<Vector3ui> out;

out.reserve( in.size() );
for ( const auto& face : in ) {
if ( face.size() == 3 ) { out.push_back( face ); }
else {
/// simple sew triangulation
int minus { int( face.size() ) - 1 };
int plus { 0 };
while ( plus + 1 < minus ) {
if ( ( plus - minus ) % 2 ) {
out.emplace_back( face[plus], face[plus + 1], face[minus] );
++plus;
}
else {
out.emplace_back( face[minus], face[plus], face[minus - 1] );
--minus;
}
}
}
}
return out;
}

template <>
inline VectorArray<Vector3ui> triangulate( const VectorArray<Vector4ui>& in ) {
VectorArray<Vector3ui> out;
out.reserve( 2 * in.size() );
// assume quads are convex
for ( const auto& face : in ) {
out.emplace_back( face[0], face[1], face[2] );
out.emplace_back( face[0], face[2], face[3] );
}
return out;
}

/// \brief Base class for index collections stored in MultiIndexedGeometry
class RA_CORE_API GeometryIndexLayerBase : public Utils::ObservableVoid,
public Utils::ObjectWithSemantic,
Expand Down Expand Up @@ -63,7 +101,7 @@ struct GeometryIndexLayer : public GeometryIndexLayerBase {

inline size_t getSize() const override final;

inline std::unique_ptr<GeometryIndexLayerBase> clone() override final;
inline std::unique_ptr<GeometryIndexLayerBase> clone() override;

inline size_t getNumberOfComponents() const override final;

Expand Down Expand Up @@ -383,20 +421,31 @@ class RA_CORE_API MultiIndexedGeometry : public AttribArrayGeometry, public Util
/// \brief Clear attributes stored as pointers
void deepClear();

using EntryType = std::pair<bool, std::unique_ptr<GeometryIndexLayerBase>>;
/// bool -> locked, ptr -> actual data
using LayerEntryType = std::pair<bool, std::unique_ptr<GeometryIndexLayerBase>>;

struct RA_CORE_API KeyHash {
public:
/// Hash function for layer keys
struct RA_CORE_API LayerKeyHash {
std::size_t operator()( const LayerKeyType& k ) const;
};

private:
/// Collection of pairs <lockStatus, Indices>
/// \note There is no natural ordering for these elements, thus
/// we need an unordered_map. In contrast to map, transparent hashing
/// require c++20, so we need to implement them explicitely here
/// https://en.cppreference.com/w/cpp/container/unordered_map/find
std::unordered_map<LayerKeyType, EntryType, KeyHash> m_indices;
std::unordered_map<LayerKeyType, LayerEntryType, LayerKeyHash> m_indices;
};

#define INDEX_LAYER_CLONE_IMPLEMENTATION( TYPE ) \
inline std::unique_ptr<GeometryIndexLayerBase> clone() override { \
auto copy = std::make_unique<TYPE>( *this ); \
copy->collection() = collection(); \
return copy; \
}

/// \name Predefined index layers
/// The use of these layers helps in generic management of geometries
/// \{
Expand All @@ -414,6 +463,7 @@ struct RA_CORE_API PointCloudIndexLayer : public GeometryIndexLayer<Vector1ui> {
void linearIndices( const AttribArrayGeometry& attr );

static constexpr const char* staticSemanticName = "PointCloud";
INDEX_LAYER_CLONE_IMPLEMENTATION( PointCloudIndexLayer )

protected:
template <class... SemanticNames>
Expand All @@ -425,6 +475,7 @@ struct RA_CORE_API PointCloudIndexLayer : public GeometryIndexLayer<Vector1ui> {
struct RA_CORE_API TriangleIndexLayer : public GeometryIndexLayer<Vector3ui> {
inline TriangleIndexLayer();
static constexpr const char* staticSemanticName = "TriangleMesh";
INDEX_LAYER_CLONE_IMPLEMENTATION( TriangleIndexLayer )

protected:
template <class... SemanticNames>
Expand All @@ -436,6 +487,7 @@ struct RA_CORE_API TriangleIndexLayer : public GeometryIndexLayer<Vector3ui> {
struct RA_CORE_API QuadIndexLayer : public GeometryIndexLayer<Vector4ui> {
inline QuadIndexLayer();
static constexpr const char* staticSemanticName = "QuadMesh";
INDEX_LAYER_CLONE_IMPLEMENTATION( QuadIndexLayer )

protected:
template <class... SemanticNames>
Expand All @@ -448,6 +500,7 @@ struct RA_CORE_API QuadIndexLayer : public GeometryIndexLayer<Vector4ui> {
struct RA_CORE_API PolyIndexLayer : public GeometryIndexLayer<VectorNui> {
inline PolyIndexLayer();
static constexpr const char* staticSemanticName = "PolyMesh";
INDEX_LAYER_CLONE_IMPLEMENTATION( PolyIndexLayer )

protected:
template <class... SemanticNames>
Expand All @@ -459,6 +512,7 @@ struct RA_CORE_API PolyIndexLayer : public GeometryIndexLayer<VectorNui> {
struct RA_CORE_API LineIndexLayer : public GeometryIndexLayer<Vector2ui> {
inline LineIndexLayer();
static constexpr const char* staticSemanticName = "LineMesh";
INDEX_LAYER_CLONE_IMPLEMENTATION( LineIndexLayer )

protected:
template <class... SemanticNames>
Expand All @@ -467,6 +521,8 @@ struct RA_CORE_API LineIndexLayer : public GeometryIndexLayer<Vector2ui> {

/// \}

#undef INDEX_LAYER_CLONE_IMPLEMENTATION

/// Temporary class providing the old API for TriangleMesh, LineMesh and PolyMesh
/// This class will be marked as deprecated soon.
namespace IndexLayerType {
Expand Down Expand Up @@ -762,13 +818,15 @@ inline void IndexedGeometry<T>::setIndices( IndexContainerType&& indices ) {
auto& abstractLayer = getLayerWithLock( m_mainIndexLayerKey );
static_cast<IndexedGeometry<T>::DefaultLayerType&>( abstractLayer ).collection() =
std::move( indices );
indicesUnlock();
notify();
}

template <typename T>
inline void IndexedGeometry<T>::setIndices( const IndexContainerType& indices ) {
auto& abstractLayer = getLayerWithLock( m_mainIndexLayerKey );
static_cast<IndexedGeometry<T>::DefaultLayerType&>( abstractLayer ).collection() = indices;
indicesUnlock();
notify();
}

Expand Down
Loading