void PyramidBrush::setNormals(ofMesh& mesh) {
    
    int nV = mesh.getNumVertices();
    int nT = mesh.getNumIndices() / 3;
    
    vector<ofPoint> norm(nV);
    for(int t=0; t < nT; t++) {
        int i1 = mesh.getIndex(3*t);
        int i2 = mesh.getIndex(3*t + 1);
        int i3 = mesh.getIndex(3*t + 2);
        
        const ofPoint &v1 = mesh.getVertex(i1);
        const ofPoint &v2 = mesh.getVertex(i2);
        const ofPoint &v3 = mesh.getVertex(i3);
        
        ofPoint dir = ( (v2 - v1).crossed(v3 - v1)).normalized();
        
        norm[i1] += dir;
        norm[i2] += dir;
        norm[i3] += dir;
    }
    for(int i = 0; i < nV; i++) {
        norm[i].normalize();
    }
    
    mesh.clearNormals();
    mesh.addNormals(norm);
}
Beispiel #2
0
//Universal function which sets normals for the triangle mesh
void ofxOcean::setNormals( ofMesh &mesh ){
    //The number of the vertices
    int nV = mesh.getNumVertices();
    //The number of the triangles
    int nT = mesh.getNumIndices() / 3;
    vector<ofPoint> norm( nV ); //Array for the normals
    //Scan all the triangles. For each triangle add its
    //normal to norm's vectors of triangle's vertices
    for (int t=0; t<nT; t++) {
        //Get indices of the triangle t
        int i1 = mesh.getIndex( 3 * t );
        int i2 = mesh.getIndex( 3 * t + 1 );
        int i3 = mesh.getIndex( 3 * t + 2 );
        //Get vertices of the triangle
        const ofPoint &v1 = mesh.getVertex( i1 );
        const ofPoint &v2 = mesh.getVertex( i2 );
        const ofPoint &v3 = mesh.getVertex( i3 );
        //Compute the triangle's normal
        ofPoint dir = ( (v2 - v1).getCrossed( v3 - v1 ) ).getNormalized();
        //Accumulate it to norm array for i1, i2, i3
        norm[ i1 ] += dir;
        norm[ i2 ] += dir;
        norm[ i3 ] += dir;
    }
    //Normalize the normal's length
    for (int i=0; i<nV; i++) {
        norm[i].normalize();
    }
    //Set the normals to mesh
    mesh.clearNormals();
    mesh.addNormals( norm );
}
Beispiel #3
0
//--------------------------------------------------------------
//Universal function which sets normals for the triangle mesh
void setNormals( ofMesh &mesh ){
    
    int nV = mesh.getNumVertices();//640
    int nT = mesh.getNumIndices() / 3;//213
    
    vector<ofPoint> norm( nV );
    
    for (int t=0; t<nT; t++) {
        
        int i1 = mesh.getIndex( 3 * t );
        int i2 = mesh.getIndex( 3 * t + 1 );
        int i3 = mesh.getIndex( 641 );
        
        const ofPoint &v1 = mesh.getVertex( i1 );
        const ofPoint &v2 = mesh.getVertex( i2 );
        const ofPoint &v3 = mesh.getVertex( i3 );
        
        //Compute the triangle's normal
        ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized();
        
        norm[ i1 ] += dir;
        norm[ i2 ] += dir;
        norm[ i3 ] += dir;
    }
    
    //Normalize the normal's length
    for (int i=0; i<nV; i++) {
        norm[i].normalize();
    }
    
    //Set the normals to mesh
    mesh.clearNormals();
    mesh.addNormals( norm );
}
Beispiel #4
0
void buildNormalsAverage(ofMesh& mesh) {
    vector<ofIndexType>& indices = mesh.getIndices();
    vector<ofVec3f> normals(mesh.getNumVertices());
    for(int i = 0; i < indices.size(); i += 3) {
        int i0 = indices[i + 0], i1 = indices[i + 1], i2 = indices[i + 2];
        ofVec3f normal = getNormal(mesh.getVertices()[i0], mesh.getVertices()[i1], mesh.getVertices()[i2]);
        normals[i0] += normal;
        normals[i1] += normal;
        normals[i2] += normal;
    }
    for(int i = 0; i < normals.size(); i++) {
        normals[i].normalize();
    }
    mesh.addNormals(normals);
}
void CloudsVisualSystem3DModelLoader::facetMesh( ofMesh& smoothedMesh, ofMesh& targetMesh )
{
	//get our vertex, uv and face info
	vector<ofVec3f>& v = smoothedMesh.getVertices();
	vector<ofVec2f>& uv = smoothedMesh.getTexCoords();
	vector<ofIndexType>& indices = smoothedMesh.getIndices();
	bool hasTC = smoothedMesh.getNumTexCoords();
	
	//use these to store our new mesh info
	vector<ofVec3f> facetedVertices( indices.size() );
	vector<ofVec3f> facetedNormals( indices.size() );
	vector<ofVec2f> facetedTexCoords;
	if(hasTC){
		facetedTexCoords.resize( indices.size() );
	}
	vector<ofIndexType> facetedIndices( indices.size() );
	
	//store vertex and uv data
	for (int i=0; i < indices.size(); i++) {
		facetedIndices[i] = i;
		facetedVertices[i] = v[indices[i]];
		if(hasTC)	facetedTexCoords[i] = uv[indices[i]];
	}
	
	//calculate our face normals
	ofVec3f n;
	for (int i=0; i < facetedIndices.size(); i+=3) {
		n = normalFrom3Points( facetedVertices[i], facetedVertices[i+1], facetedVertices[i+2]);
		facetedNormals[i] = n;
		facetedNormals[i+1] = n;
		facetedNormals[i+2] = n;
	}
	
	//setup our faceted mesh. this should still work if our targetMesh is our smoothMesh
	targetMesh.clear();
	targetMesh.addVertices( facetedVertices );
	targetMesh.addNormals( facetedNormals );
	if(hasTC)	targetMesh.addTexCoords( facetedTexCoords );
	targetMesh.addIndices( facetedIndices );
}
void getCellMesh(voro::voronoicell &_c, ofPoint _pos, ofMesh& mesh ){
    if( _c.p ) {
        
        ofPoint centroid = getCellCentroid(_c,_pos);
        
        int i,j,k,l,m,n;
        
        //  Vertex
        //
        double *ptsp=_c.pts;
        vector<ofPoint> vertices;
        vector<ofPoint> normals;
        for(i = 0; i < _c.p; i++, ptsp+=3){
            ofPoint newPoint;
            newPoint.x = _pos.x + ptsp[0]*0.5;
            newPoint.y = _pos.y + ptsp[1]*0.5;
            newPoint.z = _pos.z + ptsp[2]*0.5;
            vertices.push_back(newPoint);
            
            ofPoint newNormal;
            newNormal = _pos - newPoint;//centroid ;
            newNormal = newNormal.normalize();
            normals.push_back(newNormal);
        }
        
//        ofMesh mesh;
        mesh.setMode(OF_PRIMITIVE_TRIANGLES );
        mesh.addVertices(vertices);
        mesh.addNormals(normals);
        
        //  Index
        //
        for(i = 1; i < _c.p; i++){
            for(j = 0; j < _c.nu[i]; j++) {
                
                k = _c.ed[i][j];
                
                if( k >= 0 ) {
                    _c.ed[i][j]=-1-k;
                    l = _c.cycle_up( _c.ed[i][ _c.nu[i]+j], k);
                    m = _c.ed[k][l];
                    _c.ed[k][l]=-1-m;
                    
                    while(m!=i) {
                        n = _c.cycle_up( _c.ed[k][ _c.nu[k]+l],m);
                        mesh.addTriangle(i, k, m);
                        
                        k=m;
                        l=n;
                        m=_c.ed[k][l];
                        _c.ed[k][l]=-1-m;
                    }
                }
            }
        }
        
//        return mesh;
    }
    
//    return ofMesh();
};
void CloudsVisualSystem3DModelLoader::smoothMesh( ofMesh& facetedMesh, ofMesh& targetMesh, int precision)
{
	cout << "smoothing mesh" << endl;
	
	//get our vertex, uv and face info
	vector<ofVec3f>& v = facetedMesh.getVertices();
	vector<ofVec2f>& uv = facetedMesh.getTexCoords();
	vector<ofIndexType>& indices = facetedMesh.getIndices();
	bool hasTC = facetedMesh.getNumTexCoords();
	
	//use these to store our new mesh info
	map<string, unsigned int> mergeMap;
	vector<ofVec3f> smoothVertices;
	vector<ofVec3f> smoothNormals;
	vector<ofVec2f> smoothTexCoords;
	vector<ofIndexType> smoothIndices;
	
	//merge our vertices by pointing near by vertices to the same index
	for (int i=0; i<v.size(); i++)
	{
		mergeMap[ vec3ToString( v[i], precision ) ] = i;
	}
	
	//fill our smoothed vertex array with merged vertices & tex coords
	smoothVertices.resize( mergeMap.size() );
	if(hasTC)	smoothTexCoords.resize( mergeMap.size() );
	int smoothVertexCount = 0;
	for (map<string, unsigned int>::iterator it = mergeMap.begin(); it != mergeMap.end(); it++)
	{
		smoothVertices[smoothVertexCount] = v[it->second];
		if(hasTC)	smoothTexCoords[smoothVertexCount] = uv[it->second];
		it->second = smoothVertexCount;//store our new vertex index
		smoothVertexCount++;
	}
	
	
	//reconstruct our faces by reassigning their indices to the merged vertices
	smoothIndices.resize( indices.size() );
	for (int i=0; i<indices.size(); i++)
	{
		//use our old vertex poisition to retrieve our new index
		smoothIndices[i] = mergeMap[ vec3ToString( v[ indices[i] ], precision ) ];
	}
	
	//calculate our normals
	smoothNormals.resize( smoothVertices.size() );
	ofVec3f n;
	for (int i=0; i<smoothIndices.size(); i+=3)
	{
		n = normalFrom3Points( smoothVertices[smoothIndices[i]], smoothVertices[smoothIndices[i+1]], smoothVertices[smoothIndices[i+2]] );
		smoothNormals[smoothIndices[i]] += n;
		smoothNormals[smoothIndices[i+1]] += n;
		smoothNormals[smoothIndices[i+2]] += n;
	}
	
	for (int i=0; i<smoothNormals.size(); i++)
	{
		smoothNormals[i].normalize();
	}
	
	//setup our smoothed mesh. this should still work if our targetMesh is our facetedMesh
	targetMesh.clear();
	targetMesh.addVertices( smoothVertices );
	targetMesh.addNormals( smoothNormals );
	if(hasTC)	targetMesh.addTexCoords( smoothTexCoords );
	targetMesh.addIndices( smoothIndices );
}