Main.cpp 15 KB

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