コード例 #1
0
void NewtonDemos::CalculateFPS(float timestep)
{
	m_framesCount ++;
	m_timestepAcc += timestep;

	// this probably happing on loading of and a pause, just rest counters
	if ((m_timestepAcc <= 0.0f) || (m_timestepAcc > 2.0f)){
		m_timestepAcc = 0;
		m_framesCount = 0;
	}

	//update fps every quarter of a second
	if (m_timestepAcc >= 0.25f) {
		m_fps = float (m_framesCount) / m_timestepAcc;
		m_timestepAcc -= 0.25f;
		m_framesCount = 0.0f;

		wxString statusText;
		NewtonWorld* const world = m_scene->GetNewton();
		char platform[256];
		NewtonGetDeviceString(world, NewtonGetCurrentDevice(world), platform, sizeof (platform));
		int memoryUsed = NewtonGetMemoryUsed() / (1024) ;
		
		statusText.Printf (wxT ("render fps: %7.2f"), m_fps);
		m_statusbar->SetStatusText (statusText, 0);

		statusText.Printf (wxT ("physics time: %4.2f ms"), m_scene->GetPhysicsTime() * 1000.0f);
		m_statusbar->SetStatusText (statusText, 1);

		statusText.Printf (wxT ("memory: %d kbytes"), memoryUsed);
		m_statusbar->SetStatusText (statusText, 2);

		statusText.Printf (wxT ("bodies: %d"), NewtonWorldGetBodyCount(world));
		m_statusbar->SetStatusText (statusText, 3);

		statusText.Printf (wxT ("threads: %d"), NewtonGetThreadsCount(world));
		m_statusbar->SetStatusText (statusText, 4);

		statusText.Printf (wxT ("auto sleep: %s"), m_autoSleepState ? wxT("on") : wxT("off"));
		m_statusbar->SetStatusText (statusText, 5);

		char floatMode[128];
		NewtonGetDeviceString (world, m_hardwareDevice, floatMode, sizeof (floatMode));
		statusText.Printf (wxT ("instructions: %s"),  wxString::FromAscii(floatMode).wc_str());
		m_statusbar->SetStatusText (statusText, 6);
	}
}
コード例 #2
0
DemoEntityManager::~DemoEntityManager(void)
{
	// is we are run asynchronous we need make sure no update in on flight.
	if (m_world) {
		NewtonWaitForUpdateToFinish (m_world);
	}

	glDeleteLists(m_font, 96);	
	ReleaseTexture(m_fontImage);

	Cleanup ();

	// destroy the empty world
	if (m_world) {
		NewtonDestroy (m_world);
		m_world = NULL;
	}
	dAssert (NewtonGetMemoryUsed () == 0);

	delete m_context;
}
コード例 #3
0
void DemoEntityManager::RenderFrame ()
{
	dTimeTrackerEvent(__FUNCTION__);

	// Make context current
	if (m_mainWindow->m_suspendVisualUpdates) {
		return;
	}

	dFloat timestep = dGetElapsedSeconds();	
	m_mainWindow->CalculateFPS(timestep);

	// update the the state of all bodies in the scene
	unsigned64 time0 = dGetTimeInMicrosenconds ();
	UpdatePhysics(timestep);
	unsigned64 time1 = dGetTimeInMicrosenconds ();
	m_mainThreadPhysicsTime = dFloat ((time1 - time0) / 1000.0f);

	// Get the interpolated location of each body in the scene
	m_cameraManager->InterpolateMatrices (this, CalculateInteplationParam());

	// Our shading model--Goraud (smooth). 
	glShadeModel (GL_SMOOTH);

	// Culling. 
	glCullFace (GL_BACK);
	glFrontFace (GL_CCW);
	glEnable (GL_CULL_FACE);

	//	glEnable(GL_DITHER);

	// z buffer test
	glEnable(GL_DEPTH_TEST);
	glDepthFunc (GL_LEQUAL);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
	glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);

	glClearColor (0.5f, 0.5f, 0.5f, 0.0f );
	//glClear( GL_COLOR_BUFFER_BIT );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	// set default lightning
	//	glDisable(GL_BLEND);
	glEnable (GL_LIGHTING);

	// make sure the model view matrix is set to identity before setting world space ligh sources
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	dFloat cubeColor[] = { 1.0f, 1.0f, 1.0f, 1.0 };
	glMaterialParam(GL_FRONT, GL_SPECULAR, cubeColor);
	glMaterialParam(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cubeColor);
	glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// one light form the Camera eye point
	GLfloat lightDiffuse0[] = { 0.5f, 0.5f, 0.5f, 0.0 };
	GLfloat lightAmbient0[] = { 0.0f, 0.0f, 0.0f, 0.0 };
	dVector camPosition (m_cameraManager->GetCamera()->m_matrix.m_posit);
	GLfloat lightPosition0[] = {camPosition.m_x, camPosition.m_y, camPosition.m_z};

	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient0);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse0);
	glLightfv(GL_LIGHT0, GL_SPECULAR, lightDiffuse0);
	glEnable(GL_LIGHT0);


	// set just one directional light
	GLfloat lightDiffuse1[] = { 0.7f, 0.7f, 0.7f, 0.0 };
	GLfloat lightAmbient1[] = { 0.2f, 0.2f, 0.2f, 0.0 };
	GLfloat lightPosition1[] = { -500.0f, 200.0f, 500.0f, 0.0 };

	glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
	glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient1);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse1);
	glLightfv(GL_LIGHT1, GL_SPECULAR, lightDiffuse1);
	glEnable(GL_LIGHT1);

	// update Camera
	m_cameraManager->GetCamera()->SetViewMatrix(GetWidth(), GetHeight());

	// render all entities
	if (m_mainWindow->m_hideVisualMeshes) {
		if (m_sky) {
			glPushMatrix();	
			m_sky->Render(timestep, this);
			glPopMatrix();
		}

	} else {
		for (dListNode* node = dList<DemoEntity*>::GetFirst(); node; node = node->GetNext()) {
			DemoEntity* const entity = node->GetInfo();
			glPushMatrix();	
			entity->Render(timestep, this);
			glPopMatrix();
		}
	}

	if (m_tranparentHeap.GetCount()) {
		dMatrix modelView;
		glGetFloat (GL_MODELVIEW_MATRIX, &modelView[0][0]);
		while (m_tranparentHeap.GetCount()) {
			const TransparentMesh& transparentMesh = m_tranparentHeap[0];
			glLoadIdentity();
			glLoadMatrix(&transparentMesh.m_matrix[0][0]);
			transparentMesh.m_mesh->RenderTransparency();
			m_tranparentHeap.Pop();
		}
		glLoadMatrix(&modelView[0][0]);
	}


	m_cameraManager->RenderPickedTarget ();

	if (m_mainWindow->m_showContactPoints) {
		RenderContactPoints (GetNewton());
	}

	if (m_mainWindow->m_showNormalForces) {
		RenderNormalForces (GetNewton());
	}

	if (m_mainWindow->m_showNormalForces) {
//	if (1) {
		// see if there is a vehicle controller and 
		void* const vehListerNode = NewtonWorldGetPreListener(GetNewton(), VEHICLE_PLUGIN_NAME);
		if (vehListerNode) {
			CustomVehicleControllerManager* const manager = (CustomVehicleControllerManager*)NewtonWorldGetListenerUserData(GetNewton(), vehListerNode);
			manager->Debug();
		}

		void* const characterListerNode = NewtonWorldGetPreListener(GetNewton(), PLAYER_PLUGIN_NAME);
		if (characterListerNode) {
			CustomPlayerControllerManager* const manager = (CustomPlayerControllerManager*)NewtonWorldGetListenerUserData(GetNewton(), characterListerNode);
			manager->Debug();
		}
	}



	if (m_mainWindow->m_showAABB) {
		RenderAABB (GetNewton());
	}

	if (m_mainWindow->m_showCenterOfMass) {
		RenderCenterOfMass (GetNewton());
	}

	if (m_mainWindow->m_showJoints) {
		RenderJointsDebugInfo (GetNewton(), 0.5f);
	}


   DEBUG_DRAW_MODE mode = m_solid;
   if (m_mainWindow->m_debugDisplayMode) {
      mode = (m_mainWindow->m_debugDisplayMode == 1) ? m_solid : m_lines;
      DebugRenderWorldCollision (GetNewton(), mode);
   }

	if (m_mainWindow->m_showStatistics) {
		dVector color (1.0f, 1.0f, 1.0f, 0.0f);
		Print (color, 10,  20, "render fps: %7.2f", m_mainWindow->m_fps);
		Print (color, 10,  42, "physics time on main thread: %7.2f ms", GetPhysicsTime() * 1000.0f);
		Print (color, 10,  64, "total memory: %d kbytes", NewtonGetMemoryUsed() / (1024));
		Print (color, 10,  86, "number of bodies: %d", NewtonWorldGetBodyCount(GetNewton()));
		Print (color, 10, 108, "number of threads: %d", NewtonGetThreadsCount(GetNewton()));
		Print (color, 10, 130, "auto sleep: %s", m_mainWindow->m_autoSleepState ? "on" : "off");
	}

	int lineNumber = 130 + 22;

	if (m_renderHood) {

		// set display for 2d render mode

		dFloat width = GetWidth();
		dFloat height = GetHeight();

		glColor3f(1.0, 1.0, 1.0);

		glPushMatrix();
		glMatrixMode(GL_PROJECTION);

		glLoadIdentity();
		gluOrtho2D(0, width, 0, height);
		
			glPushMatrix();
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			glDisable(GL_LIGHTING);
			glDisable(GL_DEPTH_TEST);
			glEnable(GL_TEXTURE_2D);	

			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

			// render 2d display
			m_renderHood (this, m_renderHoodContext, lineNumber);

			// restore display mode
			glMatrixMode(GL_PROJECTION);
			glPopMatrix();


		glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
	}


	// draw everything and swap the display buffer
	glFlush();

	// Swap
	SwapBuffers();
}
コード例 #4
0
void DemoEntityManager::Cleanup ()
{
	// is we are run asynchronous we need make sure no update in on flight.
	if (m_world) {
		NewtonWaitForUpdateToFinish (m_world);
	}

	// destroy all remaining visual objects
	while (dList<DemoEntity*>::GetFirst()) {
		RemoveEntity (dList<DemoEntity*>::GetFirst());
	}

	m_sky = NULL;

	// destroy the Newton world
	if (m_world) {
		// get serialization call back before destroying the world
		NewtonDestroy (m_world);
		m_world = NULL;
	}

	//	memset (&demo, 0, sizeof (demo));
	// check that there are no memory leak on exit
	dAssert (NewtonGetMemoryUsed () == 0);

	// create the newton world
	m_world = NewtonCreate();

	// link the work with this user data
	NewtonWorldSetUserData(m_world, this);

	// set joint serialization call back
	CustomJoint::Initalize(m_world);

	// add all physics pre and post listeners
	//	m_preListenerManager.Append(new DemoVisualDebugerListener("visualDebuger", m_world));
	new DemoEntityListener (this);
	m_cameraManager = new DemoCameraListener(this);
	//	m_postListenerManager.Append (new DemoAIListener("aiManager"));

	// set the default parameters for the newton world
	// set the simplified solver mode (faster but less accurate)
	NewtonSetSolverModel (m_world, 4);

	// newton 300 does not have world size, this is better controlled by the client application
	//dVector minSize (-500.0f, -500.0f, -500.0f);
	//dVector maxSize ( 500.0f,  500.0f,  500.0f);
	//NewtonSetWorldSize (m_world, &minSize[0], &maxSize[0]); 

	// set the performance track function
	//NewtonSetPerformanceClock (m_world, dRuntimeProfiler::GetTimeInMicrosenconds);

	// clean up all caches the engine have saved
	NewtonInvalidateCache (m_world);

	// Set the Newton world user data
	NewtonWorldSetUserData(m_world, this);


	// we start without 2d render
	m_renderHood = NULL;
	m_renderHoodContext = NULL;
}
コード例 #5
0
ファイル: msp_newton.cpp プロジェクト: ralphxyz/MSPhysics
VALUE MSPhysics::Newton::get_memory_used(VALUE self) {
	return Util::to_value( NewtonGetMemoryUsed() );
}
コード例 #6
0
ファイル: Physics.cpp プロジェクト: BigFax/NazaraEngine
unsigned int NzPhysics::GetMemoryUsed()
{
	return NewtonGetMemoryUsed();
}