Main.cpp 15 KB

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