Main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. App* app = NULL; ///< The only global var. App constructor sets it
  38. // map (hard coded)
  39. MeshNode* floor__,* sarge,* horse,* crate;
  40. SkelModelNode* imp;
  41. PointLight* point_lights[10];
  42. SpotLight* spot_lights[2];
  43. ParticleEmitter* partEmitter;
  44. PhyCharacter* character;
  45. // Physics
  46. Vec<btRigidBody*> boxes;
  47. #define ARRAY_SIZE_X 5
  48. #define ARRAY_SIZE_Y 5
  49. #define ARRAY_SIZE_Z 5
  50. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  51. #define SCALING 1.
  52. #define START_POS_X -5
  53. #define START_POS_Y -5
  54. #define START_POS_Z -3
  55. void initPhysics()
  56. {
  57. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  58. Transform groundTransform;
  59. groundTransform.setIdentity();
  60. groundTransform.setOrigin(Vec3(0,-50, 0));
  61. RigidBody::Initializer init;
  62. init.mass = 0.0;
  63. init.shape = groundShape;
  64. init.startTrf = groundTransform;
  65. new RigidBody(*app->getScene()->getPhysics(), init);
  66. /*{
  67. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  68. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  69. float start_y = START_POS_Y;
  70. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  71. for (int k=0;k<ARRAY_SIZE_Y;k++)
  72. {
  73. for (int i=0;i<ARRAY_SIZE_X;i++)
  74. {
  75. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  76. {
  77. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  78. MeshNode* crate = new MeshNode;
  79. crate->init("models/crate0/crate0.mesh");
  80. crate->getLocalTransform().setScale(1.11);
  81. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  82. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  83. }
  84. }
  85. }
  86. }*/
  87. }
  88. //======================================================================================================================
  89. // init =
  90. //======================================================================================================================
  91. void init()
  92. {
  93. INFO("Engine initializing...");
  94. srand(unsigned(time(NULL)));
  95. mathSanityChecks();
  96. app->initWindow();
  97. uint ticks = app->getTicks();
  98. RendererInitializer initializer;
  99. initializer.ms.ez.enabled = false;
  100. initializer.dbg.enabled = true;
  101. initializer.is.sm.bilinearEnabled = true;
  102. initializer.is.sm.enabled = true;
  103. initializer.is.sm.pcfEnabled = true;
  104. initializer.is.sm.resolution = 512;
  105. initializer.pps.hdr.enabled = true;
  106. initializer.pps.hdr.renderingQuality = 0.5;
  107. initializer.pps.ssao.bluringQuality = 1.0;
  108. initializer.pps.ssao.enabled = true;
  109. initializer.pps.ssao.renderingQuality = 0.5;
  110. initializer.mainRendererQuality = 1.0;
  111. app->getMainRenderer()->init(initializer);
  112. Ui::init();
  113. // camera
  114. Camera* cam = new Camera(app->getMainRenderer()->getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  115. cam->moveLocalY(3.0);
  116. cam->moveLocalZ(5.7);
  117. cam->moveLocalX(-0.3);
  118. app->setActiveCam(cam);
  119. // lights
  120. point_lights[0] = new PointLight();
  121. point_lights[0]->init("maps/temple/light0.light");
  122. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  123. point_lights[1] = new PointLight();
  124. point_lights[1]->init("maps/temple/light1.light");
  125. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  126. spot_lights[0] = new SpotLight();
  127. spot_lights[0]->init("maps/temple/light2.light");
  128. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  129. spot_lights[1] = new SpotLight();
  130. spot_lights[1]->init("maps/temple/light3.light");
  131. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  132. // horse
  133. horse = new MeshNode();
  134. horse->init("meshes/horse/horse.mesh");
  135. //horse->init("models/head/head.mesh");
  136. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.5));
  137. // sarge
  138. sarge = new MeshNode();
  139. sarge->init("meshes/sphere/sphere16.mesh");
  140. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  141. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  142. // floor
  143. floor__ = new MeshNode();
  144. floor__->init("maps/temple/Cube.019.mesh");
  145. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));
  146. // imp
  147. imp = new SkelModelNode();
  148. imp->init("models/imp/imp.smdl");
  149. //imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  150. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  151. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
  152. // cave map
  153. /*for(int i=1; i<21; i++)
  154. {
  155. MeshNode* node = new MeshNode();
  156. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  157. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  158. }*/
  159. // sponza map
  160. /*MeshNode* node = new MeshNode();
  161. node->init("maps/sponza/sponza.mesh");*/
  162. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  163. // particle emitter
  164. partEmitter = new ParticleEmitter;
  165. partEmitter->init("asdf");
  166. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  167. // character
  168. PhyCharacter::Initializer init;
  169. init.sceneNode = imp;
  170. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  171. character = new PhyCharacter(*app->getScene()->getPhysics(), init);
  172. // crate
  173. /*crate = new MeshNode;
  174. crate->init("models/crate0/crate0.mesh");
  175. crate->scaleLspace = 1.0;*/
  176. //
  177. //floor_ = new floor_t;
  178. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  179. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  180. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  181. app->getScene()->skybox.load(skybox_fnames);
  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. I::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 = app->getActiveCam();
  201. if(I::keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  202. if(I::keys[SDL_SCANCODE_2]) mover = point_lights[0];
  203. if(I::keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  204. if(I::keys[SDL_SCANCODE_4]) mover = point_lights[1];
  205. if(I::keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  206. if(I::keys[SDL_SCANCODE_6]) mover = partEmitter;
  207. if(I::keys[SDL_SCANCODE_M] == 1) I::warpMouse = !I::warpMouse;
  208. if(I::keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  209. if(I::keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  210. if(I::keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  211. if(I::keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  212. if(I::keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  213. if(I::keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  214. if(!I::warpMouse)
  215. {
  216. if(I::keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  217. if(I::keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  218. if(I::keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  219. if(I::keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  220. }
  221. else
  222. {
  223. float accel = 44.0;
  224. mover->rotateLocalX(ang * I::mouseVelocity.y * accel);
  225. mover->rotateLocalY(-ang * I::mouseVelocity.x * accel);
  226. }
  227. if(I::keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  228. if(I::keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  229. if(I::keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().getScale() += scale ;
  230. if(I::keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().getScale() -= scale ;
  231. if(I::keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  232. if(I::keys[SDL_SCANCODE_I])
  233. character->moveForward(0.1);
  234. if(I::keys[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. mover->getLocalTransform().getRotation().reorthogonalize();
  243. app->getScene()->getPhysics()->update(crntTime);
  244. app->getScene()->updateAllControllers();
  245. app->getScene()->updateAllWorldStuff();
  246. app->getMainRenderer()->render(*app->getActiveCam());
  247. //map.octree.root->bounding_box.render();
  248. // print some debug stuff
  249. Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  250. Ui::setPos(-0.98, 0.95);
  251. Ui::setFontWidth(0.03);
  252. //Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer()->getFramesNum(), (App::getTicks()-ticks_));
  253. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  254. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  255. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  256. if(I::keys[SDL_SCANCODE_ESCAPE]) break;
  257. if(I::keys[SDL_SCANCODE_F11]) app->togleFullScreen();
  258. if(I::keys[SDL_SCANCODE_F12] == 1) app->getMainRenderer()->takeScreenshot("gfx/screenshot.jpg");
  259. /*char str[128];
  260. static string scrFile = (app->getSettingsPath() / "capt" / "%06d.jpg").string();
  261. sprintf(str, scrFile.c_str(), app->getMainRenderer()->getFramesNum());
  262. app->getMainRenderer()->takeScreenshot(str);*/
  263. // std stuff follow
  264. app->swapBuffers();
  265. GL_OK();
  266. if(1)
  267. {
  268. //if(R::framesNum == 10) R::takeScreenshot("gfx/screenshot.tga");
  269. app->waitForNextFrame();
  270. }
  271. else
  272. if(app->getMainRenderer()->getFramesNum() == 5000) break;
  273. }while(true);
  274. INFO("Exiting main loop (" << App::getTicks()-ticks << ")");
  275. }
  276. //======================================================================================================================
  277. // main =
  278. //======================================================================================================================
  279. int main(int argc, char* argv[])
  280. {
  281. new App(argc, argv);
  282. init();
  283. mainLoop();
  284. INFO("Exiting...");
  285. app->quit(EXIT_SUCCESS);
  286. return 0;
  287. }