Skip to content

Commit

Permalink
[engine] Add PolyMesh, wip.
Browse files Browse the repository at this point in the history
and some spacing.
  • Loading branch information
dlyr committed Jul 9, 2020
1 parent aabea95 commit 36cfec9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Engine/Renderer/Mesh/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ class RA_ENGINE_API Mesh : public IndexedGeometry<Core::Geometry::TriangleMesh>
private:
};

/// PolyMesh, own a Core::Geometry::PolyMesh
/// This class handle the GPU representation of a polyhedron mesh.
/// Each face of the polyhedron (typically quads) are assume to be planar and convex.
/// Simple triangulation is performed on the fly before sending data to the GPU.
class RA_ENGINE_API PolyMesh : public IndexedGeometry<Core::Geometry::PolyMesh>
{
using base = IndexedGeometry<Core::Geometry::PolyMesh>;

public:
using base::IndexedGeometry;
inline explicit PolyMesh(
const std::string& name,
typename base::CoreGeometry&& geom,
typename base::MeshRenderMode renderMode = base::MeshRenderMode::RM_TRIANGLES );

protected:
inline void updateGL_specific_impl() override;

private:
inline void triangulate();
Core::AlignedStdVector<Core::Vector3ui> m_triangleIndices;
};

} // namespace Engine
} // namespace Ra

Expand Down
55 changes: 55 additions & 0 deletions src/Engine/Renderer/Mesh/Mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ std::string AttribArrayDisplayable::getAttribName( MeshData type ) {
if ( type == VERTEX_WEIGHT_IDX ) return {"in_weight_idx"};
return {"invalid mesh data attr name"};
}

///////////////// VaoIndices ///////////////////////

void VaoIndices::setIndicesDirty() {
m_indicesDirty = true;
}

///////////////// IndexedAttribArrayDisplayable ///////////////////////

template <typename I>
template <typename T>
void IndexedAttribArrayDisplayable<I>::addAttrib(
Expand Down Expand Up @@ -456,6 +458,7 @@ void IndexedGeometry<T>::render( const ShaderProgram* prog ) {
}

///////// PointCloud //////////

PointCloud::PointCloud( const std::string& name,
typename base::CoreGeometry&& geom,
typename base::MeshRenderMode renderMode ) :
Expand All @@ -464,10 +467,62 @@ PointCloud::PointCloud( const std::string& name,
}

///////// LineMesh ///////////

LineMesh::LineMesh( const std::string& name,
typename base::CoreGeometry&& geom,
typename base::MeshRenderMode renderMode ) :
base( name, std::move( geom ), renderMode ) {}

///////// PolyMesh ///////////

void PolyMesh::updateGL_specific_impl() {
if ( !m_indices )
{
m_indices = globjects::Buffer::create();
m_indicesDirty = true;
}
if ( m_indicesDirty )
{
triangulate();
/// this one do not work since m_indices is not a std::vector
// m_indices->setData( m_mesh.m_indices, GL_DYNAMIC_DRAW );
m_numElements = m_triangleIndices.size() * Core::Vector3ui::RowsAtCompileTime;

m_indices->setData(
static_cast<gl::GLsizeiptr>( m_triangleIndices.size() *
sizeof( typename base::CoreGeometry::IndexType ) ),
m_triangleIndices.data(),
GL_STATIC_DRAW );
m_indicesDirty = false;
}
if ( !base::m_vao ) { base::m_vao = globjects::VertexArray::create(); }
base::m_vao->bind();
base::m_vao->bindElementBuffer( m_indices.get() );
base::m_vao->unbind();
}

void PolyMesh::triangulate() {
m_triangleIndices.clear();
m_triangleIndices.reserve( m_mesh.getIndices().size() );
for ( const auto& face : m_mesh.getIndices() )
{
LOG( logDEBUG ) << face.cols() << " " << face.rows() << " " << face.size();

if ( face.size() == 3 ) { m_triangleIndices.push_back( face ); }
else
{
auto i0 {face[0]};
auto i1 {face[1]};
/// simple fan type triangulation
for ( auto i = 2; i < face.size(); ++i )
{
auto i2 {face[i]};
m_triangleIndices.emplace_back( i0, i1, i2 );
i1 = i2;
}
}
}
}

} // namespace Engine
} // namespace Ra

0 comments on commit 36cfec9

Please sign in to comment.