예제 #1
0
파일: asst7.cpp 프로젝트: thewinniewu/CS175
static void applySubdivs(Mesh &mesh, int subdivLevel) {
  for (int i = 0; i < subdivLevel; i++) {
    // subdivide faces
    for (int j = 0, n = mesh.getNumFaces(); j < n; j++) {
      Mesh::Face face = mesh.getFace(j);
      int verticesAroundFace = face.getNumVertices();
      Cvec3 vertexSum = Cvec3();
      for (int k = 0; k < verticesAroundFace; k++) {
        vertexSum += face.getVertex(k).getPosition();
      }
      vertexSum = vertexSum * (1.0 / verticesAroundFace); 
      mesh.setNewFaceVertex(face, vertexSum);
    }

    // subdivide edges
    for (int j = 0, n = mesh.getNumEdges(); j < n; j++) {
      Mesh::Edge edge = mesh.getEdge(j);
      Cvec3 vertexSum = (edge.getVertex(0).getPosition() +
        edge.getVertex(1).getPosition() +
        mesh.getNewFaceVertex(edge.getFace(0)) +
        mesh.getNewFaceVertex(edge.getFace(1))) * 0.25;
      mesh.setNewEdgeVertex(edge, vertexSum); 
    }

    // subdivide vertices
    for (int j = 0, n = mesh.getNumVertices(); j < n; j++) {
      Mesh::Vertex v = mesh.getVertex(j);
      int numOfVertices = 0;
      
      Mesh::VertexIterator vertexIter(v.getIterator()), iterOrigin(vertexIter);
      Cvec3 accumVertices = Cvec3();
      Cvec3 accumFaceVertices = Cvec3();

      do {
        accumVertices += vertexIter.getVertex().getPosition();
        accumFaceVertices += mesh.getNewFaceVertex(vertexIter.getFace());
        
        numOfVertices++;
      } while (++vertexIter != iterOrigin);
      
      double factor = 1.0 / (numOfVertices * numOfVertices);

      Cvec3 vertexVertex = 
        v.getPosition() * ((numOfVertices - 2.0) / numOfVertices) +
        accumVertices * factor +
        accumFaceVertices * factor; 
      mesh.setNewVertexVertex(mesh.getVertex(j), vertexVertex);
    }
    
    // subdivide for each level of subdivision we need 
    mesh.subdivide();
  }
}
예제 #2
0
void collectVertexVertices(Mesh& m) {
  vector<vector<Cvec3> > vertexVertices;
  for (int i = 0; i < m.getNumVertices(); ++i) {
    const Mesh::Vertex v = m.getVertex(i);
    Mesh::VertexIterator it(v.getIterator()), it0(it);
    vector<Cvec3> vertices;
    vector<Cvec3> faces;
    do {
      vertices.push_back(it.getVertex().getPosition());
      faces.push_back(m.getNewFaceVertex(it.getFace()));
    }
    while (++it != it0);                                  // go around once the 1ring
    Cvec3 vertex = getVertexVertex(v.getPosition(), vertices, faces);
    m.setNewVertexVertex(v, vertex);
  }
}
예제 #3
0
파일: asst7.cpp 프로젝트: thewinniewu/CS175
static void updateMeshNormals(Mesh &mesh) {
  for (int i = 0, n = mesh.getNumVertices(); i < n; i++) {
    Cvec3 vectorSum = Cvec3(); 
    Mesh::Vertex v = mesh.getVertex(i);

    Mesh::VertexIterator vertexIter(v.getIterator()), iterOrigin(vertexIter);

    // walk around the vertex
    do {
      vectorSum += vertexIter.getFace().getNormal();
    } while (++vertexIter != iterOrigin);

    if (dot(vectorSum, vectorSum) > CS175_EPS2) {
      vectorSum.normalize();
    }
 
    v.setNormal(vectorSum);
  } 
}