int main(int /*argc*/, char** argv) { SurfaceMesh mesh; mesh.read(argv[1]); float mean_valence = 0.0f; unsigned int vertex_valence; // instantiate iterator and circulators SurfaceMesh::Vertex_iterator vit; SurfaceMesh::Vertex_around_vertex_circulator vc, vc_end; // loop over all vertices for (vit = mesh.vertices_begin(); vit != mesh.vertices_end(); ++vit) { // initialize circulators vc = mesh.vertices(*vit); vc_end = vc; // reset counter vertex_valence = 0; // loop over all incident vertices do { ++vertex_valence; } while (++vc != vc_end); // sum up vertex valences mean_valence += vertex_valence; } mean_valence /= mesh.n_vertices(); std::cout << "mean valence: " << mean_valence << std::endl; }
int main(int argc, char** argv) { std::string file = (argc>1) ? argv[1] : "bunny.obj"; SurfaceMesh mesh; bool success = mesh.read(file); CHECK(success); auto points = mesh.get_vertex_property<Vec3>("v:point"); Vec3 p(0,0,0); for (auto vit: mesh.vertices()) p += points[vit]; p /= mesh.n_vertices(); std::cout << "barycenter: " << p << std::endl; }
/// OpenGL initialization void init() { ///----------------------- DATA ---------------------------- auto vpoints = mesh.get_vertex_property<Vec3>("v:point"); auto vnormals = mesh.get_vertex_property<Vec3>("v:normal"); assert(vpoints); assert(vnormals); ///---------------------- TRIANGLES ------------------------ triangles.clear(); for(auto f: mesh.faces()) for(auto v: mesh.vertices(f)) triangles.push_back(v.idx()); ///---------------------- OPENGL GLOBALS-------------------- glClearColor(1.0f, 1.0f, 1.0f, 0.0f); ///< background glEnable(GL_DEPTH_TEST); // Enable depth test // glDisable(GL_CULL_FACE); // Cull back-facing /// Compile the shaders programID = load_shaders("vshader.glsl", "fshader.glsl"); if(!programID) exit(EXIT_FAILURE); glUseProgram( programID ); ///---------------------- CAMERA ---------------------------- { typedef Eigen::Vector3f vec3; typedef Eigen::Matrix4f mat4; update_projection_matrix(); /// Define the view matrix (camera extrinsics) vec3 cam_pos(0,0,5); vec3 cam_look(0,0,-1); /// Remember: GL swaps viewdir vec3 cam_up(0,1,0); view = OpenGP::lookAt(cam_pos, cam_look, cam_up); // cout << view << endl; /// Define the modelview matrix model = mat4::Identity(); // cout << model << endl; /// Set initial matrices set_uniform_matrix(programID,"M",model); ///< to get world coordinates set_uniform_matrix(programID,"MV",view*model); ///< to get camera coordinates set_uniform_matrix(programID,"MVP",projection*view*model); ///< to get clip coordinates } ///---------------------- LIGHT ----------------------------- { Vec3 light_dir(0,0,1); set_uniform_vector(programID,"LDIR",light_dir); ///< to get camera coordinates } ///---------------------- VARRAY ---------------------------- { GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); } ///---------------------- BUFFERS ---------------------------- GLuint vertexbuffer, normalbuffer, trianglebuffer; { /// Load mesh vertices glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, mesh.n_vertices() * sizeof(Vec3), vpoints.data(), GL_STATIC_DRAW); /// Load mesh normals glGenBuffers(1, &normalbuffer); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); glBufferData(GL_ARRAY_BUFFER, mesh.n_vertices() * sizeof(Vec3), vnormals.data(), GL_STATIC_DRAW); /// Triangle indexes buffer glGenBuffers(1, &trianglebuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, trianglebuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles.size() * sizeof(unsigned int), &triangles[0], GL_STATIC_DRAW); } ///---------------------- SHADER ATTRIBUTES ---------------------------- { /// Vertex positions in shader variable "vposition" GLuint vposition = glGetAttribLocation(programID, "vposition"); glEnableVertexAttribArray(vposition); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer(vposition, 3, GL_FLOAT, DONT_NORMALIZE, ZERO_STRIDE, ZERO_BUFFER_OFFSET); /// Vertex normals in in shader variable "vnormal" GLuint vnormal = glGetAttribLocation(programID, "vnormal"); glEnableVertexAttribArray(vnormal); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); glVertexAttribPointer(vnormal, 3, GL_FLOAT, DONT_NORMALIZE, ZERO_STRIDE, ZERO_BUFFER_OFFSET); } }