コード例 #1
0
    void MaterialLibrary::Load()
    {
        ResourceType* currMat = nullptr;
        std::string currLine;
        auto filename = Resource::application->GetConfig().resourceBase + "/" + GetParameters()[0];
        std::ifstream inFile(filename);

        if (!inFile.is_open()) {
            throw resource_loading_error() << ::boost::errinfo_file_name(filename) << resid_info(id) << errdesc_info("Cannot open file.");
        }

        boost::regex reg_newmtl("^newmtl\\s+(\\w+)$");
        boost::regex reg_Ka("^Ka\\s+" + regex_help::flt3 + "$");
        boost::regex reg_Kd("^Kd\\s+" + regex_help::flt3 + "$");
        boost::regex reg_Ks("^Ks\\s+" + regex_help::flt3 + "$");
        boost::regex reg_d("^d\\s+" + regex_help::flt + "$");
        boost::regex reg_d_halo("^d\\s+-halo\\s+" + regex_help::flt + "$");
        boost::regex reg_Ns("^Ns\\s+" + regex_help::flt + "$");
        boost::regex reg_Ni("^Ni\\s+" + regex_help::flt + "$");
        boost::regex reg_map_Kd("^map_Kd\\s+(.*\\s+)?([\\w-]+\\.\\w+)$");
        boost::regex reg_map_bump("^(map_bump|bump)\\s+(.*\\s+)?([\\w-]+\\.\\w+)$");

        boost::smatch lineMatch;

        while (inFile.good()) {
            std::getline(inFile, currLine);

            boost::trim(currLine);
            if (currLine.length() == 0 || boost::starts_with(currLine, "#"))
                continue; // comment or empty line
            if (boost::regex_match(currLine, lineMatch, reg_newmtl)) {
                auto mtlName = lineMatch[1].str();
                currMat = SetResource(mtlName, std::move(std::make_unique<Material>()));
            } else if (boost::regex_match(currLine, lineMatch, reg_Ka) && currMat) {
                currMat->ambient = parseColor(lineMatch);
            } else if (boost::regex_match(currLine, lineMatch, reg_Kd) && currMat) {
                currMat->diffuse = parseColor(lineMatch);
            } else if (boost::regex_match(currLine, lineMatch, reg_Ks) && currMat) {
                currMat->specular = parseColor(lineMatch);
            } else if (boost::regex_match(currLine, lineMatch, reg_d) && currMat) {
                currMat->alpha = boost::lexical_cast<float>(lineMatch[1].str());
            } else if (boost::regex_match(currLine, lineMatch, reg_d_halo) && currMat) {
                currMat->minOrientedAlpha = boost::lexical_cast<float>(lineMatch[1].str());
            } else if (boost::regex_match(currLine, lineMatch, reg_Ns) && currMat) {
                currMat->N_s = boost::lexical_cast<float>(lineMatch[1].str());
            } else if (boost::regex_match(currLine, lineMatch, reg_Ni) && currMat) {
                currMat->N_i = boost::lexical_cast<float>(lineMatch[1].str());
            } else if (boost::regex_match(currLine, lineMatch, reg_map_Kd) && currMat) {
                currMat->diffuseTex = parseTexture(lineMatch[2].str(), "sRGB");
            } else if (boost::regex_match(currLine, lineMatch, reg_map_bump) && currMat) {
                currMat->bumpTex = parseTexture(lineMatch[3].str(), "");
                currMat->bumpMultiplier = parseFloatParameter("-bm", lineMatch[2].str(), 1.0f);
            } else {
                notImplemented(currLine);
            }
        }
        inFile.close();

        Resource::Load();
    }
コード例 #2
0
bool KAbstractObjParserPrivate::parse()
{
  for (;;)
  {
    switch (nextToken())
    {
    case PT_ERROR:
      qFatal("Encountered an error! Aborting");
      return false;
    case PT_EOF:
      return true;
    case PT_VERTEX:
      parseVertex();
      break;
    case PT_TEXTURE:
      parseTexture();
      break;
    case PT_NORMAL:
      parseNormal();
      break;
    case PT_PARAMETER:
      parseParameter();
      break;
    case PT_FACE:
      parseFace();
    case PT_ENDSTATEMENT:
      break;
    }
  }
}
コード例 #3
0
void WFObject::parseLine(char *line)
{
	if(!strlen(line))
	{
		return;
	}

	char *lineType;
	lineType = strtok(_strdup(line), " ");

	// Decide what to do
	if(!strcmp(lineType, "v"))		// Vertex
	{
		parseVertex(line);
	}
	else if (!strcmp(lineType, "vt"))
	{
		parseTexture(line);
	}
	else if(!strcmp(lineType, "vn"))	// Normal
	{
		parseNormal(line);
	}
	else if(!strcmp(lineType, "f"))	// Face
	{
		parseFace(line);
	}

	return;
}
コード例 #4
0
ファイル: Parser.cpp プロジェクト: kc4271/KSoftRender
void loadTextures(void) {
	int count = 0;
	char line_buffer[MAX_LINE_SIZE];

	fprintf(stderr,"Loading textures\n");

	/* first make room for all the textures */
	tex = (Texture **)malloc(sizeof(Texture*) * num_tex);
	tex_codes = (int *)malloc(sizeof(int) * num_tex);

	while((count < num_tex) && fgets(line_buffer, MAX_LINE_SIZE, fp)) {	
		/* only do something if it's not a comment and the line is not blank */
		if (!lineCanBeIgnored(line_buffer)) {
			fprintf(stderr,"%s", line_buffer);
			parseTexture(count, line_buffer);
			count++;
		}
	}

	return;
}
コード例 #5
0
ファイル: MazeParser.cpp プロジェクト: jfiorio/renderer
void MazeParser::loadTextures()
{
   int count = 0;
   char line_buffer[MAX_LINE_SIZE];

   fprintf(stderr,"Loading textures\n");
   fflush(stderr);

   while((count < num_tex) && fgets(line_buffer, MAX_LINE_SIZE, fp))
   {
      /* only do something if it's not a comment and the line is not blank */
      if (!lineCanBeIgnored(line_buffer))
      {
         fprintf(stderr,"%s\n", line_buffer);
         fflush(stderr);
         parseTexture(count, line_buffer);
         count++;
      }
   }
   return;
}
コード例 #6
0
ファイル: Parser.cpp プロジェクト: resistor/rsl-llvm
void Parser::parsePrimary() {
  Token t = lex.peek();
  Token t2 = lex.peek(2);
  
  switch (t.type) {
    case Token::NUMERIC:
      lex.consume(Token::NUMERIC);
      break;
    case Token::STRINGCONSTANT:
      lex.consume(Token::STRINGCONSTANT);
      break;
    case Token::TEXTURE:
    case Token::ENVIRONMENT:
    case Token::SHADOW:
      parseTexture();
      break;
    case Token::LPAREN:
      lex.consume();
      parseExpression();
      lex.consume(Token::RPAREN);
      break;
    case Token::LBRACE:
      lex.consume();
      parseExpression();
      lex.consume(Token::RBRACE);
      break;
    case Token::IDENTIFIER:
      t2 = lex.peek(2);
      if (t2.type == Token::LPAREN)
        parseCallExpr();
      else if (t2.type == Token::LBRACKET)
        parseArrayExpr();
      else
        lex.consume(Token::IDENTIFIER);
      break;
    default:
      assert(0 && "Parse error, expected primary!");
  }
}
コード例 #7
0
Shape* SceneParser::parseShape(TiXmlElement *elem) {
	std::string shapeType = elem->Attribute("type");
	Shape *s;
	Color color;
	Material material;
	elem->FirstChildElement("color")->QueryDoubleAttribute("r", &color[0]);
	elem->FirstChildElement("color")->QueryDoubleAttribute("g", &color[1]);
	elem->FirstChildElement("color")->QueryDoubleAttribute("b", &color[2]);
	elem->FirstChildElement("material")->QueryDoubleAttribute("ka", &material.k_a);
	elem->FirstChildElement("material")->QueryDoubleAttribute("kd", &material.k_d);
	elem->FirstChildElement("material")->QueryDoubleAttribute("ks", &material.k_s);
	elem->FirstChildElement("material")->QueryDoubleAttribute("ns", &material.n_s);
	elem->FirstChildElement("material")->QueryDoubleAttribute("kreflex", &material.k_reflex);
	if (shapeType == "quadric") {
		double a, b, c, d, e, f, g, h, i, j;
		elem->FirstChildElement("a")->QueryDoubleAttribute("value", &a);
		elem->FirstChildElement("b")->QueryDoubleAttribute("value", &b);
		elem->FirstChildElement("c")->QueryDoubleAttribute("value", &c);
		elem->FirstChildElement("d")->QueryDoubleAttribute("value", &d);
		elem->FirstChildElement("e")->QueryDoubleAttribute("value", &e);
		elem->FirstChildElement("f")->QueryDoubleAttribute("value", &f);
		elem->FirstChildElement("g")->QueryDoubleAttribute("value", &g);
		elem->FirstChildElement("h")->QueryDoubleAttribute("value", &h);
		elem->FirstChildElement("i")->QueryDoubleAttribute("value", &i);
		elem->FirstChildElement("j")->QueryDoubleAttribute("value", &j);
		s = new Quadrics(color, material, a, b, c, d, e, f, g, h, i, j);
	} else if (shapeType == "uvsphere" || shapeType == "sphere") {
		Vector3 center;
		double radius;
		elem->FirstChildElement("center")->QueryDoubleAttribute("x", &center[0]);
		elem->FirstChildElement("center")->QueryDoubleAttribute("y", &center[1]);
		elem->FirstChildElement("center")->QueryDoubleAttribute("z", &center[2]);
		elem->FirstChildElement("radius")->QueryDoubleAttribute("value", &radius);
		if (shapeType == "uvsphere") {
			Texture *texture;
			double plusPhi;
			double plusTheta;

			texture = parseTexture(elem);

			elem->FirstChildElement("plusphi")->QueryDoubleAttribute("value", &plusPhi);
			plusPhi *= M_PI;

			elem->FirstChildElement("plustheta")->QueryDoubleAttribute("value", &plusTheta);
			plusTheta *= M_PI;

			s = new UVSphere(color, material, center, radius, texture, plusTheta, plusPhi);
		} else if (shapeType == "sphere") {
			s = new Sphere(color, material, center, radius);
		}
	} else if (shapeType == "draughtboard" || shapeType == "rectangle" || shapeType == "plane") {
		Ray normAndPoint;
		elem->FirstChildElement("center")->QueryDoubleAttribute("x", &normAndPoint[0][0]);
		elem->FirstChildElement("center")->QueryDoubleAttribute("y", &normAndPoint[0][1]);
		elem->FirstChildElement("center")->QueryDoubleAttribute("z", &normAndPoint[0][2]);
		elem->FirstChildElement("normal")->QueryDoubleAttribute("x", &normAndPoint[1][0]);
		elem->FirstChildElement("normal")->QueryDoubleAttribute("y", &normAndPoint[1][1]);
		elem->FirstChildElement("normal")->QueryDoubleAttribute("z", &normAndPoint[1][2]);
		if (shapeType == "plane") {
			s = new Plane(color, material, normAndPoint);
		} else if (shapeType == "draughtboard" || shapeType == "rectangle") {
			double height;
			double width;
			elem->FirstChildElement("height")->QueryDoubleAttribute("value", &height);
			elem->FirstChildElement("width")->QueryDoubleAttribute("value", &width);
			if (shapeType == "rectangle") {
				s = new Rectangle(color, material, normAndPoint, height, width);
			} else if (shapeType == "draughtboard") {
				Color colorD;
				double caseSize;
				elem->FirstChildElement("otherColor")->QueryDoubleAttribute("r", &colorD[0]);
				elem->FirstChildElement("otherColor")->QueryDoubleAttribute("g", &colorD[1]);
				elem->FirstChildElement("otherColor")->QueryDoubleAttribute("b", &colorD[2]);
				elem->FirstChildElement("caseSize")->QueryDoubleAttribute("value", &caseSize);
				s = new Draughtboard(color, material, colorD, normAndPoint, height, width, caseSize);
			}
		}
	}/* else if (shapeType == "cylindre") {
		Vector3 origin;
		Vector3 height;
		double radius;
		elem->FirstChildElement("origin")->QueryDoubleAttribute("x", &origin[0]);
		elem->FirstChildElement("origin")->QueryDoubleAttribute("y", &origin[1]);
		elem->FirstChildElement("origin")->QueryDoubleAttribute("z", &origin[2]);
		elem->FirstChildElement("height")->QueryDoubleAttribute("x", &height[0]);
		elem->FirstChildElement("height")->QueryDoubleAttribute("y", &height[1]);
		elem->FirstChildElement("height")->QueryDoubleAttribute("z", &height[2]);
		elem->FirstChildElement("radius")->QueryDoubleAttribute("value", &radius);
		s = new Cylindre(color, material, origin, height, radius);
	}*/ else {
		cerr << "Invalid description: " << shapeType << " does not exist.\nPlease, check the spelling and try again." << endl;
		exit(-1);
	}
	cout << "  Shape #" << _shapes.size()+1 << " (" << shapeType.substr(0, 7) << ")" << "\t[OK]" << endl;
	return s;
}