33#ifndef DART_GUI_OSG_RENDER_HEIGHTMAPSHAPENODE_HPP_
34#define DART_GUI_OSG_RENDER_HEIGHTMAPSHAPENODE_HPP_
36#include <osg/CullFace>
38#include <osg/Geometry>
40#include <osg/Material>
41#include <osg/MatrixTransform>
42#include <osg/ShapeDrawable>
44#include "dart/config.hpp"
56class HeightmapShapeGeode;
86 using osgVec3 =
typename std::conditional<
87 std::is_same<S, float>::value,
91 std::is_same<S, float>::value,
95 std::is_same<S, float>::value,
146 mHeightmapShape(shape),
148 mHeightmapVersion(dynamics::INVALID_INDEX)
160 setNodeMask(mVisualAspect->isHidden() ? 0x0u : ~0x0u);
163 && mHeightmapVersion == mHeightmapShape->getVersion())
168 mHeightmapVersion = mHeightmapShape->getVersion();
175 if (
nullptr == mGeode)
178 mHeightmapShape.get(), mParentShapeFrameNode,
this);
199 :
ShapeNode(parentNode->getShape(), parentShapeFrame, this),
200 mParentNode(parentNode),
201 mHeightmapShape(shape),
204 getOrCreateStateSet()->setMode(GL_BLEND, ::osg::StateAttribute::ON);
205 getOrCreateStateSet()->setRenderingHint(::osg::StateSet::TRANSPARENT_BIN);
206 getOrCreateStateSet()->setAttributeAndModes(
207 new ::osg::CullFace(::osg::CullFace::BACK));
208 getOrCreateStateSet()->setMode(GL_LIGHTING, ::osg::StateAttribute::ON);
225 if (
nullptr == mDrawable)
229 addDrawable(mDrawable);
233 mDrawable->refresh(
false);
252 std::is_same<S, float>::value || std::is_same<S, double>::value,
253 "Scalar type should be float or double");
259 !std::is_same<S, double>::value,
260 "OpenSceneGraph currently doesn't support double precision for "
267 mElements = new ::osg::DrawElementsUInt(::osg::PrimitiveSet::TRIANGLES);
275 const Eigen::Matrix<S, 3, 1>& p1,
276 const Eigen::Matrix<S, 3, 1>& p2,
277 const Eigen::Matrix<S, 3, 1>& p3)
279 return (p2 - p1).cross(p3 - p1).normalized();
284 const ::osg::Vec3f& p1, const ::osg::Vec3f& p2, const ::osg::Vec3f& p3)
286 auto normal = (p2 - p1) ^ (p3 - p1);
293 const ::osg::Vec3d& p1, const ::osg::Vec3d& p2, const ::osg::Vec3d& p3)
295 auto normal = (p2 - p1) ^ (p3 - p1);
305 ::osg::DrawElementsUInt& faces,
311 const auto rows = heightmap.rows();
312 const auto cols = heightmap.cols();
316 if (rows < 2 || cols < 2)
322 vertices.resize(
static_cast<std::size_t
>(heightmap.size()));
326 for (
auto i = 0; i < rows; ++i)
328 for (
auto j = 0; j < cols; ++j)
330 const auto index = cols * i + j;
332 j * scale.x(), -(i * scale.y()), heightmap(i, j) * scale.z());
362 faces.reserve(6 * (rows - 1) * (cols - 1));
363 for (
auto i = 1; i < rows; ++i)
365 for (
auto j = 1; j < cols; ++j)
368 const auto p1i = i - 1;
369 const auto p1j = j - 1;
371 const auto p2i = i - 1;
375 const auto p3j = j - 1;
378 const auto p1 = p1i * cols + p1j;
379 const auto p2 = p2i * cols + p2j;
380 const auto p3 = p3i * cols + p3j;
381 const auto curr = i * cols + j;
391 faces.push_back(curr);
395 normals.reserve(heightmap.size());
396 for (
auto i = 0; i < rows; ++i)
398 for (
auto j = 0; j < cols; ++j)
401 const auto p2i = i - 1;
405 const auto p3j = j - 1;
408 const auto p4j = j + 1;
410 const auto p5i = i + 1;
414 const auto p2 = p2i * cols + p2j;
415 const auto p3 = p3i * cols + p3j;
416 const auto p4 = p4i * cols + p4j;
417 const auto p5 = p5i * cols + p5j;
418 const auto curr = i * cols + j;
420 const auto& ptCurr = vertices[curr];
425 sum +=
getNormal(ptCurr, vertices[p2], vertices[p3]);
427 if (i + 1 < rows && j > 0)
428 sum +=
getNormal(ptCurr, vertices[p3], vertices[p5]);
430 if (i + 1 < rows && j + 1 < cols)
431 sum +=
getNormal(ptCurr, vertices[p5], vertices[p4]);
433 if (i > 0 && j + 1 < cols)
434 sum +=
getNormal(ptCurr, vertices[p4], vertices[p2]);
438 normals.push_back(sum);
448 setDataVariance(::osg::Object::STATIC);
450 setDataVariance(::osg::Object::DYNAMIC);
454 const auto&
heightmap = mHeightmapShape->getHeightField();
467 mHeightmapShape->getScale());
478 if (mColors->size() != 1)
std::string type
Definition SdfParser.cpp:82
std::size_t index
Definition SkelParser.cpp:1672
Shape for a height map.
Definition HeightmapShape.hpp:47
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:82
Eigen::Matrix< S, 3, 1 > Vector3
Definition HeightmapShapeNode.hpp:84
~HeightmapShapeDrawable() override=default
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec3f, ::osg::Vec3d >::type osgVec3
Definition HeightmapShapeNode.hpp:89
void refresh(bool firstTime)
Definition HeightmapShapeNode.hpp:445
dynamics::VisualAspect * mVisualAspect
Definition HeightmapShapeNode.hpp:110
HeightmapShapeGeode< S > * mParent
Definition HeightmapShapeNode.hpp:111
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec4Array, ::osg::Vec4dArray >::type Vec4Array
Definition HeightmapShapeNode.hpp:97
dynamics::HeightmapShape< S > * mHeightmapShape
Definition HeightmapShapeNode.hpp:109
::osg::ref_ptr<::osg::DrawElementsUInt > mElements
Definition HeightmapShapeNode.hpp:115
::osg::ref_ptr< Vec3Array > mNormals
Definition HeightmapShapeNode.hpp:116
::osg::ref_ptr<::osg::Vec4Array > mColors
Definition HeightmapShapeNode.hpp:117
HeightmapShapeDrawable(dynamics::HeightmapShape< S > *shape, dynamics::VisualAspect *visualAspect, HeightmapShapeGeode< S > *parent)
Definition HeightmapShapeNode.hpp:245
typename std::conditional< std::is_same< S, float >::value, ::osg::Vec3Array, ::osg::Vec3dArray >::type Vec3Array
Definition HeightmapShapeNode.hpp:93
::osg::ref_ptr< Vec3Array > mVertices
Definition HeightmapShapeNode.hpp:114
Definition HeightmapShapeNode.hpp:123
dynamics::HeightmapShape< S > * mHeightmapShape
Definition HeightmapShapeNode.hpp:137
HeightmapShapeDrawable< S > * mDrawable
Definition HeightmapShapeNode.hpp:138
void refresh()
Update all rendering data for this ShapeNode.
Definition HeightmapShapeNode.hpp:214
void extractData()
Definition HeightmapShapeNode.hpp:223
virtual ~HeightmapShapeGeode()
Definition HeightmapShapeNode.hpp:238
HeightmapShapeNode< S > * mParentNode
Definition HeightmapShapeNode.hpp:136
HeightmapShapeGeode(dynamics::HeightmapShape< S > *shape, ShapeFrameNode *parentShapeFrame, HeightmapShapeNode< S > *parentNode)
Definition HeightmapShapeNode.hpp:195
Definition HeightmapShapeNode.hpp:60
HeightmapShapeNode(std::shared_ptr< dynamics::HeightmapShape< S > > shape, ShapeFrameNode *parent)
Definition HeightmapShapeNode.hpp:143
void extractData(bool firstTime)
Definition HeightmapShapeNode.hpp:173
S_ S
Definition HeightmapShapeNode.hpp:62
std::shared_ptr< dynamics::HeightmapShape< S > > mHeightmapShape
Definition HeightmapShapeNode.hpp:74
virtual ~HeightmapShapeNode() override
Definition HeightmapShapeNode.hpp:188
HeightmapShapeGeode< S > * mGeode
Definition HeightmapShapeNode.hpp:75
void refresh() override
Update all rendering data for this ShapeNode.
Definition HeightmapShapeNode.hpp:156
std::size_t mHeightmapVersion
Definition HeightmapShapeNode.hpp:76
Definition ShapeNode.hpp:60
dart::dynamics::VisualAspect * mVisualAspect
Pointer to the VisualAspect associated with this ShapeNode.
Definition ShapeNode.hpp:107
::osg::Vec4d eigToOsgVec4d(const Eigen::MatrixBase< Derived > &vec)
Definition Utils.hpp:137
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:274
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:302
Definition BulletCollisionDetector.cpp:60