I try to load the obj file to the opengl but the system crash, I tested My code in console system:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <ostream>
#include <algorithm>
#include <Windows.h>
using namespace std;
class vertex
{public:
float f1;
float f2;
float f3;
vertex(float F1,float F2, float F3):f1(F1),f2(F2),f3(F3){};
};//the vertex
class face{
public:
int a;
int b;
int c;
int d;
bool four;
face(int x,int y,int z):a(x),b(y),c(z){four =false;};
face(int x,int y,int z,int k):a(x),b(y),c(z),d(k){four =true;};
};
vector<vertex>v;
vector<face>f;
void loadvertex(char*filename){
ifstream infile(filename);
ofstream outfile("out.txt");
float f1,f2,f3;
char buffer;
if(!infile.is_open())
{return;
}
while(infile>>buffer>>f1>>f2>>f3){
if(buffer=='v')
{outfile<<f1<<" "<<f2<<" "<<f3<<endl;
cout<<f1<<" "<<f2<<" "<<f3<<endl;
v.push_back(vertex(f1,f2,f3));}
}
infile.close();
outfile.close();
}
void loadface(char*filename){
ifstream infile(filename);
ofstream outfile("out.txt");
int f1,f2,f3;
char buffer;
if(!infile.is_open())
{return;
}
while(infile>>buffer>>f1>>f2>>f3){
if(buffer=='f'){
outfile<<f1<<" "<<f2<<" "<<f3<<endl;
cout<<f1<<" "<<f2<<" "<<f3<<endl;
f.push_back( face(f1,f2,f3));
}
}
infile.close();
outfile.close();
}
void Loadobj(char*filename)
{
string line;
ifstream objfile(filename);
if(objfile.is_open())
{
while(!objfile.eof())
{ getline(objfile,line);
if(line.c_str()[0]=='v')
{ float tmpx,tmpy,tmpz;
sscanf(line.c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
v.push_back(vertex(tmpx,tmpy,tmpz));
}
if(line.c_str()[0]=='f')
{ int a,b,c,d;
sscanf(line.c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
f.push_back(face(a,c,d));
}
}
}
}
int main(){
Loadobj("cube.txt");
for(int i=0;i<f.size();i++)
{
float F1=v[f[i].a-1].f1;
float F2=v[f[i].a-1].f2;
float F3=v[f[i].a-1].f3;
cout<<F1<<"--"<<F2<<"--"<<F3<<endl;
}
system("pause");
return 0;
}
the obj file: