예제 #1
0
int ObjLoader::load(const char* filename)
{
        std::ifstream in(filename);     //open the model file
        if(!in.is_open())
        {
                std::cout << "Not opened" << std::endl; //if it's not opened then error message, and return with -1
                return -1;
        }
        char buf[256];  //temp buffer
        int curmat=0;   //the current (default) material is 0, it's used, when we read the faces
        while(!in.eof())
        {
                in.getline(buf,256);    //while we are not in the end of the file, read everything as a string to the coord vector
                coord.push_back(new std::string(buf));
        }
        for(int i=0;i<coord.size();i++) //and then go through all line and decide what kind of line it is
        {
                if((*coord[i])[0]=='#') //if it's a comment
                        continue;       //we don't have to do anything with it
                else if((*coord[i])[0]=='v' && (*coord[i])[1]==' ')     //if a vertex
                {
                        float tmpx,tmpy,tmpz;
                        sscanf_s(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);       //read the 3 floats, which makes up the vertex
                        vertex.push_back(new coordinate(tmpx,tmpy,tmpz));       //and put it in the vertex vector
                }else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n')    //if it's a normal vector
                {
                        float tmpx,tmpy,tmpz;
                        sscanf_s(coord[i]->c_str(),"vn %f %f %f",&tmpx,&tmpy,&tmpz);
                        normals.push_back(new coordinate(tmpx,tmpy,tmpz));      //basically do the same
                        isnormals=true;
                }else if((*coord[i])[0]=='f')   //if it's a face
                {
                        int a,b,c,d,e;
                        if(count(coord[i]->begin(),coord[i]->end(),' ')==4)     //if this is a quad
                        {
                                if(coord[i]->find("//")!=std::string::npos)     //if it's contain a normal vector, but not contain texture coorinate
                                {
                                        sscanf_s(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);      //read in this form
                                        faces.push_back(new face(b,a,c,d,e,0,0,0,0,curmat));    //and put to the faces, we don't care about the texture coorinate in this case
                                                                                                                                                                                                                                                                //and if there is no material, it doesn't matter, what is curmat
                                }else if(coord[i]->find("/")!=std::string::npos)        //if we have texture coorinate and normal vectors
                                {
                                        int t[4];       //texture coorinates
                                        //read in this form, and put to the end of the vector
                                        sscanf_s(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b,&e,&t[3],&b);
                                        faces.push_back(new face(b,a,c,d,e,t[0],t[1],t[2],t[3],curmat));
                                }else{
                                        //else we don't have normal vectors nor texture coorinate
                                        sscanf_s(coord[i]->c_str(),"f %d %d %d %d",&a,&b,&c,&d);
                                        faces.push_back(new face(-1,a,b,c,d,0,0,0,0,curmat));          
                                }
                        }else{  //if it's a triangle
                                                        //do the same, except we use one less vertex/texture coorinate/face number
                                        if(coord[i]->find("//")!=std::string::npos)
                                        {
                                                sscanf_s(coord[i]->c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
                                                faces.push_back(new face(b,a,c,d,0,0,0,curmat));
                                        }else if(coord[i]->find("/")!=std::string::npos)
                                        {
                                                int t[3];
                                                sscanf_s(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b);
                                                faces.push_back(new face(b,a,c,d,t[0],t[1],t[2],curmat));
                                        }else{
                                                sscanf_s(coord[i]->c_str(),"f %d %d %d",&a,&b,&c);
                                                faces.push_back(new face(-1,a,b,c,0,0,0,curmat));                                      
                                        }
                        }
        }else if((*coord[i])[0]=='u' && (*coord[i])[1]=='s' && (*coord[i])[2]=='e')     //use material material_name
        {
                char tmp[200];
                sscanf_s(coord[i]->c_str(),"usemtl %s",tmp);      //read the name of the material to tmp
                for(int i=0;i<materials.size();i++)     //go through all of the materials
                {
                        if(strcmp(materials[i]->name.c_str(),tmp)==0)   //and compare the tmp with the name of the material
                        {
                                curmat=i;       //if it's equal then set the current material to that
                                break;
                        }
                }
        }else if((*coord[i])[0]=='m' && (*coord[i])[1]=='t' && (*coord[i])[2]=='l' && (*coord[i])[3]=='l')      //material library, a file, which contain
                                                                                                                                                                                                                                                                                                                                                                                                                        //all of the materials
        {
                char filen[200];
                sscanf_s(coord[i]->c_str(),"mtllib %s",filen);    //read the filename
                std::ifstream mtlin(filen);     //open the file
                if(!mtlin.is_open())    //if not opened error message, clean all memory, return with -1
                {
                        std::cout << "cannot open the material file" << std::endl;
                        clean();
                        return -1;
                }
                ismaterial=true;        //we use materials
                std::vector<std::string> tmp;//contain all of the line of the file
                char c[200];
                while(!mtlin.eof())
                {
                        mtlin.getline(c,200);   //read all lines to tmp
                        tmp.push_back(c);
                }
                char name[200]; //name of the material
                char filename[200];     //filename of the texture
                float amb[3],dif[3],spec[3],alpha,ns,ni;        //colors, shininess, and something else
                int illum;
                unsigned int texture;
                bool ismat=false;       //do we already have a material read in to these variables?
                strcpy_s(filename,"\0");  //set filename to nullbyte character
                for(int i=0;i<tmp.size();i++) //go through all lines of the mtllib file
                {
                        if(tmp[i][0]=='#')      //we don't care about comments
                                continue;
                        if(tmp[i][0]=='n' && tmp[i][1]=='e' && tmp[i][2]=='w')  //new material
                        {
                                if(ismat)       //if we have a material
                                {
                                        if(strcmp(filename,"\0")!=0)    //if we have a texture
                                        {
                                                materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture)); //push back
                                                strcpy_s(filename,"\0");
                                        }else{
                                                        materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));              //push back, but use -1 to texture             
                                        }
                                }
                                ismat=false;    //we start from a fresh material
                                sscanf_s(tmp[i].c_str(),"newmtl %s",name);        //read in the name
                        }else if(tmp[i][0]=='N' && tmp[i][1]=='s')      //the shininess
                        {
                                sscanf_s(tmp[i].c_str(),"Ns %f",&ns);
                                ismat=true;
                        }else if(tmp[i][0]=='K' && tmp[i][1]=='a')      //the ambient
                        {
                                sscanf_s(tmp[i].c_str(),"Ka %f %f %f",&amb[0],&amb[1],&amb[2]);
                                ismat=true;
                        }else if(tmp[i][0]=='K' && tmp[i][1]=='d')      //the diffuse
                        {
                                sscanf_s(tmp[i].c_str(),"Kd %f %f %f",&dif[0],&dif[1],&dif[2]);
                                ismat=true;
                        }else if(tmp[i][0]=='K' && tmp[i][1]=='s')      //the specular
                        {
                                sscanf_s(tmp[i].c_str(),"Ks %f %f %f",&spec[0],&spec[1],&spec[2]);
                                ismat=true;
                        }else if(tmp[i][0]=='N' && tmp[i][1]=='i')      //the I don't know what is this
                        {
                                sscanf_s(tmp[i].c_str(),"Ni %f",&ni);
                                ismat=true;
                        }else if(tmp[i][0]=='d' && tmp[i][1]==' ')      //the alpha
                        {
                                sscanf_s(tmp[i].c_str(),"d %f",&alpha);
                                ismat=true;
                        }else if(tmp[i][0]=='i' && tmp[i][1]=='l')      //the illum (don't ask)
                        {
                                sscanf_s(tmp[i].c_str(),"illum %d",&illum);
                                ismat=true;
                        }else if(tmp[i][0]=='m' && tmp[i][1]=='a')      //and the texture
                        {
							
                                sscanf_s(tmp[i].c_str(),"map_Kd %s",filename);
                                texture=loadTexture(filename);  //read the filename, and use the loadTexture function to load it, and get the id.
                                ismat=true;
                        }
                }
                if(ismat)       //there is no newmat after the last newmat, so we have to put the last material 'manually'
                {
                        if(strcmp(filename,"\0")!=0)
                        {
                                materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture));
                        }else{
                                        materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));                             
                        }
                }
        }else if((*coord[i])[0]=='v' && (*coord[i])[1]=='t')    //back to the obj file, texture coorinate
        {
                float u,v;
                sscanf_s(coord[i]->c_str(),"vt %f %f",&u,&v);     //read the uv coordinate
                texturecoordinate.push_back(new texcoord(u,1-v));       //I push back 1-v instead of normal v, because obj file use the upper left corner as 0,0 coorinate
                //but OpenGL use bottom left corner as 0,0, so I convert it
                istexture=true;
        }
}
        if(materials.size()==0) //if some reason the material file doesn't contain any material, we don't have material
                ismaterial=false;
        else    //else we have
                ismaterial=true;
        std::cout << vertex.size() << " " << normals.size() << " " << faces.size() << " " << materials.size() << std::endl;     //test purposes
        //draw
        int num;
        num=glGenLists(1);      //I generate a unique identifier for the list
        glNewList(num,GL_COMPILE);
        int last=-1;    //the last material (default -1, which doesn't exist, so we use the first material)
        for(int i=0;i<faces.size();i++) //go throught all faces
        {
                if(last!=faces[i]->mat && ismaterial)   //if we have a meterial AND the last material is not the same
                {
                        //set all of the material property
                        float diffuse[]={materials[faces[i]->mat]->dif[0],materials[faces[i]->mat]->dif[1],materials[faces[i]->mat]->dif[2],1.0};
                        float ambient[]={materials[faces[i]->mat]->amb[0],materials[faces[i]->mat]->amb[1],materials[faces[i]->mat]->amb[2],1.0};
                        float specular[]={materials[faces[i]->mat]->spec[0],materials[faces[i]->mat]->spec[1],materials[faces[i]->mat]->spec[2],1.0};
                        glMaterialfv(GL_FRONT,GL_AMBIENT,diffuse);
                        glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
                        glMaterialfv(GL_FRONT,GL_AMBIENT,specular);
						glColor3f(diffuse[0], diffuse[1], diffuse[2]);
                        glMaterialf(GL_FRONT,GL_SHININESS,materials[faces[i]->mat]->ns);
                        last=faces[i]->mat;     //set the current to last
                        if(materials[faces[i]->mat]->texture==-1)       //if we don't have texture, disable it, else enable it
                                glDisable(GL_TEXTURE_2D);
                        else{
                                glEnable(GL_TEXTURE_2D);
                                glBindTexture(GL_TEXTURE_2D,materials[faces[i]->mat]->texture); //and use it
                        }
                }
                if(faces[i]->four)      //if quad
                {
                        glBegin(GL_QUADS);
                                if(isnormals)   //if there are normals
                                        glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);    //use them
                                       
                                if(istexture && materials[faces[i]->mat]->texture!=-1)  //if there are textures
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);      //set the texture coorinate
                               
                                glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
                               
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);
                               
                                glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
                               
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);
                               
                                glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
                               
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[3]-1]->u,texturecoordinate[faces[i]->texcoord[3]-1]->v);
                               
                                glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
                        glEnd();
                }else{
                        glBegin(GL_TRIANGLES);
                                if(isnormals)   //if there are normals
                                        glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
 
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);
 
 
                                glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
                               
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);
                               
                                glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
                               
                               
                                if(istexture && materials[faces[i]->mat]->texture!=-1)
                                        glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);
                               
                                glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
                        glEnd();
                }
        }
        glEndList();
        clean();
        lists.push_back(num);
        return num;
}
예제 #2
0
int objloader::load(const char* filename)
{
	std::ifstream in(filename);
	if(!in.is_open())
	{
		std::cout << "Nor oepened" << std::endl;
		return -1;
	}
	char buf[256];
	int curmat=0;
	while(!in.eof())
	{
		in.getline(buf,256);
		coord.push_back(new std::string(buf));
	}
	for(int i=0;i<coord.size();i++)
	{
		if((*coord[i])[0]=='#')
			continue;
		else if((*coord[i])[0]=='v' && (*coord[i])[1]==' ')
		{
			float tmpx,tmpy,tmpz;
			sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
			vertex.push_back(new coordinate(tmpx,tmpy,tmpz));
		}else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n')
		{
			float tmpx,tmpy,tmpz;
			sscanf(coord[i]->c_str(),"vn %f %f %f",&tmpx,&tmpy,&tmpz);
			normals.push_back(new coordinate(tmpx,tmpy,tmpz));	
		}else if((*coord[i])[0]=='f')
		{
			int a,b,c,d,e;
			if(count(coord[i]->begin(),coord[i]->end(),' ')==4)
			{
				if(coord[i]->find("//")!=std::string::npos)
				{
					sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
					faces.push_back(new face(b,a,c,d,e,0,0,0,0,curmat));
				}else if(coord[i]->find("/")!=std::string::npos)
				{
					int t[4];
					sscanf(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b,&e,&t[3],&b);
					faces.push_back(new face(b,a,c,d,e,t[0],t[1],t[2],t[3],curmat));
				}else{
					sscanf(coord[i]->c_str(),"f %d %d %d %d",&a,&b,&c,&d);
					faces.push_back(new face(-1,a,b,c,d,0,0,0,0,curmat));					
				}
			}else{
					if(coord[i]->find("//")!=std::string::npos)
					{
						sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
						faces.push_back(new face(b,a,c,d,0,0,0,curmat));
					}else if(coord[i]->find("/")!=std::string::npos)
					{
						int t[3];
						sscanf(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b);
						faces.push_back(new face(b,a,c,d,t[0],t[1],t[2],curmat));
					}else{
						sscanf(coord[i]->c_str(),"f %d %d %d",&a,&b,&c);
						faces.push_back(new face(-1,a,b,c,0,0,0,curmat));					
					}
			}
	}else if((*coord[i])[0]=='u' && (*coord[i])[1]=='s' && (*coord[i])[2]=='e')
	{
		char tmp[200];
		sscanf(coord[i]->c_str(),"usemtl %s",tmp);
		for(int i=0;i<materials.size();i++)
		{
			if(strcmp(materials[i]->name.c_str(),tmp)==0)
			{
				curmat=i;
				break;
			}
		}
	}else if((*coord[i])[0]=='m' && (*coord[i])[1]=='t' && (*coord[i])[2]=='l' && (*coord[i])[3]=='l')
	{
		char filen[200];
		sscanf(coord[i]->c_str(),"mtllib %s",filen);
		std::ifstream mtlin(filen);
		if(!mtlin.is_open())
		{
			std::cout << "connot open the material file" << std::endl;
			clean();
			return -1;
		}
		ismaterial=true;
		std::vector<std::string> tmp;
		char c[200];
		while(!mtlin.eof())
		{
			mtlin.getline(c,200);
			tmp.push_back(c);
		}
		char name[200];
		char filename[200];
		float amb[3],dif[3],spec[3],alpha,ns,ni;
		int illum;
		unsigned int texture;
		bool ismat=false;
		strcpy(filename,"\0");
		std::cout << tmp.size() << std::endl;
		for(int i=0;i<tmp.size();i++)
		{
			if(tmp[i][0]=='#')
				continue;
			if(tmp[i][0]=='n' && tmp[i][1]=='e' && tmp[i][2]=='w')
			{
				if(ismat)
				{
					if(strcmp(filename,"\0")!=0)
					{
						materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture));
						strcpy(filename,"\0");
					}else{
							materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));				
					}
				}
				ismat=false;
				sscanf(tmp[i].c_str(),"newmtl %s",name);
			}else if(tmp[i][0]=='N' && tmp[i][1]=='s')
			{
				sscanf(tmp[i].c_str(),"Ns %f",&ns);
				ismat=true;
			}else if(tmp[i][0]=='K' && tmp[i][1]=='a')
			{
				sscanf(tmp[i].c_str(),"Ka %f %f %f",&amb[0],&amb[1],&amb[2]);
				ismat=true;
			}else if(tmp[i][0]=='K' && tmp[i][1]=='d')
			{
				sscanf(tmp[i].c_str(),"Kd %f %f %f",&dif[0],&dif[1],&dif[2]);
				ismat=true;
			}else if(tmp[i][0]=='K' && tmp[i][1]=='s')
			{
				sscanf(tmp[i].c_str(),"Ks %f %f %f",&spec[0],&spec[1],&spec[2]);
				ismat=true;
			}else if(tmp[i][0]=='N' && tmp[i][1]=='i')
			{
				sscanf(tmp[i].c_str(),"Ni %f",&ni);
				ismat=true;
			}else if(tmp[i][0]=='d' && tmp[i][1]==' ')
			{
				sscanf(tmp[i].c_str(),"d %f",&alpha);
				ismat=true;
			}else if(tmp[i][0]=='i' && tmp[i][1]=='l')
			{
				sscanf(tmp[i].c_str(),"illum %d",&illum);
				ismat=true;
			}else if(tmp[i][0]=='m' && tmp[i][1]=='a')
			{
				sscanf(tmp[i].c_str(),"map_Kd %s",filename);
				texture=loadTexture(filename);
				ismat=true;
			}
		}
				if(ismat)
				{
					if(strcmp(filename,"\0")!=0)
					{
						materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture));
					}else{
							materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));				
					}
				}
	}else if((*coord[i])[0]=='v' && (*coord[i])[1]=='t')
	{
		float u,v;
		sscanf(coord[i]->c_str(),"vt %f %f",&u,&v);
		texturecoordinate.push_back(new texcoord(u,1-v));
		istexture=true;
	}
}
	if(materials.size()==0)
		ismaterial=false;
	else
		ismaterial=true;
	std::cout << vertex.size() << " " << normals.size() << " " << faces.size() << " " << materials.size() << std::endl; 
	//draw
	if(isvertexnormal)
		smoothnormals();
	int num;
	num=glGenLists(1);
	glNewList(num,GL_COMPILE);
	int last=-1;
	for(int i=0;i<faces.size();i++)
	{
		if(last!=faces[i]->mat && ismaterial)
		{
			float diffuse[]={materials[faces[i]->mat]->dif[0],materials[faces[i]->mat]->dif[1],materials[faces[i]->mat]->dif[2],1.0};
			float ambient[]={materials[faces[i]->mat]->amb[0],materials[faces[i]->mat]->amb[1],materials[faces[i]->mat]->amb[2],1.0};
			float specular[]={materials[faces[i]->mat]->spec[0],materials[faces[i]->mat]->spec[1],materials[faces[i]->mat]->spec[2],1.0};
			glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
			glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
			glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
			glMaterialf(GL_FRONT,GL_SHININESS,materials[faces[i]->mat]->ns);
			last=faces[i]->mat;
			if(materials[faces[i]->mat]->texture==-1)
				glDisable(GL_TEXTURE_2D);
			else{
				glEnable(GL_TEXTURE_2D);
				glBindTexture(GL_TEXTURE_2D,materials[faces[i]->mat]->texture);
			}
		}
		if(faces[i]->four)
		{
			glBegin(GL_QUADS);
				glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);

				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);

				
				glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);


				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);
				
				glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);

				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[2]-1]->x,vertexnormals[faces[i]->faces[2]-1]->y,vertexnormals[faces[i]->faces[2]-1]->z);

				glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[3]-1]->u,texturecoordinate[faces[i]->texcoord[3]-1]->v);

				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[3]-1]->x,vertexnormals[faces[i]->faces[3]-1]->y,vertexnormals[faces[i]->faces[3]-1]->z);
			
				glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
			glEnd();
		}else{
			glBegin(GL_TRIANGLES);
				glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);

				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);

				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);


				glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);
				
				
				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);
				
				glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
				
				
				if(istexture && materials[faces[i]->mat]->texture!=-1)
					glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);


				if(isvertexnormal)
					glNormal3f(vertexnormals[faces[i]->faces[2]-1]->x,vertexnormals[faces[i]->faces[2]-1]->y,vertexnormals[faces[i]->faces[2]-1]->z);
		
				glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
			glEnd();
		}
	}
	glEndList();
	clean();
	lists.push_back(num);
	return num;
}
예제 #3
0
int objloader::load(const std::string& filename,std::vector<collisionplane>* collplane)
{
    ismaterial=false;
    isnormals=false;
    istexture=false;
    isvertexnormal=false;
    std::ifstream in(filename.c_str());
    if(!in.is_open())
    {
        out << "Not opened file " << filename << std::endl;
        return -1;
    }
    std::string path=filename.substr(0,((filename.find_last_of('/')+1!=std::string::npos) ? (filename.find_last_of('/')+1):0));
    out << filename << std::endl;
    char buf[256];
    int curmat=0;
    bool coll=false;
    int z=0;
    int h=-1;
    while(!in.eof())
    {
        in.getline(buf,256);
        coord.push_back(new std::string(buf));
    }
    for(int i=0; i<coord.size(); i++)
    {
        if((*coord[i])[0]=='#')
            continue;
        else if((*coord[i])[0]=='v' && (*coord[i])[1]==' ')
        {
            float tmpx,tmpy,tmpz;
            sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
            vertex.push_back(new coordinate(tmpx,tmpy,tmpz));
            out << "v " << tmpx << " " << tmpy << " " << tmpz << std::endl;
        }
        else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n')
        {
            float tmpx,tmpy,tmpz;
            sscanf(coord[i]->c_str(),"vn %f %f %f",&tmpx,&tmpy,&tmpz);
            normals.push_back(new coordinate(tmpx,tmpy,tmpz));
            out << "vn " << tmpx << " " << tmpy << " " << tmpz << std::endl;
        }
        else if((*coord[i])[0]=='f')
        {
            int a,b,c,d,e;
            if(coll && collplane!=NULL)
            {
                sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
                collplane->push_back(collisionplane(normals[b-1]->x,normals[b-1]->y,normals[b-1]->z,vertex[a-1]->x,vertex[a-1]->y,vertex[a-1]->z,vertex[c-1]->x,vertex[c-1]->y,vertex[c-1]->z,vertex[d-1]->x,vertex[d-1]->y,vertex[d-1]->z,vertex[e-1]->x,vertex[e-1]->y,vertex[e-1]->z));
            }
            else
            {
                if(count(coord[i]->begin(),coord[i]->end(),' ')==4)
                {
                    if(coord[i]->find("//")!=std::string::npos)
                    {
                        sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
                        faces.push_back(new face(b,a,c,d,e,0,0,0,0,curmat));
                    }
                    else if(coord[i]->find("/")!=std::string::npos)
                    {
                        int t[4];
                        sscanf(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b,&e,&t[3],&b);
                        out << t[0] << " " <<t[1] << " " <<t[2] << " " <<t[3] << " " << a << " " << b << " " << c << " " << d << " " << e << std::endl;
                        faces.push_back(new face(b,a,c,d,e,t[0],t[1],t[2],t[3],curmat));
                    }
                    else
                    {
                        sscanf(coord[i]->c_str(),"f %d %d %d %d",&a,&b,&c,&d);
                        faces.push_back(new face(-1,a,b,c,d,0,0,0,0,curmat));
                    }
                }
                else
                {
                    if(coord[i]->find("//")!=std::string::npos)
                    {
                        sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
                        faces.push_back(new face(b,a,c,d,0,0,0,curmat));
                    }
                    else if(coord[i]->find("/")!=std::string::npos)
                    {
                        int t[3];
                        sscanf(coord[i]->c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",&a,&t[0],&b,&c,&t[1],&b,&d,&t[2],&b);
                        faces.push_back(new face(b,a,c,d,t[0],t[1],t[2],curmat));
                    }
                    else
                    {
                        sscanf(coord[i]->c_str(),"f %d %d %d",&a,&b,&c);
                        faces.push_back(new face(-1,a,b,c,0,0,0,curmat));
                    }
                }
            }
        }
        else if((*coord[i])[0]=='u' && (*coord[i])[1]=='s' && (*coord[i])[2]=='e')
        {
            char tmp[200];
            sscanf(coord[i]->c_str(),"usemtl %s",tmp);
            if(strcmp(tmp,"collision")==0)
            {
                coll=true;
            }
            else
            {
                coll=false;
                for(int i=0; i<materials.size(); i++)
                {
                    if(strcmp(materials[i]->name.c_str(),tmp)==0)
                    {
                        curmat=i;
                        out << "curmat=" << i  << std::endl;
                        break;
                    }
                }
            }
        }
        else if((*coord[i])[0]=='m' && (*coord[i])[1]=='t' && (*coord[i])[2]=='l' && (*coord[i])[3]=='l')
        {
            char filen[200];
            sscanf(coord[i]->c_str(),"mtllib %s",filen);
            std::string filen2=path+filen;
            std::ifstream mtlin(filen2.c_str());
            if(!mtlin.is_open())
            {
                out << "connot open the material file " << filen2 << std::endl;
                clean();
                return -1;
            }
            ismaterial=true;
            std::vector<std::string> tmp;
            char c[200];
            while(!mtlin.eof())
            {
                mtlin.getline(c,200);
                tmp.push_back(c);
            }
            char name[200];
            char filename[200];
            float amb[3],dif[3],spec[3],alpha,ns,ni;
            int illum;
            unsigned int texture=0;
            bool ismat=false;
            strcpy(filename,"\0");
//		std::cout << tmp.size() << std::endl;
            for(int i=0; i<tmp.size(); i++)
            {
                if(tmp[i][0]=='#')
                    continue;
                if(tmp[i][0]=='n' && tmp[i][1]=='e' && tmp[i][2]=='w')
                {
                    if(ismat)
                    {
                        if(strcmp(filename,"\0")!=0 && strcmp(filename,"collision")!=0)
                        {
                            materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture));
                            strcpy(filename,"\0");
                        }
                        else
                        {
                            materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));
                        }
                    }
                    ismat=false;
                    sscanf(tmp[i].c_str(),"newmtl %s",name);
                    out << "newmtl " << name << std::endl;
                }
                else if(tmp[i][0]=='N' && tmp[i][1]=='s')
                {
                    sscanf(tmp[i].c_str(),"Ns %f",&ns);
                    ismat=true;
                }
                else if(tmp[i][0]=='K' && tmp[i][1]=='a')
                {
                    sscanf(tmp[i].c_str(),"Ka %f %f %f",&amb[0],&amb[1],&amb[2]);
                    ismat=true;
                }
                else if(tmp[i][0]=='K' && tmp[i][1]=='d')
                {
                    sscanf(tmp[i].c_str(),"Kd %f %f %f",&dif[0],&dif[1],&dif[2]);
                    ismat=true;
                }
                else if(tmp[i][0]=='K' && tmp[i][1]=='s')
                {
                    sscanf(tmp[i].c_str(),"Ks %f %f %f",&spec[0],&spec[1],&spec[2]);
                    ismat=true;
                }
                else if(tmp[i][0]=='N' && tmp[i][1]=='i')
                {
                    sscanf(tmp[i].c_str(),"Ni %f",&ni);
                    ismat=true;
                }
                else if(tmp[i][0]=='d' && tmp[i][1]==' ')
                {
                    sscanf(tmp[i].c_str(),"d %f",&alpha);
                    ismat=true;
                }
                else if(tmp[i][0]=='i' && tmp[i][1]=='l')
                {
                    sscanf(tmp[i].c_str(),"illum %d",&illum);
                    ismat=true;
                }
                else if(tmp[i][0]=='m' && tmp[i][1]=='a')
                {
                    sscanf(tmp[i].c_str(),"map_Kd %s",filename);
                    bool l=0;
                    out << "Opening image: " << filename << std::endl;
                    std::string filename2=path+filename;
                    for(int i=0; i<loadedTextures.size(); i++)
                        if(loadedTextures[i]==filename2)
                        {
                            out << "loading "  << filename2 << std::endl;
                            texture=loadedTexturesNum[i];
                            l=1;
                            break;
                        }
                    if(l==0)
                        texture=loadTexture(filename2.c_str());
                    ismat=true;
                }
            }
            if(ismat)
            {
                if(strcmp(filename,"\0")!=0)
                {
                    materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,texture));
                }
                else
                {
                    materials.push_back(new material(name,alpha,ns,ni,dif,amb,spec,illum,-1));
                }
            }
        }
        else if((*coord[i])[0]=='v' && (*coord[i])[1]=='t')
        {
            float u,v;
            sscanf(coord[i]->c_str(),"vt %f %f",&u,&v);
            texturecoordinate.push_back(new texcoord(u,1-v));
            istexture=true;
        }
    }
    if(materials.size()==0)
        ismaterial=false;
    else
        ismaterial=true;
    out << "numvertex :" << vertex.size() << " " << normals.size() << " " << faces.size() << " " << materials.size() << std::endl;
    //draw
    if(isvertexnormal)
        smoothnormals();
    int num;
    num=glGenLists(1);
    glNewList(num,GL_COMPILE);
    int last=-1;
    for(int i=0; i<faces.size(); i++)
    {
        if(last!=faces[i]->mat && ismaterial)
        {
            float diffuse[]= {materials[faces[i]->mat]->dif[0],materials[faces[i]->mat]->dif[1],materials[faces[i]->mat]->dif[2],1.0};
            float ambient[]= {materials[faces[i]->mat]->amb[0],materials[faces[i]->mat]->amb[1],materials[faces[i]->mat]->amb[2],1.0};
            float specular[]= {materials[faces[i]->mat]->spec[0],materials[faces[i]->mat]->spec[1],materials[faces[i]->mat]->spec[2],1.0};
            glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
            glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
            glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
            glMaterialf(GL_FRONT,GL_SHININESS,materials[faces[i]->mat]->ns);
            glColor3f(diffuse[0],diffuse[1],diffuse[2]);
            last=faces[i]->mat;
            out << "1....." << std::endl;
            if(materials[faces[i]->mat]->texture==-1)
                glDisable(GL_TEXTURE_2D);
            else
            {
                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D,materials[faces[i]->mat]->texture);
            }
        }
        if(faces[i]->texcoord[0]==0)
            istexture=false;
        else
            istexture=true;
        out << "2....." << std::endl;
        isnormals=false;
        if(faces[i]->four)
        {
            glBegin(GL_QUADS);
            out << "faces[i]->texcoord[0]-1 " << faces[i]->facenum-1 << std::endl;
            if(isnormals)
                glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);

            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);


            out << "faces[i]->faces[0]-1: " << faces[i]->faces[0]-1 << " " << faces[i]->faces[1]-1 << " " << faces[i]->faces[2]-1 << " " << faces[i]->faces[3]-1 << " "  << std::endl;
            glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);


            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);

            glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);

            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[2]-1]->x,vertexnormals[faces[i]->faces[2]-1]->y,vertexnormals[faces[i]->faces[2]-1]->z);

            glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[3]-1]->u,texturecoordinate[faces[i]->texcoord[3]-1]->v);

            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[3]-1]->x,vertexnormals[faces[i]->faces[3]-1]->y,vertexnormals[faces[i]->faces[3]-1]->z);

            glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
            glEnd();
        }
        else
        {
            glBegin(GL_TRIANGLES);
            if(isnormals)
                glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[0]-1]->u,texturecoordinate[faces[i]->texcoord[0]-1]->v);

            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);


            glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);

            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[1]-1]->u,texturecoordinate[faces[i]->texcoord[1]-1]->v);


            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);

            glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);


            if(istexture && materials[faces[i]->mat]->texture!=-1)
                glTexCoord2f(texturecoordinate[faces[i]->texcoord[2]-1]->u,texturecoordinate[faces[i]->texcoord[2]-1]->v);


            if(isvertexnormal)
                glNormal3f(vertexnormals[faces[i]->faces[2]-1]->x,vertexnormals[faces[i]->faces[2]-1]->y,vertexnormals[faces[i]->faces[2]-1]->z);

            glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
            glEnd();
        }
    }
    glEndList();
    out << "3....." << std::endl;
    clean();
    lists.push_back(num);
    return num;
}
예제 #4
0
int ObjLoader::Load(const char* fileName)
{
	//Open the model file
	ifstream in(fileName);

	//Temp buffer
	char buf[256];							

	if (!in.is_open())
	{
		//If it didn't load
		cout << "Not opened" << endl; 
		return -1;
	}

	while (!in.eof())
	{
		//While we are not at the end of the file, read everything as a string to the coord vector
		in.getline(buf, 256);
		coord.push_back(new string(buf));
	}

	//Go through the line and decide what kind of line it is
	for (int i = 0; i < coord.size(); i++) 
	{
		//If it's a comment
		if ((*coord[i])[0] == '#')
		{
			//We don't have to do anything with it
			continue;       
		}
		//If it's a vertex
		else if ((*coord[i])[0] == 'v' && (*coord[i])[1] == ' ')     
		{
			float tmpx, tmpy, tmpz;

			//Read the 3 floats, which makes up the vertex
			sscanf(coord[i]->c_str(), "v %f %f %f", &tmpx, &tmpy, &tmpz); 

			//And put it in the vertex vector
			vertex.push_back(new Vector3D(tmpx, tmpy, tmpz));      
		}
		//If it's a normal vector
		else if ((*coord[i])[0] == 'v' && (*coord[i])[1] == 'n')    
		{
			float tmpx, tmpy, tmpz;

			//Read the 3 floats, which makes up the normal
			sscanf(coord[i]->c_str(), "vn %f %f %f", &tmpx, &tmpy, &tmpz);
			
			//And put it in the normal vector
			normals.push_back(new Vector3D(tmpx, tmpy, tmpz));
			
			isnormals = true;
		}
		//If it's a face
		else if ((*coord[i])[0] == 'f')   
		{
			int a, b, c, d, e;
			
			//If this is a quad
			if (count(coord[i]->begin(), coord[i]->end(), ' ') == 4)     
			{
				//If it contains a normal vector, but doesn't contain a texture coorinate
				if (coord[i]->find("//") != string::npos)     
				{
					//Read in this form
					sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &a, &b, &c, &b, &d, &b, &e, &b);      
					
					//We don't care about the texture coorinate in this case
					faces.push_back(new face(b, a, c, d, e, 0, 0, 0, 0, curmat));    
				}
				//If we have texture coorinate and normal vectors
				else if (coord[i]->find("/") != string::npos)        
				{
					//Texture Coorinates
					int t[4];       

					//Read in this form, and put to the end of the vector
					sscanf(coord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &a, &t[0], &b, &c, &t[1], &b, &d, &t[2], &b, &e, &t[3], &b);
					faces.push_back(new face(b, a, c, d, e, t[0], t[1], t[2], t[3], curmat));
				}
				//Else we don't have normal vectors or texture coorinates
				else
				{
					sscanf(coord[i]->c_str(), "f %d %d %d %d", &a, &b, &c, &d);
					faces.push_back(new face(-1, a, b, c, d, 0, 0, 0, 0, curmat));
				}
			}
			//If it's a triangle
			else
			{  
				//Do the same, except we use one less vertex/texture coorinate/face number
				if (coord[i]->find("//") != string::npos)
				{
					sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d", &a, &b, &c, &b, &d, &b);
					
					faces.push_back(new face(b, a, c, d, 0, 0, 0, curmat));
				}
				else if (coord[i]->find("/") != string::npos)
				{
					int t[3];
					
					sscanf(coord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &a, &t[0], &b, &c, &t[1], &b, &d, &t[2], &b);
					
					faces.push_back(new face(b, a, c, d, t[0], t[1], t[2], curmat));
				}
				else
				{
					sscanf(coord[i]->c_str(), "f %d %d %d", &a, &b, &c);
					
					faces.push_back(new face(-1, a, b, c, 0, 0, 0, curmat));
				}
			}
		}
		//Use material_name
		else if ((*coord[i])[0] == 'u' && (*coord[i])[1] == 's' && (*coord[i])[2] == 'e')     
		{
			char tmp[200];
			
			//Read the name of the material to tmp
			sscanf(coord[i]->c_str(), "usemtl %s", tmp);  

			//Go through all of the materials
			for (int i = 0; i<materials.size(); i++)     
			{
				//And compare the tmp with the name of the material
				if (strcmp(materials[i]->name.c_str(), tmp) == 0)   
				{
					//If it's equal then set the current material to that
					curmat = i;       
					break;
				}
			}
		}
		//Material library, a file, which contain all of the materials
		else if ((*coord[i])[0] == 'm' && (*coord[i])[1] == 't' && (*coord[i])[2] == 'l' && (*coord[i])[3] == 'l')      
		{
			char filen[200];
			
			//Read the fileName
			sscanf(coord[i]->c_str(), "mtllib %s", filen);    
			
			//Open the file
			ifstream mtlin(filen);     
			
			//If not opened, show a error message, clean all memory, then return with -1
			if (!mtlin.is_open())    
			{
				cout << "connot open the material file" << endl;
				clean();
				return -1;
			}
			
			//We use materials
			ismaterial = true;        
			
			//Contain the line of the file
			vector<string> tmp;
			
			char c[200];
			
			while (!mtlin.eof())
			{
				//Read all lines to tmp
				mtlin.getline(c, 200);   
				tmp.push_back(c);
			}
			
			//Name of the material
			char name[200]; 
			
			//fileName of the texture
			char fileName[200];     
			
			//Colors, shininess, etc
			float amb[3], dif[3], spec[3], alpha, ns, ni;        
			
			int illum;
			unsigned int texture;
			
			//Do we already have a material read in to these variables?
			bool ismat = false;       

			//Set fileName to nullbyte character
			strcpy(fileName, "\0");  
			
			//Go through all lines of the mtllib file
			for (int i = 0; i<tmp.size(); i++) 
			{
				//Ignore comments
				if (tmp[i][0] == '#')      
					continue;
				
				//New material
				if (tmp[i][0] == 'n' && tmp[i][1] == 'e' && tmp[i][2] == 'w')  
				{
					//If we have a material
					if (ismat)       
					{
						//If we have a texture
						if (strcmp(fileName, "\0") != 0)    
						{
							//Push back
							materials.push_back(new materialObj(name, alpha, ns, ni, dif, amb, spec, illum, texture)); 
							strcpy(fileName, "\0");
						}
						//Push back, but use -1 to texture
						else
						{
							materials.push_back(new materialObj(name, alpha, ns, ni, dif, amb, spec, illum, -1));                           
						}
					}
					//We start from a fresh material
					ismat = false;    
					
					//Read in the name
					sscanf(tmp[i].c_str(), "newmtl %s", name);        
				}
				//Shininess
				else if (tmp[i][0] == 'N' && tmp[i][1] == 's')      
				{
					sscanf(tmp[i].c_str(), "Ns %f", &ns);
					ismat = true;
				}
				//Ambient
				else if (tmp[i][0] == 'K' && tmp[i][1] == 'a')      
				{
					sscanf(tmp[i].c_str(), "Ka %f %f %f", &amb[0], &amb[1], &amb[2]);
					ismat = true;
				}
				//Diffuse
				else if (tmp[i][0] == 'K' && tmp[i][1] == 'd')      
				{
					sscanf(tmp[i].c_str(), "Kd %f %f %f", &dif[0], &dif[1], &dif[2]);
					ismat = true;
				}
				//Specular
				else if (tmp[i][0] == 'K' && tmp[i][1] == 's')
				{
					sscanf(tmp[i].c_str(), "Ks %f %f %f", &spec[0], &spec[1], &spec[2]);
					ismat = true;
				}
				//Others
				else if (tmp[i][0] == 'N' && tmp[i][1] == 'i')      
				{
					sscanf(tmp[i].c_str(), "Ni %f", &ni);
					ismat = true;
				}
				//Alpha
				else if (tmp[i][0] == 'd' && tmp[i][1] == ' ')      
				{
					sscanf(tmp[i].c_str(), "d %f", &alpha);
					ismat = true;
				}
				//Illum (Not Used)
				else if (tmp[i][0] == 'i' && tmp[i][1] == 'l')      
				{
					sscanf(tmp[i].c_str(), "illum %d", &illum);
					ismat = true;
				}
				//Texture
				else if (tmp[i][0] == 'm' && tmp[i][1] == 'a')      
				{
					sscanf(tmp[i].c_str(), "map_Kd %s", fileName);

					//Read the fileName, and use the loadTexture function to load it, and get the id.
					texture = loadTexture(fileName);  
					ismat = true;
				}
			}
			//There is no newmat after the last newmat, so we have to put the last material 'manually'
			if (ismat)       
			{
				if (strcmp(fileName, "\0") != 0)
				{
					materials.push_back(new materialObj(name, alpha, ns, ni, dif, amb, spec, illum, texture));
				}
				else{
					materials.push_back(new materialObj(name, alpha, ns, ni, dif, amb, spec, illum, -1));
				}
			}
		}
		//Back to the obj file, texture coorinate
		else if ((*coord[i])[0] == 'v' && (*coord[i])[1] == 't')    
		{
			float u, v;

			//Read the uv coordinate
			sscanf(coord[i]->c_str(), "vt %f %f", &u, &v);     
			
			//I push back 1-v instead of normal v, because obj file use the upper left corner as 0,0 coordinate
			texturecoordinate.push_back(new texcoordObj(u, 1 - v));       
			
			//OpenGL usesvthe bottom left corner as 0, 0, so  convert it
			istexture = true;
		}
	}
	
	//If for some reason the material file doesn't contain any material, we don't have a material
	if (materials.size() == 0) 
		ismaterial = false;
	//Else we have a material
	else    
		ismaterial = true;

	//Debug purposes
	//cout << vertex.size() << " " << normals.size() << " " << faces.size() << " " << materials.size() << endl;    
	
	//Draw
	int num;

	//I generate a unique identifier for the list
	num = glGenLists(1);
	glNewList(num, GL_COMPILE);

	//The last material (default -1, which doesn't exist, so we use the first material)
	int last = -1;  

	//Go through all faces
	for (int i = 0; i<faces.size(); i++) 
	{
		//If we have a meterial AND the last material is not the same
		if (last != faces[i]->mat && ismaterial)   
		{
			//Set all of the material property
			float diffuse[] = { materials[faces[i]->mat]->dif[0], materials[faces[i]->mat]->dif[1], materials[faces[i]->mat]->dif[2], 1.0 };
			float ambient[] = { materials[faces[i]->mat]->amb[0], materials[faces[i]->mat]->amb[1], materials[faces[i]->mat]->amb[2], 1.0 };
			float specular[] = { materials[faces[i]->mat]->spec[0], materials[faces[i]->mat]->spec[1], materials[faces[i]->mat]->spec[2], 1.0 };
			glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
			glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
			glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
			glMaterialf(GL_FRONT, GL_SHININESS, materials[faces[i]->mat]->ns);

			//Set the current to last
			last = faces[i]->mat;     

			//if we don't have texture, disable it, else enable it
			if (materials[faces[i]->mat]->texture == -1)       
				glDisable(GL_TEXTURE_2D);
			else
			{
				glEnable(GL_TEXTURE_2D);

				//And use it
				glBindTexture(GL_TEXTURE_2D, materials[faces[i]->mat]->texture); 
			}
		}
		//If quad
		if (faces[i]->four)      
		{
			//glBegin(GL_QUADS);
			//
			////If there are normals
			//if (isnormals)
			//{
			//	//Use them
			//	glNormal3f(normals[faces[i]->facenum - 1]->x, normals[faces[i]->facenum - 1]->y, normals[faces[i]->facenum - 1]->z);    
			//}

			//////If there are textures
			////if (istexture && materials[faces[i]->mat]->texture != -1)  
			////{
			//	////Set the texture coorinate
			//	//glTexCoord2f(texturecoordinate[faces[i]->texcoord[0] - 1]->u, texturecoordinate[faces[i]->texcoord[0] - 1]->v);      
			////}

			//glVertex3f(vertex[faces[i]->faces[0] - 1]->x, vertex[faces[i]->faces[0] - 1]->y, vertex[faces[i]->faces[0] - 1]->z);

			///*if (istexture && materials[faces[i]->mat]->texture != -1)
			//	glTexCoord2f(texturecoordinate[faces[i]->texcoord[1] - 1]->u, texturecoordinate[faces[i]->texcoord[1] - 1]->v);*/

			//glVertex3f(vertex[faces[i]->faces[1] - 1]->x, vertex[faces[i]->faces[1] - 1]->y, vertex[faces[i]->faces[1] - 1]->z);

			///*if (istexture && materials[faces[i]->mat]->texture != -1)
			//	glTexCoord2f(texturecoordinate[faces[i]->texcoord[2] - 1]->u, texturecoordinate[faces[i]->texcoord[2] - 1]->v);*/

			//glVertex3f(vertex[faces[i]->faces[2] - 1]->x, vertex[faces[i]->faces[2] - 1]->y, vertex[faces[i]->faces[2] - 1]->z);

			///*if (istexture && materials[faces[i]->mat]->texture != -1)
			//	glTexCoord2f(texturecoordinate[faces[i]->texcoord[3] - 1]->u, texturecoordinate[faces[i]->texcoord[3] - 1]->v);*/

			//glVertex3f(vertex[faces[i]->faces[3] - 1]->x, vertex[faces[i]->faces[3] - 1]->y, vertex[faces[i]->faces[3] - 1]->z);
			//glEnd();
		}
		else
		{
			glBegin(GL_TRIANGLES);
			
			//If there are normals
			if (isnormals)   
				glNormal3f(normals[faces[i]->facenum - 1]->x, normals[faces[i]->facenum - 1]->y, normals[faces[i]->facenum - 1]->z);

			//if (istexture && materials[faces[i]->mat]->texture != -1)
				//glTexCoord2f(texturecoordinate[faces[i]->texcoord[0] - 1]->u, texturecoordinate[faces[i]->texcoord[0] - 1]->v);

			glVertex3f(vertex[faces[i]->faces[0] - 1]->x, vertex[faces[i]->faces[0] - 1]->y, vertex[faces[i]->faces[0] - 1]->z);

			//if (istexture && materials[faces[i]->mat]->texture != -1)
				//glTexCoord2f(texturecoordinate[faces[i]->texcoord[1] - 1]->u, texturecoordinate[faces[i]->texcoord[1] - 1]->v);

			glVertex3f(vertex[faces[i]->faces[1] - 1]->x, vertex[faces[i]->faces[1] - 1]->y, vertex[faces[i]->faces[1] - 1]->z);

			//if (istexture && materials[faces[i]->mat]->texture != -1)
				//glTexCoord2f(texturecoordinate[faces[i]->texcoord[2] - 1]->u, texturecoordinate[faces[i]->texcoord[2] - 1]->v);

			glVertex3f(vertex[faces[i]->faces[2] - 1]->x, vertex[faces[i]->faces[2] - 1]->y, vertex[faces[i]->faces[2] - 1]->z);
			glEnd();
		}
	}
	glEndList();
	clean();
	lists.push_back(num);
	return num;
}