Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "Input.h"
  5. #include "Camera.h"
  6. #include "Math.h"
  7. #include "Renderer.h"
  8. #include "Ui.h"
  9. #include "App.h"
  10. #include "Mesh.h"
  11. #include "Light.h"
  12. #include "PointLight.h"
  13. #include "SpotLight.h"
  14. #include "Material.h"
  15. #include "Scene.h"
  16. #include "Scanner.h"
  17. #include "skybox.h"
  18. #include "map.h"
  19. #include "SkelAnim.h"
  20. #include "LightRsrc.h"
  21. #include "Parser.h"
  22. #include "ParticleEmitter.h"
  23. #include "PhyCharacter.h"
  24. #include "Renderer.h"
  25. #include "RendererInitializer.h"
  26. #include "MainRenderer.h"
  27. #include "PhyCharacter.h"
  28. #include "RigidBody.h"
  29. #include "ScriptingEngine.h"
  30. #include "StdinListener.h"
  31. #include "ModelNode.h"
  32. #include "SkelAnimModelNodeCtrl.h"
  33. #include "Model.h"
  34. #include "Logger.h"
  35. #include "Util.h"
  36. #include "HighRezTimer.h"
  37. #include "SkinNode.h"
  38. #include "Skin.h"
  39. #include "MaterialRuntime.h"
  40. // map (hard coded)
  41. ModelNode* floor__,* sarge,* horse,* crate,* pentagram;
  42. SkinNode* imp;
  43. //SkelModelNode* imp;
  44. PointLight* point_lights[10];
  45. SpotLight* spot_lights[2];
  46. ParticleEmitter* partEmitter;
  47. PhyCharacter* character;
  48. // Physics
  49. Vec<btRigidBody*> boxes;
  50. #define ARRAY_SIZE_X 5
  51. #define ARRAY_SIZE_Y 5
  52. #define ARRAY_SIZE_Z 5
  53. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  54. #define SCALING 1.
  55. #define START_POS_X -5
  56. #define START_POS_Y -5
  57. #define START_POS_Z -3
  58. void initPhysics()
  59. {
  60. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  61. Transform groundTransform;
  62. groundTransform.setIdentity();
  63. groundTransform.setOrigin(Vec3(0,-50, 0));
  64. RigidBody::Initializer init;
  65. init.mass = 0.0;
  66. init.shape = groundShape;
  67. init.startTrf = groundTransform;
  68. new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
  69. /*{
  70. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  71. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  72. float start_y = START_POS_Y;
  73. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  74. for (int k=0;k<ARRAY_SIZE_Y;k++)
  75. {
  76. for (int i=0;i<ARRAY_SIZE_X;i++)
  77. {
  78. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  79. {
  80. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  81. MeshNode* crate = new MeshNode;
  82. crate->init("models/crate0/crate0.mesh");
  83. crate->getLocalTransform().setScale(1.11);
  84. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  85. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  86. }
  87. }
  88. }
  89. }*/
  90. }
  91. //======================================================================================================================
  92. // init =
  93. //======================================================================================================================
  94. void init()
  95. {
  96. INFO("Other init...");
  97. srand(unsigned(time(NULL)));
  98. //Ui::init();
  99. // camera
  100. Camera* cam = new Camera(false, NULL);
  101. cam->setAll(MainRendererSingleton::getInstance().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  102. cam->moveLocalY(3.0);
  103. cam->moveLocalZ(5.7);
  104. cam->moveLocalX(-0.3);
  105. AppSingleton::getInstance().setActiveCam(cam);
  106. // lights
  107. point_lights[0] = new PointLight();
  108. point_lights[0]->init("maps/temple/light0.light");
  109. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  110. point_lights[1] = new PointLight();
  111. point_lights[1]->init("maps/temple/light1.light");
  112. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  113. spot_lights[0] = new SpotLight();
  114. spot_lights[0]->init("maps/temple/light2.light");
  115. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  116. spot_lights[1] = new SpotLight();
  117. spot_lights[1]->init("maps/temple/light3.light");
  118. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  119. // horse
  120. horse = new ModelNode();
  121. horse->init("meshes/horse/horse.mdl");
  122. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(), 1.0));
  123. // Sponza
  124. ModelNode* sponza = new ModelNode();
  125. //sponza->init("maps/sponza/sponza.mdl");
  126. sponza->init("maps/sponza-crytek/sponza_crytek.mdl");
  127. sponza->setLocalTransform(Transform(Vec3(0.0), Mat3::getIdentity(), 0.05));
  128. // Pentagram
  129. pentagram = new ModelNode();
  130. pentagram->init("models/pentagram/pentagram.mdl");
  131. pentagram->setLocalTransform(Transform(Vec3(2, 0, 0), Mat3::getIdentity(), 1.0));
  132. // Imp
  133. imp = new SkinNode();
  134. imp->init("models/imp/imp.skin");
  135. imp->skelAnimModelNodeCtrl = new SkelAnimModelNodeCtrl(*imp);
  136. imp->skelAnimModelNodeCtrl->set(imp->getSkin().getSkelAnims()[0].get());
  137. imp->skelAnimModelNodeCtrl->setStep(0.8);
  138. return;
  139. // sarge
  140. /*sarge = new MeshNode();
  141. sarge->init("meshes/sphere/sphere16.mesh");
  142. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  143. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  144. // floor
  145. floor__ = new MeshNode();
  146. floor__->init("maps/temple/Cube.019.mesh");
  147. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));*/
  148. // imp
  149. /*imp = new SkelModelNode();
  150. imp->init("models/imp/imp.smdl");
  151. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  152. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  153. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  154. ctrl->step = 0.8;*/
  155. // cave map
  156. /*for(int i=1; i<21; i++)
  157. {
  158. MeshNode* node = new MeshNode();
  159. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  160. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  161. }*/
  162. // sponza map
  163. /*MeshNode* node = new MeshNode();
  164. node->init("maps/sponza/floor.mesh");
  165. node = new MeshNode();
  166. node->init("maps/sponza/walls.mesh");
  167. node = new MeshNode();
  168. node->init("maps/sponza/light-marbles.mesh");
  169. node = new MeshNode();
  170. node->init("maps/sponza/dark-marbles.mesh");*/
  171. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  172. return;
  173. // particle emitter
  174. partEmitter = new ParticleEmitter;
  175. partEmitter->init("asdf");
  176. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  177. // character
  178. /*PhyCharacter::Initializer init;
  179. init.sceneNode = imp;
  180. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  181. character = new PhyCharacter(SceneSingleton::getInstance().getPhysics(), init);*/
  182. // crate
  183. /*crate = new MeshNode;
  184. crate->init("models/crate0/crate0.mesh");
  185. crate->scaleLspace = 1.0;*/
  186. //
  187. //floor_ = new floor_t;
  188. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  189. initPhysics();
  190. //INFO("Engine initialization ends (" << (App::getTicks() - ticks) << ")");
  191. }
  192. //======================================================================================================================
  193. // =
  194. //======================================================================================================================
  195. void mainLoopExtra()
  196. {
  197. InputSingleton::getInstance().handleEvents();
  198. float dist = 0.2;
  199. float ang = toRad(3.0);
  200. float scale = 0.01;
  201. // move the camera
  202. static SceneNode* mover = AppSingleton::getInstance().getActiveCam();
  203. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_1)) mover = AppSingleton::getInstance().getActiveCam();
  204. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_2)) mover = point_lights[0];
  205. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_3)) mover = spot_lights[0];
  206. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_4)) mover = point_lights[1];
  207. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_5)) mover = spot_lights[1];
  208. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_6)) mover = imp;
  209. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_M) == 1) InputSingleton::getInstance().warpMouse = !InputSingleton::getInstance().warpMouse;
  210. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_A)) mover->moveLocalX(-dist);
  211. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_D)) mover->moveLocalX(dist);
  212. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LSHIFT)) mover->moveLocalY(dist);
  213. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_SPACE)) mover->moveLocalY(-dist);
  214. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_W)) mover->moveLocalZ(-dist);
  215. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_S)) mover->moveLocalZ(dist);
  216. if(!InputSingleton::getInstance().warpMouse)
  217. {
  218. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_UP)) mover->rotateLocalX(ang);
  219. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_DOWN)) mover->rotateLocalX(-ang);
  220. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LEFT)) mover->rotateLocalY(ang);
  221. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_RIGHT)) mover->rotateLocalY(-ang);
  222. }
  223. else
  224. {
  225. float accel = 44.0;
  226. mover->rotateLocalX(ang * InputSingleton::getInstance().mouseVelocity.y() * accel);
  227. mover->rotateLocalY(-ang * InputSingleton::getInstance().mouseVelocity.x() * accel);
  228. }
  229. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Q)) mover->rotateLocalZ(ang);
  230. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_E)) mover->rotateLocalZ(-ang);
  231. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEUP)) mover->getLocalTransform().getScale() += scale ;
  232. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEDOWN)) mover->getLocalTransform().getScale() -= scale ;
  233. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_K)) AppSingleton::getInstance().getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  234. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_I))
  235. character->moveForward(0.1);
  236. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_T))
  237. {
  238. pentagram->getModelPatchNodees()[0]->setUserDefVar(PatchNode::MT_BOTH, "specularCol", Vec3(10.0, -1.6, 1.6));
  239. pentagram->getModelPatchNodees()[0]->getCpMtlRun().getUserDefinedVarByName("shininess").get<float>() = 10.0;
  240. }
  241. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_O) == 1)
  242. {
  243. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  244. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  245. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  246. //body->clearForces();
  247. body->forceActivationState(ACTIVE_TAG);
  248. }
  249. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Y) == 1)
  250. {
  251. INFO("Exec script");
  252. ScriptingEngineSingleton::getInstance().execScript(Util::readFile("test.py").c_str());
  253. }
  254. mover->getLocalTransform().getRotation().reorthogonalize();
  255. //INFO(mover->getSceneNodeName())
  256. /*if(spot_lights[0]->getCamera().insideFrustum(spot_lights[1]->getCamera()))
  257. {
  258. INFO("in");
  259. }
  260. else
  261. {
  262. INFO("out");
  263. }*/
  264. }
  265. //======================================================================================================================
  266. // mainLoop =
  267. //======================================================================================================================
  268. void mainLoop()
  269. {
  270. INFO("Entering main loop");
  271. HighRezTimer mainLoopTimer;
  272. mainLoopTimer.start();
  273. do
  274. {
  275. HighRezTimer timer;
  276. timer.start();
  277. mainLoopExtra();
  278. AppSingleton::getInstance().execStdinScpripts();
  279. SceneSingleton::getInstance().getPhysics().update(timer.getCrntTime());
  280. SceneSingleton::getInstance().updateAllWorldStuff();
  281. SceneSingleton::getInstance().doVisibilityTests(*AppSingleton::getInstance().getActiveCam());
  282. /*SceneSingleton::getInstance().doVisibilityTests(spot_lights[0]->getCamera());
  283. AppSingleton::getInstance().getActiveCam()->getVisibleMsRenderableNodes().clear();
  284. AppSingleton::getInstance().getActiveCam()->getVisibleMsRenderableNodes() = spot_lights[0]->getCamera().getVisibleMsRenderableNodes();
  285. AppSingleton::getInstance().getActiveCam()->getVisiblePointLights() = spot_lights[0]->getCamera().getVisiblePointLights();
  286. AppSingleton::getInstance().getActiveCam()->getVisibleSpotLights() = spot_lights[0]->getCamera().getVisibleSpotLights();*/
  287. SceneSingleton::getInstance().updateAllControllers();
  288. MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
  289. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_ESCAPE))
  290. {
  291. break;
  292. }
  293. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F11))
  294. {
  295. AppSingleton::getInstance().togleFullScreen();
  296. }
  297. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F12) == 1)
  298. {
  299. MainRendererSingleton::getInstance().takeScreenshot("gfx/screenshot.jpg");
  300. }
  301. AppSingleton::getInstance().swapBuffers();
  302. uint a = timer.getElapsedTime();
  303. uint b = AppSingleton::getInstance().getTimerTick();
  304. uint timeToSpendForRsrcPostProcess;
  305. if(a < b)
  306. {
  307. timeToSpendForRsrcPostProcess = b - a;
  308. }
  309. else
  310. {
  311. timeToSpendForRsrcPostProcess = 1;
  312. }
  313. ResourceManagerSingleton::getInstance().postProcessFinishedLoadingRequests(timeToSpendForRsrcPostProcess);
  314. if(1)
  315. {
  316. //AppSingleton::getInstance().waitForNextFrame();
  317. timer.stop();
  318. if(timer.getElapsedTime() < AppSingleton::getInstance().getTimerTick())
  319. {
  320. SDL_Delay(AppSingleton::getInstance().getTimerTick() - timer.getElapsedTime());
  321. }
  322. }
  323. else
  324. {
  325. if(MainRendererSingleton::getInstance().getFramesNum() == 5000)
  326. {
  327. break;
  328. }
  329. }
  330. }while(true);
  331. INFO("Exiting main loop (" << mainLoopTimer.getElapsedTime() << ")");
  332. }
  333. //======================================================================================================================
  334. // main =
  335. //======================================================================================================================
  336. int main(int argc, char* argv[])
  337. {
  338. try
  339. {
  340. AppSingleton::getInstance().init(argc, argv);
  341. init();
  342. mainLoop();
  343. INFO("Exiting...");
  344. AppSingleton::getInstance().quit(EXIT_SUCCESS);
  345. return 0;
  346. }
  347. catch(std::exception& e)
  348. {
  349. ERROR("Aborting: " << e.what());
  350. //abort();
  351. return 1;
  352. }
  353. }