Main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
  151. ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  152. ctrl->step = 0.8;
  153. // cave map
  154. /*for(int i=1; i<21; i++)
  155. {
  156. MeshNode* node = new MeshNode();
  157. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  158. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  159. }*/
  160. // sponza map
  161. /*MeshNode* node = new MeshNode();
  162. node->init("maps/sponza/sponza.mesh");*/
  163. //node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  164. // particle emitter
  165. partEmitter = new ParticleEmitter;
  166. partEmitter->init("asdf");
  167. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  168. // character
  169. PhyCharacter::Initializer init;
  170. init.sceneNode = imp;
  171. init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
  172. character = new PhyCharacter(*app->getScene()->getPhysics(), init);
  173. // crate
  174. /*crate = new MeshNode;
  175. crate->init("models/crate0/crate0.mesh");
  176. crate->scaleLspace = 1.0;*/
  177. //
  178. //floor_ = new floor_t;
  179. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  180. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  181. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  182. app->getScene()->skybox.load(skybox_fnames);
  183. initPhysics();
  184. INFO("Engine initialization ends (" << App::getTicks()-ticks << ")");
  185. }
  186. //======================================================================================================================
  187. // mainLoop =
  188. //======================================================================================================================
  189. void mainLoop()
  190. {
  191. INFO("Entering main loop");
  192. int ticks = App::getTicks();
  193. do
  194. {
  195. float crntTime = App::getTicks() / 1000.0;
  196. I::handleEvents();
  197. float dist = 0.2;
  198. float ang = toRad(3.0);
  199. float scale = 0.01;
  200. // move the camera
  201. static SceneNode* mover = app->getActiveCam();
  202. if(I::keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  203. if(I::keys[SDL_SCANCODE_2]) mover = point_lights[0];
  204. if(I::keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  205. if(I::keys[SDL_SCANCODE_4]) mover = point_lights[1];
  206. if(I::keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  207. if(I::keys[SDL_SCANCODE_6]) mover = partEmitter;
  208. if(I::keys[SDL_SCANCODE_M] == 1) I::warpMouse = !I::warpMouse;
  209. if(I::keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  210. if(I::keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  211. if(I::keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  212. if(I::keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  213. if(I::keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  214. if(I::keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  215. if(!I::warpMouse)
  216. {
  217. if(I::keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  218. if(I::keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  219. if(I::keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  220. if(I::keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  221. }
  222. else
  223. {
  224. float accel = 44.0;
  225. mover->rotateLocalX(ang * I::mouseVelocity.y * accel);
  226. mover->rotateLocalY(-ang * I::mouseVelocity.x * accel);
  227. }
  228. if(I::keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  229. if(I::keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  230. if(I::keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().getScale() += scale ;
  231. if(I::keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().getScale() -= scale ;
  232. if(I::keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  233. if(I::keys[SDL_SCANCODE_I])
  234. character->moveForward(0.1);
  235. if(I::keys[SDL_SCANCODE_O] == 1)
  236. {
  237. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  238. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  239. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  240. //body->clearForces();
  241. body->forceActivationState(ACTIVE_TAG);
  242. }
  243. mover->getLocalTransform().getRotation().reorthogonalize();
  244. app->getScene()->getPhysics()->update(crntTime);
  245. app->getScene()->updateAllControllers();
  246. app->getScene()->updateAllWorldStuff();
  247. app->getMainRenderer()->render(*app->getActiveCam());
  248. //map.octree.root->bounding_box.render();
  249. // print some debug stuff
  250. Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  251. Ui::setPos(-0.98, 0.95);
  252. Ui::setFontWidth(0.03);
  253. //Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer()->getFramesNum(), (App::getTicks()-ticks_));
  254. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  255. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  256. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  257. if(I::keys[SDL_SCANCODE_ESCAPE]) break;
  258. if(I::keys[SDL_SCANCODE_F11]) app->togleFullScreen();
  259. if(I::keys[SDL_SCANCODE_F12] == 1) app->getMainRenderer()->takeScreenshot("gfx/screenshot.jpg");
  260. /*char str[128];
  261. static string scrFile = (app->getSettingsPath() / "capt" / "%06d.jpg").string();
  262. sprintf(str, scrFile.c_str(), app->getMainRenderer()->getFramesNum());
  263. app->getMainRenderer()->takeScreenshot(str);*/
  264. // std stuff follow
  265. app->swapBuffers();
  266. GL_OK();
  267. if(1)
  268. {
  269. //if(R::framesNum == 10) R::takeScreenshot("gfx/screenshot.tga");
  270. app->waitForNextFrame();
  271. }
  272. else
  273. if(app->getMainRenderer()->getFramesNum() == 5000) break;
  274. }while(true);
  275. INFO("Exiting main loop (" << App::getTicks()-ticks << ")");
  276. }
  277. //======================================================================================================================
  278. // main =
  279. //======================================================================================================================
  280. int main(int argc, char* argv[])
  281. {
  282. new App(argc, argv);
  283. /* Mat3 m(Axisang(-PI/2, Vec3(1,0,0)));
  284. PRINT(fixed << m);
  285. return 0;*/
  286. init();
  287. mainLoop();
  288. INFO("Exiting...");
  289. app->quit(EXIT_SUCCESS);
  290. return 0;
  291. }