58#include <OpenMesh/Core/Utils/vector_cast.hh>
60#include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh>
81 mesh_.request_face_normals();
82 mesh_.request_face_colors();
83 mesh_.request_vertex_normals();
84 mesh_.request_vertex_colors();
85 mesh_.request_vertex_texcoords2D();
87 std::cout <<
"Loading from file '" << _filename <<
"'\n";
88 if ( IO::read_mesh(mesh_, _filename, _opt ))
94 if ( ! opt_.check( IO::Options::FaceNormal ) )
97 std::cout <<
"File provides face normals\n";
99 if ( ! opt_.check( IO::Options::VertexNormal ) )
102 std::cout <<
"File provides vertex normals\n";
106 if ( opt_.check( IO::Options::VertexColor ) )
108 std::cout <<
"File provides vertex colors\n";
109 add_draw_mode(
"Colored Vertices");
112 mesh_.release_vertex_colors();
114 if ( _opt.check( IO::Options::FaceColor ) )
116 std::cout <<
"File provides face colors\n";
117 add_draw_mode(
"Solid Colored Faces");
118 add_draw_mode(
"Smooth Colored Faces");
121 mesh_.release_face_colors();
123 if ( _opt.check( IO::Options::VertexTexCoord ) )
124 std::cout <<
"File provides texture coordinates\n";
135 bbMin = bbMax = OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt));
137 for (
size_t count=0; vIt!=vEnd; ++vIt, ++count)
139 bbMin.
minimize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
140 bbMax.
maximize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
145 set_scene_pos( (bbMin+bbMax)*0.5f, (bbMin-bbMax).
norm()*0.5f );
148 normal_scale_ = (bbMax-bbMin).
min()*0.05f;
151 std::clog << mesh_.n_vertices() <<
" vertices, "
152 << mesh_.n_edges() <<
" edge, "
153 << mesh_.n_faces() <<
" faces\n";
159 mesh_.add_property( fp_normal_base_ );
160 typename M::FaceIter f_it = mesh_.faces_begin();
161 typename M::FaceVertexIter fv_it;
162 for (;f_it != mesh_.faces_end(); ++f_it)
165 for( fv_it=mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it)
166 v += OpenMesh::vector_cast<typename Mesh::Normal>(mesh_.point(*fv_it));
168 mesh_.property( fp_normal_base_, *f_it ) = v;
171 std::clog <<
"Computed base point for displaying face normals ["
177 std::clog <<
"Computing strips.." << std::flush;
182 std::clog <<
"done [" << strips_.n_strips()
183 <<
" strips created in " << t.
as_string() <<
"]\n";
191 if ( this->parentWidget() !=
nullptr )
192 this->parentWidget()->setWindowTitle(QFileInfo(_filename).fileName());
194 setWindowTitle(QFileInfo(_filename).fileName());
209 QString fname = _filename;
211 if (texsrc.load( fname ))
213 return set_texture( texsrc );
224 if ( !opt_.vertex_has_texcoord() )
229 int tex_w, w( _texsrc.width() );
230 int tex_h, h( _texsrc.height() );
232 for (tex_w=1; tex_w <= w; tex_w <<= 1) {};
233 for (tex_h=1; tex_h <= h; tex_h <<= 1) {};
236 _texsrc = _texsrc.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
239 QImage texture = _texsrc.convertToFormat(QImage::Format_ARGB32).rgbSwapped();
241 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
242 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
243 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
245 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
246 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
247 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
248 glPixelStorei(GL_PACK_ALIGNMENT, 1);
252 glDeleteTextures(1, &tex_id_);
254 glGenTextures(1, &tex_id_);
255 glBindTexture(GL_TEXTURE_2D, tex_id_);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
265 glTexImage2D(GL_TEXTURE_2D,
275 std::cout <<
"Texture loaded\n";
287 fEnd(mesh_.faces_end());
291#if defined(OM_USE_OSG) && OM_USE_OSG
292 if (_draw_mode ==
"OpenSG Indices")
294 glEnableClientState(GL_VERTEX_ARRAY);
295 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
297 glEnableClientState(GL_NORMAL_ARRAY);
298 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
300 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
302 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
303 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
304 glEnable(GL_TEXTURE_2D);
305 glBindTexture(GL_TEXTURE_2D, tex_id_);
306 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
309 glDrawElements(GL_TRIANGLES,
310 mesh_.osg_indices()->size(),
312 &mesh_.osg_indices()->getField()[0] );
314 glDisableClientState(GL_VERTEX_ARRAY);
315 glDisableClientState(GL_NORMAL_ARRAY);
316 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
321 if (_draw_mode ==
"Wireframe")
323 glBegin(GL_TRIANGLES);
324 for (; fIt!=fEnd; ++fIt)
326 fvIt = mesh_.cfv_iter(*fIt);
327 glVertex3fv( &mesh_.point(*fvIt)[0] );
329 glVertex3fv( &mesh_.point(*fvIt)[0] );
331 glVertex3fv( &mesh_.point(*fvIt)[0] );
336 else if (_draw_mode ==
"Solid Flat")
338 glBegin(GL_TRIANGLES);
339 for (; fIt!=fEnd; ++fIt)
341 glNormal3fv( &mesh_.normal(*fIt)[0] );
343 fvIt = mesh_.cfv_iter(*fIt);
344 glVertex3fv( &mesh_.point(*fvIt)[0] );
346 glVertex3fv( &mesh_.point(*fvIt)[0] );
348 glVertex3fv( &mesh_.point(*fvIt)[0] );
355 else if (_draw_mode ==
"Solid Smooth")
357 glEnableClientState(GL_VERTEX_ARRAY);
358 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
360 glEnableClientState(GL_NORMAL_ARRAY);
361 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
363 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
365 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
366 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
367 glEnable(GL_TEXTURE_2D);
368 glBindTexture(GL_TEXTURE_2D, tex_id_);
369 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
372 glBegin(GL_TRIANGLES);
373 for (; fIt!=fEnd; ++fIt)
375 fvIt = mesh_.cfv_iter(*fIt);
376 glArrayElement(fvIt->idx());
378 glArrayElement(fvIt->idx());
380 glArrayElement(fvIt->idx());
384 glDisableClientState(GL_VERTEX_ARRAY);
385 glDisableClientState(GL_NORMAL_ARRAY);
386 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
388 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
390 glDisable(GL_TEXTURE_2D);
394 else if (_draw_mode ==
"Colored Vertices")
396 glEnableClientState(GL_VERTEX_ARRAY);
397 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
399 glEnableClientState(GL_NORMAL_ARRAY);
400 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
402 if ( mesh_.has_vertex_colors() )
404 glEnableClientState( GL_COLOR_ARRAY );
405 glColorPointer(3, GL_UNSIGNED_BYTE, 0,mesh_.vertex_colors());
408 glBegin(GL_TRIANGLES);
409 for (; fIt!=fEnd; ++fIt)
411 fvIt = mesh_.cfv_iter(*fIt);
412 glArrayElement(fvIt->idx());
414 glArrayElement(fvIt->idx());
416 glArrayElement(fvIt->idx());
420 glDisableClientState(GL_VERTEX_ARRAY);
421 glDisableClientState(GL_NORMAL_ARRAY);
422 glDisableClientState(GL_COLOR_ARRAY);
426 else if (_draw_mode ==
"Solid Colored Faces")
428 glEnableClientState(GL_VERTEX_ARRAY);
429 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
431 glEnableClientState(GL_NORMAL_ARRAY);
432 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
434 glBegin(GL_TRIANGLES);
435 for (; fIt!=fEnd; ++fIt)
439 fvIt = mesh_.cfv_iter(*fIt);
440 glArrayElement(fvIt->idx());
442 glArrayElement(fvIt->idx());
444 glArrayElement(fvIt->idx());
448 glDisableClientState(GL_VERTEX_ARRAY);
449 glDisableClientState(GL_NORMAL_ARRAY);
453 else if (_draw_mode ==
"Smooth Colored Faces")
455 glEnableClientState(GL_VERTEX_ARRAY);
456 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
458 glEnableClientState(GL_NORMAL_ARRAY);
459 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
461 glBegin(GL_TRIANGLES);
462 for (; fIt!=fEnd; ++fIt)
466 fvIt = mesh_.cfv_iter(*fIt);
467 glArrayElement(fvIt->idx());
469 glArrayElement(fvIt->idx());
471 glArrayElement(fvIt->idx());
475 glDisableClientState(GL_VERTEX_ARRAY);
476 glDisableClientState(GL_NORMAL_ARRAY);
480 else if ( _draw_mode ==
"Strips'n VertexArrays" )
482 glEnableClientState(GL_VERTEX_ARRAY);
483 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
485 glEnableClientState(GL_NORMAL_ARRAY);
486 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
488 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
490 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
491 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
492 glEnable(GL_TEXTURE_2D);
493 glBindTexture(GL_TEXTURE_2D, tex_id_);
494 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
497 typename MyStripifier::StripsIterator strip_it = strips_.begin();
498 typename MyStripifier::StripsIterator strip_last = strips_.end();
501 for (; strip_it!=strip_last; ++strip_it)
503 glDrawElements(GL_TRIANGLE_STRIP,
504 static_cast<GLsizei
>(strip_it->size()), GL_UNSIGNED_INT, &(*strip_it)[0] );
507 glDisableClientState(GL_VERTEX_ARRAY);
508 glDisableClientState(GL_NORMAL_ARRAY);
509 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
513 else if (_draw_mode ==
"Show Strips" && strips_.is_valid() )
515 typename MyStripifier::StripsIterator strip_it = strips_.begin();
516 typename MyStripifier::StripsIterator strip_last = strips_.end();
520 int base = (int)cmax-range;
525 int rcol=0, gcol=dgcol, bcol=dbcol+dbcol;
528 for (; strip_it!=strip_last; ++strip_it)
530 typename MyStripifier::IndexIterator idx_it = strip_it->begin();
531 typename MyStripifier::IndexIterator idx_last = strip_it->end();
533 rcol = (rcol+drcol) % range;
534 gcol = (gcol+dgcol) % range;
535 bcol = (bcol+dbcol) % range;
537 glBegin(GL_TRIANGLE_STRIP);
538 glColor3f((rcol+base)/cmax, (gcol+base)/cmax, (bcol+base)/cmax);
539 for ( ;idx_it != idx_last; ++idx_it )
543 glColor3f(1.0, 1.0, 1.0);
547 else if( _draw_mode ==
"Points" )
549 glEnableClientState(GL_VERTEX_ARRAY);
550 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
552 if (mesh_.has_vertex_colors() && use_color_)
554 glEnableClientState(GL_COLOR_ARRAY);
555 glColorPointer(3, GL_UNSIGNED_BYTE, 0, mesh_.vertex_colors());
558 glDrawArrays( GL_POINTS, 0,
static_cast<GLsizei
>(mesh_.n_vertices()) );
559 glDisableClientState(GL_VERTEX_ARRAY);
560 glDisableClientState(GL_COLOR_ARRAY);
575 if ( ! mesh_.n_vertices() )
578#if defined(OM_USE_OSG) && OM_USE_OSG
579 else if ( _draw_mode ==
"OpenSG Indices")
581 glEnable(GL_LIGHTING);
582 glShadeModel(GL_SMOOTH);
583 draw_openmesh( _draw_mode );
587 if ( _draw_mode ==
"Points" )
589 glDisable(GL_LIGHTING);
590 draw_openmesh(_draw_mode);
592 else if (_draw_mode ==
"Wireframe")
594 glDisable(GL_LIGHTING);
595 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
596 draw_openmesh(_draw_mode);
597 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
600 else if ( _draw_mode ==
"Hidden-Line" )
602 glDisable(GL_LIGHTING);
603 glShadeModel(GL_FLAT);
604 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
605 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
606 glDepthRange(0.01, 1.0);
607 draw_openmesh(
"Wireframe" );
609 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
610 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
611 glDepthRange( 0.0, 1.0 );
612 draw_openmesh(
"Wireframe" );
614 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
617 else if (_draw_mode ==
"Solid Flat")
619 glEnable(GL_LIGHTING);
620 glShadeModel(GL_FLAT);
621 draw_openmesh(_draw_mode);
624 else if (_draw_mode ==
"Solid Smooth" ||
625 _draw_mode ==
"Strips'n VertexArrays" )
627 glEnable(GL_LIGHTING);
628 glShadeModel(GL_SMOOTH);
629 draw_openmesh(_draw_mode);
632 else if (_draw_mode ==
"Show Strips")
634 glDisable(GL_LIGHTING);
635 draw_openmesh(_draw_mode);
638 else if (_draw_mode ==
"Colored Vertices" )
640 glDisable(GL_LIGHTING);
641 glShadeModel(GL_SMOOTH);
642 draw_openmesh(_draw_mode);
645 else if (_draw_mode ==
"Solid Colored Faces")
647 glDisable(GL_LIGHTING);
648 glShadeModel(GL_FLAT);
649 draw_openmesh(_draw_mode);
650 setDefaultMaterial();
653 else if (_draw_mode ==
"Smooth Colored Faces" )
655 glEnable(GL_LIGHTING);
656 glShadeModel(GL_SMOOTH);
657 draw_openmesh(_draw_mode);
658 setDefaultMaterial();
664 glDisable(GL_LIGHTING);
666 glColor3f(1.000f, 0.803f, 0.027f);
667 for(vit=mesh_.vertices_begin(); vit!=mesh_.vertices_end(); ++vit)
670 glVertex( mesh_.point( *vit ) + normal_scale_*mesh_.normal( *vit ) );
678 glDisable(GL_LIGHTING);
680 glColor3f(0.705f, 0.976f, 0.270f);
681 for(fit=mesh_.faces_begin(); fit!=mesh_.faces_end(); ++fit)
683 glVertex( mesh_.property(fp_normal_base_, *fit) );
684 glVertex( mesh_.property(fp_normal_base_, *fit) +
685 normal_scale_*mesh_.normal( *fit ) );
701 add_draw_mode(
"Strips'n VertexArrays");
702 add_draw_mode(
"Show Strips");
715 del_draw_mode(
"Show Strips");
716 del_draw_mode(
"Strip'n VertexArrays");
723#define TEXMODE( Mode ) \
724 tex_mode_ = Mode; std::cout << "Texture mode set to " << #Mode << std::endl
730 switch( _event->key() )
733 if ( mesh_.has_vertex_colors() && (current_draw_mode()==
"Points") )
735 use_color_ = !use_color_;
736 std::cout <<
"use color: " << (use_color_?
"yes\n":
"no\n");
738 glColor3f(1.0f, 1.0f, 1.0f);
744 if ( _event->modifiers() & ShiftModifier )
746 show_fnormals_ = !show_fnormals_;
747 std::cout <<
"show face normals: " << (show_fnormals_?
"yes\n":
"no\n");
751 show_vnormals_ = !show_vnormals_;
752 std::cout <<
"show vertex normals: " << (show_vnormals_?
"yes\n":
"no\n");
758 std::cout <<
"\n# Vertices : " << mesh_.n_vertices() << std::endl;
759 std::cout <<
"# Edges : " << mesh_.n_edges() << std::endl;
760 std::cout <<
"# Faces : " << mesh_.n_faces() << std::endl;
761 std::cout <<
"binary input : " << opt_.check(opt_.Binary) << std::endl;
762 std::cout <<
"swapped input : " << opt_.check(opt_.Swap) << std::endl;
763 std::cout <<
"vertex normal : "
764 << opt_.check(opt_.VertexNormal) << std::endl;
765 std::cout <<
"vertex texcoord: "
766 << opt_.check(opt_.VertexTexCoord) << std::endl;
767 std::cout <<
"vertex color : "
768 << opt_.check(opt_.VertexColor) << std::endl;
769 std::cout <<
"face normal : "
770 << opt_.check(opt_.FaceNormal) << std::endl;
771 std::cout <<
"face color : "
772 << opt_.check(opt_.FaceColor) << std::endl;
773 this->QGLViewerWidget::keyPressEvent( _event );
779 case GL_MODULATE: TEXMODE(GL_DECAL);
break;
780 case GL_DECAL: TEXMODE(GL_BLEND);
break;
781 case GL_BLEND: TEXMODE(GL_REPLACE);
break;
782 case GL_REPLACE: TEXMODE(GL_MODULATE);
break;
788 this->QGLViewerWidget::keyPressEvent( _event );
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Definition: MeshViewerWidgetT.hh:71
virtual bool open_texture(const char *_filename)
load texture
Definition: MeshViewerWidgetT_impl.hh:206
virtual bool open_mesh(const char *_filename, OpenMesh::IO::Options _opt)
open mesh
Definition: MeshViewerWidgetT_impl.hh:75
virtual void draw_scene(const std::string &_draw_mode) override
inherited drawing method
Definition: MeshViewerWidgetT_impl.hh:572
virtual void draw_openmesh(const std::string &_drawmode)
draw the mesh
Definition: MeshViewerWidgetT_impl.hh:284
VectorT< Scalar, DIM > min(const VectorT< Scalar, DIM > &_v1, const VectorT< Scalar, DIM > &_v2)
non-member min
Definition: Vector11T.hh:797
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:588
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:560
Scalar norm(const VectorT< Scalar, DIM > &_v)
non-member norm
Definition: Vector11T.hh:749
Set options for reader/writer modules.
Definition: Options.hh:92
Kernel::ConstFaceIter ConstFaceIter
Scalar type.
Definition: PolyMeshT.hh:151
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Definition: PolyMeshT.hh:177
Kernel::FaceIter FaceIter
Scalar type.
Definition: PolyMeshT.hh:146
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:335
Kernel::ConstVertexIter ConstVertexIter
Scalar type.
Definition: PolyMeshT.hh:148
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT_impl.hh:572
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Kernel::VertexIter VertexIter
Scalar type.
Definition: PolyMeshT.hh:143
Timer class.
Definition: Timer.hh:83
void stop(void)
Stop measurement.
std::string as_string(Format format=Automatic)
Returns the measured time as a string.
void start(void)
Start measurement.