Main.cpp 13 KB

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