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";
97 std::cout <<
"File provides face normals\n";
102 std::cout <<
"File provides vertex normals\n";
108 std::cout <<
"File provides vertex colors\n";
109 add_draw_mode(
"Colored Vertices");
112 mesh_.release_vertex_colors();
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();
124 std::cout <<
"File provides texture coordinates\n";
128 typename Mesh::ConstVertexIter vIt(mesh_.vertices_begin());
129 typename Mesh::ConstVertexIter vEnd(mesh_.vertices_end());
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 setWindowTitle(QFileInfo(_filename).fileName());
202 template <
typename M>
206 QString fname = _filename;
208 if (texsrc.load( fname ))
210 return set_texture( texsrc );
218 template <
typename M>
221 if ( !opt_.vertex_has_texcoord() )
226 int tex_w, w( _texsrc.width() );
227 int tex_h, h( _texsrc.height() );
229 for (tex_w=1; tex_w <= w; tex_w <<= 1) {};
230 for (tex_h=1; tex_h <= h; tex_h <<= 1) {};
233 _texsrc = _texsrc.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
236 QImage texture( QGLWidget::convertToGLFormat ( _texsrc ) );
238 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
239 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
240 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
241 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
242 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
243 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
244 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
245 glPixelStorei(GL_PACK_ALIGNMENT, 1);
249 glDeleteTextures(1, &tex_id_);
251 glGenTextures(1, &tex_id_);
252 glBindTexture(GL_TEXTURE_2D, tex_id_);
257 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 glTexImage2D(GL_TEXTURE_2D,
272 std::cout <<
"Texture loaded\n";
279 template <
typename M>
283 typename Mesh::ConstFaceIter fIt(mesh_.faces_begin()),
284 fEnd(mesh_.faces_end());
288 #if defined(OM_USE_OSG) && OM_USE_OSG 289 if (_draw_mode ==
"OpenSG Indices")
291 glEnableClientState(GL_VERTEX_ARRAY);
292 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
294 glEnableClientState(GL_NORMAL_ARRAY);
295 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
297 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
299 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
300 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
301 glEnable(GL_TEXTURE_2D);
302 glBindTexture(GL_TEXTURE_2D, tex_id_);
303 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
306 glDrawElements(GL_TRIANGLES,
307 mesh_.osg_indices()->size(),
309 &mesh_.osg_indices()->getField()[0] );
311 glDisableClientState(GL_VERTEX_ARRAY);
312 glDisableClientState(GL_NORMAL_ARRAY);
313 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
318 if (_draw_mode ==
"Wireframe")
320 glBegin(GL_TRIANGLES);
321 for (; fIt!=fEnd; ++fIt)
323 fvIt = mesh_.cfv_iter(*fIt);
324 glVertex3fv( &mesh_.point(*fvIt)[0] );
326 glVertex3fv( &mesh_.point(*fvIt)[0] );
328 glVertex3fv( &mesh_.point(*fvIt)[0] );
333 else if (_draw_mode ==
"Solid Flat")
335 glBegin(GL_TRIANGLES);
336 for (; fIt!=fEnd; ++fIt)
338 glNormal3fv( &mesh_.normal(*fIt)[0] );
340 fvIt = mesh_.cfv_iter(*fIt);
341 glVertex3fv( &mesh_.point(*fvIt)[0] );
343 glVertex3fv( &mesh_.point(*fvIt)[0] );
345 glVertex3fv( &mesh_.point(*fvIt)[0] );
352 else if (_draw_mode ==
"Solid Smooth")
354 glEnableClientState(GL_VERTEX_ARRAY);
355 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
357 glEnableClientState(GL_NORMAL_ARRAY);
358 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
360 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
362 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
363 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
364 glEnable(GL_TEXTURE_2D);
365 glBindTexture(GL_TEXTURE_2D, tex_id_);
366 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
369 glBegin(GL_TRIANGLES);
370 for (; fIt!=fEnd; ++fIt)
372 fvIt = mesh_.cfv_iter(*fIt);
373 glArrayElement(fvIt->idx());
375 glArrayElement(fvIt->idx());
377 glArrayElement(fvIt->idx());
381 glDisableClientState(GL_VERTEX_ARRAY);
382 glDisableClientState(GL_NORMAL_ARRAY);
383 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
385 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
387 glDisable(GL_TEXTURE_2D);
391 else if (_draw_mode ==
"Colored Vertices")
393 glEnableClientState(GL_VERTEX_ARRAY);
394 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
396 glEnableClientState(GL_NORMAL_ARRAY);
397 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
399 if ( mesh_.has_vertex_colors() )
401 glEnableClientState( GL_COLOR_ARRAY );
402 glColorPointer(3, GL_UNSIGNED_BYTE, 0,mesh_.vertex_colors());
405 glBegin(GL_TRIANGLES);
406 for (; fIt!=fEnd; ++fIt)
408 fvIt = mesh_.cfv_iter(*fIt);
409 glArrayElement(fvIt->idx());
411 glArrayElement(fvIt->idx());
413 glArrayElement(fvIt->idx());
417 glDisableClientState(GL_VERTEX_ARRAY);
418 glDisableClientState(GL_NORMAL_ARRAY);
419 glDisableClientState(GL_COLOR_ARRAY);
423 else if (_draw_mode ==
"Solid Colored Faces")
425 glEnableClientState(GL_VERTEX_ARRAY);
426 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
428 glEnableClientState(GL_NORMAL_ARRAY);
429 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
431 glBegin(GL_TRIANGLES);
432 for (; fIt!=fEnd; ++fIt)
436 fvIt = mesh_.cfv_iter(*fIt);
437 glArrayElement(fvIt->idx());
439 glArrayElement(fvIt->idx());
441 glArrayElement(fvIt->idx());
445 glDisableClientState(GL_VERTEX_ARRAY);
446 glDisableClientState(GL_NORMAL_ARRAY);
450 else if (_draw_mode ==
"Smooth Colored Faces")
452 glEnableClientState(GL_VERTEX_ARRAY);
453 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
455 glEnableClientState(GL_NORMAL_ARRAY);
456 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
458 glBegin(GL_TRIANGLES);
459 for (; fIt!=fEnd; ++fIt)
463 fvIt = mesh_.cfv_iter(*fIt);
464 glArrayElement(fvIt->idx());
466 glArrayElement(fvIt->idx());
468 glArrayElement(fvIt->idx());
472 glDisableClientState(GL_VERTEX_ARRAY);
473 glDisableClientState(GL_NORMAL_ARRAY);
477 else if ( _draw_mode ==
"Strips'n VertexArrays" )
479 glEnableClientState(GL_VERTEX_ARRAY);
480 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
482 glEnableClientState(GL_NORMAL_ARRAY);
483 glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals());
485 if ( tex_id_ && mesh_.has_vertex_texcoords2D() )
487 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
488 glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D());
489 glEnable(GL_TEXTURE_2D);
490 glBindTexture(GL_TEXTURE_2D, tex_id_);
491 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
494 typename MyStripifier::StripsIterator strip_it = strips_.begin();
495 typename MyStripifier::StripsIterator strip_last = strips_.end();
498 for (; strip_it!=strip_last; ++strip_it)
500 glDrawElements(GL_TRIANGLE_STRIP,
501 static_cast<GLsizei>(strip_it->size()), GL_UNSIGNED_INT, &(*strip_it)[0] );
504 glDisableClientState(GL_VERTEX_ARRAY);
505 glDisableClientState(GL_NORMAL_ARRAY);
506 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
510 else if (_draw_mode ==
"Show Strips" && strips_.is_valid() )
512 typename MyStripifier::StripsIterator strip_it = strips_.begin();
513 typename MyStripifier::StripsIterator strip_last = strips_.end();
517 int base = (int)cmax-range;
522 int rcol=0, gcol=dgcol, bcol=dbcol+dbcol;
525 for (; strip_it!=strip_last; ++strip_it)
527 typename MyStripifier::IndexIterator idx_it = strip_it->begin();
528 typename MyStripifier::IndexIterator idx_last = strip_it->end();
530 rcol = (rcol+drcol) % range;
531 gcol = (gcol+dgcol) % range;
532 bcol = (bcol+dbcol) % range;
534 glBegin(GL_TRIANGLE_STRIP);
535 glColor3f((rcol+base)/cmax, (gcol+base)/cmax, (bcol+base)/cmax);
536 for ( ;idx_it != idx_last; ++idx_it )
540 glColor3f(1.0, 1.0, 1.0);
544 else if( _draw_mode ==
"Points" )
546 glEnableClientState(GL_VERTEX_ARRAY);
547 glVertexPointer(3, GL_FLOAT, 0, mesh_.points());
549 if (mesh_.has_vertex_colors() && use_color_)
551 glEnableClientState(GL_COLOR_ARRAY);
552 glColorPointer(3, GL_UNSIGNED_BYTE, 0, mesh_.vertex_colors());
555 glDrawArrays( GL_POINTS, 0, static_cast<GLsizei>(mesh_.n_vertices()) );
556 glDisableClientState(GL_VERTEX_ARRAY);
557 glDisableClientState(GL_COLOR_ARRAY);
567 template <
typename M>
572 if ( ! mesh_.n_vertices() )
575 #if defined(OM_USE_OSG) && OM_USE_OSG 576 else if ( _draw_mode ==
"OpenSG Indices")
578 glEnable(GL_LIGHTING);
579 glShadeModel(GL_SMOOTH);
580 draw_openmesh( _draw_mode );
584 if ( _draw_mode ==
"Points" )
586 glDisable(GL_LIGHTING);
587 draw_openmesh(_draw_mode);
589 else if (_draw_mode ==
"Wireframe")
591 glDisable(GL_LIGHTING);
592 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
593 draw_openmesh(_draw_mode);
594 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
597 else if ( _draw_mode ==
"Hidden-Line" )
599 glDisable(GL_LIGHTING);
600 glShadeModel(GL_FLAT);
601 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
602 glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
603 glDepthRange(0.01, 1.0);
604 draw_openmesh(
"Wireframe" );
606 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
607 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
608 glDepthRange( 0.0, 1.0 );
609 draw_openmesh(
"Wireframe" );
611 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
614 else if (_draw_mode ==
"Solid Flat")
616 glEnable(GL_LIGHTING);
617 glShadeModel(GL_FLAT);
618 draw_openmesh(_draw_mode);
621 else if (_draw_mode ==
"Solid Smooth" ||
622 _draw_mode ==
"Strips'n VertexArrays" )
624 glEnable(GL_LIGHTING);
625 glShadeModel(GL_SMOOTH);
626 draw_openmesh(_draw_mode);
629 else if (_draw_mode ==
"Show Strips")
631 glDisable(GL_LIGHTING);
632 draw_openmesh(_draw_mode);
635 else if (_draw_mode ==
"Colored Vertices" )
637 glDisable(GL_LIGHTING);
638 glShadeModel(GL_SMOOTH);
639 draw_openmesh(_draw_mode);
642 else if (_draw_mode ==
"Solid Colored Faces")
644 glDisable(GL_LIGHTING);
645 glShadeModel(GL_FLAT);
646 draw_openmesh(_draw_mode);
647 setDefaultMaterial();
650 else if (_draw_mode ==
"Smooth Colored Faces" )
652 glEnable(GL_LIGHTING);
653 glShadeModel(GL_SMOOTH);
654 draw_openmesh(_draw_mode);
655 setDefaultMaterial();
660 typename Mesh::VertexIter vit;
661 glDisable(GL_LIGHTING);
663 glColor3f(1.000f, 0.803f, 0.027f);
664 for(vit=mesh_.vertices_begin(); vit!=mesh_.vertices_end(); ++vit)
667 glVertex( mesh_.point( *vit ) + normal_scale_*mesh_.normal( *vit ) );
674 typename Mesh::FaceIter fit;
675 glDisable(GL_LIGHTING);
677 glColor3f(0.705f, 0.976f, 0.270f);
678 for(fit=mesh_.faces_begin(); fit!=mesh_.faces_end(); ++fit)
680 glVertex( mesh_.property(fp_normal_base_, *fit) );
681 glVertex( mesh_.property(fp_normal_base_, *fit) +
682 normal_scale_*mesh_.normal( *fit ) );
691 template <
typename M>
698 add_draw_mode(
"Strips'n VertexArrays");
699 add_draw_mode(
"Show Strips");
705 template <
typename M>
712 del_draw_mode(
"Show Strips");
713 del_draw_mode(
"Strip'n VertexArrays");
720 #define TEXMODE( Mode ) \ 721 tex_mode_ = Mode; std::cout << "Texture mode set to " << #Mode << std::endl 723 template <
typename M>
727 switch( _event->key() )
730 if ( mesh_.has_vertex_colors() && (current_draw_mode()==
"Points") )
732 use_color_ = !use_color_;
733 std::cout <<
"use color: " << (use_color_?
"yes\n":
"no\n");
735 glColor3f(1.0f, 1.0f, 1.0f);
741 if ( _event->modifiers() & ShiftModifier )
743 show_fnormals_ = !show_fnormals_;
744 std::cout <<
"show face normals: " << (show_fnormals_?
"yes\n":
"no\n");
748 show_vnormals_ = !show_vnormals_;
749 std::cout <<
"show vertex normals: " << (show_vnormals_?
"yes\n":
"no\n");
755 std::cout <<
"\n# Vertices : " << mesh_.n_vertices() << std::endl;
756 std::cout <<
"# Edges : " << mesh_.n_edges() << std::endl;
757 std::cout <<
"# Faces : " << mesh_.n_faces() << std::endl;
758 std::cout <<
"binary input : " << opt_.check(opt_.Binary) << std::endl;
759 std::cout <<
"swapped input : " << opt_.check(opt_.Swap) << std::endl;
760 std::cout <<
"vertex normal : " 761 << opt_.check(opt_.VertexNormal) << std::endl;
762 std::cout <<
"vertex texcoord: " 763 << opt_.check(opt_.VertexTexCoord) << std::endl;
764 std::cout <<
"vertex color : " 765 << opt_.check(opt_.VertexColor) << std::endl;
766 std::cout <<
"face normal : " 767 << opt_.check(opt_.FaceNormal) << std::endl;
768 std::cout <<
"face color : " 769 << opt_.check(opt_.FaceColor) << std::endl;
770 this->QGLViewerWidget::keyPressEvent( _event );
776 case GL_MODULATE: TEXMODE(GL_DECAL);
break;
777 case GL_DECAL: TEXMODE(GL_BLEND);
break;
778 case GL_BLEND: TEXMODE(GL_REPLACE);
break;
779 case GL_REPLACE: TEXMODE(GL_MODULATE);
break;
785 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.