Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "LightData.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. // map (hard coded)
  40. ModelNode* floor__,* sarge,* horse,* crate;
  41. SkinNode* imp;
  42. //SkelModelNode* imp;
  43. PointLight* point_lights[10];
  44. SpotLight* spot_lights[2];
  45. ParticleEmitter* partEmitter;
  46. PhyCharacter* character;
  47. // Physics
  48. Vec<btRigidBody*> boxes;
  49. #define ARRAY_SIZE_X 5
  50. #define ARRAY_SIZE_Y 5
  51. #define ARRAY_SIZE_Z 5
  52. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  53. #define SCALING 1.
  54. #define START_POS_X -5
  55. #define START_POS_Y -5
  56. #define START_POS_Z -3
  57. void initPhysics()
  58. {
  59. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  60. Transform groundTransform;
  61. groundTransform.setIdentity();
  62. groundTransform.setOrigin(Vec3(0,-50, 0));
  63. RigidBody::Initializer init;
  64. init.mass = 0.0;
  65. init.shape = groundShape;
  66. init.startTrf = groundTransform;
  67. new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
  68. /*{
  69. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  70. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  71. float start_y = START_POS_Y;
  72. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  73. for (int k=0;k<ARRAY_SIZE_Y;k++)
  74. {
  75. for (int i=0;i<ARRAY_SIZE_X;i++)
  76. {
  77. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  78. {
  79. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  80. MeshNode* crate = new MeshNode;
  81. crate->init("models/crate0/crate0.mesh");
  82. crate->getLocalTransform().setScale(1.11);
  83. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  84. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  85. }
  86. }
  87. }
  88. }*/
  89. }
  90. //======================================================================================================================
  91. // init =
  92. //======================================================================================================================
  93. void init()
  94. {
  95. INFO("Other init...");
  96. srand(unsigned(time(NULL)));
  97. uint ticks = AppSingleton::getInstance().getTicks();
  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. // Pentagram
  127. ModelNode* pentagram = new ModelNode();
  128. //pentagram->init("models/pentagram/pentagram.mdl");
  129. pentagram->init("models/imp/imp.mdl");
  130. pentagram->setLocalTransform(Transform(Vec3(2, 0, 0), Mat3::getIdentity(), 1.0));
  131. // Imp
  132. /*imp = new SkinNode();
  133. imp->init("models/imp/imp.skin");
  134. imp->skelAnimModelNodeCtrl = new SkelAnimModelNodeCtrl(*imp);
  135. imp->skelAnimModelNodeCtrl->set(imp->getSkin().getSkelAnims()[0].get());
  136. imp->skelAnimModelNodeCtrl->setStep(0.8);*/
  137. return;
  138. // sarge
  139. /*sarge = new MeshNode();
  140. sarge->init("meshes/sphere/sphere16.mesh");
  141. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  142. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  143. // floor
  144. floor__ = new MeshNode();
  145. floor__->init("maps/temple/Cube.019.mesh");
  146. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));*/
  147. // imp
  148. /*imp = new SkelModelNode();
  149. imp->init("models/imp/imp.smdl");
  150. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  151. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  152. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  153. ctrl->step = 0.8;*/
  154. // cave map
  155. /*for(int i=1; i<21; i++)
  156. {
  157. MeshNode* node = new MeshNode();
  158. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  159. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  160. }*/
  161. // sponza map
  162. /*MeshNode* node = new MeshNode();
  163. node->init("maps/sponza/floor.mesh");
  164. node = new MeshNode();
  165. node->init("maps/sponza/walls.mesh");
  166. node = new MeshNode();
  167. node->init("maps/sponza/light-marbles.mesh");
  168. node = new MeshNode();
  169. node->init("maps/sponza/dark-marbles.mesh");*/
  170. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  171. return;
  172. // particle emitter
  173. partEmitter = new ParticleEmitter;
  174. partEmitter->init("asdf");
  175. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  176. // character
  177. /*PhyCharacter::Initializer init;
  178. init.sceneNode = imp;
  179. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  180. character = new PhyCharacter(SceneSingleton::getInstance().getPhysics(), init);*/
  181. // crate
  182. /*crate = new MeshNode;
  183. crate->init("models/crate0/crate0.mesh");
  184. crate->scaleLspace = 1.0;*/
  185. //
  186. //floor_ = new floor_t;
  187. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  188. initPhysics();
  189. INFO("Engine initialization ends (" << (App::getTicks() - ticks) << ")");
  190. }
  191. //======================================================================================================================
  192. // =
  193. //======================================================================================================================
  194. void mainLoopExtra()
  195. {
  196. InputSingleton::getInstance().handleEvents();
  197. float dist = 0.2;
  198. float ang = toRad(3.0);
  199. float scale = 0.01;
  200. // move the camera
  201. static SceneNode* mover = AppSingleton::getInstance().getActiveCam();
  202. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_1)) mover = AppSingleton::getInstance().getActiveCam();
  203. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_2)) mover = point_lights[0];
  204. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_3)) mover = spot_lights[0];
  205. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_4)) mover = point_lights[1];
  206. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_5)) mover = spot_lights[1];
  207. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_6)) mover = horse;
  208. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_M) == 1) InputSingleton::getInstance().warpMouse = !InputSingleton::getInstance().warpMouse;
  209. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_A)) mover->moveLocalX(-dist);
  210. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_D)) mover->moveLocalX(dist);
  211. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LSHIFT)) mover->moveLocalY(dist);
  212. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_SPACE)) mover->moveLocalY(-dist);
  213. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_W)) mover->moveLocalZ(-dist);
  214. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_S)) mover->moveLocalZ(dist);
  215. if(!InputSingleton::getInstance().warpMouse)
  216. {
  217. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_UP)) mover->rotateLocalX(ang);
  218. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_DOWN)) mover->rotateLocalX(-ang);
  219. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LEFT)) mover->rotateLocalY(ang);
  220. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_RIGHT)) mover->rotateLocalY(-ang);
  221. }
  222. else
  223. {
  224. float accel = 44.0;
  225. mover->rotateLocalX(ang * InputSingleton::getInstance().mouseVelocity.y() * accel);
  226. mover->rotateLocalY(-ang * InputSingleton::getInstance().mouseVelocity.x() * accel);
  227. }
  228. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Q)) mover->rotateLocalZ(ang);
  229. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_E)) mover->rotateLocalZ(-ang);
  230. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEUP)) mover->getLocalTransform().getScale() += scale ;
  231. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEDOWN)) mover->getLocalTransform().getScale() -= scale ;
  232. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_K)) AppSingleton::getInstance().getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  233. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_I))
  234. character->moveForward(0.1);
  235. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_O) == 1)
  236. {
  237. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  238. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  239. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  240. //body->clearForces();
  241. body->forceActivationState(ACTIVE_TAG);
  242. }
  243. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Y) == 1)
  244. {
  245. INFO("Exec script");
  246. ScriptingEngineSingleton::getInstance().execScript(Util::readFile("test.py").c_str());
  247. }
  248. mover->getLocalTransform().getRotation().reorthogonalize();
  249. }
  250. //======================================================================================================================
  251. // mainLoop =
  252. //======================================================================================================================
  253. void mainLoop()
  254. {
  255. INFO("Entering main loop");
  256. int ticks = App::getTicks();
  257. do
  258. {
  259. HighRezTimer timer;
  260. timer.start();
  261. mainLoopExtra();
  262. AppSingleton::getInstance().execStdinScpripts();
  263. SceneSingleton::getInstance().getPhysics().update(timer.getCrntTime());
  264. SceneSingleton::getInstance().updateAllWorldStuff();
  265. SceneSingleton::getInstance().updateAllControllers();
  266. SceneSingleton::getInstance().doVisibilityTests(*AppSingleton::getInstance().getActiveCam());
  267. MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
  268. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_ESCAPE))
  269. {
  270. break;
  271. }
  272. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F11))
  273. {
  274. AppSingleton::getInstance().togleFullScreen();
  275. }
  276. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F12) == 1)
  277. {
  278. MainRendererSingleton::getInstance().takeScreenshot("gfx/screenshot.jpg");
  279. }
  280. AppSingleton::getInstance().swapBuffers();
  281. uint timeToSpendForRsrcPostProcess = AppSingleton::getInstance().getTimerTick() - timer.getElapsedTime();
  282. ResourceManagerSingleton::getInstance().postProcessFinishedLoadingRequests(timeToSpendForRsrcPostProcess);
  283. if(1)
  284. {
  285. //AppSingleton::getInstance().waitForNextFrame();
  286. timer.stop();
  287. if(timer.getElapsedTime() < AppSingleton::getInstance().getTimerTick())
  288. {
  289. SDL_Delay(AppSingleton::getInstance().getTimerTick() - timer.getElapsedTime());
  290. }
  291. }
  292. else
  293. {
  294. if(MainRendererSingleton::getInstance().getFramesNum() == 5000)
  295. {
  296. break;
  297. }
  298. }
  299. }while(true);
  300. INFO("Exiting main loop (" << (App::getTicks() - ticks) << ")");
  301. }
  302. //======================================================================================================================
  303. // main =
  304. //======================================================================================================================
  305. int main(int argc, char* argv[])
  306. {
  307. try
  308. {
  309. AppSingleton::getInstance().init(argc, argv);
  310. init();
  311. mainLoop();
  312. INFO("Exiting...");
  313. AppSingleton::getInstance().quit(EXIT_SUCCESS);
  314. return 0;
  315. }
  316. catch(std::exception& e)
  317. {
  318. ERROR("Aborting: " << e.what());
  319. //abort();
  320. return 1;
  321. }
  322. }