Page principale Liste des namespaces Hiérarchie des classes Liste par ordre alphabétique Liste des composants Liste des fichiers Membres des namespaces Composants Déclarations

# ExCModelASC.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program is free software; you can redistribute it and/or modify
    00005  *  it under the terms of the GNU General Public License as published by
    00006  *  the Free Software Foundation; either version 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExCModelASC.cpp,v 1.11 2002/11/19 17:09:51 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExCModelASC.h"
    00025
    00026 ExCModelASC::ExCModelASC(void)
    00027 {
    00028 Guard(ExCModelASC::ExCModelASC(void))
    00029         m_NumberObject=-1;
    00030         m_ListId=-1;
    00031 UnGuard
    00032 }
    00033
    00034 ExCModelASC::ExCModelASC(std::string FileName)
    00035 {
    00036 Guard(ExCModelASC::ExCModelASC(std::string FileName))
    00037         m_NumberObject=-1;
    00038         m_ListId=-1;
    00039         LoadFile(FileName);
    00040 UnGuard
    00041 }
    00042
    00043 ExCModelASC::~ExCModelASC(void)
    00044 {
    00045 Guard(ExCModelASC::~ExCModelASC(void))
    00046 UnGuard
    00047 }
    00048
    00049 void ExCModelASC::Draw(void)
    00050 {
    00051 Guard(void ExCModelASC::Draw(void))
    00052         glPushMatrix();
    00053                 glDisable(GL_LIGHTING);
    00054                 ExCModel::Draw();
    00055                 if(m_ListId==-1){
    00056                         BuildList(RENDER_TEXTURES);
    00057                         std::cout<<"build asc list"<<std::endl;
    00058                 }
    00059                 glCallList(m_ListId);
    00060                 glEnable(GL_LIGHTING);
    00061         glPopMatrix();
    00062 UnGuard
    00063 }
    00064
    00065 bool ExCModelASC::LoadFile(std::string FileName)
    00066 {
    00067 Guard(bool ExCModelASC::LoadFile(std::string FileName))
    00068         std::ifstream fin;
    00069         std::string buffer;
    00070         char b[256];
    00071         SetName(ExNihilo::ExtracValueFromSring(FileName,"../Data/Modeles/",".asc"));
    00072         fin.open(FileName.data(),std::ios::in);
    00073         if(fin.is_open())
    00074         {
    00075                 try
    00076                 {
    00077                         do
    00078                         {
    00079                                 memset(b,0,255);
    00080                                 fin.getline(b,256,'\n');
    00081                                 ProcessLine(b);
    00082                         }while(!fin.eof());
    00083                         //build mesh tab
    00084                         MeshAsc Mesh;
    00085                         for(unsigned int i =0;i<m_VecObject.size();++i)//draw all object
    00086                         {
    00087                                 for(unsigned int j =0;j<m_VecObject.at(i).m_VecFace.size();j++)//draw all face
    00088                                 {
    00089                                         Mesh.VecAB.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetX());
    00090                                         Mesh.VecAB.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetY());
    00091                                         Mesh.VecAB.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetZ());
    00092
    00093                                         Mesh.VecBC.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetX());
    00094                                         Mesh.VecBC.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetY());
    00095                                         Mesh.VecBC.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetZ());
    00096
    00097                                         Mesh.VecCA.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetX());
    00098                                         Mesh.VecCA.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetY());
    00099                                         Mesh.VecCA.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetZ());
    00100
    00101                                         Mesh.VecTAB.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetX());
    00102                                         Mesh.VecTAB.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetY());
    00103
    00104                                         Mesh.VecTBC.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetX());
    00105                                         Mesh.VecTBC.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetY());
    00106
    00107                                         Mesh.VecTCA.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetX());
    00108                                         Mesh.VecTCA.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetY());
    00109                                         Mesh.m_Material= m_VecObject.at(i).m_Texture;
    00110                                         m_VecMesh.push_back(Mesh);
    00111                                 }
    00112                         }
    00113                         m_VecObject.clear();//now no need object info
    00114                         return true;
    00115                 }catch(...){throw ExCExpFileReadError();}
    00116         }
    00117         throw   ExCExpFileNotFound();
    00118         return false;
    00119 UnGuard
    00120 }
    00121
    00122 void ExCModelASC::ProcessLine(std::string Line)
    00123 {
    00124 Guard(void ExCModelASC::ProcessLine(std::string Line))
    00125         if(Line.find("Ambient light color:")!=std::string::npos){//first line of file
    00126                 m_AmbiantLight.SetValue(atof(ExNihilo::ExtracValueFromSring(Line,"Red="," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Green="," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Blue="," ").data()));
    00127         }else if(Line.find("Named object: ")!=std::string::npos){//new object
    00128                 m_NumberObject++;
    00129                 ObjAsc obj;
    00130         obj.m_ObjectName=ExNihilo::ExtracValueFromSring(Line,"Named object: \"","\"");
    00131         m_VecObject.push_back(obj);
    00132         }else if(Line.find("Face ")!=std::string::npos){//read face info
    00133                 m_VecObject.at(m_NumberObject).m_VecFace.push_back(ExCVec3D(atof(ExNihilo::ExtracValueFromSring(Line,"A:"," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"B:"," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"C:"," ").data())));
    00134         }else if(Line.find("Material: ")!=std::string::npos){//read material
    00135                 m_VecObject.at(m_NumberObject).m_Texture=ExNihilo::ExtracValueFromSring(Line,"Material: \"","\"");
    00136         }else if(Line.find("Smoothing: ")!=std::string::npos){//read smothing
    00137                 m_VecObject.at(m_NumberObject).m_Smoothing=atof(ExNihilo::ExtracValueFromSring(Line,"Smoothing: ","\n").data());
    00138         }else if(Line.find("Vertex ")!=std::string::npos){//Vertex data
    00139                 if(Line.find("Vertex list:")!=std::string::npos){return;}//do nothing
    00140         m_VecObject.at(m_NumberObject).m_VecVertex.push_back(ExCVec3D(atof(ExNihilo::ExtracValueFromSring(Line,"X:"," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Y:"," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Z:"," ").data())));
    00141                 if(Line.find("U:")!=std::string::npos){m_VecObject.at(m_NumberObject).m_VecTexture.push_back(ExCVec2D(atof(ExNihilo::ExtracValueFromSring(Line,"U:"," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"V:"," ").data())));}
    00142         }
    00143 UnGuard
    00144 }
    00145
    00146 void ExCModelASC::BuildList(int Rendu)
    00147 {
    00148 Guard(void ExCModelASC::BuildList(int Rendu))
    00149         glDeleteLists(m_ListId,1);      //delete old list
    00150         m_ListId=glGenLists(1); //Ask for a new free list
    00151         glNewList(m_ListId,GL_COMPILE);
    00152                 glPushAttrib(GL_ALL_ATTRIB_BITS);
    00153                         glEnable(GL_TEXTURE_2D);
    00154                         for(unsigned int i=0;i<m_VecMesh.size();i++)
    00155                         {
    00156                                 ManagerTexture->SetCurrentObject(m_VecMesh.at(i).m_Material);
    00157                                 glBegin(GL_TRIANGLES);
    00158                                         glTexCoord2fv(m_VecMesh.at(i).VecTAB.m_Vector);glVertex3fv(m_VecMesh.at(i).VecAB.m_Vector);
    00159                                         glTexCoord2fv(m_VecMesh.at(i).VecTBC.m_Vector);glVertex3fv(m_VecMesh.at(i).VecBC.m_Vector);
    00160                                         glTexCoord2fv(m_VecMesh.at(i).VecTCA.m_Vector);glVertex3fv(m_VecMesh.at(i).VecCA.m_Vector);
    00161                                 glEnd();
    00162                         }
    00163                 glPopAttrib();
    00164         glEndList();
    00165         //delete object data because data are now in list
    00166         m_VecMesh.clear();
    00167         m_VecObject.clear();
    00168 UnGuard
    00169 }