Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    686
  • comments
    1952
  • views
    387963

Well...

Sign in to follow this  
Stompy9999

121 views

Yeah, so I whipped up a messy .OBJ loader. Currently, it only reads vertices, texture coordinates, and normals. Here is the code for it. Keep in mind, I got most of this code from the forum tutorial on the OpenGL forum, with two main differences. My version uses STL vectors, and instead of reloading the file when I need to go to the beginning, I just move the input pointer to the top of the file.

Obj_loader.h:

// .obj model file format loader
// Programmed by Joseph Cortes

#ifndef OBJ_MODEL_LOADER
#define OBJ_MODEL_LOADER

// Using file stream and STL
#include
#include
#include
#include

// Using std namespace

using namespace std;





// Structure containing vertex data
struct Vertex
{

float x, y, z;

};

// Structure containing texture data
struct TexCoord
{

float u, v;

};

// Typedef vertex structure to normal structure
typedef Vertex Normal;


// OBJ_MODEL class
class OBJ_MODEL
{

public:

// Model data. Contained in STL vectors
// because we don't know the exact amounts
// of data.
vector Vertices;
vector TexCoords;
vector Normals;

// Method loads a model.
bool m_Load(string filename);

};



#endif






Obj_loader.cpp:

#include "Obj_loader.h"

// Method which loads a .obj file given a std::string
bool OBJ_MODEL::m_Load(string filename)
{

// Get input string and load .obj file
ifstream in_file(filename.c_str());
// buffer holding file input
string buffer;

// Check that file input stream is valid
if(!in_file.is_open())
return false;

// Read in vertex data
while(!in_file.eof())
{
// Read in next line of input
getline(in_file, buffer);
istringstream line(buffer);
string temp, f1, f2, f3;

// Check that new line contains vertex data
if(buffer.substr(0, 2) == "v")
{

// Read line
line >> temp >> f1 >> f2 >> f3;

// Create a new Vertex and fill it with input data
Vertex temp_v;

temp_v.x = atof(f1.c_str());
temp_v.y = atof(f2.c_str());
temp_v.z = atof(f3.c_str());

// Push new vertex onto Vertex vector
Vertices.push_back(temp_v);

}

}

// Go back to start of file
in_file.seekg(ios::beg);

// Clear Input stream
in_file.clear();

// Read in Texture Coordinates
while(!in_file.eof())
{

// Read in next line of input
getline(in_file, buffer);
istringstream line(buffer);
string temp, f1, f2;

// Check to see that texture data is being read
if(buffer.substr(0, 2) == "vt")
{

// Read line
line >> temp >> f1 >> f2;

// Make new texture coordinate
TexCoord temp_tc;

temp_tc.u = atof(f1.c_str());
temp_tc.v = atof(f2.c_str());

// Push texture coordinate into vector
TexCoords.push_back(temp_tc);

}

}

// Once again, go back to the beginning
in_file.seekg(ios::beg);

in_file.clear();

// Finally, read in Normals
while(!in_file.eof())
{

// Read in next line of input
getline(in_file, buffer);
istringstream line(buffer);
string temp, f1, f2, f3;

// Check to see if Normals are being read
if(buffer.substr(0, 2) == "vn")
{

// Read line
line >> temp >> f1 >> f2 >> f3;

// Make new Normal
Normal temp_n;

temp_n.x = atof(f1.c_str());
temp_n.y = atof(f2.c_str());
temp_n.z = atof(f3.c_str());

// Add new Normal to Normal vector
Normals.push_back(temp_n);

}

}

// All reading is done, so close the file
in_file.close();

}






Then, I made a little program to take the data from a model, and write it to a text file called "log.txt". Here is the code for that program


// Test program for .obj loader
// Programmed by Joseph Cortes

#include
#include
#include
#include "Obj_loader.h"

using namespace std;

int main()
{

OBJ_MODEL *model;
model = new OBJ_MODEL;
char some_char;
ofstream out_file("log.txt");

try
{
if(!model->m_Load("rock.OBJ"))
throw "Error loading model";
}
catch(string error)
{
out_file << error;

return 0;
}

out_file << "Data from rock.obj" << endl;
out_file << endl;
out_file << "Vertex data" << endl;

for(vector::iterator cur_v = model->Vertices.begin(); cur_v != model->Vertices.end(); cur_v++)
out_file << "X: " << cur_v->x << "Y: " << cur_v->y << "Z: " << cur_v->z << endl;

out_file << endl;
out_file << "Texture Coordinates" << endl;

for(vector::iterator cur_tc = model->TexCoords.begin(); cur_tc != model->TexCoords.end(); cur_tc++)
out_file << "U: " << cur_tc->u << "V: " << cur_tc->v << endl;

out_file << endl;
out_file << "Normals" << endl;

for(vector::iterator cur_n = model->Normals.begin(); cur_n != model->Normals.end(); cur_n++)
out_file << "X: " << cur_n->x << "Y: " << cur_n->y << "Z: " << cur_n->z << endl;

out_file << endl;
out_file << "End of file" << endl;

delete [] model;


return 0;

}






The program compiles fine, but dosen't even write anything to the file. Any ideas?
Sign in to follow this  


2 Comments


Recommended Comments

remember you must .close(), the file stream is being left open.

out_file.close();

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!