Main.cpp 15 KB

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