- GEadded VSYNC for Mac OS X swapbuffers (can be easily undone by commenting out...
authorErwin Coumans <blender@erwincoumans.com>
Sat, 2 Dec 2006 22:25:47 +0000 (22:25 +0000)
committerErwin Coumans <blender@erwincoumans.com>
Sat, 2 Dec 2006 22:25:47 +0000 (22:25 +0000)
- GEdon't crash when attempting to add constraint on game objects without physics controller
- GEimproved some physics -> graphics synchronization issues
- small experiment with game engine timing to smooth framerate/reduce tearing

intern/ghost/intern/GHOST_WindowCarbon.cpp
source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp
source/gameengine/Ketsji/KX_KetsjiEngine.cpp
source/gameengine/Ketsji/KX_KetsjiEngine.h
source/gameengine/Ketsji/KX_MotionState.cpp
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp

index 4484ec54da6f7302f3eb6bfdab3cf91ebd10609f..2fe03fd6664c566630fbabbe91cfa39dec2b6d15 100644 (file)
@@ -403,9 +403,19 @@ GHOST_TSuccess GHOST_WindowCarbon::setOrder(GHOST_TWindowOrder order)
     return GHOST_kSuccess;
 }
 
+#define  WAIT_FOR_VSYNC 1
+#ifdef WAIT_FOR_VSYNC
+#include <OpenGL/OpenGL.h>
+#endif
 
 GHOST_TSuccess GHOST_WindowCarbon::swapBuffers()
 {
+#ifdef WAIT_FOR_VSYNC
+/* wait for vsync, to avoid tearing artifacts */
+long VBL = 1;
+CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &VBL);
+#endif
+
     GHOST_TSuccess succeeded = GHOST_kSuccess;
     if (m_drawingContextType == GHOST_kDrawingContextTypeOpenGL) {
         if (m_aglCtx) {
index b886e4ff54a56a6f2e6a18d7cdd5c37314bfcef0..f37928fe2d51ea8ef428eb256d147e32deafac00 100644 (file)
@@ -357,10 +357,13 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                        exitrequested = ketsjiengine->GetExitCode();
                                        
                                        // kick the engine
-                                       ketsjiengine->NextFrame();
+                                       bool render = ketsjiengine->NextFrame();
                                        
-                                       // render the frame
-                                       ketsjiengine->Render();
+                                       if (render)
+                                       {
+                                               // render the frame
+                                               ketsjiengine->Render();
+                                       }
                                        
                                        // test for the ESC key
                                        while (qtest())
index 2432462fecc83ee70f97c710e158826559129035..03af40ce2c98bc8b7f2b4e5547c101692a655d65 100644 (file)
@@ -1947,29 +1947,31 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                                KX_GameObject *gotar=getGameOb(dat->tar->id.name,sumolist);
                                                                physctr2 = (PHY_IPhysicsController*) gotar->GetPhysicsController()->GetUserData();
                                                        }
-                            
-                            PHY_IPhysicsController* physctrl = (PHY_IPhysicsController*) gameobj->GetPhysicsController()->GetUserData();
-                            
-                            int constraintId = kxscene->GetPhysicsEnvironment()->createConstraint(physctrl,physctr2,(PHY_ConstraintType)dat->type,(float)dat->pivX,(float)dat->pivY,(float)dat->pivZ,(float)dat->axX,(float)dat->axY,(float)dat->axZ);
-                                                       //if it is a generic 6DOF constraint, set all the limits accordingly
-                                                       if (dat->type == PHY_GENERIC_6DOF_CONSTRAINT)
+
+                                                       if (gameobj->GetPhysicsController())
                                                        {
-                                                               int dof;
-                                                               int dofbit=1;
-                                                               for (dof=0;dof<6;dof++)
+                                                               PHY_IPhysicsController* physctrl = (PHY_IPhysicsController*) gameobj->GetPhysicsController()->GetUserData();
+                                   
+                                                               int constraintId = kxscene->GetPhysicsEnvironment()->createConstraint(physctrl,physctr2,(PHY_ConstraintType)dat->type,(float)dat->pivX,(float)dat->pivY,(float)dat->pivZ,(float)dat->axX,(float)dat->axY,(float)dat->axZ);
+                                                               //if it is a generic 6DOF constraint, set all the limits accordingly
+                                                               if (dat->type == PHY_GENERIC_6DOF_CONSTRAINT)
                                                                {
-                                                                       if (dat->flag & dofbit)
-                                                                       {
-                                                                               kxscene->GetPhysicsEnvironment()->setConstraintParam(constraintId,dof,dat->minLimit[dof],dat->maxLimit[dof]);
-                                                                       } else
+                                                                       int dof;
+                                                                       int dofbit=1;
+                                                                       for (dof=0;dof<6;dof++)
                                                                        {
-                                                                               //minLimit > maxLimit means free(disabled limit) for this degree of freedom
-                                                                               kxscene->GetPhysicsEnvironment()->setConstraintParam(constraintId,dof,1,-1);
+                                                                               if (dat->flag & dofbit)
+                                                                               {
+                                                                                       kxscene->GetPhysicsEnvironment()->setConstraintParam(constraintId,dof,dat->minLimit[dof],dat->maxLimit[dof]);
+                                                                               } else
+                                                                               {
+                                                                                       //minLimit > maxLimit means free(disabled limit) for this degree of freedom
+                                                                                       kxscene->GetPhysicsEnvironment()->setConstraintParam(constraintId,dof,1,-1);
+                                                                               }
+                                                                               dofbit<<=1;
                                                                        }
-                                                                       dofbit<<=1;
                                                                }
                                                        }
-
                         }
                 }
             }
index 145b4eb47505ded8e46aad746146b9f171a74e44..85075d6364c5a8fe25e1b9a7be7ac6379189cb50 100644 (file)
@@ -999,6 +999,9 @@ void        KX_ConvertBulletObject( class   KX_GameObject* gameobj,
                {
                        if (meshobj)
                        {
+                               bm = CreateBulletShapeFromMesh(meshobj,false);
+                               ci.m_localInertiaTensor.setValue(0.f,0.f,0.f);
+
                        //      assert(0);
 
                                        /*
index 6a54c613cc0b9373c23b0ad1bdbc421cf0aba38b..dcb8e082065f2cca44ff2e6b774174c28500d278 100644 (file)
@@ -256,9 +256,9 @@ void KX_KetsjiEngine::SetSceneConverter(KX_ISceneConverter* sceneconverter)
  */
 void KX_KetsjiEngine::StartEngine(bool clearIpo)
 {
-       m_clockTime = m_kxsystem->GetTimeInSeconds();
-       m_frameTime = m_kxsystem->GetTimeInSeconds();
-       m_previousClockTime = m_kxsystem->GetTimeInSeconds();
+       m_clockTime = 0.f;// m_kxsystem->GetTimeInSeconds();
+       m_frameTime = 0.f;//m_kxsystem->GetTimeInSeconds();
+       m_previousClockTime = 0.f;//m_kxsystem->GetTimeInSeconds();
 
        m_firstframe = true;
        m_bInitialized = true;
@@ -315,22 +315,35 @@ void KX_KetsjiEngine::EndFrame()
        // swap backbuffer (drawing into this buffer) <-> front/visible buffer
        m_rasterizer->SwapBuffers();
        m_rendertools->EndFrame(m_rasterizer);
+
        
        m_canvas->EndDraw();
        
 
 }
 
-// #include "PIL_time.h"
+//#include "PIL_time.h"
+//#include "LinearMath/btQuickprof.h"
 
-void KX_KetsjiEngine::NextFrame()
+
+bool KX_KetsjiEngine::NextFrame()
 {
-       m_logger->StartLog(tc_services, m_kxsystem->GetTimeInSeconds(), true);
-       
-       if (m_bFixedTime)
-               m_clockTime += 1.0/m_ticrate;
-       else
-               m_clockTime = m_kxsystem->GetTimeInSeconds();
+
+//     static hidden::Clock sClock;
+
+m_logger->StartLog(tc_services, m_kxsystem->GetTimeInSeconds(),true);
+
+//float dt = sClock.getTimeMicroseconds() * 0.000001f;
+//sClock.reset();
+
+if (m_bFixedTime)
+       m_clockTime += 1./m_ticrate;
+else
+{
+
+//     m_clockTime += dt;
+       m_clockTime = m_kxsystem->GetTimeInSeconds();
+}
        
        double deltatime = m_clockTime - m_frameTime;
        if (deltatime<0.f)
@@ -341,30 +354,41 @@ void KX_KetsjiEngine::NextFrame()
                m_frameTime = 0.f;
        }
 
+
        // Compute the number of logic frames to do each update (fixed tic bricks)
        int frames =int(deltatime*m_ticrate);
+//     if (frames>1)
+//             printf("****************************************");
+//     printf("dt = %f, deltatime = %f, frames = %d\n",dt, deltatime,frames);
        
 //     if (!frames)
-//             PIL_sleep_ms(4);
+//             PIL_sleep_ms(1);
        
        KX_SceneList::iterator sceneit;
        int frameOut = 5;
        
        if (frames>frameOut)
        {
-               printf("framedOut: %d\n",frames);
+       
+       //      printf("framedOut: %d\n",frames);
                m_frameTime+=(frames-frameOut)*(1.0/m_ticrate);
                frames = frameOut;
        }
+       
+
+       bool doRender = frames>0;
+
+       float remainingTimeFraction = 0.f;
 
+       if (frames>0)
+       {
+               remainingTimeFraction = (m_clockTime - m_frameTime - frames*(1.0/m_ticrate)) / frames;  
+       }
        while (frames)
        {
-               if (frames > frameOut)
-               {
-                       printf ("what happened\n");
-               }
+       
 
-               m_frameTime += 1.0/m_ticrate;
+               m_frameTime += 1.0/m_ticrate + remainingTimeFraction;
                
                for (sceneit = m_scenes.begin();sceneit != m_scenes.end(); ++sceneit)
                // for each scene, call the proceed functions
@@ -419,6 +443,7 @@ void KX_KetsjiEngine::NextFrame()
                                // Do some cleanup work for this logic frame
                                m_logger->StartLog(tc_logic, m_kxsystem->GetTimeInSeconds(), true);
                                scene->LogicUpdateFrame(m_frameTime, true);
+                               
                                scene->LogicEndFrame();
        
                                // Actuators can affect the scenegraph
@@ -448,7 +473,7 @@ void KX_KetsjiEngine::NextFrame()
                                        scene->setSuspendedTime(m_clockTime);
        
                        DoSound(scene);
-       
+                       
                        m_logger->StartLog(tc_services, m_kxsystem->GetTimeInSeconds(), true);
                }
 
@@ -472,7 +497,7 @@ void KX_KetsjiEngine::NextFrame()
                frames--;
        }
 
-       bool bUseAsyncLogicBricks= false;
+       bool bUseAsyncLogicBricks= false;//true;
 
        if (bUseAsyncLogicBricks)
        {       
@@ -532,6 +557,8 @@ void KX_KetsjiEngine::NextFrame()
        
        // Start logging time spend outside main loop
        m_logger->StartLog(tc_outside, m_kxsystem->GetTimeInSeconds(), true);
+       
+       return doRender;
 }
 
 
index ae33e8da99896cf7a6e83fc46259a4456392f059..16b53b6b6886f23ec80c34353d8e0386b3e3e107 100644 (file)
@@ -201,8 +201,8 @@ public:
        void                    SetSceneConverter(KX_ISceneConverter* sceneconverter);
        void                    SetGame2IpoMode(bool game2ipo,int startFrame);
 
-
-       void                    NextFrame();
+       ///returns true if an update happened to indicate -> Render
+       bool                    NextFrame();
        void                    Render();
        
        void                    StartEngine(bool clearIpo);
index 6de9cf3b36e9a5ea4fe15a263da193c4499085df..cd01c0466cd6deae80bfe6902813ce04f500a30d 100644 (file)
@@ -73,7 +73,7 @@ void  KX_MotionState::getWorldOrientation(float& quatIma0,float& quatIma1,float&
 void   KX_MotionState::setWorldPosition(float posX,float posY,float posZ)
 {
        m_node->SetLocalPosition(MT_Point3(posX,posY,posZ));
-
+       m_node->SetWorldPosition(MT_Point3(posX,posY,posZ));
 }
 
 void   KX_MotionState::setWorldOrientation(float quatIma0,float quatIma1,float quatIma2,float quatReal)
@@ -85,6 +85,8 @@ void  KX_MotionState::setWorldOrientation(float quatIma0,float quatIma1,float qua
        orn[3] = quatReal;
 
        m_node->SetLocalOrientation(orn);
+       m_node->SetWorldOrientation(orn);
+
 }
 
 void   KX_MotionState::calculateWorldTransformations()
index db5de7c7d4393f69be47abd901f0c1fdbb11a613..a4fdffd0b58bed51ea31322f3c620a6059cdd3e8 100644 (file)
@@ -108,8 +108,8 @@ public:
                {
                        btWheelInfo& info = m_vehicle->getWheelInfo(i);
                        PHY_IMotionState* motionState = (PHY_IMotionState*)info.m_clientInfo ;
-                       m_vehicle->updateWheelTransform(i);
-                       btTransform trans = m_vehicle->getWheelTransformWS(i);
+                       m_vehicle->updateWheelTransform(i,true);
+                       btTransform trans = m_vehicle->getWheelInfo(i).m_worldTransform;
                        btQuaternion orn = trans.getRotation();
                        const btVector3& pos = trans.getOrigin();
                        motionState->setWorldOrientation(orn.x(),orn.y(),orn.z(),orn[3]);
@@ -457,13 +457,13 @@ bool      CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep)
                ctrl->SynchronizeMotionStates(timeStep);
        }
 
-       m_dynamicsWorld->stepSimulation(timeStep,5);//5);
+       m_dynamicsWorld->stepSimulation(timeStep,2);
        
        numCtrl = GetNumControllers();
        for (i=0;i<numCtrl;i++)
        {
                CcdPhysicsController* ctrl = GetPhysicsController(i);
-               ctrl->SynchronizeMotionStates(timeStep);
+               //ctrl->SynchronizeMotionStates(timeStep);
        }
 
        for (i=0;i<m_wrapperVehicles.size();i++)