Main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "Scene.h"
  16. #include "Scanner.h"
  17. #include "skybox.h"
  18. #include "map.h"
  19. #include "SkelAnim.h"
  20. #include "LightData.h"
  21. #include "Parser.h"
  22. #include "ParticleEmitter.h"
  23. #include "PhyCharacter.h"
  24. #include "Renderer.h"
  25. #include "RendererInitializer.h"
  26. #include "MainRenderer.h"
  27. #include "PhyCharacter.h"
  28. #include "RigidBody.h"
  29. #include "ScriptingEngine.h"
  30. #include "StdinListener.h"
  31. #include "ModelNode.h"
  32. #include "SkelAnimModelNodeCtrl.h"
  33. #include "Model.h"
  34. #include "Logger.h"
  35. #include "Util.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(SceneSingleton::getInstance().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("Other init...");
  92. srand(unsigned(time(NULL)));
  93. uint ticks = AppSingleton::getInstance().getTicks();
  94. //Ui::init();
  95. // camera
  96. Camera* cam = new Camera(false, NULL);
  97. cam->setAll(MainRendererSingleton::getInstance().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  98. cam->moveLocalY(3.0);
  99. cam->moveLocalZ(5.7);
  100. cam->moveLocalX(-0.3);
  101. AppSingleton::getInstance().setActiveCam(cam);
  102. // lights
  103. point_lights[0] = new PointLight();
  104. point_lights[0]->init("maps/temple/light0.light");
  105. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  106. point_lights[1] = new PointLight();
  107. point_lights[1]->init("maps/temple/light1.light");
  108. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  109. spot_lights[0] = new SpotLight();
  110. spot_lights[0]->init("maps/temple/light2.light");
  111. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  112. spot_lights[1] = new SpotLight();
  113. spot_lights[1]->init("maps/temple/light3.light");
  114. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  115. // horse
  116. horse = new ModelNode();
  117. horse->init("meshes/horse/horse.mdl");
  118. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3::getIdentity(), 1.0));
  119. return;
  120. // Sponza
  121. ModelNode* sponza = new ModelNode();
  122. sponza->init("maps/sponza/sponza.mdl");
  123. // Pentagram
  124. ModelNode* pentagram = new ModelNode();
  125. pentagram->init("models/pentagram/pentagram.mdl");
  126. pentagram->setLocalTransform(Transform(Vec3(2, 0, 0), Mat3::getIdentity(), 1.0));
  127. // Imp
  128. /*imp = new ModelNode();
  129. imp->init("models/imp/imp.mdl");
  130. imp->skelAnimModelNodeCtrl = new SkelAnimModelNodeCtrl(*imp);
  131. imp->skelAnimModelNodeCtrl->set(imp->getModel().getSkelAnims()[0].get());
  132. imp->skelAnimModelNodeCtrl->setStep(0.8);*/
  133. return;
  134. // sarge
  135. /*sarge = new MeshNode();
  136. sarge->init("meshes/sphere/sphere16.mesh");
  137. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  138. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  139. // floor
  140. floor__ = new MeshNode();
  141. floor__->init("maps/temple/Cube.019.mesh");
  142. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));*/
  143. // imp
  144. /*imp = new SkelModelNode();
  145. imp->init("models/imp/imp.smdl");
  146. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  147. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  148. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  149. ctrl->step = 0.8;*/
  150. // cave map
  151. /*for(int i=1; i<21; i++)
  152. {
  153. MeshNode* node = new MeshNode();
  154. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  155. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  156. }*/
  157. // sponza map
  158. /*MeshNode* node = new MeshNode();
  159. node->init("maps/sponza/floor.mesh");
  160. node = new MeshNode();
  161. node->init("maps/sponza/walls.mesh");
  162. node = new MeshNode();
  163. node->init("maps/sponza/light-marbles.mesh");
  164. node = new MeshNode();
  165. node->init("maps/sponza/dark-marbles.mesh");*/
  166. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  167. return;
  168. // particle emitter
  169. partEmitter = new ParticleEmitter;
  170. partEmitter->init("asdf");
  171. partEmitter->getLocalTransform().origin = Vec3(3.0, 0.0, 0.0);
  172. // character
  173. /*PhyCharacter::Initializer init;
  174. init.sceneNode = imp;
  175. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  176. character = new PhyCharacter(SceneSingleton::getInstance().getPhysics(), init);*/
  177. // crate
  178. /*crate = new MeshNode;
  179. crate->init("models/crate0/crate0.mesh");
  180. crate->scaleLspace = 1.0;*/
  181. //
  182. //floor_ = new floor_t;
  183. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  184. initPhysics();
  185. INFO("Engine initialization ends (" << (App::getTicks() - ticks) << ")");
  186. }
  187. //======================================================================================================================
  188. // mainLoop =
  189. //======================================================================================================================
  190. void mainLoop()
  191. {
  192. INFO("Entering main loop");
  193. int ticks = App::getTicks();
  194. do
  195. {
  196. float crntTime = App::getTicks() / 1000.0;
  197. InputSingleton::getInstance().handleEvents();
  198. float dist = 0.2;
  199. float ang = toRad(3.0);
  200. float scale = 0.01;
  201. // move the camera
  202. static SceneNode* mover = AppSingleton::getInstance().getActiveCam();
  203. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_1)) mover = AppSingleton::getInstance().getActiveCam();
  204. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_2)) mover = point_lights[0];
  205. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_3)) mover = spot_lights[0];
  206. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_4)) mover = point_lights[1];
  207. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_5)) mover = spot_lights[1];
  208. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_6)) mover = horse;
  209. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_M) == 1) InputSingleton::getInstance().warpMouse = !InputSingleton::getInstance().warpMouse;
  210. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_A)) mover->moveLocalX(-dist);
  211. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_D)) mover->moveLocalX(dist);
  212. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LSHIFT)) mover->moveLocalY(dist);
  213. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_SPACE)) mover->moveLocalY(-dist);
  214. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_W)) mover->moveLocalZ(-dist);
  215. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_S)) mover->moveLocalZ(dist);
  216. if(!InputSingleton::getInstance().warpMouse)
  217. {
  218. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_UP)) mover->rotateLocalX(ang);
  219. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_DOWN)) mover->rotateLocalX(-ang);
  220. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_LEFT)) mover->rotateLocalY(ang);
  221. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_RIGHT)) mover->rotateLocalY(-ang);
  222. }
  223. else
  224. {
  225. float accel = 44.0;
  226. mover->rotateLocalX(ang * InputSingleton::getInstance().mouseVelocity.y() * accel);
  227. mover->rotateLocalY(-ang * InputSingleton::getInstance().mouseVelocity.x() * accel);
  228. }
  229. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Q)) mover->rotateLocalZ(ang);
  230. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_E)) mover->rotateLocalZ(-ang);
  231. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEUP)) mover->getLocalTransform().scale += scale ;
  232. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_PAGEDOWN)) mover->getLocalTransform().scale -= scale ;
  233. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_K)) AppSingleton::getInstance().getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().origin);
  234. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_I))
  235. character->moveForward(0.1);
  236. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_O) == 1)
  237. {
  238. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  239. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  240. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  241. //body->clearForces();
  242. body->forceActivationState(ACTIVE_TAG);
  243. }
  244. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_Y) == 1)
  245. {
  246. INFO("Exec script");
  247. ScriptingEngineSingleton::getInstance().execScript(Util::readFile("test.py").c_str());
  248. }
  249. mover->getLocalTransform().rotation.reorthogonalize();
  250. AppSingleton::getInstance().execStdinScpripts();
  251. SceneSingleton::getInstance().getPhysics().update(crntTime);
  252. SceneSingleton::getInstance().updateAllControllers();
  253. SceneSingleton::getInstance().updateAllWorldStuff();
  254. SceneSingleton::getInstance().doVisibilityTests(*AppSingleton::getInstance().getActiveCam());
  255. //SceneSingleton::getInstance().doVisibilityTests(spot_lights[0]->getCamera());
  256. MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
  257. //map.octree.root->bounding_box.render();
  258. // print some debug stuff
  259. /*Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  260. Ui::setPos(-0.98, 0.95);
  261. Ui::setFontWidth(0.03);*/
  262. //Ui::printf("frame:%d fps:%dms\n", AppSingleton::getInstance().getMainRenderer().getFramesNum(), (App::getTicks()-ticks_));
  263. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  264. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  265. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  266. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_ESCAPE))
  267. break;
  268. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F11))
  269. AppSingleton::getInstance().togleFullScreen();
  270. if(InputSingleton::getInstance().getKey(SDL_SCANCODE_F12) == 1)
  271. MainRendererSingleton::getInstance().takeScreenshot("gfx/screenshot.jpg");
  272. /*char str[128];
  273. static string scrFile = (AppSingleton::getInstance().getSettingsPath() / "capt" / "%06d.jpg").string();
  274. sprintf(str, scrFile.c_str(), AppSingleton::getInstance().getMainRenderer().getFramesNum());
  275. AppSingleton::getInstance().getMainRenderer().takeScreenshot(str);*/
  276. // std stuff follow
  277. AppSingleton::getInstance().swapBuffers();
  278. if(1)
  279. {
  280. //if(AppSingleton::getInstance().getMainRenderer().getFramesNum() == 100) AppSingleton::getInstance().getMainRenderer().takeScreenshot("gfx/screenshot.tga");
  281. AppSingleton::getInstance().waitForNextFrame();
  282. }
  283. else
  284. {
  285. if(MainRendererSingleton::getInstance().getFramesNum() == 5000)
  286. {
  287. break;
  288. }
  289. }
  290. }while(true);
  291. INFO("Exiting main loop (" << (App::getTicks() - ticks) << ")");
  292. }
  293. //======================================================================================================================
  294. // main =
  295. //======================================================================================================================
  296. int main(int argc, char* argv[])
  297. {
  298. try
  299. {
  300. AppSingleton::getInstance().init(argc, argv);
  301. init();
  302. mainLoop();
  303. INFO("Exiting...");
  304. AppSingleton::getInstance().quit(EXIT_SUCCESS);
  305. return 0;
  306. }
  307. catch(std::exception& e)
  308. {
  309. ERROR("Aborting: " << e.what());
  310. //abort();
  311. return 1;
  312. }
  313. }