void* OgreNewtonSceneBody::AddCollisionTree (SceneNode* const treeNode) { OgreNewtonWorld* const world = (OgreNewtonWorld*) GetNewton(); // convert the nod and all its children to a newton mesh OgreNewtonMesh mesh (world, treeNode); mesh.Polygonize(); // create a collision tree mesh dNewtonCollisionMesh meshCollision (world, mesh, 0); // add this collision to the scene body return AddCollision (&meshCollision); }
void DemoEntityManager::LoadScene (const char* const fileName) { dScene database (GetNewton()); database.Deserialize(fileName); // this will apply all global the scale to the mesh database.FreezeScale(); // this will apply all local scale and transform to the mesh //database.FreezePivot(); // Load the Visual Scene EntityDictionary entDictionary; LoadVisualScene(&database, entDictionary); //Load the physics world dList<NewtonBody*> bodyList; database.SceneToNewtonWorld(m_world, bodyList); // bind every rigidBody loaded to the scene entity for (dList<NewtonBody*>::dListNode* bodyNode = bodyList.GetFirst(); bodyNode; bodyNode = bodyNode->GetNext()) { // find the user data and set to the visual entity in the scene NewtonBody* const body = bodyNode->GetInfo(); dScene::dTreeNode* const sceneNode = (dScene::dTreeNode*)NewtonBodyGetUserData(body); DemoEntity* const entity = entDictionary.Find(sceneNode)->GetInfo(); NewtonBodySetUserData(body, entity); // see if this body have some special setups dScene::dTreeNode* const node = database.FindChildByType(sceneNode, dRigidbodyNodeInfo::GetRttiType()); dAssert (node); dRigidbodyNodeInfo* const bodyData = (dRigidbodyNodeInfo*) database.GetInfoFromNode(node); dVariable* bodyType = bodyData->FindVariable("rigidBodyType"); // set the default call backs if (!bodyType || !strcmp (bodyType->GetString(), "default gravity")) { NewtonBodySetTransformCallback(body, DemoEntity::TransformCallback); NewtonBodySetForceAndTorqueCallback(body, PhysicsApplyGravityForce); NewtonBodySetDestructorCallback (body, PhysicsBodyDestructor); } } // clean up all caches the engine have saved NewtonInvalidateCache (m_world); }
void* OgreNewtonSceneBody::AddTerrain (Terrain* const terrain) { OgreNewtonWorld* const world = (OgreNewtonWorld*) GetNewton(); int width = terrain->getSize() - 1; int height = terrain->getSize() - 1; int size = width * height; // Real min = terrain->getMinHeight(); // Real max = terrain->getMaxHeight(); Real horizontalScale = (terrain->getWorldSize() / (terrain->getSize() - 1)); dNewtonScopeBuffer<dFloat> elevations(size); dNewtonScopeBuffer<char> attributes(size); for (int i = 0; i < width; i++) { int index = i * height; for (int k = 0; k < height; k++) { // for now make collsionID zero, until we can get material information from the terrain tile attributes[index] = 0; elevations[index] = terrain->getHeightAtPoint(i, k); index ++; } } // build the height field collision dNewtonCollisionHeightField terrainCollision (world, width, height, 5, 0, 1.0f, horizontalScale, &elevations[0], &attributes[0], 0); // set the offset matrix for this collision shape Vector3 posit (-(width / 2.0f) * horizontalScale, 0.0f, (height / 2.0f) * horizontalScale); Quaternion rot(Ogre::Degree(90.0f), Ogre::Vector3(0.0f, 1.0f, 0.0f)); Matrix4 matrix; matrix.makeTransform (posit, Vector3(1.0f, 1.0f, 1.0f), rot); matrix = matrix.transpose(); terrainCollision.SetMatrix (&matrix[0][0]); return AddCollision (&terrainCollision); }
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 OgreNewtonDynamicBody::OnForceAndTorque (dFloat timestep, int threadIndex) { const OgreNewtonWorld* const world = (OgreNewtonWorld*) GetNewton(); SetForce (world->GetGravity() * GetMass()); }