Main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "Texture.h"
  12. #include "Mesh.h"
  13. #include "Light.h"
  14. #include "collision.h"
  15. #include "Material.h"
  16. #include "Resource.h"
  17. #include "Scene.h"
  18. #include "Scanner.h"
  19. #include "skybox.h"
  20. #include "map.h"
  21. #include "MeshNode.h"
  22. #include "SkelModelNode.h"
  23. #include "MeshNode.h"
  24. #include "SkelAnim.h"
  25. #include "MeshSkelNodeCtrl.h"
  26. #include "SkelAnimCtrl.h"
  27. #include "SkelNode.h"
  28. #include "LightProps.h"
  29. #include "PhyCommon.h"
  30. #include "Parser.h"
  31. #include "ParticleEmitter.h"
  32. #include "PhyCharacter.h"
  33. #include "Renderer.h"
  34. #include "RendererInitializer.h"
  35. #include "MainRenderer.h"
  36. #include "DebugDrawer.h"
  37. #include <boost/lexical_cast.hpp>
  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. // 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. btDiscreteDynamicsWorld* dynamicsWorld = app->getScene()->getPhysics()->getDynamicsWorld();
  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. new RigidBody(0.0, groundTransform, groundShape, NULL, Physics::CG_MAP, Physics::CG_ALL);
  63. {
  64. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  65. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  66. float start_y = START_POS_Y;
  67. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  68. for (int k=0;k<ARRAY_SIZE_Y;k++)
  69. {
  70. for (int i=0;i<ARRAY_SIZE_X;i++)
  71. {
  72. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  73. {
  74. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  75. MeshNode* crate = new MeshNode;
  76. crate->init("models/crate0/crate0.mesh");
  77. crate->getLocalTransform().setScale(1.11);
  78. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  79. new RigidBody(1.0, trf, colShape, crate, Physics::CG_MAP, Physics::CG_ALL);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. //======================================================================================================================
  86. // init =
  87. //======================================================================================================================
  88. void init()
  89. {
  90. INFO("Engine initializing...");
  91. srand(unsigned(time(NULL)));
  92. mathSanityChecks();
  93. app->initWindow();
  94. uint ticks = app->getTicks();
  95. RendererInitializer initializer;
  96. initializer.ms.ez.enabled = false;
  97. initializer.dbg.enabled = true;
  98. initializer.is.sm.bilinearEnabled = true;
  99. initializer.is.sm.enabled = true;
  100. initializer.is.sm.pcfEnabled = true;
  101. initializer.is.sm.resolution = 512;
  102. initializer.pps.hdr.enabled = true;
  103. initializer.pps.hdr.renderingQuality = 0.5;
  104. initializer.pps.ssao.bluringQuality = 1.0;
  105. initializer.pps.ssao.enabled = true;
  106. initializer.pps.ssao.renderingQuality = 0.5;
  107. initializer.mainRendererQuality = 1.0;
  108. app->getMainRenderer()->init(initializer);
  109. Ui::init();
  110. // camera
  111. Camera* cam = new Camera(app->getMainRenderer()->getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  112. cam->moveLocalY(3.0);
  113. cam->moveLocalZ(5.7);
  114. cam->moveLocalX(-0.3);
  115. app->setActiveCam(cam);
  116. // lights
  117. point_lights[0] = new PointLight();
  118. point_lights[0]->init("maps/temple/light0.light");
  119. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  120. point_lights[1] = new PointLight();
  121. point_lights[1]->init("maps/temple/light1.light");
  122. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  123. spot_lights[0] = new SpotLight();
  124. spot_lights[0]->init("maps/temple/light2.light");
  125. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  126. spot_lights[1] = new SpotLight();
  127. spot_lights[1]->init("maps/temple/light3.light");
  128. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  129. // horse
  130. /*horse = new MeshNode();
  131. horse->init("meshes/horse/horse.mesh");
  132. //horse->init("models/head/head.mesh");
  133. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.5));
  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. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
  148. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;*/
  149. // cave map
  150. /*for(int i=1; i<21; i++)
  151. {
  152. MeshNode* node = new MeshNode();
  153. node->init(("maps/cave/rock." + lexical_cast<string>(i) + ".mesh").c_str());
  154. node->setLocalTransform(Transform(Vec3(0.0, -0.0, 0.0), Mat3::getIdentity(), 0.01));
  155. }*/
  156. // particle emitter
  157. partEmitter = new ParticleEmitter;
  158. partEmitter->init("asdf");
  159. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  160. // crate
  161. /*crate = new MeshNode;
  162. crate->init("models/crate0/crate0.mesh");
  163. crate->scaleLspace = 1.0;*/
  164. //
  165. //floor_ = new floor_t;
  166. //floor_->material = RsrcMngr::materials.load("materials/default.mtl");
  167. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  168. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  169. app->getScene()->skybox.load(skybox_fnames);
  170. //initPhysics();
  171. INFO("Engine initialization ends (" << App::getTicks()-ticks << ")");
  172. }
  173. //======================================================================================================================
  174. // mainLoop =
  175. //======================================================================================================================
  176. void mainLoop()
  177. {
  178. INFO("Entering main loop");
  179. int ticks = App::getTicks();
  180. float secs = 0.0;
  181. do
  182. {
  183. int ticks_ = App::getTicks();
  184. I::handleEvents();
  185. float dist = 0.2;
  186. float ang = toRad(3.0);
  187. float scale = 0.01;
  188. // move the camera
  189. static SceneNode* mover = app->getActiveCam();
  190. if(I::keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  191. if(I::keys[SDL_SCANCODE_2]) mover = point_lights[0];
  192. if(I::keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  193. if(I::keys[SDL_SCANCODE_4]) mover = point_lights[1];
  194. if(I::keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  195. if(I::keys[SDL_SCANCODE_6]) mover = partEmitter;
  196. if(I::keys[SDL_SCANCODE_M] == 1) I::warpMouse = !I::warpMouse;
  197. if(I::keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  198. if(I::keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  199. if(I::keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  200. if(I::keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  201. if(I::keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  202. if(I::keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  203. if(!I::warpMouse)
  204. {
  205. if(I::keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  206. if(I::keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  207. if(I::keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  208. if(I::keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  209. }
  210. else
  211. {
  212. float accel = 44.0;
  213. mover->rotateLocalX(ang * I::mouseVelocity.y * accel);
  214. mover->rotateLocalY(-ang * I::mouseVelocity.x * accel);
  215. }
  216. if(I::keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  217. if(I::keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  218. if(I::keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().getScale() += scale ;
  219. if(I::keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().getScale() -= scale ;
  220. if(I::keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  221. if(I::keys[SDL_SCANCODE_O] == 1)
  222. {
  223. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  224. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  225. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  226. //body->clearForces();
  227. body->forceActivationState(ACTIVE_TAG);
  228. }
  229. mover->getLocalTransform().getRotation().reorthogonalize();
  230. //static_cast<btRigidBody*>(dynamicsWorld->getCollisionObjectArray()[1])->getMotionState()->setWorldTransform(toBt(point_lights[0]->transformationWspace));
  231. //dynamicsWorld->getCollisionObjectArray()[3]->setWorldTransform(toBt(point_lights[0]->transformationWspace));
  232. secs = app->getTicks() / 1000.0 - secs;
  233. app->getScene()->getPhysics()->getDynamicsWorld()->stepSimulation(secs);
  234. app->getScene()->updateAllControllers();
  235. app->getScene()->updateAllWorldStuff();
  236. app->getMainRenderer()->render(*app->getActiveCam());
  237. //map.octree.root->bounding_box.render();
  238. // print some debug stuff
  239. Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  240. Ui::setPos(-0.98, 0.95);
  241. Ui::setFontWidth(0.03);
  242. Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer()->getFramesNum(), (App::getTicks()-ticks_));
  243. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  244. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  245. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  246. if(I::keys[SDL_SCANCODE_ESCAPE]) break;
  247. if(I::keys[SDL_SCANCODE_F11]) app->togleFullScreen();
  248. if(I::keys[SDL_SCANCODE_F12] == 1) app->getMainRenderer()->takeScreenshot("gfx/screenshot.jpg");
  249. /*char str[128];
  250. static string scrFile = (app->getSettingsPath() / "capt" / "%06d.jpg").string();
  251. sprintf(str, scrFile.c_str(), app->getMainRenderer()->getFramesNum());
  252. app->getMainRenderer()->takeScreenshot(str);*/
  253. // std stuff follow
  254. app->swapBuffers();
  255. GL_OK();
  256. if(1)
  257. {
  258. //if(R::framesNum == 10) R::takeScreenshot("gfx/screenshot.tga");
  259. app->waitForNextFrame();
  260. }
  261. else
  262. if(app->getMainRenderer()->getFramesNum() == 5000) break;
  263. }while(true);
  264. INFO("Exiting main loop (" << App::getTicks()-ticks << ")");
  265. }
  266. //======================================================================================================================
  267. // main =
  268. //======================================================================================================================
  269. int main(int argc, char* argv[])
  270. {
  271. new App(argc, argv);
  272. init();
  273. mainLoop();
  274. INFO("Exiting...");
  275. app->quit(EXIT_SUCCESS);
  276. return 0;
  277. }