visualizor.h
来自「CGAL is a collaborative effort of severa」· C头文件 代码 · 共 942 行 · 第 1/3 页
H
942 行
dy -= (y - mouse_y) * 2.0 / window_radius; break; case TRANS_Z: dz += (x - mouse_x + mouse_y -y) * 2.0 / window_radius; break; default: break; } mouse_x = x; mouse_y = y; glutPostRedisplay();}static void initialize_olg(){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* GLfloat light_ambient[4] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // white diffuse light GLfloat light_position[] = { 2.0, 3.0, -4.0, 0.0 }; // infinite location //GLfloat light_position[] = { 3.0, 5.0, 4.5, 1.0}; glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); GLfloat mat_ambient[4] = { 0.1, 0.1, 0.1, 1.0 }; //GLfloat mat_back_ambient[4] = { 0.2, 0.0, 0.0, 1.0 }; GLfloat mat_diffuse[4] = { 0.7, 0.7, 0.7, 1.0 }; //GLfloat mat_specular[4] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_specular[4] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat mat_shininess[] = { 100.0 }; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient ); //glMaterialfv(GL_BACK, GL_AMBIENT, mat_back_ambient ); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse ); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular ); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess ); */ glDepthFunc( GL_LEQUAL); glShadeModel( GL_FLAT); glEnable(GL_AUTO_NORMAL); glEnable(GL_DEPTH_TEST); glEnable(GL_POINT_SMOOTH); glEnable(GL_NORMALIZE); //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); //glEnable(GL_COLOR_MATERIAL);}static void enter_leave(int state){ glutPostRedisplay(); }static void draw(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); if (window_width > window_height) { double w = double( window_width) / double( window_height); if ( perspective) { double s = (eye - znear) / eye; glFrustum( -wsize*w*s, wsize*w*s, -wsize*s, wsize*s, eye-znear, eye+zfar); glTranslated( 0.0, 0.0, -eye); } else { glOrtho( -wsize*w, wsize*w, -wsize, wsize, -znear, zfar ); } } else { double h = double( window_height) / double( window_width); if ( perspective) { double s = (eye - znear) / eye; glFrustum( -wsize*s, wsize*s, -wsize*h*s, wsize*h*s, eye-znear, eye+zfar); glTranslated( 0.0, 0.0, -eye); } else { glOrtho( -wsize, wsize, -wsize*h, wsize*h, -znear, zfar ); } } glTranslated(dx,dy,dz); glTranslated(0,0,1); GLdouble M[16] = { rotation.m(0,0), rotation.m(1,0), rotation.m(2,0), 0.0, rotation.m(0,1), rotation.m(1,1), rotation.m(2,1), 0.0, rotation.m(0,2), rotation.m(1,2), rotation.m(2,2), 0.0, rotation.m(0,3), rotation.m(1,3), rotation.m(2,3), 1.0}; glMultMatrixd( M); glScaled(s,s,s); GLdouble z_vec[3] = { rotation.m(2,0) / s, rotation.m(2,1) / s, rotation.m(2,2) / s}; int win = glutGetWindow(); polyhedra_[win-1].draw( z_vec); glPopMatrix(); glutSwapBuffers();}static void reshape(int width, int height){ window_width = width; window_height = height; window_radius = std::min( width, height) / 2; factor_s = std::log(2.0) / (window_radius/2.0); // radius == scale factor 2 glViewport(0, 0, (GLint)width, (GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}static void start_viewer(){ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(window_width, window_height); glutInitWindowPosition(0,0); int submenu1 = glutCreateMenu(show); glutAddMenuEntry("Reset",RESET_CONTROL); glutAddMenuEntry("Rotate",ROTATE); glutAddMenuEntry("Scale",SCALE); glutAddMenuEntry("Translate in XY",TRANSLATE); glutAddMenuEntry("Translate in Z",TRANS_Z); int submenu2 = glutCreateMenu(show); glutAddMenuEntry("Boundary",BOUNDARY); glutAddMenuEntry("Skeleton",SKELETON); glutAddMenuEntry("Toggle Axes",AXES); for (unsigned i = 0; i < polyhedra_.size(); ++i) { if (i > 0 ) glutInitWindowPosition(i*(window_width+12),0); if ( i < titles_.size() ) glutCreateWindow(titles_[i].c_str()); else glutCreateWindow(" Polyhedron "); glutEntryFunc(enter_leave); initialize_olg(); glutDisplayFunc(draw); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(motion); glutCreateMenu(show); glutAddSubMenu("Control",submenu1); glutAddSubMenu("Render",submenu2); glutAddMenuEntry("Persp/Ortho",PERSP); glutAddMenuEntry("Fullscreen",FULLSCREEN); glutAddMenuEntry("Quit",QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON); } glutMainLoop();}} // namespace OGL// ----------------------------------------------------------------------------// SNC_visualizor_OGL<SNC_>// when you define a SNC_visualizor_OGL<SNC_> object then you create a// viewer window that shows the corresponding SNC_ object. All such viewers// follow the same visualization motion. Thus visualizing binary operations// is easy: just display the two input SNCs and the output SNC//// for similar viewer code see Nef_S2/demo/Nef_S2/Nef_polyhedron_S2-demo.C// ----------------------------------------------------------------------------template<typename Nef_polyhedron>class Visualizor_OpenGL_3 { typedef typename Nef_polyhedron::SNC_structure SNC_structure; typedef CGAL::SNC_decorator<SNC_structure> Base; typedef CGAL::SNC_FM_decorator<SNC_structure> FM_decorator; CGAL::OGL::Polyhedron* ppoly_;public: typedef typename SNC_structure::Vertex_const_iterator Vertex_const_iterator; typedef typename SNC_structure::Halfedge_const_iterator Halfedge_const_iterator; typedef typename SNC_structure::Halffacet_const_iterator Halffacet_const_iterator; typedef typename SNC_structure::Halffacet_cycle_const_iterator Halffacet_cycle_const_iterator; typedef typename SNC_structure::Object_const_handle Object_const_handle; typedef typename SNC_structure::SHalfedge_const_handle SHalfedge_const_handle; typedef typename SNC_structure::SHalfloop_const_handle SHalfloop_const_handle; typedef typename SNC_structure::Vertex_const_handle Vertex_const_handle; typedef typename SNC_structure::Halfedge_const_handle Halfedge_const_handle; typedef typename SNC_structure::Halffacet_const_handle Halffacet_const_handle; typedef typename SNC_structure::Point_3 Point_3; typedef typename SNC_structure::Vector_3 Vector_3; typedef typename SNC_structure::Segment_3 Segment_3; typedef typename SNC_structure::Plane_3 Plane_3; typedef typename SNC_structure::Mark Mark; typedef typename SNC_structure::SHalfedge_around_facet_const_circulator SHalfedge_around_facet_const_circulator; Nef_polyhedron N; Visualizor_OpenGL_3(const Nef_polyhedron& Nef) : N(Nef) { ppoly_ = & CGAL::OGL::add_polyhedron(); } OGL::Double_point double_point(const Point_3& p) const { return OGL::Double_point(CGAL::to_double(p.x()), CGAL::to_double(p.y()), CGAL::to_double(p.z())); } OGL::Double_segment double_segment(const Segment_3& s) const { return OGL::Double_segment(double_point(s.source()), double_point(s.target())); } void draw(Vertex_const_handle v) const { Point_3 bp = N.point(v); CGAL_NEF_TRACEN("vertex " << bp); ppoly_->push_back(double_point(bp), N.mark(v)); } void draw(Halfedge_const_handle e) const { Vertex_const_handle s = N.source(e); Vertex_const_handle t = N.source(N.twin(e)); Segment_3 seg(N.point(s),N.point(t)); CGAL_NEF_TRACEN("edge " << seg); ppoly_->push_back(double_segment(seg), N.mark(e)); } void draw(Halffacet_const_handle f) const { OGL::DFacet g; Halffacet_cycle_const_iterator fc; // all facet cycles: CGAL_forall_facet_cycles_of(fc,f) if ( fc.is_shalfedge() ) { // non-trivial facet cycle g.new_facet_cycle(); SHalfedge_const_handle h = fc; SHalfedge_around_facet_const_circulator hc(h), he(hc); CGAL_For_all(hc,he){ // all vertex coordinates in facet cycle Point_3 sp = N.point(N.source(hc)); CGAL_NEF_TRACEN(" ");CGAL_NEF_TRACEN("facet" << sp); g.push_back_vertex(double_point(sp)); } } Vector_3 v = N.plane(f).orthogonal_vector(); g.set_normal(CGAL::to_double(v.x()), CGAL::to_double(v.y()), CGAL::to_double(v.z()), N.mark(f)); ppoly_->push_back(g); } // Returns the bounding box of the finite vertices of the polyhedron. // Returns $[-1,+1]^3$ as bounding box if no finite vertex exists. Bbox_3 bounded_bbox() const { bool first_vertex = true; Bbox_3 bbox( -1.0, -1.0, -1.0, 1.0, 1.0, 1.0); Vertex_const_iterator vi; CGAL_forall_vertices(vi, N) { Point_3 p = vi->point(); double x = CGAL::to_double(p.hx()); double y = CGAL::to_double(p.hy()); double z = CGAL::to_double(p.hz()); double w = CGAL::to_double(p.hw()); if (N.is_standard(vi)) { if(first_vertex) { bbox = Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); first_vertex = false; } else { bbox = bbox + Bbox_3(x/w, y/w, z/w, x/w, y/w, z/w); first_vertex = false; } } } return bbox; } void set_R(const Bbox_3 bbox) const { if(N.is_standard_kernel()) return; double size = abs(bbox.xmin()); if(size < bbox.xmax()) size = bbox.xmax(); if(size < bbox.ymin()) size = bbox.ymin(); if(size < bbox.ymax()) size = bbox.ymax(); if(size < bbox.zmin()) size = bbox.zmin(); if(size < bbox.zmax()) size = bbox.zmax(); N.set_size_of_infimaximal_box(size*10); CGAL_NEF_TRACEN("set infi box size to " << size); } void draw() const { Bbox_3 bbox(bounded_bbox()); ppoly_->bbox() = bbox; set_R(bbox); Vertex_const_iterator v; CGAL_forall_vertices(v,*N.sncp()) draw(v); Halfedge_const_iterator e; CGAL_forall_edges(e,*N.sncp()) draw(e); Halffacet_const_iterator f; CGAL_forall_facets(f,*N.sncp()) draw(f); }}; // Visualizor_OpenGL_3CGAL_END_NAMESPACE#endif //CGAL_VISUALIZOR_OPENGL_3_H
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?