📄 meshviewerwidgett.cc
字号:
//=============================================================================// // OpenMesh // Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen // www.openmesh.org // //-----------------------------------------------------------------------------// // License // // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Library General Public License as published // by the Free Software Foundation, version 2. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // //-----------------------------------------------------------------------------// // $Revision: 1.3 $// $Date: 2005-12-21 14:01:53 $// //=============================================================================#define OPENMESHAPPS_MESHVIEWERWIDGET_CC//== INCLUDES =================================================================#ifdef _MSC_VER//# pragma warning(disable: 4267 4311)#endif//#include <iostream>#include <fstream>// --------------------#include <qimage.h>#include <qfileinfo.h>// --------------------#include <OpenMesh/Core/Utils/vector_cast.hh>#include <OpenMesh/Tools/Utils/Timer.hh>#include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh>using namespace OpenMesh;#if defined(_MSC_VER)# undef min# undef max#endif//== IMPLEMENTATION ========================================================== template <typename M>bool MeshViewerWidgetT<M>::open_mesh(const char* _filename, IO::Options _opt){ // load mesh // calculate normals // set scene center and radius mesh_.request_face_normals(); mesh_.request_face_colors(); mesh_.request_vertex_normals(); mesh_.request_vertex_colors(); mesh_.request_vertex_texcoords2D(); std::cout << "Loading from file '" << _filename << "'\n"; if ( IO::read_mesh(mesh_, _filename, _opt )) { // store read option opt_ = _opt; // update face and vertex normals if ( ! opt_.check( IO::Options::FaceNormal ) ) mesh_.update_face_normals(); else std::cout << "File provides face normals\n"; if ( ! opt_.check( IO::Options::VertexNormal ) ) mesh_.update_vertex_normals(); else std::cout << "File provides vertex normals\n"; // check for possible color information if ( opt_.check( IO::Options::VertexColor ) ) { std::cout << "File provides vertex colors\n"; add_draw_mode("Colored Vertices"); } else mesh_.release_vertex_colors(); if ( _opt.check( IO::Options::FaceColor ) ) { std::cout << "File provides face colors\n"; add_draw_mode("Solid Colored Faces"); add_draw_mode("Smooth Colored Faces"); } else mesh_.release_face_colors(); if ( _opt.check( IO::Options::VertexTexCoord ) ) std::cout << "File provides texture coordinates\n"; // bounding box typename Mesh::ConstVertexIter vIt(mesh_.vertices_begin()); typename Mesh::ConstVertexIter vEnd(mesh_.vertices_end()); typedef typename Mesh::Point Point; using OpenMesh::Vec3f; Vec3f bbMin, bbMax; bbMin = bbMax = OpenMesh::vector_cast<Vec3f>(mesh_.point(vIt)); for (size_t count=0; vIt!=vEnd; ++vIt, ++count) { bbMin.minimize( OpenMesh::vector_cast<Vec3f>(mesh_.point(vIt))); bbMax.maximize( OpenMesh::vector_cast<Vec3f>(mesh_.point(vIt))); } // set center and radius set_scene_pos( (bbMin+bbMax)*0.5, (bbMin-bbMax).norm()*0.5 ); // for normal display normal_scale_ = (bbMax-bbMin).min()*0.05f; // info std::clog << mesh_.n_vertices() << " vertices, " << mesh_.n_edges() << " edge, " << mesh_.n_faces() << " faces\n"; // base point for displaying face normals { OpenMesh::Utils::Timer t; t.start(); mesh_.add_property( fp_normal_base_ ); typename M::FaceIter f_it = mesh_.faces_begin(); typename M::FaceVertexIter fv_it; for (;f_it != mesh_.faces_end(); ++f_it) { typename Mesh::Point v(0,0,0); for( fv_it=mesh_.fv_iter(f_it); fv_it; ++fv_it) v += OpenMesh::vector_cast<typename Mesh::Normal>(mesh_.point(fv_it)); v *= 1.0f/3.0f; mesh_.property( fp_normal_base_, f_it ) = v; } t.stop(); std::clog << "Computed base point for displaying face normals [" << t.as_string() << "]" << std::endl; } // { std::clog << "Computing strips.." << std::flush; OpenMesh::Utils::Timer t; t.start(); compute_strips(); t.stop(); std::clog << "done [" << strips_.n_strips() << " strips created in " << t.as_string() << "]\n"; } // #if defined(WIN32) updateGL();#endif setCaption(QFileInfo(_filename).fileName()); // loading done return true; } return false;}//-----------------------------------------------------------------------------template <typename M>bool MeshViewerWidgetT<M>::open_texture( const char *_filename ){ QImage texsrc; QString fname = _filename; if (texsrc.load( fname )) { return set_texture( texsrc ); } return false;}//-----------------------------------------------------------------------------template <typename M>bool MeshViewerWidgetT<M>::set_texture( QImage& _texsrc ){ if ( !opt_.vertex_has_texcoord() ) return false; { // adjust texture size: 2^k * 2^l int tex_w, w( _texsrc.width() ); int tex_h, h( _texsrc.height() ); for (tex_w=1; tex_w <= w; tex_w <<= 1); for (tex_h=1; tex_h <= h; tex_h <<= 1); tex_w >>= 1; tex_h >>= 1; _texsrc = _texsrc.smoothScale( tex_w, tex_h ); } QImage texture( QGLWidget::convertToGLFormat ( _texsrc ) ); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_SKIP_ROWS, 0); glPixelStorei(GL_PACK_SKIP_PIXELS, 0); glPixelStorei(GL_PACK_ALIGNMENT, 1); if ( tex_id_ > 0 ) { glDeleteTextures(1, &tex_id_); } glGenTextures(1, &tex_id_); glBindTexture(GL_TEXTURE_2D, tex_id_); // glTexGenfv( GL_S, GL_SPHERE_MAP, 0 ); // glTexGenfv( GL_T, GL_SPHERE_MAP, 0 ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, // target 0, // level GL_RGBA, // internal format texture.width(), // width (2^n) texture.height(), // height (2^m) 0, // border GL_RGBA, // format GL_UNSIGNED_BYTE, // type texture.bits() ); // pointer to pixels std::cout << "Texture loaded\n"; return true;}//-----------------------------------------------------------------------------template <typename M>voidMeshViewerWidgetT<M>::draw_openmesh(const std::string& _draw_mode){ typename Mesh::ConstFaceIter fIt(mesh_.faces_begin()), fEnd(mesh_.faces_end()); typename Mesh::ConstFaceVertexIter fvIt;#if defined(OM_USE_OSG) && OM_USE_OSG if (_draw_mode == "OpenSG Indices") // -------------------------------------- { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, mesh_.points()); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals()); if ( tex_id_ && mesh_.has_vertex_texcoords2D() ) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D()); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex_id_); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_); } glDrawElements(GL_TRIANGLES, mesh_.osg_indices()->size(), GL_UNSIGNED_INT, &mesh_.osg_indices()->getField()[0] ); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } else#endif if (_draw_mode == "Wireframe") // ------------------------------------------- { glBegin(GL_TRIANGLES); for (; fIt!=fEnd; ++fIt) { fvIt = mesh_.cfv_iter(fIt.handle()); glVertex3fv( &mesh_.point(fvIt)[0] ); ++fvIt; glVertex3fv( &mesh_.point(fvIt)[0] ); ++fvIt; glVertex3fv( &mesh_.point(fvIt)[0] ); } glEnd(); } else if (_draw_mode == "Solid Flat") // ------------------------------------- { glBegin(GL_TRIANGLES); for (; fIt!=fEnd; ++fIt) { glNormal3fv( &mesh_.normal(fIt)[0] ); fvIt = mesh_.cfv_iter(fIt.handle()); glVertex3fv( &mesh_.point(fvIt)[0] ); ++fvIt; glVertex3fv( &mesh_.point(fvIt)[0] ); ++fvIt; glVertex3fv( &mesh_.point(fvIt)[0] ); } glEnd(); } else if (_draw_mode == "Solid Smooth") // ----------------------------------- { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, mesh_.points()); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals()); if ( tex_id_ && mesh_.has_vertex_texcoords2D() ) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, mesh_.texcoords2D()); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex_id_); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_); } glBegin(GL_TRIANGLES); for (; fIt!=fEnd; ++fIt) { fvIt = mesh_.cfv_iter(fIt.handle()); glArrayElement(fvIt.handle().idx()); ++fvIt; glArrayElement(fvIt.handle().idx()); ++fvIt; glArrayElement(fvIt.handle().idx()); } glEnd(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); if ( tex_id_ && mesh_.has_vertex_texcoords2D() ) { glDisable(GL_TEXTURE_2D); } } else if (_draw_mode == "Colored Vertices") // -------------------------------- { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, mesh_.points()); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, mesh_.vertex_normals()); if ( mesh_.has_vertex_colors() ) { glEnableClientState( GL_COLOR_ARRAY ); glColorPointer(3, GL_UNSIGNED_BYTE, 0,mesh_.vertex_colors()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -