Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

KX_KetsjiEngine.cpp

Go to the documentation of this file.
00001                 
00002 #ifdef WIN32
00003 #pragma warning (disable : 4786)
00004 #endif //WIN32
00005 
00006 #include "KX_KetsjiEngine.h"
00007 
00008 
00009 #include "ListValue.h"
00010 #include "IntValue.h"
00011 #include "VectorValue.h"
00012 #include "BoolValue.h"
00013 #include "FloatValue.h"
00014 
00015 
00016 
00017 #define KX_NUM_ITERATIONS 4
00018 #include "RAS_BucketManager.h"
00019 #include "RAS_OpenGLRasterizer.h"
00020 #include "RAS_IRenderTools.h"
00021 #include "RAS_ICanvas.h"
00022 
00023 #include "InputParser.h"
00024 #include "StdString.h"
00025 
00026 #include "MT_Vector3.h"
00027 #include "MT_Transform.h"
00028 
00029 #include "KX_Scene.h"
00030 
00031 #include "KX_Matrix4x4.h"
00032 
00033 #include "SCA_KeyboardManager.h"
00034 #include "SCA_MouseManager.h"
00035 #include "SCA_AlwaysEventManager.h"
00036 #include "KX_TouchEventManager.h"
00037 #include "SCA_PropertyEventManager.h"
00038 #include "SCA_TimeEventManager.h"
00039 #include "SCA_RandomEventManager.h"
00040 #include "KX_GameObject.h"
00041 #include "KX_EmptyObject.h"
00042 #include "RAS_MeshObject.h"
00043 #include "SCA_LogicManager.h"
00044 #include "SCA_ISensor.h"
00045 #include "SCA_IController.h"
00046 #include "KX_Camera.h"
00047 #include "KX_Light.h"
00048 #include "SCA_ISystem.h"
00049 
00050 #include "KX_BlenderGL.h"
00051 #include "KX_BlenderDevice.h"
00052 
00053 #include "SM_Scene.h"
00054 #include "SND_IScene.h"
00055 #include "SYS_System.h"
00056 
00057 
00058 const MT_Scalar UpperBoundForFuzzicsIntegrator = 0.01; // At least 100Hz (isn't this CPU hungry ?)
00059 
00060 
00064 
00065 bool useketsji = true;//false;
00066 
00067 KX_KetsjiEngine::KX_KetsjiEngine(SCA_ISystem* system)
00068 :m_bInitialized(false)
00069         ,m_activecam(0),
00070         m_viewdist(0),
00071         m_rasty(NULL),
00072         m_sndscene(NULL),
00073         m_bUseKetsjiRenderer(useketsji)

00074 

00075 

00076 {
00077         m_kxsystem = system;
00078         SYS_SystemHandle syshandle = SYS_GetSystem();
00079         int usefixed = SYS_GetCommandLineInt(syshandle,"fixedtime",0);
00080         m_bFixedTime = (usefixed > 0);
00081 
00082 }
00083 
00087 KX_KetsjiEngine::~KX_KetsjiEngine()

00088 {
00089         
00090 }
00091 
00092 // todo: replace by new texturing load/processing system (with shader language etc)
00093 extern "C" void update_realtime_textures();
00094 
00095 
00096 void                            KX_KetsjiEngine::Run()

00097 {
00098         m_kxsystem->SetMainLoop(this);
00099         m_kxsystem->StartMainLoop();
00100 }
00101 
00106 
00107 
00108 int KX_KetsjiEngine::MainLoopCallback()

00109 {
00110 
00111         // for backwards compatibility taken from prototype gameengine
00112         // when all unwanted dependencies are resolved
00113         // this mainloop can be enhanced
00114 
00115         // 20 Jan 2001: start enhancing mainloop stuff :)
00116 
00117         double deltatime=0.1; 
00118         unsigned short event=0;
00119         
00120         float time = m_kxsystem->GetTimeInSeconds()*100.0;//->BL_blender_timer();
00121         m_dtime+= (time - m_lasttime);
00122         m_lasttime= time;
00123         //if (m_dtime<1.0)
00124         //      m_kxsystem->Sleep(1);
00125 
00126         // do some blender specific things
00127         BL_BeginTimeFrame(m_dtime);
00128         
00129                 
00130         if(m_dtime>25) 
00131         {
00132                 m_dtime= 25;
00133         }
00134         
00135         
00136         bool myupdate = false;
00137         
00138         //while(m_dtime>=2) 
00139         //if(m_dtime>=1) 
00140         {
00141                 m_dtime -= 1.0;
00142 
00143                 if (m_dtime >= 1)
00144                 {
00145                         if(BL_UpdateGdfra()) 
00146                                 update_realtime_textures();
00147                         
00148                         m_dtime -= 1;
00149                 }
00150         
00151         //BL_BuildSectorList();
00152         
00153                 deltatime = 0.02;
00154                 double curtime;
00155                 
00156 
00157                 if (m_bFixedTime)
00158                 {
00159                         curtime= m_previoustime + deltatime;    
00160                 } else
00161                 {
00162                         curtime = m_kxsystem->GetTimeInSeconds();
00163                 }
00164 
00165                 if (m_previoustime)             {       
00166                         deltatime = curtime - m_previoustime;
00167                 } 
00168                 m_previoustime = curtime;
00169         
00170                 m_swapTime = deltatime;//m_dtime;
00171         
00172 
00173                 
00174         
00175         m_sumoScene->proceed(deltatime, UpperBoundForFuzzicsIntegrator);
00176 
00177         m_sndscene->proceed(curtime,deltatime);
00178 
00179         // for linked objects, the matrix hierarchy has to be updated
00180         // necessary for both linked objects to dynas as well as ipos
00181         UpdateParents(curtime);
00182         
00183         // update system devices
00184         m_kxsystem->NextFrame();
00185 
00186         
00187         // process sensors, and controllers
00188         m_scene->LogicBeginFrame(curtime,deltatime);
00189         
00190         // process actuators
00191         m_scene->LogicUpdateFrame(curtime,deltatime);           
00192         
00193         // do some cleanup work for this logic frame
00194         m_scene->LogicEndFrame();
00195 
00196         UpdateParents(curtime);
00197 
00198         // let the physics system do it's job
00199         //m_sumoScene->proceed(deltatime, UpperBoundForFuzzicsIntegrator);
00200 
00201         // for linked objects, the matrix hierarchy has to be updated
00202         // necessary for both linked objects to dynas as well as ipos
00203         //UpdateParents(curtime);
00204 
00205         // do the rendering
00206         NextFrame(m_lasttime,deltatime,m_rasty);
00207 
00208         }
00209         // backwards compatibility thing:
00210         // a slow machine would deadlock on some systems
00211         // when deltatime grew too large (spiral effect)
00212 
00213 
00214 
00215         
00216         return 0;
00217 }
00218 
00219 
00220 // update graphics
00221 void KX_KetsjiEngine::NextFrame (double curtime,double realdeltatime,RAS_IRasterizer* rasty)

00222 {
00223 
00224 
00225         
00226         m_rasty->BeginFrame();
00227         this->m_rendertools->BeginFrame(m_rasty);
00228 
00229         
00230         double deltatime = realdeltatime;
00231         if (deltatime > 0.2)
00232         {
00233                 deltatime = 0.2;
00234         }
00235         
00236 
00237 
00238         KX_Camera* cam = m_scene->GetActiveCamera();
00239         
00240         CMatrix4x4 projmat = m_projectionmat;
00241         rasty->SetProjectionMatrix(projmat);
00242 
00243         MT_Scalar cammat[16];
00244         cam->GetWorldToCamera().getValue(cammat);
00245         CMatrix4x4 viewmat = cammat;
00246         m_rasty->SetViewMatrix(viewmat,cam->NodeGetWorldPosition());
00247 
00248 
00249         KX_Camera* realcam = (KX_Camera*)m_scene->GetCameraList()->GetValue(0);
00250 
00251         KX_Renderbuckets(cam->GetWorldToCamera(),m_rasty,       m_viewdist,m_rendertools);
00252 
00253         m_scene->CalculateVisibleMeshes(m_rasty);
00254         // display things that don't go into a materialbucket, like camera's and lights
00255         
00256         m_rasty->EndFrame();
00257 
00258         MT_Point3 listenerposition = cam->NodeGetWorldPosition();
00259         m_sndscene->SetListenerPosition(listenerposition);
00260 
00261         // BL_RenderDebugInfo();
00262 
00263         CCString debugtext;
00264         debugtext.Format("swap : %f",m_swapTime);
00265         m_rendertools->RenderText2D(debugtext,m_canvas->GetWidth()/2,14);
00266 
00267         if (m_rasty->GetDrawingMode() < RAS_IRasterizer::KX_TEXTURED)
00268         {
00269                 RenderDebugProperties();
00270                 RenderNonMeshes();
00271         }
00272 
00273         m_rasty->SwapBuffers();
00274         this->m_rendertools->EndFrame(m_rasty);
00275         
00276 }
00277 
00278 void KX_KetsjiEngine::RenderNonMeshes()

00279 {
00280         int i;
00281         
00282         for (i=0;i<m_scene->GetObjectList()->GetCount();i++)
00283         {
00284                 
00285                 /*
00286                 KX_IClientObject* clientobj = ((KX_GameObject*)m_scene->GetObjectList()->GetValue(i))->GetClientObject();

00287                 if (clientobj)

00288                 {

00289                         m_rasty->LoadViewMatrix();

00290                         clientobj->DisplaySymbolicShape(m_rasty->GetDrawingMode());

00291                 }

00292                 */
00293                 //Object* blenderobj = (Object*) ((KX_GameObject*)m_scene->GetObjectList()->GetValue(i))->GetBlenderObject();
00294                 //if (blenderobj)
00295                 //{
00296                         
00297                 
00298                 //}
00299                 
00300         }
00301         
00302 }
00303 
00307 void KX_KetsjiEngine::UpdateParents(double curtime)

00308 {
00309         int i=0;
00310         
00311         int numrootobjects = m_scene->GetRootParentList()->GetCount();
00312         for (i=0;i<m_scene->GetRootParentList()->GetCount();i++)
00313         {
00314                 KX_GameObject* parentobj = (KX_GameObject*) m_scene->GetRootParentList()->GetValue(i);
00315                 parentobj->NodeUpdateGS(curtime,true);
00316         }
00317 }
00318 
00319 
00320 
00325 
00326 bool KX_KetsjiEngine::Init(Global* glob,ScrArea* curarea,RAS_ICanvas* canvas,
00327                                                    RAS_IRenderTools* rendertools)//,KX_Camera* camera)
00328 {
00329         m_canvas = canvas;
00330         m_rendertools = rendertools;
00331 
00332         m_previoustime = 0.0;
00333         m_dtime = 4;
00334         m_missedtime = 0.0;
00335 
00336         BL_setviewmatrixview3d();
00337 
00338         
00339         // maximum viewing distance
00340         m_viewdist = BL_GetViewDistance(glob);
00341 
00342         KX_Camera* activecam = NULL;
00343 
00344         if (m_scene->GetActiveCamera())
00345         {
00346                 activecam = m_scene->GetActiveCamera();
00347         } else
00348         {
00349                 activecam = new KX_Camera(m_scene,KX_Scene::m_callbacks);
00350 
00351                 const float* cammatdata = BL_GetViewMatrix(glob);
00352         
00353                 MT_Transform trans=MT_Transform(cammatdata);
00354                 MT_Transform camtrans;
00355                 camtrans.invert(trans);
00356         
00357         
00358                 //m_pCamera = camera;
00359                 // set transformation
00360                 activecam->NodeSetLocalPosition(camtrans.getOrigin());
00361                 activecam->NodeSetLocalOrientation(camtrans.getBasis());
00362                 activecam->NodeUpdateGS(0,true);
00363 
00364                 m_scene->GetCameraList()->Add(activecam);
00365                 m_scene->SetActiveCamera(activecam);
00366 
00367                 m_scene->GetRootParentList()->Add(activecam->AddRef());
00368         
00369                 activecam->SetWorldToCamera(MT_Transform(cammatdata));
00370         }
00371 
00372         m_projectionmat=CMatrix4x4(  *BL_GetProjectionMatrix(curarea));//CMatrix4x4(curarea->winmat);
00373         
00374 
00375         
00376         // create 3D graphics rasterizing object
00377         m_rasty = new RAS_OpenGLRasterizer(m_canvas);
00378 
00379         int drawtype = BL_GetDrawType(glob);
00380         
00381         
00382         m_rasty->SetDrawingMode(drawtype);
00383 
00384         // Initialize graphics subsystem
00385         if (m_rasty->Init())
00386         {
00387                 // enable fogging if requested
00388         
00389                 
00390                 // convert Blender datastructures               
00391                 BL_BlenderWorldInfo info = BL_GetBlenderWorldInfo(glob);
00392 
00393                 if(info.hasworld)
00394                         {
00395 
00396                         if (m_rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {        
00397                                 
00398                                 m_rasty->SetBackColor(
00399                                                 info.backcolor_red,
00400                                                 info.backcolor_green,
00401                                                 info.backcolor_blue,
00402                                         0.0f);
00403                                 
00404                                 if (info.hasmist)
00405                                 {
00406                                         m_rasty->SetFog(
00407                                                         info.backcolor_miststart,
00408                                                         info.backcolor_mistdistance,
00409                                                         info.backcolor_mistcolor_red,
00410                                                         info.backcolor_mistcolor_green,
00411                                                         info.backcolor_mistcolor_blue
00412                                                 );
00413                                 }
00414 
00415                                 
00416                         }
00417                 
00418 
00419                         //MT_Vector3 gravity(0,0,-info.gravity);
00420                         //cam->SetProperty("gravity",new CVectorValue(gravity.getValue()));
00421                 }
00422 
00423                 // Create a system object, mainly for providing accurate time
00424                 // and system specific devices like keyboard, mouse etc.
00425                 // Can be used for logging errors/warnings, debugging and other purposes in the future
00426                 
00427 //              m_sndscene->init();
00428                                 
00429                 m_bInitialized=true;
00430                 
00431         }
00432 
00433  
00434         
00435         UpdateParents(0.0);
00436         
00437         
00438 
00439         m_lasttime = m_kxsystem->GetTimeInSeconds()*100.0;//BL_blender_timer();
00440         
00441         return m_bInitialized;
00442                 
00443 }
00444 
00445 void KX_KetsjiEngine::Exit()

00446 {
00447         if (m_bInitialized)
00448         {
00449                 // cleanup all the stuff
00450                 RAS_BucketManagerClearAll();
00451                 
00452                 m_rasty->Exit();
00453 
00454                 delete m_rasty;
00455                 delete m_canvas;
00456                 delete m_rendertools;
00457                 
00458         }
00459 }
00460 
00461 void    KX_KetsjiEngine::SetScene(KX_Scene* scene)

00462 { 
00463                 m_scene = scene;
00464                 m_sumoScene = scene->GetSumoScene();
00465                 m_sndscene = (SND_IScene*)scene->GetSoundScene();
00466 }
00467 
00468 
00469 void KX_KetsjiEngine::RenderDebugProperties()

00470 {
00471 
00472         CCString debugtxt;
00473         int xcoord = 10;// mmmm, these constants were taken from blender source
00474         int ycoord = 14;// to 'mimic' behaviour
00475 
00476         vector<SCA_DebugProp*>& debugproplist = m_scene->GetDebugProperties();
00477 
00478         for (vector<SCA_DebugProp*>::iterator it = debugproplist.begin();
00479         !(it==debugproplist.end());it++)
00480         {
00481                 
00482                 CValue* propobj = (*it)->m_obj;
00483                 CCString objname = propobj->GetName();
00484                 CCString propname = (*it)->m_name;
00485                 CValue* propval = propobj->GetProperty(propname);
00486                 if (propval)
00487                 {
00488                         CCString text = propval->GetText();
00489                         debugtxt = objname + "." + propname + " = " + text;
00490                         m_rendertools->RenderText2D(debugtxt.Ptr(),xcoord,ycoord);
00491                         ycoord += 14;
00492 
00493                 }
00494                 
00495         }
00496 }

Generated at Thu Feb 1 13:03:05 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000