Changeset 3363

Show
Ignore:
Timestamp:
02/03/10 16:44:16 (6 weeks ago)
Author:
Azaezel
Message:

simplified direct serialization from raw

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsZone.cpp

    r3361 r3363  
    3737#include <Zen/Core/Math/Vector4.hpp> 
    3838 
     39#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" 
    3940//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    4041namespace Zen { 
     
    410411    pCollisionShape_type pShape(pRawPointer, boost::bind(&PhysicsZone::destroyCollisionShape, this, _1)); 
    411412 
    412     dynamic_cast<CollisionShape*>(pShape.get())->setShapePtr(NewtonCreateTreeCollision(m_pZone, NULL)); 
    413     std::cout << "\nTreeCollision construction begun from RAW file..." << std::endl; 
    414     NewtonTreeCollisionBeginBuild((dynamic_cast<CollisionShape*>(pShape.get())->getShapePtr())); 
    415  
    416413    // TODO - verify the file is a RAW file 
    417414    FILE* file; 
    418415    file = fopen(_filename.c_str(), "rb"); 
    419416    if( file== NULL ) 
    420         { 
     417    { 
    421418        std::cout << "Error: could not open file " << _filename.c_str() << "in CollisionShape::initHeightFieldShape()." << std::endl; 
    422          
    423             throw Zen::Utility::runtime_exception("Zen::ZBullet::PhysicsZone::createHeightFieldShapeFromRaw - Could not Open File!"); 
     419        throw Zen::Utility::runtime_exception("Zen::ZBullet::PhysicsZone::createHeightFieldShapeFromRaw - Could not Open File!"); 
    424420    } 
    425421 
     
    436432        { 
    437433            fread(&height, sizeof(U16), 1, file); 
    438             pHeightFieldArray[column + (row * _size)] = height / heightScaleFactor; 
     434            pHeightFieldArray[column + (row * _size)] = height; 
    439435        } 
    440436    } 
    441437    fclose(file); 
    442438 
    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; 
    492442    delete pHeightFieldArray; 
    493443 
    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//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     448PhysicsZone::pCollisionShape_type 
     449PhysicsZone::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     
    512470    /* this is not description logic 
    513471    // Note: collision is deleted in attachBody() so do not use it after this function call. 
     
    519477    return true; 
    520478    */ 
    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     */ 
    557479    return pShape; 
    558480}