Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBitMaster

Posted 16 October 2012 - 01:09 AM

The first important step is verifying the writer works. Being able to do that with an external, already verified tool, will (as I already said) save a lot of headache.

Once that is done, reading the same Wavefronts as generated again is pretty trivial using iostreams (I would not implement a full loader, that is what Assimp and co is for). String splitting via streams is pretty trivial.

Third, nothing in the OPs post says he has to implement a loader as well. And if he does, here is one that can loads the files generated by the writer above:
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

void readObject(const std::string& fileName, std::vector<point>& points, std::vector<int>& indices)
{
   points.clear();
   indices.clear();

   std::string line;
   std::ifstream is(fileName.c_str());
   while (std::getline(is, line))
   {
	  std::string prefix;
	  std::istringstream iss(line);
	  iss >> prefix;
	  if (prefix == "v")
	  {
		  point pt;
		  iss >> pt.x >> pt.y >> pt.z;
		  points.push_back(pt);
	  }
	  if (prefix == "f")
	  {
		  int idx[3];
		  iss >> idx[0] >> idx[1] >> idx[2];
		  indices.push_back(idx[0] - 1);
		  indices.push_back(idx[1] - 1);
		  indices.push_back(idx[2] - 1);
	  }
   }
}
It's not pretty and really does not work with more complicated Wavefronts or faulty files (production code must be able to deal with invalid files) but it gets the job done for the moment. Again, for loading I would strongly advise using a full fledged loader library since developing a full and error prone loader is a rather tedious and boring exercise.

Edit: <fstream> is needed, not <iostream>. Sorry for the inconvenience.
Edit2: istrstream was the old deprecated string stream. istringstream is what should be used.

#2BitMaster

Posted 15 October 2012 - 11:03 AM

The first important step is verifying the writer works. Being able to do that with an external, already verified tool, will (as I already said) save a lot of headache.

Once that is done, reading the same Wavefronts as generated again is pretty trivial using iostreams (I would not implement a full loader, that is what Assimp and co is for). String splitting via streams is pretty trivial.

Third, nothing in the OPs post says he has to implement a loader as well. And if he does, here is one that can loads the files generated by the writer above:
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

void readObject(const std::string& fileName, std::vector<point>& points, std::vector<int>& indices)
{
   points.clear();
   indices.clear();

   std::string line;
   std::ifstream is(fileName.c_str());
   while (std::getline(is, line))
   {
	  std::string prefix;
	  std::istrstream iss(line);
	  iss >> prefix;
	  if (prefix == "v")
	  {
		  point pt;
		  iss >> pt.x >> pt.y >> pt.z;
		  points.push_back(pt);
	  }
	  if (prefix == "f")
	  {
		  int idx[3];
		  iss >> idx[0] >> idx[1] >> idx[2];
		  indices.push_back(idx[0] - 1);
		  indices.push_back(idx[1] - 1);
		  indices.push_back(idx[2] - 1);
	  }
   }
}
It's not pretty and really does not work with more complicated Wavefronts or faulty files (production code must be able to deal with invalid files) but it gets the job done for the moment. Again, for loading I would strongly advise using a full fledged loader library since developing a full and error prone loader is a rather tedious and boring exercise.

Edit: <fstream> is needed, not <iostream>. Sorry for the inconvenience.

#1BitMaster

Posted 15 October 2012 - 02:48 AM

The first important step is verifying the writer works. Being able to do that with an external, already verified tool, will (as I already said) save a lot of headache.

Once that is done, reading the same Wavefronts as generated again is pretty trivial using iostreams (I would not implement a full loader, that is what Assimp and co is for). String splitting via streams is pretty trivial.

Third, nothing in the OPs post says he has to implement a loader as well. And if he does, here is one that can loads the files generated by the writer above:
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

void readObject(const std::string& fileName, std::vector<point>& points, std::vector<int>& indices)
{
   points.clear();
   indices.clear();

   std::string line;
   std::ifstream is(fileName.c_str());
   while (std::getline(is, line))
   {
      std::string prefix;
      std::istrstream iss(line);
      iss >> prefix;
      if (prefix == "v")
      {
          point pt;
          iss >> pt.x >> pt.y >> pt.z;
          points.push_back(pt);
      }
      if (prefix == "f")
      {
          int idx[3];
          iss >> idx[0] >> idx[1] >> idx[2];
          indices.push_back(idx[0] - 1);
          indices.push_back(idx[1] - 1);
          indices.push_back(idx[2] - 1);
      }
   }
}
It's not pretty and really does not work with more complicated Wavefronts or faulty files (production code must be able to deal with invalid files) but it gets the job done for the moment. Again, for loading I would strongly advise using a full fledged loader library since developing a full and error prone loader is a rather tedious and boring exercise.

PARTNERS