Am trying to create a obj file parser, so far the parser reads a obj file and stores all the vertices into a struct named coordinate.the problem and having is, storing the coordinates into an array after the struct.
my parser:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
void ObjParser(const char* szFileName)
{
ifstream myfile; //use all the method of the ifstream
std::vector<std::string*>coord;
std::vector<coordinate*> vertex;
myfile.open(szFileName);
if(myfile.is_open())
{
//The file is open,use while loop to check file
char buf[256]; //create buffer to read line
while(!myfile.eof())//while we are not at the end of the file.
{
myfile.getline(buf,256);
coord.push_back(new std::string(buf));
}
for(int i = 0; i <coord.size(); i++)
{
//check if it's a comment or not
if(coord[i]->c_str()[0]=='#')
continue;
else if(coord[i]->c_str()[0]=='v' && coord[i]->c_str()[1]==' ')
{
char tmp;
float tmp_x,tmp_y,tmp_z;
sscanf_s(coord[i]->c_str(),"v %f %f %f",&tmp_x,&tmp_y,&tmp_z); //read in the 3
vertex.push_back(new coordinate(tmp_x,tmp_y,tmp_z)); //and then add it to the end of our vertex list
}
}
//Delete coord to avoid memory leaks
for(int i = 0; i < coord.size();i++)
delete coord[i];
}
else{
//Display error message, cause the file connot be opened
MessageBox(NULL, "Error occured while opening file", NULL, NULL);
}
//once complete close file.
myfile.close();
}
|
my struct
1 2 3 4 5
|
struct coordinate{
float x,y,z;
coordinate(float a, float b,float c) : x(a),y(b),z(c) {};
};
|
I wanna be able to store the array into my geometry pipeline and create a wireframe of it.
for example at the moment my vertices are had coded like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void wireframe(HDC hdc){
//vertices coordinates:
const int verticescount = 8;
Vertex vertices[verticescount] = {Vertex(-5.0f, -5.0f, 5.0f, 1.0f),
Vertex(5.0f, -5.0f, 5.0f, 1.0f),
Vertex(5.0f, 5.0f, 5.0f, 1.0f),
Vertex(-5.0f, 5.0f, 5.0f, 1.0f),
Vertex(-5.0f, -5.0f, -5.0f, 1.0f),
Vertex(5.0f, -5.0f, -5.0f, 1.0f),
Vertex(5.0f, 5.0f, -5.0f, 1.0f),
Vertex(-5.0f, 5.0f, -5.0f, 1.0f)};
}
|
|