Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <typeinfo>
  5. #include "Common.h"
  6. #include "Input.h"
  7. #include "Camera.h"
  8. #include "Math.h"
  9. #include "Renderer.h"
  10. #include "Ui.h"
  11. #include "App.h"
  12. #include "particles.h"
  13. #include "Texture.h"
  14. #include "Mesh.h"
  15. #include "Light.h"
  16. #include "collision.h"
  17. #include "Material.h"
  18. #include "Resource.h"
  19. #include "Scene.h"
  20. #include "Scanner.h"
  21. #include "skybox.h"
  22. #include "map.h"
  23. #include "MeshNode.h"
  24. #include "SkelModelNode.h"
  25. #include "MeshNode.h"
  26. #include "SkelAnim.h"
  27. #include "MeshSkelNodeCtrl.h"
  28. #include "SkelAnimCtrl.h"
  29. #include "SkelNode.h"
  30. #include "LightProps.h"
  31. #include "BulletDebuger.h"
  32. #include "PhyCommon.h"
  33. #include "Parser.h"
  34. #include "ParticleEmitter.h"
  35. #include "PhyCharacter.h"
  36. #include "Renderer.hpp"
  37. App* app = NULL;
  38. // map (hard coded)
  39. MeshNode* floor__,* sarge,* horse,* crate;
  40. SkelModelNode* imp;
  41. PointLight* point_lights[10];
  42. SpotLight* spot_lights[2];
  43. ParticleEmitter* partEmitter;
  44. // Physics
  45. BulletDebuger debugDrawer;
  46. Vec<btRigidBody*> boxes;
  47. #define ARRAY_SIZE_X 5
  48. #define ARRAY_SIZE_Y 5
  49. #define ARRAY_SIZE_Z 5
  50. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  51. #define SCALING 1.
  52. #define START_POS_X -5
  53. #define START_POS_Y -5
  54. #define START_POS_Z -3
  55. void initPhysics()
  56. {
  57. btDiscreteDynamicsWorld* dynamicsWorld = app->getScene()->getPhyWorld()->getDynamicsWorld();
  58. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  59. btTransform groundTransform;
  60. groundTransform.setIdentity();
  61. groundTransform.setOrigin(btVector3(0,-50,0));
  62. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  63. {
  64. btScalar mass(0.);
  65. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  66. bool isDynamic = (mass != 0.f);
  67. btVector3 localInertia(0,0,0);
  68. if (isDynamic)
  69. groundShape->calculateLocalInertia(mass,localInertia);
  70. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  71. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  72. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  73. btRigidBody* body = new btRigidBody(rbInfo);
  74. //add the body to the dynamics world
  75. dynamicsWorld->addRigidBody(body);
  76. }
  77. {
  78. //create a few dynamic rigidbodies
  79. // Re-using the same collision is better for memory usage and performance
  80. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  81. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  82. /// Create Dynamic Objects
  83. btTransform startTransform;
  84. startTransform.setIdentity();
  85. btScalar mass(1.0);
  86. btVector3 localInertia(0,0,0);
  87. colShape->calculateLocalInertia(mass,localInertia);
  88. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  89. float start_y = START_POS_Y;
  90. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  91. btRigidBody* body;
  92. for (int k=0;k<ARRAY_SIZE_Y;k++)
  93. {
  94. for (int i=0;i<ARRAY_SIZE_X;i++)
  95. {
  96. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  97. {
  98. /*startTransform.setOrigin(SCALING*btVector3(
  99. btScalar(2.0*i + start_x),
  100. btScalar(20+2.0*k + start_y),
  101. btScalar(2.0*j + start_z)));*/
  102. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  103. MeshNode* crate = new MeshNode;
  104. crate->init( "models/crate0/crate0.mesh" );
  105. crate->getLocalTransform().setScale( 1.11 );
  106. Transform trf( SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0 );
  107. body = app->getScene()->getPhyWorld()->createNewRigidBody( mass, trf, colShape, crate );
  108. /*MotionState* myMotionState = new MotionState( startTransform, crate);
  109. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  110. //btRigidBody* body = new btRigidBody(rbInfo);
  111. body = new btRigidBody(rbInfo);
  112. //if( i=2 ) body->setActivationState(ISLAND_SLEEPING);
  113. //body->setActivationState(ISLAND_SLEEPING);
  114. dynamicsWorld->addRigidBody(body);
  115. //body->setGravity( toBt( Vec3( Util::randRange(-1.0, 1.0), Util::randRange(-1.0, 1.0), Util::randRange(-1.0, 1.0) ) ) );*/
  116. boxes.push_back( body );
  117. }
  118. }
  119. }
  120. }
  121. //dynamicsWorld->setDebugDrawer(&debugDrawer);
  122. /*for( int i=0; i<app->getScene()->getPhyWorld()->getDynamicsWorld()->getCollisionObjectArray().size();i++ )
  123. {
  124. btCollisionObject* colObj = app->getScene()->getPhyWorld()->getDynamicsWorld()->getCollisionObjectArray()[i];
  125. btRigidBody* body = btRigidBody::upcast(colObj);
  126. if( body )
  127. {
  128. if( body->getMotionState() )
  129. {
  130. MotionState* myMotionState = (MotionState*)body->getMotionState();
  131. myMotionState->m_graphicsWorldTrans = myMotionState->m_startWorldTrans;
  132. body->setCenterOfMassTransform( myMotionState->m_graphicsWorldTrans );
  133. colObj->setInterpolationWorldTransform( myMotionState->m_startWorldTrans );
  134. colObj->forceActivationState(ACTIVE_TAG);
  135. colObj->activate();
  136. colObj->setDeactivationTime(0);
  137. //colObj->setActivationState(WANTS_DEACTIVATION);
  138. }
  139. //removed cached contact points (this is not necessary if all objects have been removed from the dynamics world)
  140. //m_dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(colObj->getBroadphaseHandle(),getDynamicsWorld()->getDispatcher());
  141. btRigidBody* body = btRigidBody::upcast(colObj);
  142. if (body && !body->isStaticObject())
  143. {
  144. btRigidBody::upcast(colObj)->setLinearVelocity(btVector3(0,0,0));
  145. btRigidBody::upcast(colObj)->setAngularVelocity(btVector3(0,0,0));
  146. }
  147. }
  148. }*/
  149. }
  150. //=====================================================================================================================================
  151. // init =
  152. //=====================================================================================================================================
  153. void init()
  154. {
  155. INFO( "Engine initializing..." );
  156. srand( unsigned(time(NULL)) );
  157. mathSanityChecks();
  158. app->initWindow();
  159. uint ticks = app->getTicks();
  160. R::init();
  161. Ui::init();
  162. // camera
  163. Camera* cam = new Camera( R::aspectRatio*toRad(60.0), toRad(60.0), 0.5, 200.0 );
  164. cam->moveLocalY( 3.0 );
  165. cam->moveLocalZ( 5.7 );
  166. cam->moveLocalX( -0.3 );
  167. app->setActiveCam( cam );
  168. // lights
  169. point_lights[0] = new PointLight();
  170. point_lights[0]->init( "maps/temple/light0.light" );
  171. point_lights[0]->setLocalTransform( Transform( Vec3( -1.0, 2.4, 1.0 ), Mat3::getIdentity(), 1.0 ) );
  172. point_lights[1] = new PointLight();
  173. point_lights[1]->init( "maps/temple/light1.light" );
  174. point_lights[1]->setLocalTransform( Transform( Vec3( 2.5, 1.4, 1.0 ), Mat3::getIdentity(), 1.0 ) );
  175. spot_lights[0] = new SpotLight();
  176. spot_lights[0]->init( "maps/temple/light2.light" );
  177. spot_lights[0]->setLocalTransform( Transform( Vec3( 1.3, 4.3, 3.0 ), Mat3( Euler(toRad(-20), toRad(20), 0.0) ), 1.0 ) );
  178. spot_lights[1] = new SpotLight();
  179. spot_lights[1]->init( "maps/temple/light3.light" );
  180. spot_lights[1]->setLocalTransform( Transform( Vec3( -2.3, 6.3, 2.9 ), Mat3( Euler(toRad(-70), toRad(-20), 0.0) ), 1.0 ) );
  181. // horse
  182. horse = new MeshNode();
  183. horse->init( "meshes/horse/horse.mesh" );
  184. horse->setLocalTransform( Transform( Vec3( -2, 0, 1 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.5 ) );
  185. // sarge
  186. sarge = new MeshNode();
  187. sarge->init( "meshes/sphere/sphere16.mesh" );
  188. //sarge->setLocalTransform( Vec3( 0, -2.8, 1.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 1.1 );
  189. sarge->setLocalTransform( Transform( Vec3( 0, 2.0, 2.0 ), Mat3::getIdentity(), 0.4 ) );
  190. // floor
  191. floor__ = new MeshNode();
  192. floor__->init( "maps/temple/Cube.019.mesh" );
  193. floor__->setLocalTransform( Transform( Vec3(0.0, -0.19, 0.0), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.8 ) );
  194. // imp
  195. imp = new SkelModelNode();
  196. imp->init( "models/imp/imp.smdl" );
  197. imp->setLocalTransform( Transform( Vec3( 0.0, 2.11, 0.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.7 ) );
  198. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim = Rsrc::skelAnims.load( "models/imp/walk.imp.anim" );
  199. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
  200. // particle emitter
  201. partEmitter = new ParticleEmitter;
  202. partEmitter->init( NULL );
  203. partEmitter->getLocalTransform().setOrigin( Vec3( 3.0, 0.0, 0.0 ) );
  204. // crate
  205. /*crate = new MeshNode;
  206. crate->init( "models/crate0/crate0.mesh" );
  207. crate->scaleLspace = 1.0;*/
  208. //
  209. //floor_ = new floor_t;
  210. //floor_->material = Rsrc::materials.load( "materials/default.mtl" );
  211. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  212. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  213. app->getScene()->skybox.load( skybox_fnames );
  214. initPhysics();
  215. INFO( "Engine initialization ends (" << App::getTicks()-ticks << ")" );
  216. }
  217. //=====================================================================================================================================
  218. // main =
  219. //=====================================================================================================================================
  220. int main( int argc, char* argv[] )
  221. {
  222. App::printAppInfo();
  223. app = new App( argc, argv );
  224. init();
  225. INFO( "Entering main loop" );
  226. int ticks = App::getTicks();
  227. do
  228. {
  229. int ticks_ = App::getTicks();
  230. I::handleEvents();
  231. R::prepareNextFrame();
  232. float dist = 0.2;
  233. float ang = toRad(3.0);
  234. float scale = 0.01;
  235. // move the camera
  236. static SceneNode* mover = app->getActiveCam();
  237. if( I::keys[ SDL_SCANCODE_1 ] ) mover = app->getActiveCam();
  238. if( I::keys[ SDL_SCANCODE_2 ] ) mover = point_lights[0];
  239. if( I::keys[ SDL_SCANCODE_3 ] ) mover = spot_lights[0];
  240. if( I::keys[ SDL_SCANCODE_4 ] ) mover = point_lights[1];
  241. if( I::keys[ SDL_SCANCODE_5 ] ) mover = spot_lights[1];
  242. if( I::keys[ SDL_SCANCODE_6 ] ) mover = partEmitter;
  243. if( I::keys[ SDL_SCANCODE_M ] == 1 ) I::warpMouse = !I::warpMouse;
  244. if( I::keys[ SDL_SCANCODE_A ] ) mover->moveLocalX( -dist );
  245. if( I::keys[ SDL_SCANCODE_D ] ) mover->moveLocalX( dist );
  246. if( I::keys[ SDL_SCANCODE_LSHIFT ] ) mover->moveLocalY( dist );
  247. if( I::keys[ SDL_SCANCODE_SPACE ] ) mover->moveLocalY( -dist );
  248. if( I::keys[ SDL_SCANCODE_W ] ) mover->moveLocalZ( -dist );
  249. if( I::keys[ SDL_SCANCODE_S ] ) mover->moveLocalZ( dist );
  250. if( !I::warpMouse )
  251. {
  252. if( I::keys[ SDL_SCANCODE_UP ] ) mover->rotateLocalX( ang );
  253. if( I::keys[ SDL_SCANCODE_DOWN ] ) mover->rotateLocalX( -ang );
  254. if( I::keys[ SDL_SCANCODE_LEFT ] ) mover->rotateLocalY( ang );
  255. if( I::keys[ SDL_SCANCODE_RIGHT ] ) mover->rotateLocalY( -ang );
  256. }
  257. else
  258. {
  259. float accel = 44.0;
  260. mover->rotateLocalX( ang * I::mouseVelocity.y * accel );
  261. mover->rotateLocalY( -ang * I::mouseVelocity.x * accel );
  262. }
  263. if( I::keys[ SDL_SCANCODE_Q ] ) mover->rotateLocalZ( ang );
  264. if( I::keys[ SDL_SCANCODE_E ] ) mover->rotateLocalZ( -ang );
  265. if( I::keys[ SDL_SCANCODE_PAGEUP ] ) mover->getLocalTransform().getScale() += scale ;
  266. if( I::keys[ SDL_SCANCODE_PAGEDOWN ] ) mover->getLocalTransform().getScale() -= scale ;
  267. if( I::keys[ SDL_SCANCODE_K ] ) app->getActiveCam()->lookAtPoint( point_lights[0]->getWorldTransform().getOrigin() );
  268. if( I::keys[ SDL_SCANCODE_O ] == 1 )
  269. {
  270. btRigidBody* body = static_cast<btRigidBody*>( boxes[0] );
  271. //body->getMotionState()->setWorldTransform( toBt( Mat4( Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0 ) ) );
  272. body->setWorldTransform( toBt( Mat4( Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0 ) ) );
  273. //body->clearForces();
  274. body->forceActivationState( ACTIVE_TAG );
  275. }
  276. mover->getLocalTransform().getRotation().reorthogonalize();
  277. //static_cast<btRigidBody*>(dynamicsWorld->getCollisionObjectArray()[1])->getMotionState()->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  278. //dynamicsWorld->getCollisionObjectArray()[3]->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  279. app->getScene()->updateAllControllers();
  280. app->getScene()->updateAllWorldStuff();
  281. //partEmitter->update();
  282. app->getScene()->getPhyWorld()->getDynamicsWorld()->stepSimulation( app->timerTick );
  283. app->getScene()->getPhyWorld()->getDynamicsWorld()->debugDrawWorld();
  284. R::render( *app->getActiveCam() );
  285. //map.octree.root->bounding_box.render();
  286. // print some debug stuff
  287. Ui::setColor( Vec4(1.0, 1.0, 1.0, 1.0) );
  288. Ui::setPos( -0.98, 0.95 );
  289. Ui::setFontWidth( 0.03 );
  290. Ui::printf( "frame:%d fps:%dms\n", R::framesNum, (App::getTicks()-ticks_) );
  291. //Ui::print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
  292. /*Ui::printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  293. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z) );*/
  294. if( I::keys[SDL_SCANCODE_ESCAPE] ) break;
  295. if( I::keys[SDL_SCANCODE_F11] ) app->togleFullScreen();
  296. if( I::keys[SDL_SCANCODE_F12] == 1 ) R::takeScreenshot("gfx/screenshot.jpg");
  297. /*char str[128];
  298. sprintf( str, "capt/%06d.jpg", R::framesNum );
  299. R::takeScreenshot(str);*/
  300. // std stuff follow
  301. app->swapBuffers();
  302. R::printLastError();
  303. if( 1 )
  304. {
  305. //if( R::framesNum == 10 ) R::takeScreenshot("gfx/screenshot.tga");
  306. app->waitForNextFrame();
  307. }
  308. else
  309. if( R::framesNum == 5000 ) break;
  310. }while( true );
  311. INFO( "Exiting main loop (" << App::getTicks()-ticks << ")" );
  312. INFO( "Exiting..." );
  313. app->quitApp( EXIT_SUCCESS );
  314. return 0;
  315. }