void GLMesh::fillBuffers(const std::vector<Eigen::Vector3f>& colors)
{
    vertices.resize(3*(mesh.faces.size() - mesh.boundaries.size()));
    for (FaceCIter f = mesh.faces.begin(); f != mesh.faces.end(); f++) {
        if (!f->isBoundary()) {
            int i = 0;
            int fIdx = 3*f->index;
            
            HalfEdgeCIter h = f->he;
            do {
                VertexCIter v = h->vertex;
                
                // set vertex
                vertices[fIdx+i].position = v->position.cast<float>();
                vertices[fIdx+i].normal = v->normal().cast<float>();
                vertices[fIdx+i].color = colors[f->index];
                vertices[fIdx+i].uv = v->uv.cast<float>();
                i++;
                
                h = h->next;
            } while (h != f->he);
            
            // set barycenters
            vertices[fIdx+0].barycenter = Eigen::Vector3f(1.0, 0.0, 0.0);
            vertices[fIdx+1].barycenter = Eigen::Vector3f(0.0, 1.0, 0.0);
            vertices[fIdx+2].barycenter = Eigen::Vector3f(0.0, 0.0, 1.0);
        }
    }
}
Exemplo n.º 2
0
   Vector3D Vertex::normal( void ) const
   // TODO Returns an approximate unit normal at this vertex, computed by
   // TODO taking the area-weighted average of the normals of neighboring
   // TODO triangles, then normalizing.
   {
      // TODO Compute and return the area-weighted unit normal.
		 
			//no boundary polygon
			HalfedgeCIter h = this->halfedge();
			Vector3D nrm(0, 0, 0);
			double totalarea = 0;
			do
			{
				h = h->twin();
				FaceCIter f = h->face();
				if(!f->isBoundary())
				{
					VertexCIter v1 = h->vertex();
					VertexCIter v2 = h->next()->twin()->vertex();
					Vector3D out = cross(v1->position - position, v2->position - position);
					nrm += out;
					totalarea += out.norm();
				}
				h = h->next();
			}
			while(h != this->halfedge());
			nrm /= totalarea;
			nrm.normalize();
			return nrm;
	 }
Exemplo n.º 3
0
 void Viewer :: drawPolygons( void )
 {
     for (FaceCIter f = mesh->faces.begin();
          f != mesh->faces.end();
         f ++ )
     {
         bool onBoundary = f->isBoundary();
         if( onBoundary )
             continue;
         
         glBegin( GL_POLYGON );
         if( renderWireframe )
         {
             Vector3D N = f->normal();
             glNormal3dv( &N[0] );
         }
         
         HalfEdgeCIter he = f->he;
         do
         {
             if( not renderWireframe )
             {
                 Vector3D N = he->vertex->normal();
                 glNormal3dv( &N[0] );
             }
             
             glVertex3dv( &he->vertex->position[0] );
             
             he = he->next;
         }
         while( he != f->he );
         glEnd();
     }
 }
void GLMesh::fillPickBuffers()
{
    int tris = (int)(mesh.faces.size() - mesh.boundaries.size());
    
    // add faces
    int i = 0;
    pickVertices.resize(3*tris);
    for (FaceCIter f = mesh.faces.begin(); f != mesh.faces.end(); f++) {
        if (!f->isBoundary()) {
            int j = 0;
            Eigen::Vector3f color = elementColor(f->index);
            
            HalfEdgeCIter h = f->he;
            do {
                // set vertex
                pickVertices[3*i+j].position = h->vertex->position.cast<float>();
                pickVertices[3*i+j].color = color;
                j++;
                
                h = h->next;
            } while (h != f->he);
            i++;
        }
    }
    
    // add vertex faces
    int verts = 0;
    for (VertexCIter v = mesh.vertices.begin(); v != mesh.vertices.end(); v++) {
        verts += v->degree();
        if (v->isBoundary()) verts -= 1;
    }
    
    pickVertices.resize(3*(tris + verts));
    for (VertexCIter v = mesh.vertices.begin(); v != mesh.vertices.end(); v++) {
        Eigen::Vector3d n = 0.0015*v->normal();
        Eigen::Vector3f p1 = (v->position + n).cast<float>();
        Eigen::Vector3f color = elementColor(tris + v->index);
        
        HalfEdgeCIter h = v->he;
        do {
            if (!h->onBoundary) {
                Eigen::Vector3f p2 = (h->next->vertex->position + n).cast<float>();
                Eigen::Vector3f p3 = (h->next->next->vertex->position + n).cast<float>();
                
                pickVertices[3*i+0].position = p1;
                pickVertices[3*i+1].position = p1 + 0.125*(p2 - p1);
                pickVertices[3*i+2].position = p1 + 0.125*(p3 - p1);
                pickVertices[3*i+0].color = pickVertices[3*i+1].color = pickVertices[3*i+2].color = color;
                i++;
            }
            h = h->flip->next;
        } while (h != v->he);
    }
}
Exemplo n.º 5
0
void drawFaces()
{
    glColor4f(0.0, 0.0, 1.0, 0.6);
    for (FaceCIter f = mesh.faces.begin(); f != mesh.faces.end(); f++) {
        
        if (f->isBoundary()) continue;
        
        glBegin(GL_LINE_LOOP);
        HalfEdgeCIter he = f->he;
        do {
            glVertex3d(he->vertex->position.x(), he->vertex->position.y(), he->vertex->position.z());
            
            he = he->next;
            
        } while (he != f->he);
        
        glEnd();
    }
}
Exemplo n.º 6
0
void Viewer :: drawVectorField( void )
{
    shader.disable();
    glPushAttrib( GL_ALL_ATTRIB_BITS );

    double h = 0.25*mesh.meanEdgeLength();

    glDisable( GL_LIGHTING );
    glColor3f( 0., 0., 0. );
    glLineWidth( 2.0 );

    for( FaceCIter f  = mesh.faces.begin();
            f != mesh.faces.end();
            f ++ )
    {
        if( f->isBoundary() ) continue;
        Vector a = f->barycenter();
        Vector b = a + h*f->vector;
        Vector n = f->normal();

        Vector v = b - a;
        Vector v90 = cross(n, v);
        Vector p0 = b;
        Vector p1 = p0 - 0.2 * v - 0.1 * v90;
        Vector p2 = p0 - 0.2 * v + 0.1 * v90;

        glBegin( GL_LINES );
        glVertex3dv( &a[0] );
        glVertex3dv( &b[0] );
        glEnd();

        glBegin(GL_TRIANGLES);
        glVertex3dv( &p0[0] );
        glVertex3dv( &p1[0] );
        glVertex3dv( &p2[0] );
        glEnd();
    }

    glPopAttrib();
}
Exemplo n.º 7
0
void Viewer :: drawPolygons( void )
{
    glEnable(GL_COLOR_MATERIAL);
    for( FaceCIter f  = mesh.faces.begin();
            f != mesh.faces.end();
            f ++ )
    {
        if( f->isBoundary() ) continue;

        glBegin( GL_POLYGON );
        if( renderWireframe )
        {
            Vector N = f->normal();
            glNormal3dv( &N[0] );
        }

        HalfEdgeCIter he = f->he;
        do
        {
            if( not renderWireframe )
            {
                Vector N = he->vertex->normal();
                glNormal3dv( &N[0] );
            }

            double alpha = he->vertex->distance / maxDistance;
            glColor4f( alpha, alpha, alpha, 1. );
            glVertex3dv( &he->vertex->position[0] );

            he = he->next;
        }
        while( he != f->he );
        glEnd();
    }
    glDisable(GL_COLOR_MATERIAL);
}