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         ExCModel::ExCModel();
    00032         SetName("ExCModelASC");
    00033         SetType(typeid(this).name());
    00034 UnGuard
    00035 }
    00036
    00037 ExCModelASC::ExCModelASC(std::string FileName)
    00038 {
    00039 Guard(ExCModelASC::ExCModelASC(std::string FileName))
    00040         m_NumberObject=-1;
    00041         m_ListId=-1;
    00042         LoadFile(FileName);
    00043 UnGuard
    00044 }
    00045
    00046 ExCModelASC::~ExCModelASC(void)
    00047 {
    00048 Guard(ExCModelASC::~ExCModelASC(void))
    00049 UnGuard
    00050 }
    00051
    00052 void ExCModelASC::Draw(void)
    00053 {
    00054 Guard(void ExCModelASC::Draw(void))
    00055         glPushMatrix();
    00056                 glDisable(GL_LIGHTING);
    00057                 ExCModel::Draw();
    00058                 glRotatef(-90,1,0,0);
    00059                 if(m_ListId==-1){
    00060                         BuildList(RENDER_TEXTURES);
    00061                         std::cout<<"build asc list"<<std::endl;
    00062                 }
    00063                 glCallList(m_ListId);
    00064                 glEnable(GL_LIGHTING);
    00065         glPopMatrix();
    00066 UnGuard
    00067 }
    00068
    00069 bool ExCModelASC::Load(std::string FileName)
    00070 {
    00071 Guard(bool ExCModelASC::Load(std::string FileName))
    00072         std::ifstream fin;
    00073         std::string buffer;
    00074         char b[256];
    00075         SetName(ExNihilo::ExtracValueFromSring(FileName,"../Data/Modeles/",".asc"));
    00076         fin.open(FileName.data(),std::ios::in);
    00077         if(fin.is_open())
    00078         {
    00079                 try
    00080                 {
    00081                         do
    00082                         {
    00083                                 memset(b,0,255);
    00084                                 fin.getline(b,256,'\n');
    00085                                 ProcessLine(b);
    00086                         }while(!fin.eof());
    00087                         //build mesh tab
    00088                         MeshAsc Mesh;
    00089                         for(unsigned int i =0;i<m_VecObject.size();++i)//draw all object
    00090                         {
    00091                                 for(unsigned int j =0;j<m_VecObject.at(i).m_VecFace.size();j++)//draw all face
    00092                                 {
    00093                                         Mesh.VecAB.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetX());
    00094                                         Mesh.VecAB.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetY());
    00095                                         Mesh.VecAB.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetZ());
    00096
    00097                                         Mesh.VecBC.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetX());
    00098                                         Mesh.VecBC.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetY());
    00099                                         Mesh.VecBC.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetZ());
    00100
    00101                                         Mesh.VecCA.SetX(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetX());
    00102                                         Mesh.VecCA.SetY(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetY());
    00103                                         Mesh.VecCA.SetZ(m_VecObject.at(i).m_VecVertex.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetZ());
    00104
    00105                                         Mesh.VecTAB.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetX());
    00106                                         Mesh.VecTAB.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetX()).GetY());
    00107
    00108                                         Mesh.VecTBC.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetX());
    00109                                         Mesh.VecTBC.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetY()).GetY());
    00110
    00111                                         Mesh.VecTCA.SetX(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetX());
    00112                                         Mesh.VecTCA.SetY(m_VecObject.at(i).m_VecTexture.at(m_VecObject.at(i).m_VecFace.at(j).GetZ()).GetY());
    00113                                         Mesh.m_Material= m_VecObject.at(i).m_Texture;
    00114                                         m_VecMesh.push_back(Mesh);
    00115                                 }
    00116                         }
    00117                         m_VecObject.clear();//now no need object info
    00118                         return true;
    00119                 }catch(...){throw ExCExpFileReadError();}
    00120         }
    00121         throw   ExCExpFileNotFound();
    00122         return false;
    00123 UnGuard
    00124 }
    00125
    00126 void ExCModelASC::ProcessLine(std::string Line)
    00127 {
    00128 Guard(void ExCModelASC::ProcessLine(std::string Line))
    00129         if(Line.find("Ambient light color:")!=std::string::npos){//first line of file
    00130                 m_AmbiantLight.SetValue(atof(ExNihilo::ExtracValueFromSring(Line,"Red="," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Green="," ").data()),atof(ExNihilo::ExtracValueFromSring(Line,"Blue="," ").data()));
    00131         }else if(Line.find("Named object: ")!=std::string::npos){//new object
    00132                 m_NumberObject++;
    00133                 ObjAsc obj;
    00134         obj.m_ObjectName=ExNihilo::ExtracValueFromSring(Line,"Named object: \"","\"");
    00135         m_VecObject.push_back(obj);
    00136         }else if(Line.find("Face ")!=std::string::npos){//read face info
    00137                 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())));
    00138         }else if(Line.find("Material: ")!=std::string::npos){//read material
    00139                 m_VecObject.at(m_NumberObject).m_Texture=ExNihilo::ExtracValueFromSring(Line,"Material: \"","\"");
    00140                 ManagerTexture->Add(m_VecObject.at(m_NumberObject).m_Texture);
    00141         }else if(Line.find("Smoothing: ")!=std::string::npos){//read smothing
    00142                 m_VecObject.at(m_NumberObject).m_Smoothing=atof(ExNihilo::ExtracValueFromSring(Line,"Smoothing: ","\n").data());
    00143         }else if(Line.find("Vertex ")!=std::string::npos){//Vertex data
    00144                 if(Line.find("Vertex list:")!=std::string::npos){return;}//do nothing
    00145         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())));
    00146                 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())));}
    00147         }
    00148 UnGuard
    00149 }
    00150
    00151 void ExCModelASC::BuildList(int Rendu)
    00152 {
    00153 Guard(void ExCModelASC::BuildList(int Rendu))
    00154         glDeleteLists(m_ListId,1);      //delete old list
    00155         m_ListId=glGenLists(1); //Ask for a new free list
    00156         glNewList(m_ListId,GL_COMPILE);
    00157                 glPushAttrib(GL_ALL_ATTRIB_BITS);
    00158                         glEnable(GL_TEXTURE_2D);
    00159                         for(unsigned int i=0;i<m_VecMesh.size();i++)
    00160                         {
    00161                                 ManagerTexture->SetCurrentObject(m_VecMesh.at(i).m_Material);
    00162                                 glBegin(GL_TRIANGLES);
    00163                                         glTexCoord2fv(m_VecMesh.at(i).VecTAB.m_Vector);glVertex3fv(m_VecMesh.at(i).VecAB.m_Vector);
    00164                                         glTexCoord2fv(m_VecMesh.at(i).VecTBC.m_Vector);glVertex3fv(m_VecMesh.at(i).VecBC.m_Vector);
    00165                                         glTexCoord2fv(m_VecMesh.at(i).VecTCA.m_Vector);glVertex3fv(m_VecMesh.at(i).VecCA.m_Vector);
    00166                                 glEnd();
    00167                         }
    00168                 glPopAttrib();
    00169         glEndList();
    00170         //delete object data because data are now in list
    00171         m_VecMesh.clear();
    00172         m_VecObject.clear();
    00173 UnGuard
    00174 }