Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "Resource.h"
  16. #include "Scene.h"
  17. #include "Scanner.h"
  18. #include "skybox.h"
  19. #include "map.h"
  20. #include "SkelAnim.h"
  21. #include "LightData.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. // map (hard coded)
  37. ModelNode* floor__,* sarge,* horse,* crate, *imp;
  38. //SkelModelNode* imp;
  39. PointLight* point_lights[10];
  40. SpotLight* spot_lights[2];
  41. ParticleEmitter* partEmitter;
  42. PhyCharacter* character;
  43. // Physics
  44. Vec<btRigidBody*> boxes;
  45. #define ARRAY_SIZE_X 5
  46. #define ARRAY_SIZE_Y 5
  47. #define ARRAY_SIZE_Z 5
  48. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  49. #define SCALING 1.
  50. #define START_POS_X -5
  51. #define START_POS_Y -5
  52. #define START_POS_Z -3
  53. void initPhysics()
  54. {
  55. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  56. Transform groundTransform;
  57. groundTransform.setIdentity();
  58. groundTransform.origin = Vec3(0,-50, 0);
  59. RigidBody::Initializer init;
  60. init.mass = 0.0;
  61. init.shape = groundShape;
  62. init.startTrf = groundTransform;
  63. new RigidBody(app->getScene().getPhysics(), init);
  64. /*{
  65. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  66. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  67. float start_y = START_POS_Y;
  68. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  69. for (int k=0;k<ARRAY_SIZE_Y;k++)
  70. {
  71. for (int i=0;i<ARRAY_SIZE_X;i++)
  72. {
  73. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  74. {
  75. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  76. MeshNode* crate = new MeshNode;
  77. crate->init("models/crate0/crate0.mesh");
  78. crate->getLocalTransform().setScale(1.11);
  79. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  80. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  81. }
  82. }
  83. }
  84. }*/
  85. }
  86. //======================================================================================================================
  87. // init =
  88. //======================================================================================================================
  89. void init()
  90. {
  91. INFO("Engine initializing...");
  92. srand(unsigned(time(NULL)));
  93. mathSanityChecks();
  94. app->initWindow();
  95. uint ticks = app->getTicks();
  96. RendererInitializer initializer;
  97. initializer.ms.ez.enabled = false;
  98. initializer.dbg.enabled = true;
  99. initializer.is.sm.bilinearEnabled = true;
  100. initializer.is.sm.enabled = true;
  101. initializer.is.sm.pcfEnabled = true;
  102. initializer.is.sm.resolution = 512;
  103. initializer.pps.hdr.enabled = true;
  104. initializer.pps.hdr.renderingQuality = 0.25;
  105. initializer.pps.hdr.blurringDist = 1.0;
  106. initializer.pps.hdr.blurringIterationsNum = 2;
  107. initializer.pps.hdr.exposure = 4.0;
  108. initializer.pps.ssao.blurringIterationsNum = 2;
  109. initializer.pps.ssao.enabled = true;
  110. initializer.pps.ssao.renderingQuality = 0.5;
  111. initializer.mainRendererQuality = 1.0;
  112. app->getMainRenderer().init(initializer);
  113. //Ui::init();
  114. // camera
  115. Camera* cam = new Camera(app->getMainRenderer().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  116. cam->moveLocalY(3.0);
  117. cam->moveLocalZ(5.7);
  118. cam->moveLocalX(-0.3);
  119. app->setActiveCam(cam);
  120. // lights
  121. point_lights[0] = new PointLight();
  122. point_lights[0]->init("maps/temple/light0.light");
  123. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  124. point_lights[1] = new PointLight();
  125. point_lights[1]->init("maps/temple/light1.light");
  126. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  127. spot_lights[0] = new SpotLight();
  128. spot_lights[0]->init("maps/temple/light2.light");
  129. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  130. spot_lights[1] = new SpotLight();
  131. spot_lights[1]->init("maps/temple/light3.light");
  132. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  133. // horse
  134. horse = new ModelNode();
  135. horse->init("meshes/horse/horse.mdl");
  136. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3::getIdentity(), 1.0));
  137. // Sponza
  138. ModelNode* sponza = new ModelNode();
  139. sponza->init("maps/sponza/sponza.mdl");
  140. // Pentagram
  141. ModelNode* pentagram = new ModelNode();
  142. pentagram->init("models/pentagram/pentagram.mdl");
  143. pentagram->setLocalTransform(Transform(Vec3(2, 0, 0), Mat3::getIdentity(), 1.0));
  144. // Imp
  145. /*imp = new ModelNode();
  146. imp->init("models/imp/imp.mdl");
  147. imp->skelAnimModelNodeCtrl = new SkelAnimModelNodeCtrl(*imp);
  148. imp->skelAnimModelNodeCtrl->set(imp->getModel().getSkelAnims()[0].get());
  149. imp->skelAnimModelNodeCtrl->setStep(0.8);*/
  150. return;
  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. return;
  185. // particle emitter
  186. partEmitter = new ParticleEmitter;
  187. partEmitter->init("asdf");
  188. partEmitter->getLocalTransform().origin = Vec3(3.0, 0.0, 0.0);
  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(app->getScene().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. // mainLoop =
  206. //======================================================================================================================
  207. void mainLoop()
  208. {
  209. INFO("Entering main loop");
  210. int ticks = App::getTicks();
  211. do
  212. {
  213. float crntTime = App::getTicks() / 1000.0;
  214. Input::getInstance().handleEvents();
  215. float dist = 0.2;
  216. float ang = toRad(3.0);
  217. float scale = 0.01;
  218. // move the camera
  219. static SceneNode* mover = app->getActiveCam();
  220. if(Input::getInstance().keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  221. if(Input::getInstance().keys[SDL_SCANCODE_2]) mover = point_lights[0];
  222. if(Input::getInstance().keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  223. if(Input::getInstance().keys[SDL_SCANCODE_4]) mover = point_lights[1];
  224. if(Input::getInstance().keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  225. if(Input::getInstance().keys[SDL_SCANCODE_6]) mover = partEmitter;
  226. if(Input::getInstance().keys[SDL_SCANCODE_M] == 1) Input::getInstance().warpMouse = !Input::getInstance().warpMouse;
  227. if(Input::getInstance().keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  228. if(Input::getInstance().keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  229. if(Input::getInstance().keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  230. if(Input::getInstance().keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  231. if(Input::getInstance().keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  232. if(Input::getInstance().keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  233. if(!Input::getInstance().warpMouse)
  234. {
  235. if(Input::getInstance().keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  236. if(Input::getInstance().keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  237. if(Input::getInstance().keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  238. if(Input::getInstance().keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  239. }
  240. else
  241. {
  242. float accel = 44.0;
  243. mover->rotateLocalX(ang * Input::getInstance().mouseVelocity.y * accel);
  244. mover->rotateLocalY(-ang * Input::getInstance().mouseVelocity.x * accel);
  245. }
  246. if(Input::getInstance().keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  247. if(Input::getInstance().keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  248. if(Input::getInstance().keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().scale += scale ;
  249. if(Input::getInstance().keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().scale -= scale ;
  250. if(Input::getInstance().keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().origin);
  251. if(Input::getInstance().keys[SDL_SCANCODE_I])
  252. character->moveForward(0.1);
  253. if(Input::getInstance().keys[SDL_SCANCODE_O] == 1)
  254. {
  255. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  256. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  257. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  258. //body->clearForces();
  259. body->forceActivationState(ACTIVE_TAG);
  260. }
  261. if(Input::getInstance().keys[SDL_SCANCODE_Y] == 1)
  262. {
  263. INFO("Exec script");
  264. ScriptingEngine::getInstance().exposeVar("app", app);
  265. ScriptingEngine::getInstance().execScript(Util::readFile("test.py").c_str());
  266. }
  267. mover->getLocalTransform().rotation.reorthogonalize();
  268. app->execStdinScpripts();
  269. app->getScene().getPhysics().update(crntTime);
  270. app->getScene().updateAllControllers();
  271. app->getScene().updateAllWorldStuff();
  272. app->getMainRenderer().render(*app->getActiveCam());
  273. //map.octree.root->bounding_box.render();
  274. // print some debug stuff
  275. /*Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  276. Ui::setPos(-0.98, 0.95);
  277. Ui::setFontWidth(0.03);*/
  278. //Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer().getFramesNum(), (App::getTicks()-ticks_));
  279. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  280. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  281. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  282. if(Input::getInstance().keys[SDL_SCANCODE_ESCAPE])
  283. break;
  284. if(Input::getInstance().keys[SDL_SCANCODE_F11])
  285. app->togleFullScreen();
  286. if(Input::getInstance().keys[SDL_SCANCODE_F12] == 1)
  287. app->getMainRenderer().takeScreenshot("gfx/screenshot.jpg");
  288. /*char str[128];
  289. static string scrFile = (app->getSettingsPath() / "capt" / "%06d.jpg").string();
  290. sprintf(str, scrFile.c_str(), app->getMainRenderer().getFramesNum());
  291. app->getMainRenderer().takeScreenshot(str);*/
  292. // std stuff follow
  293. app->swapBuffers();
  294. if(1)
  295. {
  296. //if(app->getMainRenderer().getFramesNum() == 100) app->getMainRenderer().takeScreenshot("gfx/screenshot.tga");
  297. app->waitForNextFrame();
  298. }
  299. else
  300. {
  301. if(app->getMainRenderer().getFramesNum() == 5000)
  302. {
  303. break;
  304. }
  305. }
  306. }while(true);
  307. INFO("Exiting main loop (" << (App::getTicks() - ticks) << ")");
  308. }
  309. //======================================================================================================================
  310. // main =
  311. //======================================================================================================================
  312. int main(int argc, char* argv[])
  313. {
  314. try
  315. {
  316. new App(argc, argv);
  317. init();
  318. mainLoop();
  319. INFO("Exiting...");
  320. app->quit(EXIT_SUCCESS);
  321. return 0;
  322. }
  323. catch(std::exception& e)
  324. {
  325. ERROR("Aborting: " << e.what());
  326. //abort();
  327. return 1;
  328. }
  329. }