| 1 | /*************************************************************************** |
| 2 | AC3DModel.h - description |
| 3 | ------------------- |
| 4 | begin : june 29th, 05 |
| 5 | copyright : (C) 2005-2007 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: ac3dobject.cpp 395 2008-12-27 21:12:05Z 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 "ac3dobject.h" |
| 21 | |
| 22 | #include "macros.h" // debug macros |
| 23 | #include "ac3dmacros.h" // AC3D specific macros |
| 24 | |
| 25 | #include <cstring> |
| 26 | #include <cstdio> |
| 27 | #include <cstdlib> |
| 28 | |
| 29 | |
| 30 | namespace AC3D { |
| 31 | |
| 32 | |
| 33 | /*=====================================================================*/ |
| 34 | AC3DObject::AC3DObject(): |
| 35 | strName(""), |
| 36 | fCrease(45), |
| 37 | _bTranslucent(false), |
| 38 | uiNumVert(0), |
| 39 | uiNumSurf(0), |
| 40 | uiKids(0) |
| 41 | { |
| 42 | // WARNING: uninitialized attributes |
| 43 | OPENCITY_DEBUG("ctor1"); |
| 44 | |
| 45 | this->ac3dSetDefault(); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /*=====================================================================*/ |
| 50 | AC3DObject::AC3DObject |
| 51 | ( |
| 52 | stringstream& data |
| 53 | ): |
| 54 | strName(""), |
| 55 | fCrease(45), |
| 56 | _bTranslucent(false), |
| 57 | uiNumVert(0), |
| 58 | uiNumSurf(0), |
| 59 | uiKids(0) |
| 60 | { |
| 61 | // OPENCITY_DEBUG("ctor2"); |
| 62 | |
| 63 | this->ac3dSetDefault(); |
| 64 | this->Parse( data ); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /*=====================================================================*/ |
| 69 | AC3DObject::~AC3DObject() |
| 70 | { |
| 71 | //OPENCITY_DEBUG("dtor"); |
| 72 | vector<AC3DObject*>::size_type pos, size; |
| 73 | |
| 74 | |
| 75 | size = this->vpObject.size(); |
| 76 | for (pos = 0; pos < size; pos++) |
| 77 | delete vpObject[pos]; |
| 78 | |
| 79 | size = this->vpSurface.size(); |
| 80 | for (pos = 0; pos < size; pos++) |
| 81 | delete vpSurface[pos]; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /*=====================================================================*/ |
| 86 | void |
| 87 | AC3DObject::AddKid |
| 88 | ( |
| 89 | AC3DObject* kid |
| 90 | ) |
| 91 | { |
| 92 | this->vpObject.push_back( kid ); |
| 93 | |
| 94 | // Automatically set the translucent attribute |
| 95 | if (_bTranslucent && (this->type == OBJECT_GROUP)) |
| 96 | kid->SetTranslucent(true); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /*=====================================================================*/ |
| 101 | bool |
| 102 | AC3DObject::IsTranslucent() const |
| 103 | { |
| 104 | return _bTranslucent; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*=====================================================================*/ |
| 109 | void |
| 110 | AC3DObject::SetTranslucent(bool bValue) |
| 111 | { |
| 112 | _bTranslucent = bValue; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /*=====================================================================*/ |
| 117 | string |
| 118 | AC3DObject::GetName() const |
| 119 | { |
| 120 | return this->strName; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /*=====================================================================*/ |
| 125 | unsigned int |
| 126 | AC3DObject::GetNumberKid() const |
| 127 | { |
| 128 | return this->uiKids; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /*=====================================================================*/ |
| 133 | const string& |
| 134 | AC3DObject::GetTextureFile() const |
| 135 | { |
| 136 | return this->strTexture; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /*=====================================================================*/ |
| 141 | const float* |
| 142 | AC3DObject::GetLoc() const |
| 143 | { |
| 144 | return this->fLoc; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /*=====================================================================*/ |
| 149 | const vector<Vertex>& |
| 150 | AC3DObject::GetVVertex() const |
| 151 | { |
| 152 | return this->vVertex; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /*=====================================================================*/ |
| 157 | const vector<AC3DSurface*>& |
| 158 | AC3DObject::GetVPSurface() const |
| 159 | { |
| 160 | return this->vpSurface; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /*=====================================================================*/ |
| 165 | const vector<AC3DObject*>& |
| 166 | AC3DObject::GetVPObject() const |
| 167 | { |
| 168 | return this->vpObject; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /*=====================================================================*/ |
| 173 | void |
| 174 | AC3DObject::Parse( |
| 175 | stringstream& data |
| 176 | ) |
| 177 | { |
| 178 | char line[AC3D_MAX_LINE_LENGTH]; |
| 179 | char cstr[AC3D_MAX_LINE_LENGTH]; |
| 180 | uint uiDataSize; // The size of the optional data |
| 181 | |
| 182 | //debug cout << "Given ss: " << data.str() << endl; |
| 183 | |
| 184 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 185 | while (data.good()) { |
| 186 | if ( strncmp( line, AC3D_TOKEN_OBJECT, AC3D_TOKEN_OBJECT_L ) == 0 ) { |
| 187 | sscanf( line, "OBJECT %s", cstr ); |
| 188 | if (strncmp( cstr, "world", 5 ) == 0) |
| 189 | this->type = OBJECT_WORLD; |
| 190 | else if (strncmp( cstr, "poly", 5 ) == 0) |
| 191 | this->type = OBJECT_POLYGON; |
| 192 | else if (strncmp( cstr, "group", 5 ) == 0) |
| 193 | this->type = OBJECT_GROUP; |
| 194 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 195 | } else |
| 196 | if ( strncmp( line, AC3D_TOKEN_NAME, AC3D_TOKEN_NAME_L ) == 0 ) { |
| 197 | sscanf( line, "name %s", cstr ); |
| 198 | this->strName = cstr; |
| 199 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 200 | } else |
| 201 | if ( strncmp( line, AC3D_TOKEN_DATA, AC3D_TOKEN_DATA_L ) == 0 ) { |
| 202 | sscanf( line, "data %u", &uiDataSize ); |
| 203 | // Buffer overflow protection |
| 204 | if ( uiDataSize >= AC3D_MAX_LINE_LENGTH ) { |
| 205 | OPENCITY_FATAL( "The optional data is too big." ); |
| 206 | abort(); |
| 207 | } |
| 208 | // Extract the optional data |
| 209 | data.getline( cstr, AC3D_MAX_LINE_LENGTH ); |
| 210 | cstr[uiDataSize] = '\0'; |
| 211 | _sData = cstr; |
| 212 | // OpenCity AC3D "alpha" command detection |
| 213 | _bTranslucent = (_sData.find(OC_AC3D_TOKEN_ALPHA) != string::npos); |
| 214 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 215 | OPENCITY_DEBUG( "The AC3D optional data is : |" << _sData << "|" ); |
| 216 | } else |
| 217 | if ( strncmp( line, AC3D_TOKEN_TEXTURE, AC3D_TOKEN_TEXTURE_L ) == 0 ) { |
| 218 | sscanf( line, "texture %s", cstr ); |
| 219 | this->strTexture = cstr; |
| 220 | // Strip out the doublequotes |
| 221 | if (this->strTexture.size() >= 2) { |
| 222 | this->strTexture = this->strTexture.substr( 1, this->strTexture.size()-2 ); |
| 223 | } else { |
| 224 | OPENCITY_DEBUG("Unable to strip doublequotes"); |
| 225 | } |
| 226 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 227 | } else |
| 228 | if ( strncmp( line, AC3D_TOKEN_TEXREP, AC3D_TOKEN_TEXREP_L ) == 0 ) { |
| 229 | sscanf( line, "texrep %f %f", this->fTexRep, this->fTexRep+1 ); |
| 230 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 231 | } else |
| 232 | if ( strncmp( line, AC3D_TOKEN_ROT, AC3D_TOKEN_ROT_L ) == 0 ) { |
| 233 | sscanf( |
| 234 | line, |
| 235 | "rot %f %f %f %f %f %f %f %f %f", |
| 236 | this->fRot , this->fRot+1, this->fRot+2, |
| 237 | this->fRot+3, this->fRot+4, this->fRot+5, |
| 238 | this->fRot+6, this->fRot+7, this->fRot+8 |
| 239 | ); |
| 240 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 241 | } else |
| 242 | if ( strncmp( line, AC3D_TOKEN_LOC, AC3D_TOKEN_LOC_L ) == 0 ) { |
| 243 | sscanf( line, "loc %f %f %f", this->fLoc, this->fLoc+1, this->fLoc+2 ); |
| 244 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 245 | } else |
| 246 | if ( strncmp( line, AC3D_TOKEN_CREASE, AC3D_TOKEN_CREASE_L ) == 0 ) { |
| 247 | sscanf( line, "crease %f", &(this->fCrease) ); |
| 248 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 249 | } else |
| 250 | if ( strncmp( line, AC3D_TOKEN_URL, AC3D_TOKEN_URL_L ) == 0 ) { |
| 251 | sscanf( line, "url %s", cstr ); |
| 252 | this->strURL = cstr; |
| 253 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 254 | } else |
| 255 | if ( strncmp( line, AC3D_TOKEN_KIDS, AC3D_TOKEN_KIDS_L ) == 0 ) { |
| 256 | sscanf( line, "kids %u", &(this->uiKids) ); |
| 257 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 258 | } else |
| 259 | if ( strncmp( line, AC3D_TOKEN_NUMVERT, AC3D_TOKEN_NUMVERT_L ) == 0 ) { |
| 260 | sscanf( line, "numvert %u", &(this->uiNumVert) ); |
| 261 | ac3dobjectParseVertex( this->uiNumVert, data, line ); |
| 262 | } else |
| 263 | if ( strncmp( line, AC3D_TOKEN_NUMSURF, AC3D_TOKEN_NUMSURF_L ) == 0 ) { |
| 264 | sscanf( line, "numsurf %u", &(this->uiNumSurf) ); |
| 265 | ac3dobjectParseSurface( this->uiNumSurf, data, line ); |
| 266 | } else { |
| 267 | OPENCITY_DEBUG("WARNING: unknown command discarded. See below: "); |
| 268 | OPENCITY_DEBUG( "|" << line << "|" ); |
| 269 | data.getline( line, AC3D_MAX_LINE_LENGTH ); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | data.clear(); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /*=====================================================================*/ |
| 278 | string |
| 279 | AC3DObject::ToStr() const |
| 280 | { |
| 281 | stringstream ss; |
| 282 | vector<AC3DObject*>::size_type pos, size; |
| 283 | |
| 284 | |
| 285 | ss << "OBJECT "; |
| 286 | switch (this->type) { |
| 287 | case OBJECT_WORLD: ss << "world"; break; |
| 288 | case OBJECT_POLYGON: ss << "poly"; break; |
| 289 | case OBJECT_GROUP: ss << "group"; break; |
| 290 | } |
| 291 | ss << endl; |
| 292 | |
| 293 | ss << ((this->strName != "") ? "name " + this->strName + "\n" : "") |
| 294 | << "loc " << this->fLoc[0] << " " |
| 295 | << this->fLoc[1] << " " |
| 296 | << this->fLoc[2] << endl |
| 297 | << "texrep " << this->fTexRep[0] << " " |
| 298 | << this->fTexRep[1] << endl; |
| 299 | |
| 300 | ss << ((this->strTexture != "") ? "texture " + this->strTexture + "\n" : "") |
| 301 | << "crease " << this->fCrease << endl |
| 302 | << "numvert " << this->uiNumVert << endl; |
| 303 | |
| 304 | size = this->vVertex.size(); |
| 305 | for (pos = 0; pos < size; pos++) { |
| 306 | ss << this->vVertex[pos].x << " " |
| 307 | << this->vVertex[pos].y << " " |
| 308 | << this->vVertex[pos].z << endl; |
| 309 | } |
| 310 | |
| 311 | ss << "numsurf " << this->uiNumSurf << endl; |
| 312 | |
| 313 | size = this->vpSurface.size(); |
| 314 | for (pos = 0; pos < size; pos++) |
| 315 | ss << this->vpSurface[pos]->ToStr(); |
| 316 | |
| 317 | ss << "kids " << this->uiKids << endl; |
| 318 | |
| 319 | size = this->vpObject.size(); |
| 320 | for (pos = 0; pos < size; pos++) |
| 321 | ss << this->vpObject[pos]->ToStr(); |
| 322 | |
| 323 | return ss.str(); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /*=====================================================================*/ |
| 328 | /* PRIVATE METHODS */ |
| 329 | /*=====================================================================*/ |
| 330 | void |
| 331 | AC3DObject::ac3dSetDefault() |
| 332 | { |
| 333 | // Default "texrep" values |
| 334 | this->fTexRep[0] = 1.; |
| 335 | this->fTexRep[1] = 1.; |
| 336 | |
| 337 | // Default "loc" values |
| 338 | this->fLoc[0] = 0.; this->fLoc[1] = 0.; this->fLoc[2] = 0.; |
| 339 | |
| 340 | // Default "rot" values |
| 341 | this->fRot[0] = 1.; this->fRot[1] = 0.; this->fRot[2] = 0.; |
| 342 | this->fRot[3] = 0.; this->fRot[4] = 1.; this->fRot[5] = 0.; |
| 343 | this->fRot[3] = 0.; this->fRot[4] = 0.; this->fRot[5] = 1.; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /*=====================================================================*/ |
| 348 | void |
| 349 | AC3DObject::ac3dobjectParseVertex |
| 350 | ( |
| 351 | unsigned int nb, |
| 352 | stringstream& data, |
| 353 | char* buffer |
| 354 | ) |
| 355 | { |
| 356 | Vertex newVertex; |
| 357 | |
| 358 | // WARNING no error checking |
| 359 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 360 | while (data.good() && (nb-- > 0)) { |
| 361 | sscanf( buffer, "%f %f %f", &newVertex.x, &newVertex.y, &newVertex.z ); |
| 362 | this->vVertex.push_back( newVertex ); |
| 363 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /*=====================================================================*/ |
| 369 | void |
| 370 | AC3DObject::ac3dobjectParseSurface |
| 371 | ( |
| 372 | unsigned int nb, |
| 373 | stringstream& data, |
| 374 | char* buffer |
| 375 | ) |
| 376 | { |
| 377 | stringstream ss; |
| 378 | |
| 379 | bool endBlock = false; |
| 380 | bool endSurf = false; |
| 381 | |
| 382 | AC3DSurface* pSurf = NULL; |
| 383 | |
| 384 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 385 | while (data.good() && !endSurf) { |
| 386 | if ( strncmp( buffer, AC3D_TOKEN_SURF, AC3D_TOKEN_SURF_L ) == 0 ) { |
| 387 | ss << buffer << endl; |
| 388 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 389 | } else |
| 390 | if ( strncmp( buffer, AC3D_TOKEN_MAT, AC3D_TOKEN_MAT_L ) == 0 ) { |
| 391 | ss << buffer << endl; |
| 392 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 393 | } else |
| 394 | if ( strncmp( buffer, AC3D_TOKEN_KIDS, AC3D_TOKEN_KIDS_L ) == 0 ) { |
| 395 | endSurf = true; |
| 396 | } else |
| 397 | if ( strncmp( buffer, AC3D_TOKEN_REFS, AC3D_TOKEN_REFS_L ) == 0 ) { |
| 398 | ss << buffer << endl; |
| 399 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 400 | |
| 401 | endBlock = false; |
| 402 | while (data.good() && !endBlock) { |
| 403 | // IF this is a new "SURF", "mat" or "refs" command |
| 404 | // THEN we reach the end of the block |
| 405 | if ((strncmp( buffer, AC3D_TOKEN_SURF, AC3D_TOKEN_SURF_L ) == 0 ) |
| 406 | || (strncmp( buffer, AC3D_TOKEN_MAT, AC3D_TOKEN_MAT_L ) == 0 ) |
| 407 | || (strncmp( buffer, AC3D_TOKEN_REFS, AC3D_TOKEN_REFS_L ) == 0 ) |
| 408 | || (strncmp( buffer, AC3D_TOKEN_KIDS, AC3D_TOKEN_KIDS_L ) == 0 )) { |
| 409 | endBlock = true; |
| 410 | } |
| 411 | else { |
| 412 | //debug cout << "New buffer: " << buffer << endl; |
| 413 | ss << buffer << endl; |
| 414 | data.getline( buffer, AC3D_MAX_LINE_LENGTH ); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Create new surface from the data block |
| 419 | pSurf = new AC3DSurface( ss ); |
| 420 | this->vpSurface.push_back( pSurf ); |
| 421 | ss.str(""); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | } |
| 426 | |
| 427 | |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 |