| 443 | | // now parse the array to create triangle vertices |
| 444 | | std::cout << "TreeCollision finished reading into Array. Now assigning vertices for row "; |
| 445 | | Zen::Math::Real vertexArray[9]; |
| 446 | | const Zen::Math::Real* pVertexArray = &vertexArray[0]; |
| 447 | | size_t sizeMinusOne = _size - 1; |
| 448 | | size_t arrIndex; |
| 449 | | int strideSize = sizeof(Zen::Math::Real) * 3; |
| 450 | | for (row = 0; row < sizeMinusOne; row++) |
| 451 | | { |
| 452 | | std::cout << " " << row; |
| 453 | | for (column = 0; column < sizeMinusOne; column++) |
| 454 | | { |
| 455 | | arrIndex = column + (row * _size); |
| 456 | | |
| 457 | | vertexArray[0] = (Zen::Math::Real)column * _scaleXY; |
| 458 | | vertexArray[1] = pHeightFieldArray[arrIndex + _size]; |
| 459 | | vertexArray[2] = (Zen::Math::Real)(row + 1.0f) * _scaleXY; |
| 460 | | |
| 461 | | vertexArray[3] = (Zen::Math::Real)(column + 1.0f) * _scaleXY; |
| 462 | | vertexArray[4] = pHeightFieldArray[arrIndex + 1]; |
| 463 | | vertexArray[5] = (Zen::Math::Real)row * _scaleXY; |
| 464 | | |
| 465 | | vertexArray[6] = (Zen::Math::Real)column * _scaleXY; |
| 466 | | vertexArray[7] = pHeightFieldArray[arrIndex]; |
| 467 | | vertexArray[8] = (Zen::Math::Real)row * _scaleXY; |
| 468 | | |
| 469 | | NewtonTreeCollisionAddFace(dynamic_cast<CollisionShape*>(pShape.get())->getShapePtr(), 3, pVertexArray, strideSize, 0); |
| 470 | | |
| 471 | | vertexArray[0] = (Zen::Math::Real)(column + 1.0f) * _scaleXY; |
| 472 | | vertexArray[1] = pHeightFieldArray[arrIndex + _size + 1]; |
| 473 | | vertexArray[2] = (Zen::Math::Real)(row + 1.0f) * _scaleXY; |
| 474 | | |
| 475 | | vertexArray[3] = (Zen::Math::Real)(column + 1.0f) * _scaleXY; |
| 476 | | vertexArray[4] = pHeightFieldArray[arrIndex + 1]; |
| 477 | | vertexArray[5] = (Zen::Math::Real)row * _scaleXY; |
| 478 | | |
| 479 | | vertexArray[6] = (Zen::Math::Real)column * _scaleXY; |
| 480 | | vertexArray[7] = pHeightFieldArray[arrIndex + _size]; |
| 481 | | vertexArray[8] = (Zen::Math::Real)(row + 1.0f) * _scaleXY; |
| 482 | | |
| 483 | | NewtonTreeCollisionAddFace(dynamic_cast<CollisionShape*>(pShape.get())->getShapePtr(), 3, pVertexArray, strideSize, 0); |
| 484 | | } |
| 485 | | } |
| 486 | | |
| 487 | | std::cout << "\nTreeCollision finished assigning vertices.\nNow beginning mesh optimization...\n\nPlease be patient, as this may take a few MINUTES depending on your computer." << std::endl; |
| 488 | | // TODO - reset this to final parm of 1 to force optimization. set to 0 for testing speed only. |
| 489 | | NewtonTreeCollisionEndBuild(dynamic_cast<CollisionShape*>(pShape.get())->getShapePtr(), 1); |
| 490 | | //NewtonTreeCollisionEndBuild(collision, 0); |
| 491 | | std::cout << "TreeCollision finished optimizing. Completed." << std::endl; |
| | 439 | //btHeightfieldTerrainShape::btHeightfieldTerrainShape(int heightStickWidth, int heightStickLength,void* heightfieldData,btScalar maxHeight,int upAxis,bool useFloatData,bool flipQuadEdges) |
| | 440 | dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(new btHeightfieldTerrainShape(_scaleXY,_scaleXY,&pHeightFieldArray,_maxHeight,0,true,false)); |
| | 441 | // std::cout << "\nTreeCollision finished assigning vertices.\nNow beginning mesh optimization...\n\nPlease be patient, as this may take a few MINUTES depending on your computer." << std::endl; |
| 494 | | if (_bSerialize) |
| 495 | | { |
| 496 | | //FILE* file; |
| 497 | | //errno_t errcode; |
| 498 | | std::string serializeFile = _filename; |
| 499 | | serializeFile.append(".collision"); |
| 500 | | file = fopen(serializeFile.c_str(), "wb"); |
| 501 | | if( file== NULL ) |
| 502 | | { |
| 503 | | std::cout << "Error: could not open file " << _filename.c_str() << " for writing while serializing terrain." << std::endl; |
| 504 | | throw Zen::Utility::runtime_exception("Zen::ZBullet::PhysicsZone::createHeightFieldShapeFromRaw - Could not Serialize File!"); |
| 505 | | } |
| 506 | | std::cout << "\nTreeCollision serializing started to file " << serializeFile.c_str() << std::endl; |
| 507 | | NewtonTreeCollisionSerialize(dynamic_cast<CollisionShape*>(pShape.get())->getShapePtr(), ZBulletSerializeFile, file); |
| 508 | | std::cout << "TreeCollision serializing completed." << std::endl; |
| 509 | | fclose(file); |
| 510 | | } |
| 511 | | |
| | 444 | return pShape; |
| | 445 | } |
| | 446 | |
| | 447 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
| | 448 | PhysicsZone::pCollisionShape_type |
| | 449 | PhysicsZone::createHeightFieldShapeFromSerialization(std::string _filename, const Math::Matrix4& _transform) |
| | 450 | { |
| | 451 | std::cout << "\nLoading TreeCollision from file " << _filename.c_str() << std::endl; |
| | 452 | FILE* file; |
| | 453 | |
| | 454 | file = fopen(_filename.c_str(), "rb"); |
| | 455 | if( file==0 ) |
| | 456 | { |
| | 457 | std::cout << "Error: could not open file " << _filename.c_str() << " for loading terrain." << std::endl; |
| | 458 | throw Zen::Utility::runtime_exception("Zen::ZBullet::PhysicsZone::createHeightFieldShapeFromSerialization - Could not Open File!"); |
| | 459 | } |
| | 460 | |
| | 461 | CollisionShape* pRawPointer = new CollisionShape(); |
| | 462 | pCollisionShape_type pShape(pRawPointer, boost::bind(&PhysicsZone::destroyCollisionShape, this, _1)); |
| | 463 | |
| | 464 | //dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(NewtonCreateTreeCollision(m_pZone, NULL)); |
| | 465 | |
| | 466 | //dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(NewtonCreateTreeCollisionFromSerialization(m_pZone, NULL, ZBulletDeSerializeFile, file)); |
| | 467 | fclose (file); |
| | 468 | std::cout << "Finished loading TreeCollision from file." << std::endl; |
| | 469 | |
| 521 | | |
| 522 | | return pShape; |
| 523 | | } |
| 524 | | |
| 525 | | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
| 526 | | PhysicsZone::pCollisionShape_type |
| 527 | | PhysicsZone::createHeightFieldShapeFromSerialization(std::string _filename, const Math::Matrix4& _transform) |
| 528 | | { |
| 529 | | std::cout << "\nLoading TreeCollision from file " << _filename.c_str() << std::endl; |
| 530 | | FILE* file; |
| 531 | | |
| 532 | | file = fopen(_filename.c_str(), "rb"); |
| 533 | | if( file==0 ) |
| 534 | | { |
| 535 | | std::cout << "Error: could not open file " << _filename.c_str() << " for loading terrain." << std::endl; |
| 536 | | throw Zen::Utility::runtime_exception("Zen::ZBullet::PhysicsZone::createHeightFieldShapeFromSerialization - Could not Open File!"); |
| 537 | | } |
| 538 | | |
| 539 | | CollisionShape* pRawPointer = new CollisionShape(); |
| 540 | | pCollisionShape_type pShape(pRawPointer, boost::bind(&PhysicsZone::destroyCollisionShape, this, _1)); |
| 541 | | |
| 542 | | dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(NewtonCreateTreeCollision(m_pZone, NULL)); |
| 543 | | |
| 544 | | dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(NewtonCreateTreeCollisionFromSerialization(m_pZone, NULL, ZBulletDeSerializeFile, file)); |
| 545 | | fclose (file); |
| 546 | | std::cout << "Finished loading TreeCollision from file." << std::endl; |
| 547 | | |
| 548 | | /* this is not description logic |
| 549 | | // Note: collision is deleted in attachBody() so do not use it after this function call. |
| 550 | | if (!attachBody((btCollisionShape *)pShape->getShapePtr())) |
| 551 | | return false; |
| 552 | | |
| 553 | | btRigidBodySetMatrix(pShape->getActorPtr(), _transform.m_array); |
| 554 | | |
| 555 | | return true; |
| 556 | | */ |