Advertisement
Guest User

vcglib refine example

a guest
May 17th, 2011
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <sstream>
  7.  
  8. #include "GL/glew.h"
  9. #include "GL/glfw.h"
  10. #include <GL/gl.h>
  11.  
  12. #include <vcg/space/point3.h>
  13. #include <vcg/space/box3.h>
  14.  
  15. #include <vcg/math/perlin_noise.h>
  16.  
  17. #include <vcg/simplex/vertex/base.h>
  18. #include <vcg/simplex/face/base.h>
  19. #include <vcg/simplex/vertex/component_ocf.h>
  20. #include <vcg/simplex/face/component_ocf.h>
  21.  
  22. #include <vcg/complex/complex.h>
  23. #include <vcg/complex/allocate.h>
  24.  
  25. #include <vcg/complex/algorithms/smooth.h>
  26.  
  27. #include <vcg/complex/algorithms/create/marching_cubes.h>
  28. #include <vcg/complex/algorithms/create/extended_marching_cubes.h>
  29. #include <vcg/complex/algorithms/create/mc_trivial_walker.h>
  30. #include <vcg/complex/algorithms/create/platonic.h>
  31.  
  32. #include <vcg/complex/algorithms/update/bounding.h>
  33. #include <vcg/complex/algorithms/update/normal.h>
  34. #include <vcg/complex/algorithms/update/topology.h>
  35. #include <vcg/complex/algorithms/update/flag.h>
  36.  
  37. #include <vcg/complex/algorithms/refine.h>
  38.  
  39. // for opengl
  40. #include <wrap/gl/trimesh.h>
  41.  
  42. using namespace std;
  43. using namespace vcg;
  44.  
  45. typedef float ScalarType;
  46.  
  47. class MyFace;
  48. class MyVertex;
  49.  
  50. struct MyUsedTypes : public UsedTypes<
  51. Use<MyVertex>   ::AsVertexType,
  52. Use<MyFace>     ::AsFaceType>{};
  53.  
  54. class MyVertex: public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags, vertex::VFAdj, vertex::InfoOcf, vertex::Mark>{};
  55. class MyFace: public Face< MyUsedTypes, face::FFAdj, face::Mark, face::VertexRef, face::Normal3f, face::BitFlags, face::InfoOcf> {};
  56. class MyMesh: public vcg::tri::TriMesh< std::vector< MyVertex>, std::vector< MyFace > > {};
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60.     int running = GL_TRUE;
  61.     int width = 800;
  62.     int height = 800;
  63.  
  64.     if (!glfwInit()){
  65.         exit(EXIT_FAILURE);
  66.     }
  67.  
  68.     if (!glfwOpenWindow(width,height, 0,0,0,0,8,0, GLFW_WINDOW)){
  69.         glfwTerminate();
  70.         exit(EXIT_FAILURE);
  71.     }
  72.  
  73.     GLenum err = glewInit();
  74.     if (GLEW_OK != err){
  75.         glfwTerminate();
  76.         exit(EXIT_FAILURE);
  77.     }
  78.  
  79.     MyMesh mc_mesh;
  80.     vcg::tri::Icosahedron<MyMesh>(mc_mesh);
  81.  
  82.     // update infos
  83.     vcg::tri::UpdateBounding<MyMesh>::Box(mc_mesh);
  84.     vcg::tri::UpdateNormals<MyMesh>::PerVertexNormalizedPerFace(mc_mesh);
  85.     vcg::tri::UpdateNormals<MyMesh>::PerFaceNormalized(mc_mesh);
  86.     vcg::tri::UpdateTopology<MyMesh>::FaceFace(mc_mesh);
  87.     vcg::tri::UpdateTopology<MyMesh>::VertexFace(mc_mesh);
  88.  
  89.     Refine(mc_mesh,MidPointButterfly<MyMesh>());
  90.     Refine(mc_mesh,MidPointButterfly<MyMesh>());
  91.     Refine(mc_mesh,MidPointButterfly<MyMesh>());
  92.  
  93.     vcg::tri::UpdateNormals<MyMesh>::PerVertexNormalizedPerFace(mc_mesh);
  94.     vcg::tri::UpdateNormals<MyMesh>::PerFaceNormalized(mc_mesh);
  95.  
  96.     GlTrimesh<MyMesh> tm;
  97.     tm.m = &mc_mesh;
  98.     tm.Update();
  99.  
  100.     glClearColor(0, 0, 0, 0);
  101.  
  102.     glEnable(GL_CULL_FACE);
  103.     glEnable(GL_DEPTH_TEST);
  104.     glEnable(GL_NORMALIZE);
  105.     glLineWidth(2.0);
  106.  
  107.     glEnable(GL_COLOR_MATERIAL);
  108.     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  109.     glEnable(GL_LIGHTING);
  110.     glEnable(GL_LIGHT0);
  111.     GLfloat amb[] = {0,0,0,1};
  112.     GLfloat diff[] = {1,1,1,1};
  113.     glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  114.     glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  115.  
  116.     GLfloat global_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  117.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
  118.  
  119.     glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,50);
  120.  
  121.     GLfloat col[] = {1.f,0.f,1.f,0.f};
  122.     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,col);
  123.     GLfloat white[] = {1.f,1.f,1.f,0.f};
  124.     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
  125.  
  126.     float rotate = 0;
  127.     vcg::Point3<float> c = mc_mesh.bbox.Center();
  128.     float meshSize = mc_mesh.bbox.DimY();
  129.  
  130.     std::cout << "meshSize: " << meshSize << "\n";
  131.  
  132.     while(running){
  133.         double time = glfwGetTime();
  134.  
  135.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  136.         glMatrixMode(GL_PROJECTION);
  137.         glLoadIdentity();
  138.         gluPerspective(40, 1.0*width/height, 0.1, 100);
  139.         glMatrixMode(GL_MODELVIEW);
  140.         glLoadIdentity();
  141.         gluLookAt(.7,.7,1.7,   0,0,0,   0,1,0);
  142.  
  143.         glColor3f(1,1,1);
  144.  
  145.         GLfloat lp[] = {0,1,1,0};
  146.         glLightfv(GL_LIGHT0,GL_POSITION,lp);
  147.  
  148.         glRotatef(10*time,0,1,0);
  149.         // glScalef(.15,.15,.15);
  150.         glScalef(1/meshSize,1/meshSize,1/meshSize);
  151.  
  152.         glTranslatef(-c.X(),-c.Y(),-c.Z());
  153.  
  154.         tm.Draw<vcg::GLW::DMSmooth, vcg::GLW::CMNone, vcg::GLW::TMNone> ();
  155.  
  156.         glfwSwapBuffers();
  157.  
  158.         running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
  159.     }
  160.  
  161.     glfwTerminate();
  162.     exit(EXIT_SUCCESS);
  163. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement