Exemple #1
0
MeshObj* ObjLoader::loadObjFile(std::string fileName, std::string ID) {
  // sanity check for identfier -> must not be empty //
  if (ID.length() == 0) {
    return NULL;
  }
  // try to load the MeshObj for ID //
  MeshObj* meshObj = getMeshObj(ID);
  if (meshObj != NULL) {
    // if found, return it instead of loading a new one from file //
    return meshObj;
  }
  // ID is not known yet -> try to load mesh from file //

  // TODO: import mesh from given file //
  // setup variables used for parsing //
  std::string key;
  float x, y, z;
  unsigned int i, j, k;

  // setup local lists //
  MeshData meshData;

  // setup tools for parsing a line correctly //
  std::string line;
  std::stringstream sstr;

  // open file //
  std::ifstream file(fileName.c_str());
  if (file.is_open()) {
    while (file.good()) {
      key = "";
      getline(file, line);
      sstr.clear();
      sstr.str(line);
      sstr >> key;
      if (!key.compare("v")) {
        // read in vertex //
        sstr >> x >> y >> z;
	meshData.vertex_position.push_back(x);
	meshData.vertex_position.push_back(y);
	meshData.vertex_position.push_back(z);
      }
      if (!key.compare("vn")) {
        // read in vertex normal //
        sstr >> x >> y >> z;
	meshData.vertex_normal.push_back(x);
	meshData.vertex_normal.push_back(y);
	meshData.vertex_normal.push_back(z);
      }
      if (!key.compare("f")) {
        // read in vertex indices for a face //
        sstr >> i >> j >> k;
        meshData.indices.push_back(i - 1);
        meshData.indices.push_back(j - 1);
        meshData.indices.push_back(k - 1);
      }
Exemple #2
0
MeshObj* ObjLoader::loadObjFile(std::string fileName, std::string ID, float scale) {
  // sanity check for identfier -> must not be empty //
  if (ID.length() == 0) {
    return NULL;
  }
  // try to load the MeshObj for ID //
  MeshObj* obj = getMeshObj(ID);
  if (obj != NULL) {
    // if found, return it instead of loading a new one from file //
    return obj;
  }
  // ID is not known yet -> try to load mesh from file //
  
  // setup variables used for parsing //
  std::string key;
  float x, y, z;
  int vi[4];
  int ni[4];
  int ti[4];

  // setup local lists //
  std::vector<Point3D> localVertexList;
  std::vector<Point3D> localNormalList;
  std::vector<Point3D> localTexCoordList;
  std::vector<Face> localFaceList;

  // setup tools for parsing a line correctly //
  std::string line;
  std::stringstream sstr;

  // open file //
  std::ifstream file(fileName.c_str());
  unsigned int lineNumber = 0;
  if (file.is_open()) {
    while (file.good()) {
      key = "";
      getline(file, line);
      sstr.clear();
      sstr.str(line);
      sstr >> key;
      if (!key.compare("v")) {
        // read in vertex //
        sstr >> x >> y >> z;
        localVertexList.push_back(Point3D(x * scale, y * scale, z * scale));
      }
      if (!key.compare("vn")) {
        // read in normal //
        sstr >> x >> y >> z;
        localNormalList.push_back(Point3D(x, y, z));
      }
      if (!key.compare("vt")) {
        // read in texture coordinate //
        sstr >> x >> y;
        localTexCoordList.push_back(Point3D(x, y, 0));
      }
Exemple #3
0
MeshObj* ObjLoader::loadObjFile(std::string fileName, std::string ID, float scale) {
  // sanity check for identfier -> must not be empty //
  if (ID.length() == 0) {
    return NULL;
  }
  // try to load the MeshObj for ID //
  MeshObj* obj = getMeshObj(ID);
  if (obj != NULL) {
    // if found, return it instead of loading a new one from file //
    return obj;
  }
  // ID is not known yet -> try to load mesh from file //
  
  // TODO: extend your objLoader to load vertices, vertex normals and texture coordinates
  //       note: faces using normals and texture coordinates are defines as "f vi0/ti0/ni0 ... viN/tiN/niN"
  //       vi0 .. viN : vertex index of vertex 0..N
  //       ti0 .. tiN : texture coordinate index of vertex 0..N
  //       ni0 .. niN : vertex normal index of vertex 0..N
  //       faces without normals and texCoords are defined as  "f vi0// ... viN//"
  //       faces without texCoords are defined as              "f vi0//ni0 ... viN//niN"
  //       make your parser robust against ALL possible combinations
  //       also allow to import QUADS as faces. directly split them up into two triangles!
  
  // setup variables used for parsing //
  std::string key;
  float x, y, z;
  int vi[4];
  int ni[4];
  int ti[4];

  // setup local lists                        //
  // import all data in temporary lists first //
  std::vector<Point3D> localVertexList;
  std::vector<Point3D> localNormalList;
  std::vector<Point3D> localTexCoordList;
  std::vector<Face> localFaceList;

  // setup tools for parsing a line correctly //
  std::string line;
  std::stringstream sstr;

  // open file //
  std::ifstream file(fileName.c_str());
  unsigned int lineNumber = 0;
  if (file.is_open()) {
    while (file.good()) {
      key = "";
      getline(file, line);
      sstr.clear();
      sstr.str(line);
      sstr >> key;
      if (!key.compare("v")) {
        // read in vertex //
        sstr >> x >> y >> z;
        localVertexList.push_back(Point3D(x * scale, y * scale, z * scale));
      }
      // DONE: implement parsing of vertex normals and texture coordinates //
      if (key == "vn") {
        sstr >> x >> y >> z;
        localNormalList.push_back(Point3D(x, y, z));
      }
      if (key == "vt") {
        sstr >> x >> y;
        localTexCoordList.push_back(Point3D(x, y));
      }
Exemple #4
0
MeshObj* ObjLoader::loadObjFile(std::string fileName, std::string ID) {
  // sanity check for identfier -> must not be empty //
  if (ID.length() == 0) {
    return NULL;
  }
  // try to load the MeshObj for ID //
  MeshObj* meshObj = getMeshObj(ID);
  if (meshObj != NULL) {
    // if found, return it instead of loading a new one from file //
    return meshObj;
  }
  // ID is not known yet -> try to load mesh from file //
  
  // import mesh from given file //

  // setup temporary data container //
  MeshData meshData;
  
  // Öffne die angeforderte Datei
  std::fstream file ;
  file.open(fileName.c_str() , std::fstream::in );
  // Prüfe, ob das Öffnen erfolgreich war
  if (! file.is_open()) {
      std::cerr << fileName << " konnte nicht geöffnet werden.";
      return NULL;
  }

  // Lies die Zeilen einzeln ein, entscheide je nach Anfang der Zeile, ob sie
  // eine Position eine Normale oder ein Index ist. Hänge die Werte ans richtige
  // Array an.
  char temp[50];
  GLfloat x,y,z;
  while(file.getline(temp,50) && !file.eof()){
	  if(strncmp(temp,"v ",2) == 0){
	    sscanf(temp,"v %f %f %f", &x,&y,&z);
		meshData.vertex_position.push_back(x);
		meshData.vertex_position.push_back(y);
		meshData.vertex_position.push_back(z);
	  }
	  if(strncmp(temp,"vn ",3) == 0){
	    sscanf(temp,"vn %f %f %f", &x, &y, &z);
		meshData.vertex_normal.push_back(x);
		meshData.vertex_normal.push_back(y);
		meshData.vertex_normal.push_back(z);
	  }
	  if(strncmp(temp,"f ",2) == 0 ){
	    sscanf(temp,"f %f %f %f", &x,&y,&z);
		meshData.indices.push_back(x);
		meshData.indices.push_back(y);
		meshData.indices.push_back(z);
	  }
  }
  
  // setup variables used for parsing //
    
  // create new MeshObj and set imported geoemtry data //
  meshObj = new MeshObj();
  // TODO: assign imported data to this new MeshObj //
  meshObj->setData(meshData);
  // insert MeshObj into map //
  mMeshMap.insert(std::make_pair(ID, meshObj));
  
  // return newly created MeshObj //
  return meshObj;
}