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());
140 for (
size_t count=0; vIt!=vEnd; ++vIt, ++count)
142 bbMin.
minimize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
143 bbMax.
maximize( OpenMesh::vector_cast<Vec3f>(mesh_.point(*vIt)));
148 set_scene_pos( (bbMin+bbMax)*0.5f, (bbMin-bbMax).norm()*0.5f );
151 normal_scale_ = (bbMax-bbMin).min()*0.05f;
154 std::clog << mesh_.n_vertices() <<
" vertices, " 155 << mesh_.n_edges() <<
" edge, " 156 << mesh_.n_faces() <<
" faces\n";
162 mesh_.add_property( fp_normal_base_ );
163 typename M::FaceIter f_it = mesh_.faces_begin();
164 typename M::FaceVertexIter fv_it;
165 for (;f_it != mesh_.faces_end(); ++f_it)
168 for( fv_it=mesh_.fv_iter(*f_it); fv_it.is_valid(); ++fv_it)
169 v += OpenMesh::vector_cast<typename Mesh::Normal>(mesh_.point(*fv_it));
171 mesh_.property( fp_normal_base_, *f_it ) = v;
174 std::clog <<
"Computed base point for displaying face normals [" 180 std::clog <<
"Computing strips.." << std::flush;
185 std::clog <<
"done [" << strips_.n_strips()
186 <<
" strips created in " << t.
as_string() <<
"]\n";
194 setWindowTitle(QFileInfo(_filename).fileName());
205 template <
typename M>
209 QString fname = _filename;
211 if (texsrc.load( fname ))
213 return set_texture( texsrc );
221 template <
typename M>
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( QGLWidget::convertToGLFormat ( _texsrc ) );
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";
282 template <
typename M>
286 typename Mesh::ConstFaceIter fIt(mesh_.faces_begin()),
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);
570 template <
typename M>
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();
663 typename Mesh::VertexIter vit;
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 ) );
677 typename Mesh::FaceIter fit;
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 ) );
694 template <
typename M>
701 add_draw_mode(
"Strips'n VertexArrays");
702 add_draw_mode(
"Show Strips");
708 template <
typename M>
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 726 template <
typename M>
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 );
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.
Kernel::ConstFaceVertexIter ConstFaceVertexIter
Circulator.
Has (r) / store (w) vertex colors.
Has (r) / store (w) face colors.
Kernel::Point Point
Coordinate type.
VectorT< float, 3 > Vec3f
std::string as_string(Format format=Automatic)
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
void stop(void)
Stop measurement.
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
void update_vertex_normals()
Update normal vectors for all vertices.
Set options for reader/writer modules.
void update_face_normals()
Update normal vectors for all faces.
Has (r) / store (w) vertex normals.
Has (r) / store (w) face normals.
void start(void)
Start measurement.
Has (r) / store (w) texture coordinates.
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.