Main.cpp 13 KB

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