44 #define OPENMESHAPPS_MESHVIEWERWIDGET_CC 60 #include <OpenMesh/Core/Utils/vector_cast.hh> 62 #include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh> 84 mesh_.request_face_normals();
85 mesh_.request_face_colors();
86 mesh_.request_vertex_normals();
87 mesh_.request_vertex_colors();
88 mesh_.request_vertex_texcoords2D();
90 std::cout <<
"Loading from file '" << _filename <<
"'\n";
100 std::cout <<
"File provides face normals\n";
105 std::cout <<
"File provides vertex normals\n";
111 std::cout <<
"File provides vertex colors\n";
112 add_draw_mode(
"Colored Vertices");
115 mesh_.release_vertex_colors();
119 std::cout <<
"File provides face colors\n";
120 add_draw_mode(
"Solid Colored Faces");
121 add_draw_mode(
"Smooth Colored Faces");
124 mesh_.release_face_colors();
127 std::cout <<
"File provides texture coordinates\n";
131 typename Mesh::ConstVertexIter vIt(mesh_.vertices_begin());
132 typename Mesh::ConstVertexIter vEnd(mesh_.vertices_end());
141 for (
size_t count=0; vIt!=vEnd; ++vIt, ++count)
143 bbMin.
minimize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
144 bbMax.
maximize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
149 set_scene_pos( (bbMin+bbMax)*0.5, (bbMin-bbMax).norm()*0.5 );
152 normal_scale_ = (bbMax-bbMin).min()*0.05f;
155 std::clog << mesh_.n_vertices() <<
" vertices, " 156 << mesh_.n_edges() <<
" edge, " 157 << mesh_.n_faces() <<
" faces\n";
163 mesh_.add_property( fp_normal_base_ );
164 typename M::FaceIter f_it = mesh_.faces_begin();
165 typename M::FaceVertexIter fv_it;
166 for (;f_it != mesh_.faces_end(); ++f_it)
169 for( fv_it=mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it)
170 v += OpenMesh::vector_cast<typename Mesh::Normal>(mesh_.point(*fv_it));
172 mesh_.property( fp_normal_base_, *f_it ) = v;
175 std::clog <<
"Computed base point for displaying face normals [" 181 std::clog <<
"Computing strips.." << std::flush;
186 std::clog <<
"done [" << strips_.n_strips()
187 <<
" strips created in " << t.
as_string() <<
"]\n";
195 setWindowTitle(QFileInfo(_filename).fileName());
206 template <
typename M>
210 QString fname = _filename;
212 if (texsrc.load( fname ))
214 return set_texture( texsrc );
222 template <
typename M>
225 if ( !opt_.vertex_has_texcoord() )
230 int tex_w, w( _texsrc.width() );
231 int tex_h, h( _texsrc.height() );
233 for (tex_w=1; tex_w <= w; tex_w <<= 1) {};
234 for (tex_h=1; tex_h <= h; tex_h <<= 1) {};
237 _texsrc = _texsrc.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
240 QImage texture( QGLWidget::convertToGLFormat ( _texsrc ) );
242 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
243 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
244 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
245 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
246 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
247 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
248 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
249 glPixelStorei(GL_PACK_ALIGNMENT, 1);
253 glDeleteTextures(1, &tex_id_);
255 glGenTextures(1, &tex_id_);
256 glBindTexture(GL_TEXTURE_2D, tex_id_);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
266 glTexImage2D(GL_TEXTURE_2D,
276 std::cout <<
"Texture loaded\n";
283 template <
typename M>
287 typename Mesh::ConstFaceIter fIt(mesh_.faces_begin()),
288 fEnd(mesh_.faces_end());
292 #if defined(OM_USE_OSG) && OM_USE_OSG 293 if (_draw_mode ==
"OpenSG Indices")
295 glEnableClientState(GL_VERTEX_ARRAY);
296 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
298 glEnableClientState(GL_NORMAL_ARRAY);
299 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
301 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
303 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
304 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
305 glEnable(GL_TEXTURE_2D);
306 glBindTexture(GL_TEXTURE_2D, tex_id_);
307 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
310 glDrawElements(GL_TRIANGLES,
311 mesh_.osg_indices()->size(),
313 &mesh_.osg_indices()->getField()[0] );
315 glDisableClientState(GL_VERTEX_ARRAY);
316 glDisableClientState(GL_NORMAL_ARRAY);
317 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
322 if (_draw_mode ==
"Wireframe")
324 glBegin(GL_TRIANGLES);
325 for (; fIt!=fEnd; ++fIt)
327 fvIt = mesh_.cfv_iter(*fIt);
328 glVertex3fv( &mesh_.point(*fvIt)[0] );
330 glVertex3fv( &mesh_.point(*fvIt)[0] );
332 glVertex3fv( &mesh_.point(*fvIt)[0] );
337 else if (_draw_mode ==
"Solid Flat")
339 glBegin(GL_TRIANGLES);
340 for (; fIt!=fEnd; ++fIt)
342 glNormal3fv( &mesh_.normal(*fIt)[0] );
344 fvIt = mesh_.cfv_iter(*fIt);
345 glVertex3fv( &mesh_.point(*fvIt)[0] );
347 glVertex3fv( &mesh_.point(*fvIt)[0] );
349 glVertex3fv( &mesh_.point(*fvIt)[0] );
356 else if (_draw_mode ==
"Solid Smooth")
358 glEnableClientState(GL_VERTEX_ARRAY);
359 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
361 glEnableClientState(GL_NORMAL_ARRAY);
362 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
364 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
366 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
367 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
368 glEnable(GL_TEXTURE_2D);
369 glBindTexture(GL_TEXTURE_2D, tex_id_);
370 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
373 glBegin(GL_TRIANGLES);
374 for (; fIt!=fEnd; ++fIt)
376 fvIt = mesh_.cfv_iter(*fIt);
377 glArrayElement(fvIt->idx());
379 glArrayElement(fvIt->idx());
381 glArrayElement(fvIt->idx());
385 glDisableClientState(GL_VERTEX_ARRAY);
386 glDisableClientState(GL_NORMAL_ARRAY);
387 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
389 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
391 glDisable(GL_TEXTURE_2D);
395 else if (_draw_mode ==
"Colored Vertices")
397 glEnableClientState(GL_VERTEX_ARRAY);
398 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
400 glEnableClientState(GL_NORMAL_ARRAY);
401 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
403 if ( mesh_.has_vertex_colors() )
405 glEnableClientState( GL_COLOR_ARRAY );
406 glColorPointer(3, GL_UNSIGNED_BYTE, 0,mesh_.vertex_colors());
409 glBegin(GL_TRIANGLES);
410 for (; fIt!=fEnd; ++fIt)
412 fvIt = mesh_.cfv_iter(*fIt);
413 glArrayElement(fvIt->idx());
415 glArrayElement(fvIt->idx());
417 glArrayElement(fvIt->idx());
421 glDisableClientState(GL_VERTEX_ARRAY);
422 glDisableClientState(GL_NORMAL_ARRAY);
423 glDisableClientState(GL_COLOR_ARRAY);
427 else if (_draw_mode ==
"Solid Colored Faces")
429 glEnableClientState(GL_VERTEX_ARRAY);
430 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
432 glEnableClientState(GL_NORMAL_ARRAY);
433 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
435 glBegin(GL_TRIANGLES);
436 for (; fIt!=fEnd; ++fIt)
440 fvIt = mesh_.cfv_iter(*fIt);
441 glArrayElement(fvIt->idx());
443 glArrayElement(fvIt->idx());
445 glArrayElement(fvIt->idx());
449 glDisableClientState(GL_VERTEX_ARRAY);
450 glDisableClientState(GL_NORMAL_ARRAY);
454 else if (_draw_mode ==
"Smooth Colored Faces")
456 glEnableClientState(GL_VERTEX_ARRAY);
457 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
459 glEnableClientState(GL_NORMAL_ARRAY);
460 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
462 glBegin(GL_TRIANGLES);
463 for (; fIt!=fEnd; ++fIt)
467 fvIt = mesh_.cfv_iter(*fIt);
468 glArrayElement(fvIt->idx());
470 glArrayElement(fvIt->idx());
472 glArrayElement(fvIt->idx());
476 glDisableClientState(GL_VERTEX_ARRAY);
477 glDisableClientState(GL_NORMAL_ARRAY);
481 else if ( _draw_mode ==
"Strips'n VertexArrays" )
483 glEnableClientState(GL_VERTEX_ARRAY);
484 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
486 glEnableClientState(GL_NORMAL_ARRAY);
487 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
489 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
491 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
492 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
493 glEnable(GL_TEXTURE_2D);
494 glBindTexture(GL_TEXTURE_2D, tex_id_);
495 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
498 typename MyStripifier::StripsIterator strip_it = strips_.begin();
499 typename MyStripifier::StripsIterator strip_last = strips_.end();
502 for (; strip_it!=strip_last; ++strip_it)
504 glDrawElements(GL_TRIANGLE_STRIP,
505 static_cast<GLsizei>(strip_it->size()), GL_UNSIGNED_INT, &(*strip_it)[0] );
508 glDisableClientState(GL_VERTEX_ARRAY);
509 glDisableClientState(GL_NORMAL_ARRAY);
510 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
514 else if (_draw_mode ==
"Show Strips" && strips_.is_valid() )
516 typename MyStripifier::StripsIterator strip_it = strips_.begin();
517 typename MyStripifier::StripsIterator strip_last = strips_.end();
521 int base = (int)cmax-range;
526 int rcol=0, gcol=dgcol, bcol=dbcol+dbcol;
529 for (; strip_it!=strip_last; ++strip_it)
531 typename MyStripifier::IndexIterator idx_it = strip_it->begin();
532 typename MyStripifier::IndexIterator idx_last = strip_it->end();
534 rcol = (rcol+drcol) % range;
535 gcol = (gcol+dgcol) % range;
536 bcol = (bcol+dbcol) % range;
538 glBegin(GL_TRIANGLE_STRIP);
539 glColor3f((rcol+base)/cmax, (gcol+base)/cmax, (bcol+base)/cmax);
540 for ( ;idx_it != idx_last; ++idx_it )
544 glColor3f(1.0, 1.0, 1.0);
548 else if( _draw_mode ==
"Points" )
550 glEnableClientState(GL_VERTEX_ARRAY);
551 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
553 if (mesh_.has_vertex_colors() && use_color_)
555 glEnableClientState(GL_COLOR_ARRAY);
556 glColorPointer(3, GL_UNSIGNED_BYTE, 0, mesh_.vertex_colors());
559 glDrawArrays( GL_POINTS, 0, static_cast<GLsizei>(mesh_.n_vertices()) );
560 glDisableClientState(GL_VERTEX_ARRAY);
561 glDisableClientState(GL_COLOR_ARRAY);
571 template <
typename M>
576 if ( ! mesh_.n_vertices() )
579 #if defined(OM_USE_OSG) && OM_USE_OSG 580 else if ( _draw_mode ==
"OpenSG Indices")
582 glEnable(GL_LIGHTING);
583 glShadeModel(GL_SMOOTH);
584 draw_openmesh( _draw_mode );
588 if ( _draw_mode ==
"Points" )
590 glDisable(GL_LIGHTING);
591 draw_openmesh(_draw_mode);
593 else if (_draw_mode ==
"Wireframe")
595 glDisable(GL_LIGHTING);
596 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
597 draw_openmesh(_draw_mode);
598 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
601 else if ( _draw_mode ==
"Hidden-Line" )
603 glDisable(GL_LIGHTING);
604 glShadeModel(GL_FLAT);
605 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
606 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
607 glDepthRange(0.01, 1.0);
608 draw_openmesh(
"Wireframe" );
610 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
611 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
612 glDepthRange( 0.0, 1.0 );
613 draw_openmesh(
"Wireframe" );
615 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
618 else if (_draw_mode ==
"Solid Flat")
620 glEnable(GL_LIGHTING);
621 glShadeModel(GL_FLAT);
622 draw_openmesh(_draw_mode);
625 else if (_draw_mode ==
"Solid Smooth" ||
626 _draw_mode ==
"Strips'n VertexArrays" )
628 glEnable(GL_LIGHTING);
629 glShadeModel(GL_SMOOTH);
630 draw_openmesh(_draw_mode);
633 else if (_draw_mode ==
"Show Strips")
635 glDisable(GL_LIGHTING);
636 draw_openmesh(_draw_mode);
639 else if (_draw_mode ==
"Colored Vertices" )
641 glDisable(GL_LIGHTING);
642 glShadeModel(GL_SMOOTH);
643 draw_openmesh(_draw_mode);
646 else if (_draw_mode ==
"Solid Colored Faces")
648 glDisable(GL_LIGHTING);
649 glShadeModel(GL_FLAT);
650 draw_openmesh(_draw_mode);
651 setDefaultMaterial();
654 else if (_draw_mode ==
"Smooth Colored Faces" )
656 glEnable(GL_LIGHTING);
657 glShadeModel(GL_SMOOTH);
658 draw_openmesh(_draw_mode);
659 setDefaultMaterial();
664 typename Mesh::VertexIter vit;
665 glDisable(GL_LIGHTING);
667 glColor3f(1.000f, 0.803f, 0.027f);
668 for(vit=mesh_.vertices_begin(); vit!=mesh_.vertices_end(); ++vit)
671 glVertex( mesh_.point( *vit ) + normal_scale_*mesh_.normal( *vit ) );
678 typename Mesh::FaceIter fit;
679 glDisable(GL_LIGHTING);
681 glColor3f(0.705f, 0.976f, 0.270f);
682 for(fit=mesh_.faces_begin(); fit!=mesh_.faces_end(); ++fit)
684 glVertex( mesh_.property(fp_normal_base_, *fit) );
685 glVertex( mesh_.property(fp_normal_base_, *fit) +
686 normal_scale_*mesh_.normal( *fit ) );
695 template <
typename M>
702 add_draw_mode(
"Strips'n VertexArrays");
703 add_draw_mode(
"Show Strips");
709 template <
typename M>
716 del_draw_mode(
"Show Strips");
717 del_draw_mode(
"Strip'n VertexArrays");
724 #define TEXMODE( Mode ) \ 725 tex_mode_ = Mode; std::cout << "Texture mode set to " << #Mode << std::endl 727 template <
typename M>
731 switch( _event->key() )
734 if ( mesh_.has_vertex_colors() && (current_draw_mode()==
"Points") )
736 use_color_ = !use_color_;
737 std::cout <<
"use color: " << (use_color_?
"yes\n":
"no\n");
739 glColor3f(1.0f, 1.0f, 1.0f);
745 if ( _event->modifiers() & ShiftModifier )
747 show_fnormals_ = !show_fnormals_;
748 std::cout <<
"show face normals: " << (show_fnormals_?
"yes\n":
"no\n");
752 show_vnormals_ = !show_vnormals_;
753 std::cout <<
"show vertex normals: " << (show_vnormals_?
"yes\n":
"no\n");
759 std::cout <<
"\n# Vertices : " << mesh_.n_vertices() << std::endl;
760 std::cout <<
"# Edges : " << mesh_.n_edges() << std::endl;
761 std::cout <<
"# Faces : " << mesh_.n_faces() << std::endl;
762 std::cout <<
"binary input : " << opt_.check(opt_.Binary) << std::endl;
763 std::cout <<
"swapped input : " << opt_.check(opt_.Swap) << std::endl;
764 std::cout <<
"vertex normal : " 765 << opt_.check(opt_.VertexNormal) << std::endl;
766 std::cout <<
"vertex texcoord: " 767 << opt_.check(opt_.VertexTexCoord) << std::endl;
768 std::cout <<
"vertex color : " 769 << opt_.check(opt_.VertexColor) << std::endl;
770 std::cout <<
"face normal : " 771 << opt_.check(opt_.FaceNormal) << std::endl;
772 std::cout <<
"face color : " 773 << opt_.check(opt_.FaceColor) << std::endl;
774 this->QGLViewerWidget::keyPressEvent( _event );
780 case GL_MODULATE: TEXMODE(GL_DECAL);
break;
781 case GL_DECAL: TEXMODE(GL_BLEND);
break;
782 case GL_BLEND: TEXMODE(GL_REPLACE);
break;
783 case GL_REPLACE: TEXMODE(GL_MODULATE);
break;
789 this->QGLViewerWidget::keyPressEvent( _event );
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
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) vertex colors.
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Kernel::Point Point
Coordinate type.
Has (r) / store (w) face colors.
VectorT< float, 3 > Vec3f
std::string as_string(Format format=Automatic)
Has (r) / store (w) face normals.
virtual void draw_scene(const std::string &_draw_mode)
inherited drawing method
virtual bool open_mesh(const char *_filename, OpenMesh::IO::Options _opt)
open mesh
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
void update_vertex_normals()
Update normal vectors for all vertices.
Has (r) / store (w) texture coordinates.
virtual void draw_openmesh(const std::string &_drawmode)
draw the mesh
Set options for reader/writer modules.
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Has (r) / store (w) vertex normals.
void update_face_normals()
Update normal vectors for all faces.
void start(void)
Start measurement.
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
void stop(void)
Stop measurement.
virtual bool open_texture(const char *_filename)
load texture