Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "MeshNode.h"
  21. #include "SkelModelNode.h"
  22. #include "MeshNode.h"
  23. #include "SkelAnim.h"
  24. #include "MeshSkelNodeCtrl.h"
  25. #include "SkelAnimCtrl.h"
  26. #include "SkelNode.h"
  27. #include "LightData.h"
  28. #include "Parser.h"
  29. #include "ParticleEmitter.h"
  30. #include "PhyCharacter.h"
  31. #include "Renderer.h"
  32. #include "RendererInitializer.h"
  33. #include "MainRenderer.h"
  34. #include "DebugDrawer.h"
  35. #include "PhyCharacter.h"
  36. #include "RigidBody.h"
  37. #include "ScriptingEngine.h"
  38. #include "StdinListener.h"
  39. #include "Messaging.h"
  40. // map (hard coded)
  41. MeshNode* floor__,* sarge,* horse,* crate;
  42. SkelModelNode* imp;
  43. PointLight* point_lights[10];
  44. SpotLight* spot_lights[2];
  45. ParticleEmitter* partEmitter;
  46. PhyCharacter* character;
  47. // Physics
  48. Vec<btRigidBody*> boxes;
  49. #define ARRAY_SIZE_X 5
  50. #define ARRAY_SIZE_Y 5
  51. #define ARRAY_SIZE_Z 5
  52. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  53. #define SCALING 1.
  54. #define START_POS_X -5
  55. #define START_POS_Y -5
  56. #define START_POS_Z -3
  57. void initPhysics()
  58. {
  59. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  60. Transform groundTransform;
  61. groundTransform.setIdentity();
  62. groundTransform.origin = Vec3(0,-50, 0);
  63. RigidBody::Initializer init;
  64. init.mass = 0.0;
  65. init.shape = groundShape;
  66. init.startTrf = groundTransform;
  67. new RigidBody(app->getScene().getPhysics(), init);
  68. /*{
  69. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  70. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  71. float start_y = START_POS_Y;
  72. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  73. for (int k=0;k<ARRAY_SIZE_Y;k++)
  74. {
  75. for (int i=0;i<ARRAY_SIZE_X;i++)
  76. {
  77. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  78. {
  79. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  80. MeshNode* crate = new MeshNode;
  81. crate->init("models/crate0/crate0.mesh");
  82. crate->getLocalTransform().setScale(1.11);
  83. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  84. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  85. }
  86. }
  87. }
  88. }*/
  89. }
  90. //======================================================================================================================
  91. // init =
  92. //======================================================================================================================
  93. void init()
  94. {
  95. INFO("Engine initializing...");
  96. srand(unsigned(time(NULL)));
  97. mathSanityChecks();
  98. app->initWindow();
  99. uint ticks = app->getTicks();
  100. RendererInitializer initializer;
  101. initializer.ms.ez.enabled = false;
  102. initializer.dbg.enabled = true;
  103. initializer.is.sm.bilinearEnabled = true;
  104. initializer.is.sm.enabled = true;
  105. initializer.is.sm.pcfEnabled = true;
  106. initializer.is.sm.resolution = 512;
  107. initializer.pps.hdr.enabled = true;
  108. initializer.pps.hdr.renderingQuality = 0.25;
  109. initializer.pps.hdr.blurringDist = 1.0;
  110. initializer.pps.hdr.blurringIterations = 2;
  111. initializer.pps.hdr.exposure = 4.0;
  112. initializer.pps.ssao.blurringIterations = 2;
  113. initializer.pps.ssao.enabled = true;
  114. initializer.pps.ssao.renderingQuality = 0.5;
  115. initializer.mainRendererQuality = 1.0;
  116. app->getMainRenderer().init(initializer);
  117. //Ui::init();
  118. // camera
  119. Camera* cam = new Camera(app->getMainRenderer().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  120. cam->moveLocalY(3.0);
  121. cam->moveLocalZ(5.7);
  122. cam->moveLocalX(-0.3);
  123. app->setActiveCam(cam);
  124. // lights
  125. point_lights[0] = new PointLight();
  126. point_lights[0]->init("maps/temple/light0.light");
  127. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  128. point_lights[1] = new PointLight();
  129. point_lights[1]->init("maps/temple/light1.light");
  130. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  131. spot_lights[0] = new SpotLight();
  132. spot_lights[0]->init("maps/temple/light2.light");
  133. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  134. spot_lights[1] = new SpotLight();
  135. spot_lights[1]->init("maps/temple/light3.light");
  136. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  137. /*const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  138. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  139. app->getScene().skybox.load(skybox_fnames);*/
  140. // horse
  141. horse = new MeshNode();
  142. horse->init("meshes/horse/horse.mesh");
  143. //horse->init("models/head/head.mesh");
  144. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3::getIdentity(), 1.0));
  145. return;
  146. // sarge
  147. /*sarge = new MeshNode();
  148. sarge->init("meshes/sphere/sphere16.mesh");
  149. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  150. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  151. // floor
  152. floor__ = new MeshNode();
  153. floor__->init("maps/temple/Cube.019.mesh");
  154. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));*/
  155. // imp
  156. imp = new SkelModelNode();
  157. imp->init("models/imp/imp.smdl");
  158. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  159. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  160. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  161. ctrl->step = 0.8;
  162. // cave map
  163. /*for(int i=1; i<21; i++)
  164. {
  165. MeshNode* node = new MeshNode();
  166. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  167. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  168. }*/
  169. // sponza map
  170. MeshNode* node = new MeshNode();
  171. node->init("maps/sponza/floor.mesh");
  172. node = new MeshNode();
  173. node->init("maps/sponza/walls.mesh");
  174. node = new MeshNode();
  175. node->init("maps/sponza/light-marbles.mesh");
  176. node = new MeshNode();
  177. node->init("maps/sponza/dark-marbles.mesh");
  178. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  179. return;
  180. // particle emitter
  181. partEmitter = new ParticleEmitter;
  182. partEmitter->init("asdf");
  183. partEmitter->getLocalTransform().origin = Vec3(3.0, 0.0, 0.0);
  184. // character
  185. PhyCharacter::Initializer init;
  186. init.sceneNode = imp;
  187. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  188. character = new PhyCharacter(app->getScene().getPhysics(), init);
  189. // crate
  190. /*crate = new MeshNode;
  191. crate->init("models/crate0/crate0.mesh");
  192. crate->scaleLspace = 1.0;*/
  193. //
  194. //floor_ = new floor_t;
  195. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  196. initPhysics();
  197. INFO("Engine initialization ends (" + boost::lexical_cast<std::string>(App::getTicks() - ticks) + ")");
  198. }
  199. //======================================================================================================================
  200. // mainLoop =
  201. //======================================================================================================================
  202. void mainLoop()
  203. {
  204. INFO("Entering main loop");
  205. int ticks = App::getTicks();
  206. do
  207. {
  208. float crntTime = App::getTicks() / 1000.0;
  209. app->getInput().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 = app->getActiveCam();
  215. if(app->getInput().keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  216. if(app->getInput().keys[SDL_SCANCODE_2]) mover = point_lights[0];
  217. if(app->getInput().keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  218. if(app->getInput().keys[SDL_SCANCODE_4]) mover = point_lights[1];
  219. if(app->getInput().keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  220. if(app->getInput().keys[SDL_SCANCODE_6]) mover = partEmitter;
  221. if(app->getInput().keys[SDL_SCANCODE_M] == 1) app->getInput().warpMouse = !app->getInput().warpMouse;
  222. if(app->getInput().keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  223. if(app->getInput().keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  224. if(app->getInput().keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  225. if(app->getInput().keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  226. if(app->getInput().keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  227. if(app->getInput().keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  228. if(!app->getInput().warpMouse)
  229. {
  230. if(app->getInput().keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  231. if(app->getInput().keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  232. if(app->getInput().keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  233. if(app->getInput().keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  234. }
  235. else
  236. {
  237. float accel = 44.0;
  238. mover->rotateLocalX(ang * app->getInput().mouseVelocity.y * accel);
  239. mover->rotateLocalY(-ang * app->getInput().mouseVelocity.x * accel);
  240. }
  241. if(app->getInput().keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  242. if(app->getInput().keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  243. if(app->getInput().keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().scale += scale ;
  244. if(app->getInput().keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().scale -= scale ;
  245. if(app->getInput().keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().origin);
  246. if(app->getInput().keys[SDL_SCANCODE_I])
  247. character->moveForward(0.1);
  248. if(app->getInput().keys[SDL_SCANCODE_O] == 1)
  249. {
  250. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  251. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  252. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  253. //body->clearForces();
  254. body->forceActivationState(ACTIVE_TAG);
  255. }
  256. if(app->getInput().keys[SDL_SCANCODE_Y] == 1)
  257. {
  258. INFO("Exec script");
  259. app->getScriptingEngine().exposeVar("app", app);
  260. app->getScriptingEngine().execScript(Util::readFile("test.py").c_str());
  261. }
  262. mover->getLocalTransform().rotation.reorthogonalize();
  263. app->execStdinScpripts();
  264. app->getScene().getPhysics().update(crntTime);
  265. app->getScene().updateAllControllers();
  266. app->getScene().updateAllWorldStuff();
  267. app->getMainRenderer().render(*app->getActiveCam());
  268. //map.octree.root->bounding_box.render();
  269. // print some debug stuff
  270. /*Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  271. Ui::setPos(-0.98, 0.95);
  272. Ui::setFontWidth(0.03);*/
  273. //Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer().getFramesNum(), (App::getTicks()-ticks_));
  274. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  275. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  276. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  277. if(app->getInput().keys[SDL_SCANCODE_ESCAPE])
  278. break;
  279. if(app->getInput().keys[SDL_SCANCODE_F11])
  280. app->togleFullScreen();
  281. if(app->getInput().keys[SDL_SCANCODE_F12] == 1)
  282. app->getMainRenderer().takeScreenshot("gfx/screenshot.jpg");
  283. /*char str[128];
  284. static string scrFile = (app->getSettingsPath() / "capt" / "%06d.jpg").string();
  285. sprintf(str, scrFile.c_str(), app->getMainRenderer().getFramesNum());
  286. app->getMainRenderer().takeScreenshot(str);*/
  287. // std stuff follow
  288. app->swapBuffers();
  289. if(1)
  290. {
  291. //if(app->getMainRenderer().getFramesNum() == 100) app->getMainRenderer().takeScreenshot("gfx/screenshot.tga");
  292. app->waitForNextFrame();
  293. }
  294. else
  295. {
  296. if(app->getMainRenderer().getFramesNum() == 5000)
  297. {
  298. break;
  299. }
  300. }
  301. }while(true);
  302. INFO("Exiting main loop (" + boost::lexical_cast<std::string>(App::getTicks() - ticks) + ")");
  303. }
  304. //======================================================================================================================
  305. // main =
  306. //======================================================================================================================
  307. int main(int argc, char* argv[])
  308. {
  309. try
  310. {
  311. new App(argc, argv);
  312. init();
  313. mainLoop();
  314. INFO("Exiting...");
  315. app->quit(EXIT_SUCCESS);
  316. return 0;
  317. }
  318. catch(std::exception& e)
  319. {
  320. ERROR("Aborting: " + e.what());
  321. //abort();
  322. return 1;
  323. }
  324. }