| 1 | /*************************************************************************** |
| 2 | AC3DModel.cpp - description |
| 3 | ------------------- |
| 4 | begin : june 29th, 05 |
| 5 | copyright : (C) 2005-2007 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: ac3dmodel.cpp 375 2008-10-28 14:47:15Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | #include "ac3dmodel.h" |
| 21 | #include "ac3dobject.h" |
| 22 | |
| 23 | #include "macros.h" // For debugging macros |
| 24 | #include "ac3dmacros.h" // Our macros |
| 25 | |
| 26 | #include <fstream> |
| 27 | #include <cstring> |
| 28 | #include <sstream> |
| 29 | |
| 30 | using std::ifstream; |
| 31 | using std::stringstream; |
| 32 | |
| 33 | namespace AC3D { |
| 34 | |
| 35 | /*=====================================================================*/ |
| 36 | AC3DModel::AC3DModel |
| 37 | ( |
| 38 | string fileName |
| 39 | ): |
| 40 | strFileName(fileName), |
| 41 | pObject(NULL) |
| 42 | { |
| 43 | OPENCITY_DEBUG("ctor"); |
| 44 | this->bGood = this->ac3dmodelParse(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /*=====================================================================*/ |
| 49 | AC3DModel::~AC3DModel() |
| 50 | { |
| 51 | OPENCITY_DEBUG("dtor"); |
| 52 | |
| 53 | delete this->pObject; |
| 54 | this->pObject = NULL; |
| 55 | this->bGood = false; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /*=====================================================================*/ |
| 60 | const vector<AC3DMaterial> & |
| 61 | AC3DModel::GetVMaterial() const |
| 62 | { |
| 63 | return this->vMaterial; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /*=====================================================================*/ |
| 68 | const AC3DObject* const |
| 69 | AC3DModel::GetPObject() const |
| 70 | { |
| 71 | return this->pObject; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /*=====================================================================*/ |
| 76 | bool |
| 77 | AC3DModel::IsGood() const |
| 78 | { |
| 79 | return this->bGood; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /*=====================================================================*/ |
| 84 | string |
| 85 | AC3DModel::ToStr() const |
| 86 | { |
| 87 | string str = this->strHeader + "\n"; |
| 88 | vector<AC3DMaterial>::size_type pos, size; |
| 89 | |
| 90 | // Output all materials |
| 91 | size = this->vMaterial.size(); |
| 92 | for (pos = 0; pos < size; pos++) |
| 93 | str += this->vMaterial[pos].ToStr(); |
| 94 | |
| 95 | // Output all objects |
| 96 | if (this->pObject != NULL) |
| 97 | str += this->pObject->ToStr(); |
| 98 | |
| 99 | return str; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /*=====================================================================*/ |
| 104 | /* PRIVATE METHODS */ |
| 105 | /*=====================================================================*/ |
| 106 | bool |
| 107 | AC3DModel::ac3dmodelParse() |
| 108 | { |
| 109 | ifstream in; |
| 110 | stringstream ss; |
| 111 | |
| 112 | char line[AC3D_MAX_LINE_LENGTH]; |
| 113 | bool endBlock; |
| 114 | |
| 115 | AC3DObject* pNewObj = NULL; |
| 116 | AC3DObject* pCurrentObj = NULL; |
| 117 | vector<AC3DObject*> vpObj; // Stack used for recursive object loading |
| 118 | |
| 119 | |
| 120 | in.open(this->strFileName.c_str()); |
| 121 | |
| 122 | if (!in.good()) { |
| 123 | OPENCITY_DEBUG("File open error"); |
| 124 | OPENCITY_DEBUG( this->strFileName.c_str() ); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Get the header. Ex: "AC3Db" |
| 129 | // TODO: version check |
| 130 | in.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 131 | if (in.good()) |
| 132 | strHeader = line; |
| 133 | |
| 134 | // Parse the file |
| 135 | in.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 136 | while (in.good()) { |
| 137 | // Looks for "MATERIAL" token |
| 138 | if ( strncmp( line, AC3D_TOKEN_MATERIAL, AC3D_TOKEN_MATERIAL_L ) == 0 ) { |
| 139 | // Create new MATERIAL |
| 140 | this->vMaterial.push_back( AC3DMaterial( line ) ); |
| 141 | in.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 142 | } else |
| 143 | if( strncmp( line, AC3D_TOKEN_OBJECT, AC3D_TOKEN_OBJECT_L ) == 0 ) { |
| 144 | ss.str(""); |
| 145 | // debug cout << "New line: " << line << endl; |
| 146 | ss << line << endl; |
| 147 | |
| 148 | // Get all the OBJECT block |
| 149 | in.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 150 | endBlock = false; |
| 151 | while (in.good() && !endBlock) { |
| 152 | // IF this is a new OBJECT command then we reach the end of the block |
| 153 | if( strncmp( line, AC3D_TOKEN_OBJECT, AC3D_TOKEN_OBJECT_L ) == 0 ) { |
| 154 | endBlock = true; |
| 155 | } |
| 156 | else { |
| 157 | // debug cout << "New line: " << line << endl; |
| 158 | ss << line << endl; |
| 159 | in.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 160 | } |
| 161 | } // while |
| 162 | |
| 163 | // Create new OBJECT from the block of data |
| 164 | pNewObj = new AC3DObject( ss ); |
| 165 | |
| 166 | // IF this is the first object THEN |
| 167 | if (this->pObject == NULL) { |
| 168 | this->pObject = pNewObj; |
| 169 | pCurrentObj = pNewObj; |
| 170 | } |
| 171 | else { |
| 172 | assert( pCurrentObj != NULL ); |
| 173 | pCurrentObj->AddKid( pNewObj ); |
| 174 | |
| 175 | // IF this object has any kids THEN |
| 176 | if (pNewObj->GetNumberKid() > 0) { |
| 177 | // Save the current level on the stack |
| 178 | vpObj.push_back( pCurrentObj ); |
| 179 | pCurrentObj = pNewObj; |
| 180 | } |
| 181 | else { |
| 182 | // IF all the kids have been loaded THEN |
| 183 | while ((pCurrentObj->GetVPObject().size() == pCurrentObj->GetNumberKid()) |
| 184 | && (vpObj.empty() == false)) { |
| 185 | // Restore the previous level |
| 186 | pCurrentObj = vpObj.back(); |
| 187 | vpObj.pop_back(); |
| 188 | } |
| 189 | } |
| 190 | } // IF first object |
| 191 | |
| 192 | } // new OBJECT |
| 193 | } // while good |
| 194 | |
| 195 | if (in.is_open()) |
| 196 | in.close(); |
| 197 | |
| 198 | return true; // everything is OK |
| 199 | } |
| 200 | |
| 201 | |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 |