asked on
#include "quad_obj.h"
#include <GL/gl.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<cctype>
#include<string>
#include<cstdlib>
using namespace std;
typedef vector<GLdouble> coords;
typedef vector<coords> points;
typedef vector<vector<int> > faces;
/**************************************************************************
0. Auxiliary Functions (undeclared in header obj.h)
**************************************************************************/
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
bool slash(char c)
{
return c=='/';
}
bool not_slash(char c)
{
return !(c=='/');
}
/**************************************************************************
1. quad_obj Class Function definitions
**************************************************************************/
//Reads a wavefront .obj file into a format that is easily drawn using GL vert array operations.
void quad_obj::read(const char* filename)
{
//set the input filestream to the appropriate file.
ifstream in(filename);
points vert_list;
points norm_list;
faces face_list;
//first read enumerated lists of vertices, norms and faces.
while (in)
{
string line, prefix;
getline(in, line);
string::iterator i=line.begin();
string::iterator j=find_if(i, line.end(), space);
copy(i, j, back_inserter(prefix));
i=find_if(j,line.end(),not_space);
if(prefix=="v")
{
//store data in vert_list
coords p;
while (i!=line.end())
{
string sub;
j=find_if(i, line.end(), space);
copy(i, j, back_inserter(sub));
GLdouble d = atof(sub.c_str());
p.push_back(d);
i=find_if(j,line.end(), not_space);
}
vert_list.push_back(p);
}
else if (prefix=="vn")
{
//store data in norm_list
coords p;
while (i!=line.end())
{
string sub;
j=find_if(i, line.end(), space);
copy(i, j, back_inserter(sub));
GLdouble d = atof(sub.c_str());
p.push_back(d);
i=find_if(j,line.end(), not_space);
}
norm_list.push_back(p);
}
else if (prefix=="f")
{
//store data in face_list
while(i!=line.end())
{
vector<int> f;
string sub1, sub2;
j=find_if(i,line.end(),slash);
copy(i,j,back_inserter(sub1));
f.push_back(atoi(sub1.c_str()));
i=find_if(j,line.end(),not_slash);
j=find_if(i,line.end(),space);
copy(i,j,back_inserter(sub2));
f.push_back(atoi(sub2.c_str()));
face_list.push_back(f);
i=find_if(j,line.end(),not_space);
}
}
else;
}
//use vert_list, norm_list and face_list to order verts and norms
for (faces::size_type i=0; i!=face_list.size(); ++i)
cout<<vert_list[face_list[i][0]][0];
}