Main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/ParticleEmitterNode.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. ParticleEmitterNode* 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) / R::MainRendererSingleton::getInstance().getAspectRatio(), 0.5, 200.0);
  110. cam->setAll(R::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. INFO(cam->getSceneNodeName());
  116. OrthographicCamera* ocam = new OrthographicCamera(false, NULL);
  117. ocam->setAll(-1, 1, 1.0, -1.0, 0.1, 10.0);
  118. // lights
  119. point_lights[0] = new PointLight(false, NULL);
  120. point_lights[0]->init("maps/temple/light0.light");
  121. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  122. point_lights[1] = new PointLight(false, NULL);
  123. point_lights[1]->init("maps/temple/light1.light");
  124. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  125. spot_lights[0] = new SpotLight(false, NULL);
  126. spot_lights[0]->init("maps/temple/light2.light");
  127. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  128. spot_lights[1] = new SpotLight(false, NULL);
  129. spot_lights[1]->init("maps/temple/light3.light");
  130. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  131. // horse
  132. horse = new ModelNode(false, NULL);
  133. horse->init("meshes/horse/horse.mdl");
  134. horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(), 1.0));
  135. // Pentagram
  136. pentagram = new ModelNode(false, NULL);
  137. pentagram->init("models/pentagram/pentagram.mdl");
  138. pentagram->setLocalTransform(Transform(Vec3(2, 0, 0), Mat3::getIdentity(), 1.0));
  139. // Sponza
  140. ModelNode* sponza = new ModelNode(false, NULL);
  141. //sponza->init("maps/sponza/sponza.mdl");
  142. sponza->init("maps/sponza-crytek/sponza_crytek.mdl");
  143. sponza->setLocalTransform(Transform(Vec3(0.0), Mat3::getIdentity(), 0.05));
  144. // Imp
  145. imp = new SkinNode(false, NULL);
  146. imp->setLocalTransform(Transform(Vec3(0.0, 2.0, 0.0), Mat3::getIdentity(), 0.7));
  147. imp->init("models/imp/imp.skin");
  148. imp->skelAnimModelNodeCtrl = new SkelAnimModelNodeCtrl(*imp);
  149. imp->skelAnimModelNodeCtrl->set(imp->getSkin().getSkelAnims()[0].get());
  150. imp->skelAnimModelNodeCtrl->setStep(0.8);
  151. imp->addChild(*cam);
  152. // sarge
  153. /*sarge = new MeshNode();
  154. sarge->init("meshes/sphere/sphere16.mesh");
  155. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  156. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  157. // floor
  158. floor__ = new MeshNode();
  159. floor__->init("maps/temple/Cube.019.mesh");
  160. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));*/
  161. // imp
  162. /*imp = new SkelModelNode();
  163. imp->init("models/imp/imp.smdl");
  164. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  165. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  166. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  167. ctrl->step = 0.8;*/
  168. // cave map
  169. /*for(int i=1; i<21; i++)
  170. {
  171. MeshNode* node = new MeshNode();
  172. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  173. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  174. }*/
  175. // sponza map
  176. /*MeshNode* node = new MeshNode();
  177. node->init("maps/sponza/floor.mesh");
  178. node = new MeshNode();
  179. node->init("maps/sponza/walls.mesh");
  180. node = new MeshNode();
  181. node->init("maps/sponza/light-marbles.mesh");
  182. node = new MeshNode();
  183. node->init("maps/sponza/dark-marbles.mesh");*/
  184. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  185. // particle emitter
  186. partEmitter = new ParticleEmitterNode(false, NULL);
  187. partEmitter->init("asdf");
  188. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  189. return;
  190. // character
  191. /*PhyCharacter::Initializer init;
  192. init.sceneNode = imp;
  193. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  194. character = new PhyCharacter(SceneSingleton::getInstance().getPhysics(), init);*/
  195. // crate
  196. /*crate = new MeshNode;
  197. crate->init("models/crate0/crate0.mesh");
  198. crate->scaleLspace = 1.0;*/
  199. //
  200. //floor_ = new floor_t;
  201. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  202. initPhysics();
  203. //INFO("Engine initialization ends (" << (App::getTicks() - ticks) << ")");
  204. }
  205. //==============================================================================
  206. // =
  207. //==============================================================================
  208. void mainLoopExtra()
  209. {
  210. InputSingleton::getInstance().handleEvents();
  211. float dist = 0.2;
  212. float ang = toRad(3.0);
  213. float scale = 0.01;
  214. // move the camera
  215. static SceneNode* mover = AppSingleton::getInstance().getActiveCam();
  216. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_1)) mover = AppSingleton::getInstance().getActiveCam();
  217. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_2)) mover = point_lights[0];
  218. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_3)) mover = spot_lights[0];
  219. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_4)) mover = point_lights[1];
  220. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_5)) mover = spot_lights[1];
  221. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_6)) mover = imp;
  222. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_7)) mover =
  223. SceneSingleton::getInstance().getParticleEmitterNodes()[0];
  224. //if(InputSingleton::getInstance().getKey(SDL_SCANCODE_M) == 1) InputSingleton::getInstance().warpMouse = !InputSingleton::getInstance().warpMouse;
  225. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_A)) mover->moveLocalX(-dist);
  226. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_D)) mover->moveLocalX(dist);
  227. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LSHIFT)) mover->moveLocalY(dist);
  228. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_SPACE)) mover->moveLocalY(-dist);
  229. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_W)) mover->moveLocalZ(-dist);
  230. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_S)) mover->moveLocalZ(dist);
  231. if(!InputSingleton::getInstance().warpMouse())
  232. {
  233. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_UP)) mover->rotateLocalX(ang);
  234. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_DOWN)) mover->rotateLocalX(-ang);
  235. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LEFT)) mover->rotateLocalY(ang);
  236. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_RIGHT)) mover->rotateLocalY(-ang);
  237. }
  238. else
  239. {
  240. float accel = 44.0;
  241. mover->rotateLocalX(ang * InputSingleton::getInstance().mouseVelocity.y() * accel);
  242. mover->rotateLocalY(-ang * InputSingleton::getInstance().mouseVelocity.x() * accel);
  243. }
  244. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Q)) mover->rotateLocalZ(ang);
  245. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_E)) mover->rotateLocalZ(-ang);
  246. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEUP)) mover->getLocalTransform().getScale() += scale ;
  247. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEDOWN)) mover->getLocalTransform().getScale() -= scale ;
  248. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_K))
  249. AppSingleton::getInstance().getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  250. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_I))
  251. character->moveForward(0.1);
  252. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_T))
  253. {
  254. //pentagram->getModelPatchNodees()[0]->setUserDefVar(PatchNode::MT_BOTH, "specularCol", Vec3(10.0, -1.6, 1.6));
  255. pentagram->getModelPatchNodes()[0]->getCpMtlRun().getUserDefinedVarByName("shininess").get<float>() = 10.0;
  256. }
  257. /*if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F) == 1)
  258. {
  259. Event::ManagerSingleton::getInstance().createEvent(Event::MainRendererPpsHdr(HighRezTimer::getCrntTime() + 5,
  260. 5, R::MainRendererSingleton::getInstance().getPps().getHdr().getExposure() + 20.0, 3, 1.4));
  261. }*/
  262. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_O) == 1)
  263. {
  264. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  265. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  266. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  267. //body->clearForces();
  268. body->forceActivationState(ACTIVE_TAG);
  269. }
  270. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Y) == 1)
  271. {
  272. INFO("Exec script");
  273. ScriptingEngineSingleton::getInstance().execScript(Util::readFile("test.py").c_str());
  274. }
  275. mover->getLocalTransform().getRotation().reorthogonalize();
  276. //INFO(mover->getSceneNodeName())
  277. /*if(spot_lights[0]->getCamera().insideFrustum(spot_lights[1]->getCamera()))
  278. {
  279. INFO("in");
  280. }
  281. else
  282. {
  283. INFO("out");
  284. }*/
  285. }
  286. //==============================================================================
  287. // mainLoop =
  288. //==============================================================================
  289. void mainLoop()
  290. {
  291. INFO("Entering main loop");
  292. HighRezTimer mainLoopTimer;
  293. mainLoopTimer.start();
  294. HighRezTimer::Scalar prevUpdateTime = HighRezTimer::getCrntTime();
  295. HighRezTimer::Scalar crntTime = prevUpdateTime;
  296. while(1)
  297. {
  298. HighRezTimer timer;
  299. timer.start();
  300. prevUpdateTime = crntTime;
  301. crntTime = HighRezTimer::getCrntTime();
  302. //
  303. // Update
  304. //
  305. mainLoopExtra();
  306. AppSingleton::getInstance().execStdinScpripts();
  307. SceneSingleton::getInstance().getPhysMasterContainer().update(prevUpdateTime, crntTime);
  308. SceneSingleton::getInstance().updateAllWorldStuff(prevUpdateTime, crntTime);
  309. SceneSingleton::getInstance().doVisibilityTests(*AppSingleton::getInstance().getActiveCam());
  310. SceneSingleton::getInstance().updateAllControllers();
  311. Event::ManagerSingleton::getInstance().updateAllEvents(prevUpdateTime, crntTime);
  312. R::MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
  313. painter->setPosition(Vec2(0.0, 0.1));
  314. painter->setColor(Vec4(1.0));
  315. //painter->drawText("A");
  316. painter->drawText("Once upon a time in a place called Kickapoo.");
  317. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_ESCAPE))
  318. {
  319. break;
  320. }
  321. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F11))
  322. {
  323. AppSingleton::getInstance().togleFullScreen();
  324. }
  325. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F12) == 1)
  326. {
  327. R::MainRendererSingleton::getInstance().takeScreenshot("gfx/screenshot.jpg");
  328. }
  329. AppSingleton::getInstance().swapBuffers();
  330. //
  331. // Async resource loading
  332. //
  333. if(ResourceManagerSingleton::getInstance().getAsyncLoadingRequestsNum() > 0)
  334. {
  335. HighRezTimer::Scalar a = timer.getElapsedTime();
  336. HighRezTimer::Scalar b = AppSingleton::getInstance().getTimerTick();
  337. HighRezTimer::Scalar timeToSpendForRsrcPostProcess;
  338. if(a < b)
  339. {
  340. timeToSpendForRsrcPostProcess = b - a;
  341. }
  342. else
  343. {
  344. timeToSpendForRsrcPostProcess = 0.001;
  345. }
  346. ResourceManagerSingleton::getInstance().postProcessFinishedLoadingRequests(timeToSpendForRsrcPostProcess);
  347. }
  348. //
  349. // Sleep
  350. //
  351. timer.stop();
  352. if(timer.getElapsedTime() < AppSingleton::getInstance().getTimerTick())
  353. {
  354. SDL_Delay((AppSingleton::getInstance().getTimerTick() - timer.getElapsedTime()) * 1000.0);
  355. }
  356. /*if(R::MainRendererSingleton::getInstance().getFramesNum() == 100)
  357. {
  358. break;
  359. }*/
  360. }
  361. INFO("Exiting main loop (" << mainLoopTimer.getElapsedTime() << " sec)");
  362. }
  363. //==============================================================================
  364. // main =
  365. //==============================================================================
  366. int main(int argc, char* argv[])
  367. {
  368. /*std::stringstream ss("lala\n\n\n\n1a");
  369. Scanner::Scanner scanner(ss);
  370. scanner.getAllPrintAll();
  371. return 0;*/
  372. try
  373. {
  374. AppSingleton::getInstance().init(argc, argv);
  375. init();
  376. mainLoop();
  377. INFO("Exiting...");
  378. AppSingleton::getInstance().quit(EXIT_SUCCESS);
  379. return 0;
  380. }
  381. catch(std::exception& e)
  382. {
  383. ERROR("Aborting: " << e.what());
  384. //abort();
  385. return 1;
  386. }
  387. }