49 #define OPENMESHAPPS_MESHVIEWERWIDGET_CC
65 #include <OpenMesh/Core/Utils/vector_cast.hh>
67 #include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh>
89 mesh_.request_face_normals();
90 mesh_.request_face_colors();
91 mesh_.request_vertex_normals();
92 mesh_.request_vertex_colors();
93 mesh_.request_vertex_texcoords2D();
95 std::cout <<
"Loading from file '" << _filename <<
"'\n";
105 std::cout <<
"File provides face normals\n";
110 std::cout <<
"File provides vertex normals\n";
116 std::cout <<
"File provides vertex colors\n";
117 add_draw_mode(
"Colored Vertices");
120 mesh_.release_vertex_colors();
124 std::cout <<
"File provides face colors\n";
125 add_draw_mode(
"Solid Colored Faces");
126 add_draw_mode(
"Smooth Colored Faces");
129 mesh_.release_face_colors();
132 std::cout <<
"File provides texture coordinates\n";
136 typename Mesh::ConstVertexIter vIt(mesh_.vertices_begin());
137 typename Mesh::ConstVertexIter vEnd(mesh_.vertices_end());
146 for (
size_t count=0; vIt!=vEnd; ++vIt, ++count)
148 bbMin.minimize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
149 bbMax.maximize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
154 set_scene_pos( (bbMin+bbMax)*0.5, (bbMin-bbMax).norm()*0.5 );
157 normal_scale_ = (bbMax-bbMin).min()*0.05f;
160 std::clog << mesh_.n_vertices() <<
" vertices, "
161 << mesh_.n_edges() <<
" edge, "
162 << mesh_.n_faces() <<
" faces\n";
168 mesh_.add_property( fp_normal_base_ );
169 typename M::FaceIter f_it = mesh_.faces_begin();
170 typename M::FaceVertexIter fv_it;
171 for (;f_it != mesh_.faces_end(); ++f_it)
174 for( fv_it=mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it)
175 v += OpenMesh::vector_cast<typename Mesh::Normal>(mesh_.point(*fv_it));
177 mesh_.property( fp_normal_base_, *f_it ) = v;
180 std::clog <<
"Computed base point for displaying face normals ["
186 std::clog <<
"Computing strips.." << std::flush;
191 std::clog <<
"done [" << strips_.n_strips()
192 <<
" strips created in " << t.
as_string() <<
"]\n";
200 setWindowTitle(QFileInfo(_filename).fileName());
211 template <
typename M>
215 QString fname = _filename;
217 if (texsrc.load( fname ))
219 return set_texture( texsrc );
227 template <
typename M>
230 if ( !opt_.vertex_has_texcoord() )
235 int tex_w, w( _texsrc.width() );
236 int tex_h, h( _texsrc.height() );
238 for (tex_w=1; tex_w <= w; tex_w <<= 1) {};
239 for (tex_h=1; tex_h <= h; tex_h <<= 1) {};
242 _texsrc = _texsrc.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
245 QImage texture( QGLWidget::convertToGLFormat ( _texsrc ) );
247 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
248 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
249 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
250 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
251 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
252 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
253 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
254 glPixelStorei(GL_PACK_ALIGNMENT, 1);
258 glDeleteTextures(1, &tex_id_);
260 glGenTextures(1, &tex_id_);
261 glBindTexture(GL_TEXTURE_2D, tex_id_);
266 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
269 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
271 glTexImage2D(GL_TEXTURE_2D,
281 std::cout <<
"Texture loaded\n";
288 template <
typename M>
292 typename Mesh::ConstFaceIter fIt(mesh_.faces_begin()),
293 fEnd(mesh_.faces_end());
297 #if defined(OM_USE_OSG) && OM_USE_OSG
298 if (_draw_mode ==
"OpenSG Indices")
300 glEnableClientState(GL_VERTEX_ARRAY);
301 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
303 glEnableClientState(GL_NORMAL_ARRAY);
304 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
306 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
308 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
309 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
310 glEnable(GL_TEXTURE_2D);
311 glBindTexture(GL_TEXTURE_2D, tex_id_);
312 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
315 glDrawElements(GL_TRIANGLES,
316 mesh_.osg_indices()->size(),
318 &mesh_.osg_indices()->getField()[0] );
320 glDisableClientState(GL_VERTEX_ARRAY);
321 glDisableClientState(GL_NORMAL_ARRAY);
322 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
327 if (_draw_mode ==
"Wireframe")
329 glBegin(GL_TRIANGLES);
330 for (; fIt!=fEnd; ++fIt)
332 fvIt = mesh_.cfv_iter(*fIt);
333 glVertex3fv( &mesh_.point(*fvIt)[0] );
335 glVertex3fv( &mesh_.point(*fvIt)[0] );
337 glVertex3fv( &mesh_.point(*fvIt)[0] );
342 else if (_draw_mode ==
"Solid Flat")
344 glBegin(GL_TRIANGLES);
345 for (; fIt!=fEnd; ++fIt)
347 glNormal3fv( &mesh_.normal(*fIt)[0] );
349 fvIt = mesh_.cfv_iter(*fIt);
350 glVertex3fv( &mesh_.point(*fvIt)[0] );
352 glVertex3fv( &mesh_.point(*fvIt)[0] );
354 glVertex3fv( &mesh_.point(*fvIt)[0] );
361 else if (_draw_mode ==
"Solid Smooth")
363 glEnableClientState(GL_VERTEX_ARRAY);
364 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
366 glEnableClientState(GL_NORMAL_ARRAY);
367 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
369 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
371 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
372 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
373 glEnable(GL_TEXTURE_2D);
374 glBindTexture(GL_TEXTURE_2D, tex_id_);
375 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
378 glBegin(GL_TRIANGLES);
379 for (; fIt!=fEnd; ++fIt)
381 fvIt = mesh_.cfv_iter(*fIt);
382 glArrayElement(fvIt->idx());
384 glArrayElement(fvIt->idx());
386 glArrayElement(fvIt->idx());
390 glDisableClientState(GL_VERTEX_ARRAY);
391 glDisableClientState(GL_NORMAL_ARRAY);
392 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
394 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
396 glDisable(GL_TEXTURE_2D);
400 else if (_draw_mode ==
"Colored Vertices")
402 glEnableClientState(GL_VERTEX_ARRAY);
403 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
405 glEnableClientState(GL_NORMAL_ARRAY);
406 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
408 if ( mesh_.has_vertex_colors() )
410 glEnableClientState( GL_COLOR_ARRAY );
411 glColorPointer(3, GL_UNSIGNED_BYTE, 0,mesh_.vertex_colors());
414 glBegin(GL_TRIANGLES);
415 for (; fIt!=fEnd; ++fIt)
417 fvIt = mesh_.cfv_iter(*fIt);
418 glArrayElement(fvIt->idx());
420 glArrayElement(fvIt->idx());
422 glArrayElement(fvIt->idx());
426 glDisableClientState(GL_VERTEX_ARRAY);
427 glDisableClientState(GL_NORMAL_ARRAY);
428 glDisableClientState(GL_COLOR_ARRAY);
432 else if (_draw_mode ==
"Solid Colored Faces")
434 glEnableClientState(GL_VERTEX_ARRAY);
435 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
437 glEnableClientState(GL_NORMAL_ARRAY);
438 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
440 glBegin(GL_TRIANGLES);
441 for (; fIt!=fEnd; ++fIt)
445 fvIt = mesh_.cfv_iter(*fIt);
446 glArrayElement(fvIt->idx());
448 glArrayElement(fvIt->idx());
450 glArrayElement(fvIt->idx());
454 glDisableClientState(GL_VERTEX_ARRAY);
455 glDisableClientState(GL_NORMAL_ARRAY);
459 else if (_draw_mode ==
"Smooth Colored Faces")
461 glEnableClientState(GL_VERTEX_ARRAY);
462 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
464 glEnableClientState(GL_NORMAL_ARRAY);
465 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
467 glBegin(GL_TRIANGLES);
468 for (; fIt!=fEnd; ++fIt)
472 fvIt = mesh_.cfv_iter(*fIt);
473 glArrayElement(fvIt->idx());
475 glArrayElement(fvIt->idx());
477 glArrayElement(fvIt->idx());
481 glDisableClientState(GL_VERTEX_ARRAY);
482 glDisableClientState(GL_NORMAL_ARRAY);
486 else if ( _draw_mode ==
"Strips'n VertexArrays" )
488 glEnableClientState(GL_VERTEX_ARRAY);
489 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
491 glEnableClientState(GL_NORMAL_ARRAY);
492 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
494 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
496 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
497 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
498 glEnable(GL_TEXTURE_2D);
499 glBindTexture(GL_TEXTURE_2D, tex_id_);
500 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
503 typename MyStripifier::StripsIterator strip_it = strips_.begin();
504 typename MyStripifier::StripsIterator strip_last = strips_.end();
507 for (; strip_it!=strip_last; ++strip_it)
509 glDrawElements(GL_TRIANGLE_STRIP,
510 static_cast<GLsizei>(strip_it->size()), GL_UNSIGNED_INT, &(*strip_it)[0] );
513 glDisableClientState(GL_VERTEX_ARRAY);
514 glDisableClientState(GL_NORMAL_ARRAY);
515 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
519 else if (_draw_mode ==
"Show Strips" && strips_.is_valid() )
521 typename MyStripifier::StripsIterator strip_it = strips_.begin();
522 typename MyStripifier::StripsIterator strip_last = strips_.end();
526 int base = (int)cmax-range;
531 int rcol=0, gcol=dgcol, bcol=dbcol+dbcol;
534 for (; strip_it!=strip_last; ++strip_it)
536 typename MyStripifier::IndexIterator idx_it = strip_it->begin();
537 typename MyStripifier::IndexIterator idx_last = strip_it->end();
539 rcol = (rcol+drcol) % range;
540 gcol = (gcol+dgcol) % range;
541 bcol = (bcol+dbcol) % range;
543 glBegin(GL_TRIANGLE_STRIP);
544 glColor3f((rcol+base)/cmax, (gcol+base)/cmax, (bcol+base)/cmax);
545 for ( ;idx_it != idx_last; ++idx_it )
549 glColor3f(1.0, 1.0, 1.0);
553 else if( _draw_mode ==
"Points" )
555 glEnableClientState(GL_VERTEX_ARRAY);
556 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
558 if (mesh_.has_vertex_colors() && use_color_)
560 glEnableClientState(GL_COLOR_ARRAY);
561 glColorPointer(3, GL_UNSIGNED_BYTE, 0, mesh_.vertex_colors());
564 glDrawArrays( GL_POINTS, 0, static_cast<GLsizei>(mesh_.n_vertices()) );
565 glDisableClientState(GL_VERTEX_ARRAY);
566 glDisableClientState(GL_COLOR_ARRAY);
576 template <
typename M>
581 if ( ! mesh_.n_vertices() )
584 #if defined(OM_USE_OSG) && OM_USE_OSG
585 else if ( _draw_mode ==
"OpenSG Indices")
587 glEnable(GL_LIGHTING);
588 glShadeModel(GL_SMOOTH);
589 draw_openmesh( _draw_mode );
593 if ( _draw_mode ==
"Points" )
595 glDisable(GL_LIGHTING);
596 draw_openmesh(_draw_mode);
598 else if (_draw_mode ==
"Wireframe")
600 glDisable(GL_LIGHTING);
601 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
602 draw_openmesh(_draw_mode);
603 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
606 else if ( _draw_mode ==
"Hidden-Line" )
608 glDisable(GL_LIGHTING);
609 glShadeModel(GL_FLAT);
610 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
611 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
612 glDepthRange(0.01, 1.0);
613 draw_openmesh(
"Wireframe" );
615 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
616 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
617 glDepthRange( 0.0, 1.0 );
618 draw_openmesh(
"Wireframe" );
620 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
623 else if (_draw_mode ==
"Solid Flat")
625 glEnable(GL_LIGHTING);
626 glShadeModel(GL_FLAT);
627 draw_openmesh(_draw_mode);
630 else if (_draw_mode ==
"Solid Smooth" ||
631 _draw_mode ==
"Strips'n VertexArrays" )
633 glEnable(GL_LIGHTING);
634 glShadeModel(GL_SMOOTH);
635 draw_openmesh(_draw_mode);
638 else if (_draw_mode ==
"Show Strips")
640 glDisable(GL_LIGHTING);
641 draw_openmesh(_draw_mode);
644 else if (_draw_mode ==
"Colored Vertices" )
646 glDisable(GL_LIGHTING);
647 glShadeModel(GL_SMOOTH);
648 draw_openmesh(_draw_mode);
651 else if (_draw_mode ==
"Solid Colored Faces")
653 glDisable(GL_LIGHTING);
654 glShadeModel(GL_FLAT);
655 draw_openmesh(_draw_mode);
656 setDefaultMaterial();
659 else if (_draw_mode ==
"Smooth Colored Faces" )
661 glEnable(GL_LIGHTING);
662 glShadeModel(GL_SMOOTH);
663 draw_openmesh(_draw_mode);
664 setDefaultMaterial();
669 typename Mesh::VertexIter vit;
670 glDisable(GL_LIGHTING);
672 glColor3f(1.000f, 0.803f, 0.027f);
673 for(vit=mesh_.vertices_begin(); vit!=mesh_.vertices_end(); ++vit)
676 glVertex( mesh_.point( *vit ) + normal_scale_*mesh_.normal( *vit ) );
683 typename Mesh::FaceIter fit;
684 glDisable(GL_LIGHTING);
686 glColor3f(0.705f, 0.976f, 0.270f);
687 for(fit=mesh_.faces_begin(); fit!=mesh_.faces_end(); ++fit)
689 glVertex( mesh_.property(fp_normal_base_, *fit) );
690 glVertex( mesh_.property(fp_normal_base_, *fit) +
691 normal_scale_*mesh_.normal( *fit ) );
700 template <
typename M>
707 add_draw_mode(
"Strips'n VertexArrays");
708 add_draw_mode(
"Show Strips");
714 template <
typename M>
721 del_draw_mode(
"Show Strips");
722 del_draw_mode(
"Strip'n VertexArrays");
729 #define TEXMODE( Mode ) \
730 tex_mode_ = Mode; std::cout << "Texture mode set to " << #Mode << std::endl
732 template <
typename M>
736 switch( _event->key() )
739 if ( mesh_.has_vertex_colors() && (current_draw_mode()==
"Points") )
741 use_color_ = !use_color_;
742 std::cout <<
"use color: " << (use_color_?
"yes\n":
"no\n");
744 glColor3f(1.0f, 1.0f, 1.0f);
750 if ( _event->modifiers() & ShiftModifier )
752 show_fnormals_ = !show_fnormals_;
753 std::cout <<
"show face normals: " << (show_fnormals_?
"yes\n":
"no\n");
757 show_vnormals_ = !show_vnormals_;
758 std::cout <<
"show vertex normals: " << (show_vnormals_?
"yes\n":
"no\n");
764 std::cout <<
"\n# Vertices : " << mesh_.n_vertices() << std::endl;
765 std::cout <<
"# Edges : " << mesh_.n_edges() << std::endl;
766 std::cout <<
"# Faces : " << mesh_.n_faces() << std::endl;
767 std::cout <<
"binary input : " << opt_.check(opt_.Binary) << std::endl;
768 std::cout <<
"swapped input : " << opt_.check(opt_.Swap) << std::endl;
769 std::cout <<
"vertex normal : "
770 << opt_.check(opt_.VertexNormal) << std::endl;
771 std::cout <<
"vertex texcoord: "
772 << opt_.check(opt_.VertexTexCoord) << std::endl;
773 std::cout <<
"vertex color : "
774 << opt_.check(opt_.VertexColor) << std::endl;
775 std::cout <<
"face normal : "
776 << opt_.check(opt_.FaceNormal) << std::endl;
777 std::cout <<
"face color : "
778 << opt_.check(opt_.FaceColor) << std::endl;
779 this->QGLViewerWidget::keyPressEvent( _event );
785 case GL_MODULATE: TEXMODE(GL_DECAL);
break;
786 case GL_DECAL: TEXMODE(GL_BLEND);
break;
787 case GL_BLEND: TEXMODE(GL_REPLACE);
break;
788 case GL_REPLACE: TEXMODE(GL_MODULATE);
break;
794 this->QGLViewerWidget::keyPressEvent( _event );
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
void stop(void)
Stop measurement.
Has (r) / store (w) vertex normals.
Has (r) / store (w) face normals.
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Has (r) / store (w) face colors.
Kernel::Point Point
Coordinate type.
Has (r) / store (w) texture coordinates.
Set options for reader/writer modules.
std::string as_string(Format format=Automatic)
void update_vertex_normals()
Update normal vectors for all vertices.
void update_face_normals()
Update normal vectors for all faces.
void start(void)
Start measurement.
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
virtual bool open_mesh(const char *_filename, OpenMesh::IO::Options _opt)
open mesh
virtual void draw_scene(const std::string &_draw_mode)
inherited drawing method
Has (r) / store (w) vertex colors.
virtual void draw_openmesh(const std::string &_drawmode)
draw the mesh
VectorT< float, 3 > Vec3f
virtual bool open_texture(const char *_filename)
load texture