table.cpp

00001 
00002 #include "main.h"
00003 #include "table.h"
00004 
00005 
00006 // constructor
00007 Table::Table()
00008 : nxActor(NULL), node(NULL)
00009 {
00010         logger->log( "Table ctor()" );
00011 }
00012 
00013 
00014 // destructor
00015 Table::~Table()
00016 {
00017         if ( NULL != this->nxActor )
00018         {
00019                 // todo: Avoid release calls while the scene is simulating (in between simulate() and fetchResults() calls)
00020                 nxScene->releaseActor( *this->nxActor );
00021                 this->nxActor = NULL;
00022         }
00023         if ( NULL != this->node )
00024         {
00025                 this->node->remove();
00026                 this->node = NULL;
00027         }
00028         logger->log( "Table dtor()" );
00029 }
00030 
00031 
00032 
00033 ERR_TYPE Table::update( u32 timeMS )
00034 {
00035         // nothing to do here, because of static mesh
00036         return ERR_OK;
00037 }
00038 
00039 
00040 
00041 // apply mesh and texture files and make physX actor
00042 ERR_TYPE Table::load( stringc meshFileName, stringc diffuseMapFileName, vector3df scale )
00043 {
00044         IAnimatedMesh* tableMesh = smgr->getMesh( meshFileName.c_str() );
00045         if ( NULL == tableMesh )
00046         {
00047                 return ERR_TABLE_LOAD_FAILED;
00048         }
00049 
00050         // load diffuse map
00051         ITexture* diffuseMap = driver->getTexture( diffuseMapFileName.c_str() );
00052         if ( NULL == diffuseMap )
00053         {
00054                 return ERR_TABLE_LOAD_FAILED;
00055         }
00056         
00057         // add scene node
00058         this->node = smgr->addAnimatedMeshSceneNode( tableMesh );
00059         if ( NULL == this->node )
00060         {
00061                 return ERR_TABLE_LOAD_FAILED;
00062         }
00063 
00064         // set desired scale
00065         this->node->setScale( scale );
00066         
00067         // dynamic lighting NO
00068         this->node->setMaterialFlag( EMF_LIGHTING, false );
00069 
00070         // apply texture at material settings
00071         this->node->setMaterialTexture( 0, diffuseMap );
00072 
00073         // only allow texture to appear either on front or on back side of table mesh
00074         // do not display texture on "side polygons"
00075         // get the meshBuffer and iterate all vertices
00076         S3DVertex* vertices = (S3DVertex*) this->node->getMesh()->getMesh(0)->getMeshBuffer(0)->getVertices();
00077         u32 vertexCount = this->node->getMesh()->getMesh(0)->getMeshBuffer(0)->getVertexCount();
00078         for ( u32 i=0; i<vertexCount; i++ )
00079         {
00080                 // check if the normal of the vertex faces not up
00081                 if ( false == vertices[i].Normal.equals( vector3df(0.0f, 1.0f, 0.0f ) ) )
00082                 {
00083                         // disable texture on side ploygon
00084                         vertices[i].TCoords.X = 0.0f;
00085                         vertices[i].TCoords.Y = 0.0f;
00086                 }
00087         }
00088         
00089         // shininess works only with solid material with dynamic lighting ON
00090         this->node->getMaterial(0).SpecularColor.set( 255, 255, 255, 255 );
00091         this->node->getMaterial(0).Shininess = 20.0f;
00092         
00093         return ERR_OK;
00094 }
00095 
00096 
00097 // not used anymore, now done by XML dataset:
00098 ERR_TYPE Table::createNxActor( vector3df size )
00099 {
00100         // box dimension in [m] on a side
00101         NxReal irrToPhysxScaler = 0.4f;
00102         NxVec3 boxDim( size.X * irrToPhysxScaler, 1.0f, size.Z * irrToPhysxScaler );
00103 
00104     // Add a single-shape actor to the scene
00105     NxActorDesc actorDesc;
00106     NxBodyDesc bodyDesc;
00107 
00108     // The actor has one shape, a box
00109     NxBoxShapeDesc boxDesc;
00110         boxDesc.dimensions.set( boxDim );
00111 
00112         actorDesc.shapes.pushBack( &boxDesc );
00113 
00114     actorDesc.body = &bodyDesc;
00115     actorDesc.density = 10000.0f;
00116 
00117     this->nxActor = nxScene->createActor( actorDesc );
00118         if ( NULL == this->nxActor )
00119         {
00120                 return ERR_TABLE_CREATEACTOR_FAILED;
00121         }
00122 
00123         this->nxActor->setName( "Table" );
00124         // make it static in space
00125         this->nxActor->setGlobalPosition( NxVec3(50, 50, 0) );
00126         this->nxActor->raiseBodyFlag( NX_BF_FROZEN );
00127 
00128         return ERR_OK;
00129 }

Generated on Sun Dec 2 03:10:23 2007 for TableTop by  doxygen 1.5.4