void DemoEntityManager::UpdatePhysics(float timestep) { // update the physics if (m_world) { dFloat timestepInSecunds = 1.0f / MAX_PHYSICS_FPS; unsigned64 timestepMicrosecunds = unsigned64 (timestepInSecunds * 1000000.0f); unsigned64 currentTime = dGetTimeInMicrosenconds (); unsigned64 nextTime = currentTime - m_microsecunds; int loops = 0; while ((nextTime >= timestepMicrosecunds) && (loops < MAX_PHYSICS_LOOPS)) { loops ++; dTimeTrackerEvent(__FUNCTION__); // run the newton update function if (!m_reEntrantUpdate) { m_reEntrantUpdate = true; if (m_physicsUpdate && m_world) { ClearDebugDisplay(m_world); // update the physics world if (!m_mainWindow->m_physicsUpdateMode) { NewtonUpdate (m_world, timestepInSecunds); } else { NewtonUpdateAsync(m_world, timestepInSecunds); } } m_reEntrantUpdate = false; } nextTime -= timestepMicrosecunds; m_microsecunds += timestepMicrosecunds; } if (loops) { m_physicsTime = dFloat (dGetTimeInMicrosenconds () - currentTime) / 1000000.0f; if (m_physicsTime >= MAX_PHYSICS_LOOPS * (1.0f / MAX_PHYSICS_FPS)) { m_microsecunds = currentTime; } } } }
dFloat DemoEntityManager::CalculateInteplationParam () const { unsigned64 timeStep = dGetTimeInMicrosenconds () - m_microsecunds; dFloat param = (dFloat (timeStep) * MAX_PHYSICS_FPS) / 1.0e6f; dAssert (param >= 0.0f); if (param > 1.0f) { param = 1.0f; } return param; }
void OnBeginUpdate(dFloat timestepInSecunds) { NewtonDemos* const mainWindow = m_scene->GetRootWindow(); int key = 0; static dLong timer = dGetTimeInMicrosenconds() + 100000; if (dGetTimeInMicrosenconds() > timer) { timer = dGetTimeInMicrosenconds() + 100000; key = mainWindow->GetKeyState('R'); } dVector dir = GetLookAtDirction(m_scene); dVector pos = GetCamPosition(m_scene); // fire ammo if (key) { dFloat ammo_vel = 1000.0f; dVector vel(dir.m_x*ammo_vel, dir.m_y*ammo_vel, dir.m_z*ammo_vel); FireNewtonCcdBox(m_scene->GetNewton(), pos, vel); } }
unsigned dRuntimeProfiler::GetTimeInMicrosenconds() { return unsigned (dGetTimeInMicrosenconds()); }
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(); }
void DemoEntityManager::ResetTimer() { dResetTimer(); m_microsecunds = dGetTimeInMicrosenconds (); }
NewtonCollision* CreateCollisionTree (NewtonWorld* world, DemoEntity* const entity, int materialID, bool optimize) { // measure the time to build a collision tree unsigned64 timer0 = dGetTimeInMicrosenconds(); // create the collision tree geometry NewtonCollision* collision = NewtonCreateTreeCollision(world, materialID); // set the application level callback #ifdef USE_STATIC_MESHES_DEBUG_COLLISION NewtonStaticCollisionSetDebugCallback (collision, ShowMeshCollidingFaces); #endif // prepare to create collision geometry NewtonTreeCollisionBeginBuild(collision); // iterate the entire geometry an build the collision for (DemoEntity* model = entity->GetFirst(); model; model = model->GetNext()) { dMatrix matrix (model->GetMeshMatrix() * model->CalculateGlobalMatrix(entity)); DemoMesh* const mesh = (DemoMesh*)model->GetMesh(); dAssert (mesh->IsType(DemoMesh::GetRttiType())); dFloat* const vertex = mesh->m_vertex; for (DemoMesh::dListNode* nodes = mesh->GetFirst(); nodes; nodes = nodes->GetNext()) { DemoSubMesh& segment = nodes->GetInfo(); int matID = segment.m_textureHandle; for (int i = 0; i < segment.m_indexCount; i += 3) { int index; dVector face[3]; index = segment.m_indexes[i + 0] * 3; face[0] = dVector (vertex[index + 0], vertex[index + 1], vertex[index + 2]); index = segment.m_indexes[i + 1] * 3; face[1] = dVector (vertex[index + 0], vertex[index + 1], vertex[index + 2]); index = segment.m_indexes[i + 2] * 3; face[2] = dVector (vertex[index + 0], vertex[index + 1], vertex[index + 2]); matrix.TransformTriplex (&face[0].m_x, sizeof (dVector), &face[0].m_x, sizeof (dVector), 3); // use material ids as physics materials NewtonTreeCollisionAddFace(collision, 3, &face[0].m_x, sizeof (dVector), matID); } } } NewtonTreeCollisionEndBuild(collision, optimize ? 1 : 0); // test Serialization #if 0 FILE* file = fopen ("serialize.bin", "wb"); NewtonCollisionSerialize (world, collision, DemoEntityManager::SerializeFile, file); fclose (file); NewtonDestroyCollision (collision); file = fopen ("serialize.bin", "rb"); collision = NewtonCreateCollisionFromSerialization (world, DemoEntityManager::DeserializeFile, file); fclose (file); #endif // measure the time to build a collision tree timer0 = (dGetTimeInMicrosenconds() - timer0) / 1000; return collision; }