void renderScene(int iteration) {
	RenderUtil::startRender(sCamera->getEye(), sCamera->getDir());
	
	PxScene* scene;
	PxGetPhysics().getScenes(&scene,1);
	PxU32 nbActors = scene->getNbActors(PxActorTypeSelectionFlag::eRIGID_DYNAMIC);
	if(nbActors) {
		std::vector<PxRigidActor*> actors(nbActors);
		scene->getActors(PxActorTypeSelectionFlag::eRIGID_DYNAMIC, 
							(PxActor**)&actors[0], nbActors);
		for (PxU32 i = 0; i < nbActors; i++)
			RenderUtil::renderActors(&actors[i], 1, false, PxVec3(0.3,0.3,0.3));
	}		
	if (iteration == 0) {
		char* result = "Initial Guess";
		glRasterPos2i(20,-25);
		for(int i = 0; i < strlen(result); i++)
			glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, result[i]);
	} else {
		char* label = "Iteration #";
		char buffer[10]; itoa(iteration, buffer, 10);
		char* result = new char[strlen(label)+strlen(buffer)];
		sprintf(result,"%s%s",label,buffer);
		glRasterPos2i(20,-25);
		for(int i = 0; i < strlen(result); i++)
			glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, result[i]);
	}
	
	std::string buffer, result;
	matrix<double> state = render_system->getState();
	std::string theta1 = "theta1 = " + to_string((long double) state(0,0));
	std::string theta2 = "theta2 = " + to_string((long double) state(1,0));
	std::string thetaDot1 = "thetaDot1 = " + to_string((long double) state(2,0));
	std::string thetaDot2 = "thetaDot1 = " + to_string((long double) state(3,0));
	glRasterPos2i(-35,38);
	for(int i = 0; i < theta1.length(); i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, theta1[i]);
	glRasterPos2i(-37,33);
	for(int i = 0; i < theta2.length(); i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, theta2[i]);
	glRasterPos2i(-39,28);
	for(int i = 0; i < thetaDot1.length(); i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, thetaDot1[i]);
	glRasterPos2i(-41,23);
	for(int i = 0; i < thetaDot2.length(); i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, thetaDot2[i]);
	RenderUtil::finishRender();
}
Exemplo n.º 2
0
Gameplay::~Gameplay()
{
    if( !_isUnsafeCleanup )
    {
        if( _soundTrack ) 
        {
            _soundTrack->stop();
            _soundTrack->release();
        }
        if( _fadedSoundTrack )
        {
            _fadedSoundTrack->stop();
            _fadedSoundTrack->release();
        }
    
        // delete activities
        while( _activities.size() )
        {
            Activity* activity = _activities.top();
            _activities.pop();
            activity->onBecomeInactive();
            if( _activities.size() ) 
            {
                _activities.top()->onReturnFromActivity( activity );
                delete activity;
                _activities.top()->onBecomeActive();
            }
            else
            {
                delete activity;
            }
        }
    
        // delete careers
        saveCareers();
        for( unsigned int i=0; i<_careers.size(); i++ ) delete _careers[i];

        // delete user events
        cleanupUserCommunityEvents();
    
        // delete render target
        if (_renderTarget) delete _renderTarget;
    
        // delete sdks
		PxGetPhysics().release();
		pxCooking->release();
		foundation->release();
    
        // cleanup action mapping
        destroyActionMap();

        // delete input device
        _inputDevice->release();

        delete _config;
    }

    // reset internal interface pointers
    iGameplay = NULL;
    iEngine   = NULL;
    iGui      = NULL;
    iLanguage = NULL;
    iInput    = NULL;
    iAudio    = NULL;
}
Exemplo n.º 3
0
void Scene::initializePhysics(void)
{
    // database record for scene location
    database::LocationInfo* locationInfo = database::LocationInfo::getRecord( _location->getDatabaseId() );

    // find collision geometry
    engine::IClump* collisionClump = NULL;
    callback::ClumpL clumpL;
    _extrasAsset->forAllClumps( callback::enumerateClumps, &clumpL );
    for( callback::ClumpI clumpI=clumpL.begin(); clumpI!=clumpL.end(); clumpI++ )
    {
        if( strcmp( (*clumpI)->getName(), "CollisionGeometry" ) == 0 )
        {
            collisionClump = (*clumpI);
            break;
        }
    }
    assert( collisionClump != NULL );
    collisionClump->getFrame()->translate( Vector3f( 0,0,0 ) );
    collisionClump->getFrame()->getLTM();

    // determine physics scene bounds by collision geometry
    callback::AtomicL atomicL;
    collisionClump->forAllAtomics( callback::enumerateAtomics, &atomicL );
    assert( atomicL.size() == 1 );
    _collisionGeometry = *atomicL.begin();
    Vector3f sceneInf = _collisionGeometry->getAABBInf();
    Vector3f sceneSup = _collisionGeometry->getAABBSup();
    _phSceneBounds = PxBounds3 (PxVec3(sceneInf[0], sceneInf[1], sceneInf[2]),
                                PxVec3(sceneSup[0], sceneSup[1], sceneSup[2]));

    // determine limits of scene
    _phSceneLimits.maxNbActors = locationInfo->physicsLimits.numActors;
    _phSceneLimits.maxNbBodies = locationInfo->physicsLimits.numBodies;
    //_phSceneLimits.maxNbJoints = locationInfo->physicsLimits.numJoints;
    _phSceneLimits.maxNbDynamicShapes = locationInfo->physicsLimits.numDynamicShapes;
    _phSceneLimits.maxNbStaticShapes  = locationInfo->physicsLimits.numStaticShapes;

    // initialize physics scene
    PxSceneDesc _phSceneDesc(PxGetPhysics().getTolerancesScale());
    _phSceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
    _phSceneDesc.broadPhaseType = PxBroadPhaseType::eSAP;

    //PHYSX3 fix collision filters
    //_phSceneDesc.userNotify = NULL;
    //_phSceneDesc.userTriggerReport = this;
    //_phSceneDesc.userContactReport = this;
    //_phSceneDesc.maxTimestep = simulationStepTime;
    //_phSceneDesc.maxIter = 1;
    //_phSceneDesc.timeStepMethod = NX_TIMESTEP_VARIABLE;
    //_phSceneDesc.groundPlane = false;
    //_phSceneDesc.boundsPlanes = false;
    //_phSceneDesc.collisionDetection = true;
    _phSceneDesc.userData = this;

    // create scene
    //mNbThreads = PxMax(PxI32(shdfnd::Thread::getNbPhysicalCores())-1, 0);
    //#ifdef PX_PS3
    //	mNbThreads = 1; // known issue, 0 worker threads and SPU batched query can deadlock.
    //#endif
    //PHYSX3
    mNbThreads = 1;
    if(!_phSceneDesc.cpuDispatcher) {
        mCpuDispatcher = PxDefaultCpuDispatcherCreate(mNbThreads);
        //PHYSX3
        //if(!mCpuDispatcher)
        //	fatalError("PxDefaultCpuDispatcherCreate failed!");
        _phSceneDesc.cpuDispatcher    = mCpuDispatcher;
    }
    if(!_phSceneDesc.filterShader)
        _phSceneDesc.filterShader    = gDefaultFilterShader;

#ifdef PX_WINDOWS
    if(!_phSceneDesc.gpuDispatcher && mCudaContextManager) {
        _phSceneDesc.gpuDispatcher = mCudaContextManager->getGpuDispatcher();
    }
#endif
    _phScene = PxGetPhysics().createScene(_phSceneDesc);
    assert( _phScene );

    // post-creation scene settings
    _phScene->setVisualizationCullingBox(_phSceneBounds);
    _phScene->setLimits(_phSceneLimits);

    // default material
    PxMaterial* defaultMaterial = PxGetPhysics().createMaterial(0.25f, 0.25f, 0.5f);

    // add ground plane
    PxRigidStatic* groundPlane = PxCreatePlane(PxGetPhysics(), PxPlane(0,1,0,0), *defaultMaterial);
    _phScene->addActor(*groundPlane);

    // fixed flesh material
    _phFleshMaterial = PxGetPhysics().createMaterial(0.75f, 0.75f, 0.25f);
    _phFleshMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phFleshMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phFleshMaterial );

    // moving flesh material
    _phMovingFleshMaterial = PxGetPhysics().createMaterial(0.25f, 0.25f, 0.125f);
    _phMovingFleshMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phMovingFleshMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phMovingFleshMaterial );

    // cloth material
    _phClothMaterial = PxGetPhysics().createMaterial(0.995f, 0.995f, 0.05f);
    _phClothMaterial->setFrictionCombineMode(PxCombineMode::eMAX);
    _phClothMaterial->setRestitutionCombineMode(PxCombineMode::eMULTIPLY);
    assert( _phClothMaterial );

    // build terrain mesh data
    Matrix4f collisionGeometryLTM = _collisionGeometry->getFrame()->getLTM();
    Vector3f worldVertex;
    engine::Mesh* mesh = _collisionGeometry->getGeometry()->createMesh();
    _phTerrainVerts = new PxVec3[mesh->numVertices];
    _phTerrainTriangles = new PxU32[3*mesh->numTriangles];
    //PHYSX3
    //_phTerrainMaterials = new NxMaterialIndex[mesh->numTriangles];
    unsigned int i;
    for( i=0; i<mesh->numVertices; i++ )
    {
        worldVertex = Gameplay::iEngine->transformCoord( mesh->vertices[i], collisionGeometryLTM );
        _phTerrainVerts[i] = wrap( worldVertex );
    }
    for( i=0; i<mesh->numTriangles; i++ )
    {
        _phTerrainTriangles[i*3+0] = mesh->triangles[i].vertexId[0];
        _phTerrainTriangles[i*3+1] = mesh->triangles[i].vertexId[1];
        _phTerrainTriangles[i*3+2] = mesh->triangles[i].vertexId[2];
        //PHYSX3
        //_phTerrainMaterials[i] = 0;
    }

    // initialize terrain descriptor
    _phTerrainDesc.points.count = mesh->numVertices;
    _phTerrainDesc.points.data = _phTerrainVerts;
    _phTerrainDesc.points.stride = sizeof(PxVec3);
    _phTerrainDesc.triangles.count = mesh->numTriangles;
    _phTerrainDesc.triangles.data = _phTerrainTriangles;
    _phTerrainDesc.triangles.stride = 3 * sizeof(PxU32);

    //PHYSX3
    //_phTerrainDesc.materialIndexStride = sizeof( PxMaterialIndex );
    //_phTerrainDesc.materialIndices = _phTerrainMaterials;
    //_phTerrainDesc.heightFieldVerticalAxis = NX_NOT_HEIGHTFIELD;
    //_phTerrainDesc.heightFieldVerticalExtent = -1000;

    // cook terrain

    _phTerrainMesh = Gameplay::pxCooking->createTriangleMesh(_phTerrainDesc, PxGetPhysics().getPhysicsInsertionCallback());

    assert (_phTerrainMesh);

    _phTerrain = PxGetPhysics().createRigidStatic(PxTransform(PxVec3(0.0f, 1.0f, 0.0f), PxQuat(PxHalfPi / 60.0f, PxVec3(0.0f, 1.0f, 0.0f))));

    assert (_phTerrain);

    PxTriangleMeshGeometry triGeom(_phTerrainMesh);
    PxShape* triangleMeshShape = _phTerrain->createShape(triGeom, *defaultMaterial);
    assert (triangleMeshShape);

    _phScene->addActor(*_phTerrain);

    //PHYSX3
    // retrieve terrain shape
    _phTerrain->getShapes(&_phTerrainShape, 1);
    assert( _phTerrainShape );

    Gameplay::iEngine->releaseMesh( mesh );
}