Main.cpp 12 KB

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