Esempio n. 1
0
Object3D* SceneParser::parseObject(char token[MAX_PARSER_TOKEN_LENGTH]) {
	Object3D *answer = NULL;
	if (!strcmp(token, "Group")) {
		answer = (Object3D*)parseGroup();
	}
	else if (!strcmp(token, "Sphere")) {
		answer = (Object3D*)parseSphere();
	}
	else if (!strcmp(token, "Plane")) {
		answer = (Object3D*)parsePlane();
	}
	else if (!strcmp(token, "Triangle")) {
		answer = (Object3D*)parseTriangle();
	}
	else if (!strcmp(token, "TriangleMesh")) {
		answer = (Object3D*)parseTriangleMesh();
	}
	else if (!strcmp(token, "Transform")) {
		answer = (Object3D*)parseTransform();
	}
	else {
		printf("Unknown token in parseObject: '%s'\n", token);
		exit(0);
	}
	return answer;
}
Esempio n. 2
0
GeometryPtr parseGeometry(TiXmlElement *g)
{
  GeometryPtr geom;
  if (!g) return geom;

  TiXmlElement *shape = g->FirstChildElement();
  if (!shape)
  {
    logError("Geometry tag contains no child element.");
    return geom;
  }

  std::string type_name = shape->ValueStr();
  if (type_name == "sphere")
  {
    Sphere *s = new Sphere();
    resetPtr(geom,s);
    if (parseSphere(*s, shape))
      return geom;
  }
  else if (type_name == "box")
  {
    Box *b = new Box();
    resetPtr(geom,b);
    if (parseBox(*b, shape))
      return geom;
  }
  else if (type_name == "cylinder")
  {
    Cylinder *c = new Cylinder();
    resetPtr(geom,c);
    if (parseCylinder(*c, shape))
      return geom;
  }
  else if (type_name == "mesh")
  {
    Mesh *m = new Mesh();
    resetPtr(geom,m);
    if (parseMesh(*m, shape))
      return geom;
  }
  else
  {
    logError("Unknown geometry type '%s'", type_name.c_str());
    return geom;
  }

  return GeometryPtr();
}
Esempio n. 3
0
my_shared_ptr<Geometry> parseGeometry(TiXmlElement *g)
{
  my_shared_ptr<Geometry> geom;
  if (!g) return geom;

  TiXmlElement *shape = g->FirstChildElement();
  if (!shape)
  {
    logError("Geometry tag contains no child element.");
    return geom;
  }

  const std::string type_name = shape->ValueTStr().c_str();
  if (type_name == "sphere")
  {
    Sphere *s = new Sphere();
    geom.reset(s);
    if (parseSphere(*s, shape))
      return geom;
  }
  else if (type_name == "box")
  {
    Box *b = new Box();
    geom.reset(b);
    if (parseBox(*b, shape))
      return geom;
  }
  else if (type_name == "cylinder")
  {
    Cylinder *c = new Cylinder();
    geom.reset(c);
    if (parseCylinder(*c, shape))
      return geom;
  }
  else if (type_name == "mesh")
  {
    Mesh *m = new Mesh();
    geom.reset(m);
    if (parseMesh(*m, shape))
      return geom;    
  }
  else
  {
    logError("Unknown geometry type '%s'", type_name.c_str());
    return geom;
  }
  
  return my_shared_ptr<Geometry>();
}
Esempio n. 4
0
/* Parses the "Group" token */
rtObjGroup* scene::parseGroup()
{
    char token[MAX_PARSER_TOKEN_LENGTH];


    getToken(token);
    assert (!strcmp(token, "{"));

    /**********************************************/
    /* Instantiate the group object               */
    /**********************************************/
    rtObjGroup *answer     = new rtObjGroup();

    bool working=true;
    while (working)
    {
        getToken(token);
        if (!strcmp(token,"}"))
        {
            working=false;
        }
        else
        {
            if (!strcmp(token, "Sphere"))
            {
                sphere *sceneElem = parseSphere();
                assert(sceneElem != NULL);
                answer->addObj(sceneElem);
            }
            else if (!strcmp(token, "Triangle"))
            {
                triangle *sceneElem = parseTriangle();
                assert(sceneElem != NULL);
                answer->addObj(sceneElem);
            }
            else
            {
                cout << "Unknown token in Group: '" << token << "' at line "
                     << curline << "\n";
                exit(0);
            }
        }
    }

    /* Return the group */
    return answer;
}
Esempio n. 5
0
void parseFile(char * inputFilePath){
    FILE * fp = fopen(inputFilePath, "r");
    char inputBuffer[2048];
    char * shapeToken = NULL;
    int lineCounter = 0;

    GLfloat currentReflection = 0.0;
    GLfloat currentOpacity = 1.0;
    GLfloat currentRefractIndex = 1.0;

    if(fp == NULL){
        printf("Error could not open the file %s", inputFilePath);
        deallocExit(0);
    }

    do{
        fgets(inputBuffer, 2048, fp);
        if(strlen(inputBuffer) > 1){
            shapeToken = strtok(inputBuffer, PARSE_DELIMITERS);

            if(strcmp(shapeToken, "triangle") == 0){
                parseTriangle(currentRefractIndex);
            }
            else if(strcmp(shapeToken, "sphere") == 0){
                parseSphere(currentRefractIndex);
            }
            else if(strcmp(shapeToken, "light") == 0){
                parseLight();
            }
            else if(strcmp(shapeToken, "lens") == 0){
                parseLens();
            }
            else{
                /*Lines starting with '#' are comments, and are ignored*/
                if(shapeToken[0] != '#'){
                    printf("Warning: Ignoring unrecognized input on line %d.\n", lineCounter);
                }
            }
        }
        lineCounter++;
    }
    while((feof(fp) == 0) && (ferror(fp) == 0));

    sortLensList();

}
Esempio n. 6
0
void SceneParser::parseObjects(void)
{
    if(currentToken == Scanner::StreamDone)
        return;
    else{
        while(errorFlag == false && currentToken != Scanner::StreamDone)
        {
            acceptToken(Scanner::Id);
            string tokenText = scanner.tokenText();

            if(tokenText == "sphere")
                parseSphere();
            else if(tokenText == "box")
                parseBox();
            else if(tokenText == "plane")
                parsePlane();
            else if(tokenText == "triangle")
                parseTriangle();
            else if(tokenText == "mesh")
                parseMesh();
            else if(tokenText == "cone")
                parseCone();
            else if(tokenText == "cylinder")
                parseCylinder();
            else if(tokenText == "pointlight")
                parsePointLight();
            else if(tokenText == "directionallight")
                parseDirectionalLight();
            else if(tokenText == "spotlight")
                parseSpotlight();
            else if(tokenText == "arealight")
                parseAreaLight();
            else{
                error("undefined command: " + tokenText);
            }

            advance();
        }
    }
}
Esempio n. 7
0
Scene parseFile( std::string filename )
{
   Scene scene;
   int maxSpheres = 1000;
   int maxTriangles = 1000;
   int maxPlanes = 1000;
   int maxPointLights = 10;
   scene.numPointLights = 0;
   scene.numSpheres = 0;
   scene.numTriangles = 0;
   scene.numPlanes = 0;
   scene.spheres = (Sphere *)malloc( sizeof(Sphere) * maxSpheres );
   scene.triangles = (Triangle *)malloc( sizeof(Triangle) * maxTriangles );
   scene.planes = (Plane *)malloc( sizeof(Plane) * maxSpheres );
   scene.pointLights = (PointLight *)malloc( sizeof(PointLight) * maxPointLights );

   //Open file for writing
   FILE *file = fopen(filename.c_str(), "r");
   if(file == NULL)
   {
      printf("Error Occured opening file\n");
      exit(EXIT_FAILURE);
   }

   while(1)
   {
      //starting off eating all whitespace
      char cur = ' ';
      while( isspace(cur) )
      {
         if( fscanf( file, "%c", &cur ) == EOF )
         {
            printf("End of File reached\n");
            return scene;
         }
      }

      //check for comment
      if(cur == '/')
      {
         while(cur != '\n')
         {
            if( fscanf( file, "%c", &cur ) == EOF )
            {
               printf("Error Occured reading file\n");
               exit(EXIT_FAILURE);
            }
         }
      }
      else if( cur == 'c' || cur == 'C' )
      {
         scene.camera = parseCamera( file );
      }
      else if( cur == 'l' || cur == 'L' )
      {
         PointLight light = parsePointLight( file );
         if( scene.numPointLights + 1 >= maxPointLights )
         {
            maxPointLights = maxPointLights*10;
            scene.pointLights = (PointLight *)realloc( scene.pointLights, sizeof(PointLight)*maxPointLights );
         }
         scene.pointLights[scene.numPointLights] = light;
         scene.numPointLights++;
      }
      else if( cur == 's' || cur == 'S' )
      {
         if( scene.numSpheres+1 >= maxSpheres )
         {
            maxSpheres = maxSpheres*1000;
            scene.spheres = (Sphere *) realloc( scene.spheres, sizeof(Sphere) * maxSpheres );
         }
         scene.spheres[scene.numSpheres] = parseSphere(file);
         scene.numSpheres++;
      }
      else if( cur == 'p' || cur == 'P' )
      {
         if( scene.numPlanes+1 >= maxPlanes )
         {
            maxPlanes = maxPlanes*1000;
            scene.planes = (Plane *) realloc( scene.planes, maxPlanes * sizeof(Plane) );
         }
         scene.planes[scene.numPlanes] = parsePlane(file);
         scene.numPlanes++;
      }
      else if( cur == 't' || cur == 'T' )
      {
         if( scene.numTriangles+1 >= maxTriangles )
         {
            maxTriangles = maxTriangles*1000;
            scene.triangles = (Triangle *) realloc( scene.triangles, maxTriangles * sizeof(Triangle) );
         }
         scene.triangles[scene.numTriangles] = parseTriangle(file);
         scene.numTriangles++;
      }
      else
      {
         printf("Unknown Keyword Failure char was |%c|\n", cur);
         exit(EXIT_FAILURE);
      }
   }
}
Esempio n. 8
0
Scene* Parser::parseScene(){
	///
	xml_node scene = root.child("scene");

	// optional name
	Scene* _s;
	if (scene.attribute("name") != NULL)
		_s = new Scene(scene.attribute("name").value());
	else
		_s = Scene::New();

	// optional settings
	xml_node op;
	op = scene.child("background");
	float x, y, z;
	if (op != NULL)
	{
		const char* back = op.text().as_string();
		sscanf(back, "%f %f %f", &x, &y, &z);

		_s->backgroundColor = Color(x, y, z);
		scene.remove_child("background");
	}
	op = scene.child("ambient");
	if (op != NULL)
	{
		const char* amb = op.text().as_string();
		sscanf(amb, "%f %f %f", &x, &y, &z);

		_s->ambientLight = Color(x, y, z);
		scene.remove_child("ambient");
	}

	// processing the objets in the scene
	xml_object_range<xml_node_iterator> objets = scene.children();
	Light* light;
	for (xml_node_iterator sceneElement = objets.begin(); sceneElement != objets.end(); ++sceneElement) {

		if (strcmp(sceneElement->name(), "mesh") == 0)
			_s->addActor(parseMesh(sceneElement));

		if (strcmp(sceneElement->name(), "sphere") == 0)
			_s->addActor(parseSphere(sceneElement));

		if (strcmp(sceneElement->name(), "box") == 0)
			_s->addActor(parseBox(sceneElement));

		if (strcmp(sceneElement->name(), "cone") == 0)
			_s->addActor(parseCone(sceneElement));

		if (strcmp(sceneElement->name(), "cylinder") == 0)
			_s->addActor(parseCylinder(sceneElement));

		else if (strcmp(sceneElement->name(), "light") == 0){
			light = parseLight(sceneElement);
			_s->addLight(light);
		}
	}

	return _s;
}