Ejemplo n.º 1
0
mesh OBJFileReader::getMesh() {
    std::ifstream in;
    in.open(m_filename);
    
    mesh objMesh;
    OBJFileData* data = new OBJFileData;
    // Indexing is different, so we pad.
    data->vertexList.push_back(float4());
    data->normalList.push_back(float4());
    data->textureCoordList.push_back(float4());
    
    int normalCounter = 0;
    
    std::map<std::string,material> materials;
    std::string mtl_filename = std::string(m_filename);
    size_t backslash_loc = mtl_filename.rfind("/");
    if(backslash_loc == mtl_filename.npos)
        backslash_loc = -1;
    
    std::vector<triangle> triangles;
    
    MTLFileReader *mtl_reader;
    
    std::string mtl_in_use;
    
    material cur_material = material();
    
    while(!in.eof()) {
        char line[1024];

        in.getline(line, 1024);
        
        float4 tmp;
        char* filename;
        
        switch(getLineType(line)){
            case TYPE_VERTEX_DECLARATION:
                data->vertexList.push_back(getVertexFromLine(line)); 
                break;
            case TYPE_NORMAL_DECLARATION:
                tmp = getVertexFromLine(line); // tmp = normal
                tmp.setW(0.0f);
                data->normalList.push_back(tmp);
                break;
            case TYPE_TEXTURE_COORD_DECLARATION:
                tmp = getVertexFromLine(line); 
                data->textureCoordList.push_back(tmp);
                break;
            case TYPE_FACE_DECLARATION:
                triangles = getFacesFromLine(line,data,cur_material);
                objMesh.appendTriangles(triangles);
                //objMesh.appendTriangles(getFacesFromLine(line,data,cur_material));
                break;
            case TYPE_MTLLIB_REF:
                strtok(line," ");
                filename = strtok(NULL," \n\0");
                mtl_filename.replace(backslash_loc+1, strlen(filename), filename);
                printf("%s\n",mtl_filename.c_str());
                mtl_reader = new MTLFileReader(mtl_filename.c_str());
                materials = mtl_reader->getMaterials();
                break;
            case TYPE_USEMTL:
                strtok(line," ");
                filename = strtok(NULL," \n\0");
                mtl_in_use = std::string(filename);
                cur_material = materials[mtl_in_use];
                break;
            default:
                ; // We do nothing.
        }
    }
    
    printf("This model has %d triangles\n", objMesh.getTriangleCount());
    
    return objMesh;
}
/**
	This static method reads an obj file, whose name is sent in as a parameter, and returns a pointer to a GLMesh object that it created based on the file information.
	This method throws errors if the file doesn't exist, is not an obj file, etc.
*/
GLMesh* OBJReader::loadOBJFile(const char* fileName){
	if (fileName == NULL)
		throwError("fileName is NULL.");
	
//	Logger::out()<< "Loading mesh: " << fileName <<std::endl;

	FILE* f = fopen(fileName, "r");
	if (f == NULL)
		throwError("Cannot open file \'%s\'.", fileName);

	GLMesh* result = new GLMesh();

	result->setOriginalFilename( fileName );

	//have a temporary buffer used to read the file line by line...
	char buffer[200];

	//and this is an array of texture coordinates - the Point3d is a simple data type so I can use the DynamicArray
	DynamicArray<Point3d> texCoordinates;


	//this variable will keep getting populated with face information
	GLIndexedPoly temporaryPolygon;

	//this is where it happens.
	while (!feof(f)){
		//get a line from the file...
		fgets(buffer, 200, f);
		//see what line it is...
		int lineType = getLineType(buffer);
		if (lineType == VERTEX_INFO){
			//we need to read in the three coordinates - skip over the v
			Point3d vertexCoords = readCoordinates(buffer + 1);
			result->addVertex(vertexCoords);
		}

		if (lineType == TEXTURE_INFO){
			Point3d texCoords = readCoordinates(buffer + 2);
			texCoordinates.push_back(texCoords);
		}

		if (lineType == FACE_INFO){
			temporaryPolygon.indexes.clear();
			int vIndex, tIndex;
			int flag;
			char* tmpPointer = buffer+1;
			while (tmpPointer = getNextIndex(tmpPointer, vIndex, tIndex, flag)){
				temporaryPolygon.indexes.push_back(vIndex-1);
				if (flag & READ_TEXTCOORD_INDEX){
					if (tIndex<0)
						result->setVertexTexCoordinates(vIndex, texCoordinates[texCoordinates.size()+tIndex]);
					else
						result->setVertexTexCoordinates(vIndex, texCoordinates[tIndex]);
				}
			}
			if (temporaryPolygon.indexes.size() == 0)
				tprintf("Found a polygon with zero vertices.\n");
			else
				result->addPoly(temporaryPolygon);
		}

	}

	fclose(f);
	return result;
}
Ejemplo n.º 3
0
void Interpreter::interpretLine(string lineFromFile, ofstream &outputFile, ifstream &inputFile, bool _eval) {
    if (_eval) {
        LINE_TYPE lineType = getLineType(lineFromFile);
        vector<string> *tokens = new vector<string>();
        vector<string> quotes = tokenize(lineFromFile, "\"");
        vector<string> splitByEqual = tokenize(lineFromFile, "=");

        while (!lineFromFile.empty()) {
            tokens->push_back(getNextSymbol(lineFromFile));
        }

        printVector(*tokens);
        printVector(quotes);
        printVector(splitByEqual);
        //std::cout << "Outside switch";
        //std::cout << "Line type : " << (lineType) << std::endl;

        for (auto it = functionMap.begin(); it != functionMap.end(); it++) {
            //std::cout << it->first << std::endl;
            it->second.tostring();
        }

        //std::cout << std::endl;
        switch (lineType) {
            case DEFINE_VAR: {
                if (isFunction(splitByEqual[1])) {
                    // std::cout << "Passed isFunction";
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[1].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[1]));
                    }
                    //std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int index = 2;
                    while (index < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(index)));
                        index += 2;
                    }

                    //std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                    double functionReturn = function.thisReturn;

                    //std::cout << "Return value :" << functionReturn << std::endl;
                    string x = tokens->at(0);

                    if (x == "var") {
                        variableMap.insert(make_pair(tokens->at(1), functionReturn));
                    } else {
                        variableMap[x] = functionReturn;
                    }
                } else {
                    // std::cout << "1";
                    string name = tokens->at(1);
                    tokens->erase(tokens->begin(), tokens->begin() + 3);
                    double value = evaluateInfix(*tokens);
                    variableMap.insert(make_pair(name, value));
                }
            }
                break;

            case USER_DEFINED: {
                if (splitByEqual.size() == 2 && isFunction(splitByEqual[1])) {
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[1].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[1]));
                    }
                    //std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int x = 2;
                    while (x < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(x)));
                        x++;
                        x++;
                    }

                    //std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                    double returnValue = function.thisReturn;
                    variableMap[tokens->at(0)] = returnValue;
                }
                else if (splitByEqual.size() == 1) {
                    vector<string> parts;
                    vector<double> parameters;
                    while (!splitByEqual[0].empty()) {
                        parts.push_back(getNextSymbol(splitByEqual[0]));
                    }
                    // std::cout << "Before print parts" << std::endl;
                    printVector(parts);

                    int x = 2;
                    while (x < parts.size() - 1) {
                        parameters.push_back(variableMap.at(parts.at(x)));
                        x++;
                        x++;
                    }

                    // std::cout << "Hey !" << std::endl;
                    UserFunction function = functionMap.at(parts.front());
                    function.call(parameters, outputFile);
                } else {
                    // std::cout << "2";
                    string name = tokens->at(0);
                    tokens->erase(tokens->begin(), tokens->begin() + 2);
                    double result = evaluateInfix(*tokens);
                    variableMap[name] = result;
                }
            }
                break;
            case DOC_WRITE: {
                //std::cout << "3";
                quotes.size() > 1 ? outputFile << quotes.at(1) : outputFile << variableMap.at(tokens->at(2));
            }
                break;

            case FUNCTION_DEF: {
                int ifCounter = 0;
                UserFunction function(functionMap);
                int closing;
                for (int i = 0; i < tokens->size(); i++) { if (tokens->at(i) == ")") closing = i; }
                for (int i = 3; i < closing; i = i + 2) { function.arguments.push_back(tokens->at(i)); }
                printVector(function.arguments);
                string functionLine;

                while (getline(inputFile, functionLine)) {

                    LINE_TYPE type = getLineType(functionLine);
                    if (type == IF) {
                        ifCounter = ifCounter + 1;
                    }
                    if (type == END_BLOCK) {
                        if (ifCounter == 0) break;
                        else ifCounter = ifCounter - 1;
                    }
                    //if(getLineType(functionLine) == END_BLOCK) break;
                    //   std::cout << ifCounter << " " << functionLine << std::endl;
                    function.functionDefinition.push_back(functionLine);
                }
                functionMap.insert(make_pair(tokens->at(1), function));
            }
                break;

            case RETURN: {
                returnValue = variableMap.at(tokens->at(1));
            }
                break;

            case BLANK_LINE: {
                break;
            }

            case END_BLOCK: {
                if (!conditionStack.empty()) {
                    conditionStack.pop();
                }
                break;
            }
        }
    }
}