52 #define ACG_MESHNODE_C
54 #include <ACG/Geometry/GPUCacheOptimizer.hh>
55 #include <ACG/GL/DrawMesh.hh>
61 namespace SceneGraph {
67 #include "MeshNode2T.hh"
80 updateVertexPicking_(true),
81 vertexPickingBaseIndex_(0),
82 updateEdgePicking_(true),
83 edgePickingBaseIndex_(0),
84 updateFacePicking_(true),
85 facePickingBaseIndex_(0),
86 updateAnyPicking_(true),
87 anyPickingBaseIndex_(0),
88 perFaceTextureIndexAvailable_(false),
89 perFaceTextureCoordsAvailable_(false),
95 std::cerr <<
"Error! Vertex buffer objects are not supported! The meshNode will not work without them!" << std::endl;
122 if (mesh_.has_vertex_normals())
129 if (mesh_.has_face_normals())
132 if (mesh_.has_halfedge_normals())
135 if (mesh_.has_vertex_colors())
140 if (mesh_.has_vertex_normals())
144 if(mesh_.has_edge_colors())
149 if(mesh_.has_halfedge_colors())
154 if (mesh_.has_face_colors()) {
157 if( mesh_.has_face_normals() )
160 if( mesh().has_vertex_normals() )
164 if ( mesh_.has_vertex_texcoords2D() ) {
167 if (mesh_.has_vertex_normals())
171 if ( perFaceTextureCoordsAvailable_ ) {
174 if (mesh_.has_face_normals())
185 _bbMin.minimize(bbMin_);
186 _bbMax.maximize(bbMax_);
216 unsigned int arrays = NONE;
218 glPushAttrib(GL_ENABLE_BIT);
232 if ( _drawMode & DrawModes::POINTS_SHADED ) {
238 if ( enableColors_ && (_drawMode & DrawModes::POINTS_COLORED) )
240 drawMesh_->usePerVertexColors();
243 if ( enableNormals_ && (_drawMode & DrawModes::POINTS_SHADED ) )
249 drawMesh_->disableColors();
266 drawMesh_->disableColors();
279 clear_color[3] = 1.0;
283 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
287 drawMesh_->disableColors();
302 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
308 enable_arrays( PER_EDGE_VERTEX_ARRAY | PER_EDGE_COLOR_ARRAY );
316 enable_arrays( PER_HALFEDGE_VERTEX_ARRAY);
324 enable_arrays( PER_HALFEDGE_VERTEX_ARRAY | PER_HALFEDGE_COLOR_ARRAY );
336 drawMesh_->usePerVertexColors();
347 if ( enableNormals_ ) {
353 drawMesh_->usePerVertexColors();
365 drawMesh_->setFlatShading();
366 drawMesh_->disableColors();
379 drawMesh_->usePerVertexNormals();
380 drawMesh_->setSmoothShading();
381 drawMesh_->disableColors();
403 drawMesh_->usePerVertexNormals();
404 drawMesh_->setSmoothShading();
405 drawMesh_->disableColors();
419 Vec4f base_color_backup = _state.
base_color();
426 drawMesh_->usePerFaceColors();
436 Vec4f base_color_backup = _state.
base_color();
442 drawMesh_->setSmoothShading();
443 drawMesh_->usePerVertexNormals();
444 drawMesh_->usePerFaceColors();
458 drawMesh_->disableColors();
459 drawMesh_->setSmoothShading();
460 drawMesh_->usePerHalfedgeNormals();
469 Vec4f base_color_backup = _state.
base_color();
476 drawMesh_->setFlatShading();
477 drawMesh_->usePerFaceColors();
497 drawMesh_->disableColors();
498 drawMesh_->usePerVertexTexcoords();
513 drawMesh_->setSmoothShading();
514 drawMesh_->disableColors();
515 drawMesh_->usePerVertexTexcoords();
534 drawMesh_->disableColors();
535 drawMesh_->usePerHalfedgeTexcoords();
554 drawMesh_->usePerVertexNormals();
555 drawMesh_->setSmoothShading();
556 drawMesh_->disableColors();
557 drawMesh_->usePerHalfedgeTexcoords();
574 template <
class Mesh>
590 ro.depthWrite =
true;
598 drawMesh_->setFlatShading();
600 drawMesh_->setSmoothShading();
605 switch (props->colorSource())
611 drawMesh_->disableColors();
616 switch (props->normalSource())
625 switch (props->texcoordSource())
646 switch (props->lightStage())
656 if (props->primitive() == DrawModes::PRIMITIVE_WIREFRAME)
659 drawMesh_->disableColors();
661 add_line_RenderObjects(_renderer, &ro);
664 if (props->primitive() == DrawModes::PRIMITIVE_HIDDENLINE)
675 ro.glColorMask(0,0,0,0);
677 ro.depthWrite =
true;
680 ro.fillMode = GL_FILL;
682 drawMesh_->disableColors();
686 add_face_RenderObjects(_renderer, &ro);
693 ro.glColorMask(1,1,1,1);
695 ro.depthWrite =
true;
700 add_line_RenderObjects(_renderer, &ro);
703 if (props->
colored() && props->primitive() == DrawModes::PRIMITIVE_EDGE)
709 ro.
vertexDecl = drawMesh_->getEdgeColoredVertexDeclaration();
710 ro.glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
718 if (props->primitive() == DrawModes::PRIMITIVE_HALFEDGE)
724 ro.
vertexDecl = drawMesh_->getHalfedgeVertexDeclaration();
726 ro.
vertexDecl = drawMesh_->getHalfedgeColoredVertexDeclaration();
728 ro.glDrawArrays(GL_LINES, 0, mesh_.n_halfedges() * 2);
739 switch (props->primitive())
741 case DrawModes::PRIMITIVE_POINT: add_point_RenderObjects(_renderer, &ro);
break;
742 case DrawModes::PRIMITIVE_EDGE: add_line_RenderObjects(_renderer, &ro);
break;
743 case DrawModes::PRIMITIVE_POLYGON: add_face_RenderObjects(_renderer, &ro);
break;
755 drawMesh_->addPointRenderObjects(_renderer, _baseObj);
762 drawMesh_->drawVertices();
770 if ((enabled_arrays_ & PER_EDGE_COLOR_ARRAY) && (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY))
773 glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
776 drawMesh_->drawLines();
784 if ((enabled_arrays_ & PER_EDGE_COLOR_ARRAY) && (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY))
787 glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
790 drawMesh_->addLineRenderObjects(_renderer, _baseObj);
798 if ( enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY )
799 glDrawArrays(GL_LINES, 0, mesh_.n_halfedges() * 2);
802 std::cerr <<
"Unable to Draw! halfedge array configuration is invalid!!" << std::endl;
809 drawMesh_->draw(textureMap_);
816 drawMesh_->addTriRenderObjects(_renderer, _baseObj, textureMap_);
833 if (_arrays & PER_EDGE_VERTEX_ARRAY) {
836 if (!(enabled_arrays_ & PER_EDGE_VERTEX_ARRAY)) {
837 enabled_arrays_ |= PER_EDGE_VERTEX_ARRAY;
846 }
else if (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY) {
848 enabled_arrays_ &= ~PER_EDGE_VERTEX_ARRAY;
857 if ( mesh_.has_edge_colors() && ( _arrays & PER_EDGE_COLOR_ARRAY) ) {
860 if (!(enabled_arrays_ & PER_EDGE_COLOR_ARRAY)) {
861 enabled_arrays_ |= PER_EDGE_COLOR_ARRAY;
870 }
else if (enabled_arrays_ & PER_EDGE_COLOR_ARRAY) {
872 enabled_arrays_ &= ~PER_EDGE_COLOR_ARRAY;
882 if (_arrays & PER_HALFEDGE_VERTEX_ARRAY) {
885 if (!(enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY)) {
886 enabled_arrays_ |= PER_HALFEDGE_VERTEX_ARRAY;
895 }
else if (enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY) {
897 enabled_arrays_ &= ~PER_HALFEDGE_VERTEX_ARRAY;
906 if ( mesh_.has_halfedge_colors() && ( _arrays & PER_HALFEDGE_COLOR_ARRAY) ) {
909 if (!(enabled_arrays_ & PER_HALFEDGE_COLOR_ARRAY)) {
910 enabled_arrays_ |= PER_HALFEDGE_COLOR_ARRAY;
919 }
else if (enabled_arrays_ & PER_HALFEDGE_COLOR_ARRAY) {
921 enabled_arrays_ &= ~PER_HALFEDGE_COLOR_ARRAY;
939 pick_vertices(_state);
944 pick_vertices(_state,
true);
967 pick_edges(_state,
true);
983 typename Mesh::ConstVertexIter v_it(mesh_.vertices_begin()),
984 v_end(mesh_.vertices_end());
987 omerr() <<
"MeshNode::pick_vertices: color range too small, " <<
"picking failed\n";
991 if ( mesh_.n_vertices() == 0 ) {
992 std::cerr <<
"pick_vertices: No vertices in Mesh!" << std::endl;
996 if (_front && ( mesh_.n_faces() != 0 ) ) {
1000 clear_color[3] = 1.0;
1002 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1009 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1013 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1018 if ( updateVertexPicking_ || _state.
pick_current_index () != vertexPickingBaseIndex_) {
1019 drawMesh_->updatePickingVertices(_state);
1021 updateVertexPicking_ =
false;
1036 glDrawArrays(GL_POINTS, 0, mesh_.n_vertices());
1045 std::cerr <<
"pick_vertices: No vertices in Mesh!" << std::endl;
1049 std::cerr <<
"No fallback pick_vertices!" << std::endl;
1056 template<
class Mesh>
1064 omerr() <<
"MeshNode::pick_edges: color range too small, " <<
"picking failed\n";
1068 if ( mesh_.n_vertices() == 0 ) {
1069 std::cerr <<
"pick_edges: No vertices in Mesh!" << std::endl;
1073 if ( _front && ( mesh_.n_faces() != 0 ) ) {
1077 clear_color[3] = 1.0;
1079 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1086 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1090 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1099 drawMesh_->updatePickingEdges(_state);
1101 updateEdgePicking_ =
false;
1104 if ( mesh_.n_edges() != 0 && drawMesh_) {
1115 glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
1126 std::cerr <<
"No fallback pick_edges!" << std::endl;
1133 template<
class Mesh>
1138 typename Mesh::ConstFaceIter f_it(mesh_.faces_sbegin()),
1139 f_end(mesh_.faces_end());
1140 typename Mesh::ConstFaceVertexIter fv_it;
1142 if ( mesh_.n_vertices() == 0 ) {
1143 std::cerr <<
"pick_faces: No vertices in Mesh!" << std::endl;
1147 if ( mesh_.n_faces() > 0 ) {
1149 omerr() <<
"MeshNode::pick_faces: color range too small, " <<
"picking failed\n";
1154 omerr() <<
"Strange pickSetMAximum failed for index 1 in MeshNode\n";
1162 drawMesh_->updatePickingFaces(_state);
1164 updateFacePicking_ =
false;
1167 if ( mesh_.n_faces() != 0 ) {
1178 glDrawArrays(GL_TRIANGLES, 0, 3 * drawMesh_->getNumTris());
1189 std::cerr <<
"No fallback pick_faces!" << std::endl;
1193 template<
class Mesh>
1199 unsigned int numElements = mesh_.n_faces() + mesh_.n_edges() + mesh_.n_vertices();
1201 if ( mesh_.n_vertices() == 0 ) {
1202 std::cerr <<
"pick_any: No vertices in Mesh!" << std::endl;
1207 if (numElements <= 0) {
1208 std::cerr <<
"pick_any: Number of elements : 0 " << std::endl;
1214 omerr() <<
"MeshNode::pick_any: color range too small, " <<
"picking failed\n";
1221 drawMesh_->updatePickingAny(_state);
1223 updateAnyPicking_ =
false;
1236 if ( mesh_.n_faces() != 0 && drawMesh_) {
1241 glDrawArrays(GL_TRIANGLES, 0, 3 * drawMesh_->getNumTris());
1247 if ( mesh_.n_edges() != 0 && drawMesh_) {
1252 glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
1261 glDrawArrays(GL_POINTS, 0, mesh_.n_vertices());
1269 std::cerr <<
"No fallback pick_any!" << std::endl;
1277 template<
class Mesh>
1287 updateVertexPicking_ =
true;
1288 updateEdgePicking_ =
true;
1289 updateFacePicking_ =
true;
1290 updateAnyPicking_ =
true;
1293 drawMesh_->invalidatePerEdgeBuffers();
1296 drawMesh_->invalidatePerHalfedgeBuffers();
1298 drawMesh_->updateGeometry();
1302 bbMin_ =
Vec3d(FLT_MAX, FLT_MAX, FLT_MAX);
1303 bbMax_ =
Vec3d(-FLT_MAX, -FLT_MAX, -FLT_MAX);
1304 typename Mesh::ConstVertexIter v_it(mesh_.vertices_begin()),
1305 v_end(mesh_.vertices_end());
1307 for (; v_it!=v_end; ++v_it)
1309 bbMin_.minimize(mesh_.point(v_it));
1310 bbMax_.maximize(mesh_.point(v_it));
1314 template<
class Mesh>
1319 drawMesh_->invalidatePerEdgeBuffers();
1320 drawMesh_->invalidatePerHalfedgeBuffers();
1323 drawMesh_->updateTopology();
1329 template<
class Mesh>
1333 drawMesh_->updateTextures();
1339 template<
class Mesh>
1344 drawMesh_->invalidatePerEdgeBuffers();
1345 drawMesh_->invalidatePerHalfedgeBuffers();
1350 drawMesh_->updateFull();
1353 template<
class Mesh>
1358 drawMesh_->setTextureIndexPropertyName(_indexPropertyName);
1359 perFaceTextureIndexAvailable_ = drawMesh_->perFaceTextureIndexAvailable() != 0;
1362 template<
class Mesh>
1367 return drawMesh_->getTextureIndexPropertyName();
1370 template<
class Mesh>
1375 drawMesh_->setPerFaceTextureCoordinatePropertyName(_halfedgeTextcoordPropertyName);
1376 perFaceTextureCoordsAvailable_ = drawMesh_->perFaceTextureCoordinateAvailable() != 0;
1383 template<
class Mesh>
1387 unsigned int res = 0;
1390 res += drawMesh_->getMemoryUsage();