コード例 #1
0
ファイル: mesh.cpp プロジェクト: tsong/viewer
/* parse a line in the form of "<f> <v1/t1/n1> <v2/t2/n2> <v3/t3/n3> <v4/t4/n4>"
   into a face. The texture and normal indices are optional */
bool parseFace(QString line, uint *vertices, uint *normals, bool &hasNormals) {
    line = line.simplified();

    //ensure that the face is a quad
    QStringList tokens = line.split(" ", QString::SkipEmptyParts);
    if (tokens.size() != 5)
        return false;

    hasNormals = false;
    QStringListIterator it(tokens);
    it.next();
    for (uint i = 0; i < 4; i++) {
        //parse the token into its vertex, texture, and normal indices
        QString vertexStr = it.next();
        QStringList vertexTokens = vertexStr.split("/");

        //check if data is properly formatted
        bool validVertex = vertexTokens.size() > 0 && vertexTokens.size() <= 3;
        bool validNormals = !hasNormals || vertexTokens.size() == 3; //has normals --> 3 vertex tokens
        if (!validVertex || !validNormals)
            return false;

        //add vertex index
        bool ok = false;
        QStringListIterator vertexIt(vertexTokens);
        vertices[i] = vertexIt.next().toInt(&ok) - 1;

        //since we ignore texture indices, we can skip the second token
        if (vertexTokens.size() == 3) {
            hasNormals = true;
            vertexIt.next();
            normals[i] = vertexIt.next().toInt(&ok) - 1;
        }

        if (!ok) return false;
    }

    return true;
}
コード例 #2
0
ファイル: expTerrain.cpp プロジェクト: mikowiec/harvest
void exportTerrain::printPolygonData(const MFnMesh &theMesh,const bool phy)
{
    MStatus status;   
    MPointArray   vertices;
    MIntArray     perPolyVertices, numVerticesPerPoly;
    MVector	  tNorm;
    MVectorArray  perVertexNormals, normals;
    MItMeshPolygon itPoly( theMesh.object() );
    
    long numPolygons = theMesh.numPolygons();
    long numVertices = theMesh.numVertices();
   
    theMesh.getPoints(vertices);

    fout << "Vertices: " << endl;
    fout << "Number of Vertices: " << numVertices << endl;
    fout << vertices << endl;


    fout << "Number of polygons: " << numPolygons << endl;
    fout << "Polygon Connection List:" << endl;
    fout << "["; 
    for(int i = 0; i < numPolygons - 1; ++i){
	fout << i << ": ";
	status = theMesh.getPolygonVertices(i, perPolyVertices);
	fout << perPolyVertices;
	fout << ", " << endl;
    }
    //last is a special case
    fout << numPolygons -1 << ": ";
    status = theMesh.getPolygonVertices(numPolygons -1, perPolyVertices);
    fout << perPolyVertices;
    fout << "]" << endl;

    //Per Vertex Normals
    for(i=0; i < numVertices; ++i){
	theMesh.getVertexNormal( i, tNorm);
	perVertexNormals.append(tNorm);
    }
    fout << "Per Vertex Normals: \n";
    fout << perVertexNormals << endl;


/*    //per vertex per polygon Normals
      //Not supported by the Reaper graphic engine
    fout << "Normals" << endl;
    i = 0;
    fout << "[ ";
    while (!itPoly.isDone() ){
	itPoly.getNormals(normals);
	fout << i << ": " << normals << endl;
	++i;
	itPoly.next();
    }
    fout << " ]" << endl;
*/

    if( !phy ){
	   //Texture coordinate information

    MIntArray ids;
    int index;
    
    MFloatArray Us, Vs;
    theMesh.getUVs(Us,Vs);

    MFloatArray u_s(numVertices),v_s(numVertices);
    MIntArray numuvs(numVertices,0);    
    //Now the tactic is:
    //Iterate over all polygons, and for each vertex read out the UV out of the list
    //Insert that in two float arrays, and keep track of how many uvs 
    //that have been inserted. At last divd the us and vs by the number you 
    //already go there

    //Shared uvs will look strange, but it is the average uvs that is shown
    //Make sure to model using no shared uvs



    for(i = 0;i<numPolygons;++i){
        MIntArray polyVertices;
        theMesh.getPolygonVertices(i,polyVertices);
        for(int j = 0;j<polyVertices.length();++j){
                int uv_id;
                theMesh.getPolygonUVid(i,j,uv_id);
                u_s[polyVertices[j]] += Us[uv_id];
                v_s[polyVertices[j]] += Vs[uv_id];
                numuvs[polyVertices[j]]+=1;
        }
    }
    


    for(int k = 0;k<numVertices;++k){
            u_s[k] /= numuvs[k];
            v_s[k] /= numuvs[k];
    }

    fout << "Texture Coordinates: \n";
    fout << "[" ;
    for(index = 0;index < (u_s.length() - 1); ++index){
	fout << index << ": [" << u_s[index] << ", " <<
	    v_s[index] << "]," << endl;
    }
	
	//last is a special case
	fout << index << ": [" << u_s[index] << ", " <<
	    v_s[index] << "]";
    fout << "]" << endl;

	
	//Color data, for coloring. Make sure
	
	MItMeshVertex vertexIt(theMesh.object());
	MColor color;
	std::vector<MColor> colorArray;
	while (!vertexIt.isDone() ){
	    vertexIt.getColor(color);
	    colorArray.push_back(color);
	    vertexIt.next();
	}
	
	fout << "PerVertexColors: \n";
	fout << "[" ;
	for(index = 0;index < colorArray.size()-1; ++index){
	    fout << index << ": " << colorArray[index] << "," << endl;
	}
    //last is a special case
	fout << index << ": " << colorArray[index];
    fout << "]" << endl;
}
	
	}