33 #ifndef DART_GUI_OSG_RENDER_HEIGHTMAPSHAPENODE_HPP_
34 #define DART_GUI_OSG_RENDER_HEIGHTMAPSHAPENODE_HPP_
36 #include "dart/config.hpp"
38 #include <osg/CullFace>
40 #include <osg/Geometry>
42 #include <osg/Material>
43 #include <osg/MatrixTransform>
44 #include <osg/ShapeDrawable>
57 class HeightmapShapeGeode;
59 template <
typename S_>
87 using osgVec3 =
typename std::conditional<
88 std::is_same<S, float>::value,
92 std::is_same<S, float>::value,
96 std::is_same<S, float>::value,
122 template <
typename S>
143 template <
typename S>
147 mHeightmapShape(shape),
156 template <
typename S>
161 setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);
163 if (mShape->getDataVariance() == dynamics::Shape::STATIC
164 && mHeightmapVersion == mHeightmapShape->getVersion())
169 mHeightmapVersion = mHeightmapShape->getVersion();
173 template <
typename S>
176 if (
nullptr == mGeode)
179 mHeightmapShape.get(), mParentShapeFrameNode,
this);
188 template <
typename S>
195 template <
typename S>
200 :
ShapeNode(parentNode->getShape(), parentShapeFrame, this),
201 mParentNode(parentNode),
202 mHeightmapShape(shape),
205 getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
206 getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
207 getOrCreateStateSet()->setAttributeAndModes(
208 new ::osg::CullFace(::osg::CullFace::BACK));
209 getOrCreateStateSet()->setMode(GL_LIGHTING, ::osg::StateAttribute::ON);
214 template <
typename S>
223 template <
typename S>
226 if (
nullptr == mDrawable)
230 addDrawable(mDrawable);
234 mDrawable->refresh(
false);
238 template <
typename S>
245 template <
typename S>
250 : mHeightmapShape(shape), mVisualAspect(visualAspect), mParent(parent)
253 std::is_same<S, float>::value || std::is_same<S, double>::value,
254 "Scalar type should be float or double");
260 !std::is_same<S, double>::value,
261 "OpenSceneGraph currently doesn't support double precision for "
268 mElements = new ::osg::DrawElementsUInt(::osg::PrimitiveSet::TRIANGLES);
274 template <
typename S>
276 const Eigen::Matrix<S, 3, 1>& p1,
277 const Eigen::Matrix<S, 3, 1>& p2,
278 const Eigen::Matrix<S, 3, 1>& p3)
280 return (p2 - p1).cross(p3 - p1).normalized();
285 const ::osg::Vec3f& p1, const ::osg::Vec3f& p2, const ::osg::Vec3f& p3)
287 auto normal = (p2 - p1) ^ (p3 - p1);
294 const ::osg::Vec3d& p1, const ::osg::Vec3d& p2, const ::osg::Vec3d& p3)
296 auto normal = (p2 - p1) ^ (p3 - p1);
302 template <
typename S>
306 ::osg::DrawElementsUInt& faces,
312 const auto rows = heightmap.rows();
313 const auto cols = heightmap.cols();
317 if (rows < 2 || cols < 2)
323 vertices.resize(
static_cast<std::size_t
>(heightmap.size()));
327 for (
auto i = 0; i < rows; ++i)
329 for (
auto j = 0; j < cols; ++j)
331 const auto index = cols * i + j;
333 j * scale.x(), -(i * scale.y()), heightmap(i, j) * scale.z());
363 faces.reserve(6 * (rows - 1) * (cols - 1));
364 for (
auto i = 1; i < rows; ++i)
366 for (
auto j = 1; j < cols; ++j)
369 const auto p1i = i - 1;
370 const auto p1j = j - 1;
372 const auto p2i = i - 1;
376 const auto p3j = j - 1;
379 const auto p1 = p1i * cols + p1j;
380 const auto p2 = p2i * cols + p2j;
381 const auto p3 = p3i * cols + p3j;
382 const auto curr = i * cols + j;
392 faces.push_back(curr);
396 normals.reserve(heightmap.size());
397 for (
auto i = 0; i < rows; ++i)
399 for (
auto j = 0; j < cols; ++j)
402 const auto p2i = i - 1;
406 const auto p3j = j - 1;
409 const auto p4j = j + 1;
411 const auto p5i = i + 1;
415 const auto p2 = p2i * cols + p2j;
416 const auto p3 = p3i * cols + p3j;
417 const auto p4 = p4i * cols + p4j;
418 const auto p5 = p5i * cols + p5j;
419 const auto curr = i * cols + j;
421 const auto& ptCurr = vertices[curr];
426 sum +=
getNormal(ptCurr, vertices[p2], vertices[p3]);
428 if (i + 1 < rows && j > 0)
429 sum +=
getNormal(ptCurr, vertices[p3], vertices[p5]);
431 if (i + 1 < rows && j + 1 < cols)
432 sum +=
getNormal(ptCurr, vertices[p5], vertices[p4]);
434 if (i > 0 && j + 1 < cols)
435 sum +=
getNormal(ptCurr, vertices[p4], vertices[p2]);
439 normals.push_back(sum);
445 template <
typename S>
449 setDataVariance(::osg::Object::STATIC);
451 setDataVariance(::osg::Object::DYNAMIC);
455 const auto& heightmap = mHeightmapShape->getHeightField();
468 mHeightmapShape->getScale());
469 addPrimitiveSet(mElements);
471 setVertexArray(mVertices);
472 setNormalArray(mNormals, ::osg::Array::BIND_PER_VERTEX);
479 if (mColors->size() != 1)
484 setColorArray(mColors, ::osg::Array::BIND_OVERALL);
std::string type
Definition: SdfParser.cpp:82
std::size_t index
Definition: SkelParser.cpp:1672
Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > HeightField
Definition: HeightmapShape.hpp:53
@ STATIC
Definition: Shape.hpp:83
Definition: ShapeFrame.hpp:54
bool isHidden() const
True iff the ShapeNode is set to be hidden.
Definition: ShapeFrame.cpp:177
Definition: ShapeFrameNode.hpp:61
Definition: HeightmapShapeNode.hpp:83
Eigen::Matrix< S, 3, 1 > Vector3
Definition: HeightmapShapeNode.hpp:85
~HeightmapShapeDrawable() override=default
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec3f, ::osg::Vec3d >::type osgVec3
Definition: HeightmapShapeNode.hpp:90
void refresh(bool firstTime)
Definition: HeightmapShapeNode.hpp:446
dynamics::VisualAspect * mVisualAspect
Definition: HeightmapShapeNode.hpp:111
HeightmapShapeGeode< S > * mParent
Definition: HeightmapShapeNode.hpp:112
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec4Array, ::osg::Vec4dArray >::type Vec4Array
Definition: HeightmapShapeNode.hpp:98
dynamics::HeightmapShape< S > * mHeightmapShape
Definition: HeightmapShapeNode.hpp:110
::osg::ref_ptr<::osg::DrawElementsUInt > mElements
Definition: HeightmapShapeNode.hpp:116
::osg::ref_ptr< Vec3Array > mNormals
Definition: HeightmapShapeNode.hpp:117
::osg::ref_ptr<::osg::Vec4Array > mColors
Definition: HeightmapShapeNode.hpp:118
HeightmapShapeDrawable(dynamics::HeightmapShape< S > *shape, dynamics::VisualAspect *visualAspect, HeightmapShapeGeode< S > *parent)
Definition: HeightmapShapeNode.hpp:246
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec3Array, ::osg::Vec3dArray >::type Vec3Array
Definition: HeightmapShapeNode.hpp:94
::osg::ref_ptr< Vec3Array > mVertices
Definition: HeightmapShapeNode.hpp:115
Definition: HeightmapShapeNode.hpp:124
dynamics::HeightmapShape< S > * mHeightmapShape
Definition: HeightmapShapeNode.hpp:138
HeightmapShapeDrawable< S > * mDrawable
Definition: HeightmapShapeNode.hpp:139
void refresh()
Update all rendering data for this ShapeNode.
Definition: HeightmapShapeNode.hpp:215
void extractData()
Definition: HeightmapShapeNode.hpp:224
virtual ~HeightmapShapeGeode()
Definition: HeightmapShapeNode.hpp:239
HeightmapShapeNode< S > * mParentNode
Definition: HeightmapShapeNode.hpp:137
HeightmapShapeGeode(dynamics::HeightmapShape< S > *shape, ShapeFrameNode *parentShapeFrame, HeightmapShapeNode< S > *parentNode)
Definition: HeightmapShapeNode.hpp:196
Definition: HeightmapShapeNode.hpp:61
HeightmapShapeNode(std::shared_ptr< dynamics::HeightmapShape< S >> shape, ShapeFrameNode *parent)
Definition: HeightmapShapeNode.hpp:144
void extractData(bool firstTime)
Definition: HeightmapShapeNode.hpp:174
S_ S
Definition: HeightmapShapeNode.hpp:63
std::shared_ptr< dynamics::HeightmapShape< S > > mHeightmapShape
Definition: HeightmapShapeNode.hpp:75
virtual ~HeightmapShapeNode() override
Definition: HeightmapShapeNode.hpp:189
HeightmapShapeGeode< S > * mGeode
Definition: HeightmapShapeNode.hpp:76
void refresh() override
Update all rendering data for this ShapeNode.
Definition: HeightmapShapeNode.hpp:157
std::size_t mHeightmapVersion
Definition: HeightmapShapeNode.hpp:77
Definition: ShapeNode.hpp:59
dart::dynamics::VisualAspect * mVisualAspect
Pointer to the VisualAspect associated with this ShapeNode.
Definition: ShapeNode.hpp:106
::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase< Derived > &vec)
Definition: Utils.hpp:138
constexpr std::size_t INVALID_INDEX
Definition: InvalidIndex.hpp:41
Eigen::Matrix< S, 3, 1 > getNormal(const Eigen::Matrix< S, 3, 1 > &p1, const Eigen::Matrix< S, 3, 1 > &p2, const Eigen::Matrix< S, 3, 1 > &p3)
Definition: HeightmapShapeNode.hpp:275
void setVertices(const typename dynamics::HeightmapShape< S >::HeightField &heightmap, typename HeightmapShapeDrawable< S >::Vec3Array &vertices, ::osg::DrawElementsUInt &faces, typename HeightmapShapeDrawable< S >::Vec3Array &normals, typename HeightmapShapeDrawable< S >::Vector3 scale)
Definition: HeightmapShapeNode.hpp:303
Definition: BulletCollisionDetector.cpp:65