Main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <typeinfo>
  5. #include "Common.h"
  6. #include "Input.h"
  7. #include "Camera.h"
  8. #include "Math.h"
  9. #include "Renderer.h"
  10. #include "Ui.h"
  11. #include "App.h"
  12. #include "Texture.h"
  13. #include "Mesh.h"
  14. #include "Light.h"
  15. #include "collision.h"
  16. #include "Material.h"
  17. #include "Resource.h"
  18. #include "Scene.h"
  19. #include "Scanner.h"
  20. #include "skybox.h"
  21. #include "map.h"
  22. #include "MeshNode.h"
  23. #include "SkelModelNode.h"
  24. #include "MeshNode.h"
  25. #include "SkelAnim.h"
  26. #include "MeshSkelNodeCtrl.h"
  27. #include "SkelAnimCtrl.h"
  28. #include "SkelNode.h"
  29. #include "LightProps.h"
  30. #include "PhyCommon.h"
  31. #include "Parser.h"
  32. #include "ParticleEmitter.h"
  33. #include "PhyCharacter.h"
  34. #include "Renderer.h"
  35. #include "RendererInitializer.h"
  36. #include "MainRenderer.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. // Physics
  45. Vec<btRigidBody*> boxes;
  46. #define ARRAY_SIZE_X 5
  47. #define ARRAY_SIZE_Y 5
  48. #define ARRAY_SIZE_Z 5
  49. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  50. #define SCALING 1.
  51. #define START_POS_X -5
  52. #define START_POS_Y -5
  53. #define START_POS_Z -3
  54. void initPhysics()
  55. {
  56. btDiscreteDynamicsWorld* dynamicsWorld = app->getScene()->getPhyWorld()->getDynamicsWorld();
  57. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  58. btTransform groundTransform;
  59. groundTransform.setIdentity();
  60. groundTransform.setOrigin(btVector3(0,-50, 0));
  61. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  62. {
  63. btScalar mass(0.);
  64. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  65. bool isDynamic = (mass != 0.f);
  66. btVector3 localInertia(0, 0, 0);
  67. if (isDynamic)
  68. groundShape->calculateLocalInertia(mass,localInertia);
  69. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  70. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  71. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  72. btRigidBody* body = new btRigidBody(rbInfo);
  73. //add the body to the dynamics world
  74. dynamicsWorld->addRigidBody(body);
  75. }
  76. {
  77. //create a few dynamic rigidbodies
  78. // Re-using the same collision is better for memory usage and performance
  79. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  80. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  81. /// Create Dynamic Objects
  82. btTransform startTransform;
  83. startTransform.setIdentity();
  84. btScalar mass(1.0);
  85. btVector3 localInertia(0, 0, 0);
  86. colShape->calculateLocalInertia(mass,localInertia);
  87. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  88. float start_y = START_POS_Y;
  89. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  90. btRigidBody* body;
  91. for (int k=0;k<ARRAY_SIZE_Y;k++)
  92. {
  93. for (int i=0;i<ARRAY_SIZE_X;i++)
  94. {
  95. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  96. {
  97. /*startTransform.setOrigin(SCALING*btVector3(
  98. btScalar(2.0*i + start_x),
  99. btScalar(20+2.0*k + start_y),
  100. btScalar(2.0*j + start_z)));*/
  101. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  102. /*MeshNode* crate = new MeshNode;
  103. crate->init("models/crate0/crate0.mesh");
  104. crate->getLocalTransform().setScale(1.11);
  105. Transform trf(SCALING*Vec3(2.0*i + start_x, 20+2.0*k + start_y, 2.0*j + start_z), Mat3::getIdentity(), 1.0);
  106. body = app->getScene()->getPhyWorld()->createNewRigidBody(mass, trf, colShape, crate);*/
  107. /*MotionState* myMotionState = new MotionState(startTransform, crate);
  108. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  109. //btRigidBody* body = new btRigidBody(rbInfo);
  110. body = new btRigidBody(rbInfo);
  111. //if(i=2) body->setActivationState(ISLAND_SLEEPING);
  112. //body->setActivationState(ISLAND_SLEEPING);
  113. dynamicsWorld->addRigidBody(body);
  114. //body->setGravity(toBt(Vec3(Util::randRange(-1.0, 1.0), Util::randRange(-1.0, 1.0), Util::randRange(-1.0, 1.0))));*/
  115. boxes.push_back(body);
  116. }
  117. }
  118. }
  119. }
  120. //dynamicsWorld->setDebugDrawer(&debugDrawer);
  121. /*for(int i=0; i<app->getScene()->getPhyWorld()->getDynamicsWorld()->getCollisionObjectArray().size();i++)
  122. {
  123. btCollisionObject* colObj = app->getScene()->getPhyWorld()->getDynamicsWorld()->getCollisionObjectArray()[i];
  124. btRigidBody* body = btRigidBody::upcast(colObj);
  125. if(body)
  126. {
  127. if(body->getMotionState())
  128. {
  129. MotionState* myMotionState = (MotionState*)body->getMotionState();
  130. myMotionState->m_graphicsWorldTrans = myMotionState->m_startWorldTrans;
  131. body->setCenterOfMassTransform(myMotionState->m_graphicsWorldTrans);
  132. colObj->setInterpolationWorldTransform(myMotionState->m_startWorldTrans);
  133. colObj->forceActivationState(ACTIVE_TAG);
  134. colObj->activate();
  135. colObj->setDeactivationTime(0);
  136. //colObj->setActivationState(WANTS_DEACTIVATION);
  137. }
  138. //removed cached contact points (this is not necessary if all objects have been removed from the dynamics world)
  139. //m_dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(colObj->getBroadphaseHandle(),getDynamicsWorld()->getDispatcher());
  140. btRigidBody* body = btRigidBody::upcast(colObj);
  141. if (body && !body->isStaticObject())
  142. {
  143. btRigidBody::upcast(colObj)->setLinearVelocity(btVector3(0, 0, 0));
  144. btRigidBody::upcast(colObj)->setAngularVelocity(btVector3(0, 0, 0));
  145. }
  146. }
  147. }*/
  148. }
  149. //======================================================================================================================
  150. // init =
  151. //======================================================================================================================
  152. void init()
  153. {
  154. INFO("Engine initializing...");
  155. srand(unsigned(time(NULL)));
  156. mathSanityChecks();
  157. app->initWindow();
  158. uint ticks = app->getTicks();
  159. RendererInitializer initializer;
  160. initializer.ms.ez.enabled = false;
  161. initializer.dbg.enabled = true;
  162. initializer.is.sm.bilinearEnabled = true;
  163. initializer.is.sm.enabled = true;
  164. initializer.is.sm.pcfEnabled = true;
  165. initializer.is.sm.resolution = 512;
  166. initializer.pps.hdr.enabled = true;
  167. initializer.pps.hdr.renderingQuality = 0.5;
  168. initializer.pps.ssao.bluringQuality = 1.0;
  169. initializer.pps.ssao.enabled = true;
  170. initializer.pps.ssao.renderingQuality = 0.5;
  171. initializer.mainRendererQuality = 1.0;
  172. app->getMainRenderer()->init(initializer);
  173. Ui::init();
  174. // camera
  175. Camera* cam = new Camera(app->getMainRenderer()->getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
  176. cam->moveLocalY(3.0);
  177. cam->moveLocalZ(5.7);
  178. cam->moveLocalX(-0.3);
  179. app->setActiveCam(cam);
  180. // lights
  181. point_lights[0] = new PointLight();
  182. point_lights[0]->init("maps/temple/light0.light");
  183. point_lights[0]->setLocalTransform(Transform(Vec3(-1.0, 2.4, 1.0), Mat3::getIdentity(), 1.0));
  184. point_lights[1] = new PointLight();
  185. point_lights[1]->init("maps/temple/light1.light");
  186. point_lights[1]->setLocalTransform(Transform(Vec3(2.5, 1.4, 1.0), Mat3::getIdentity(), 1.0));
  187. spot_lights[0] = new SpotLight();
  188. spot_lights[0]->init("maps/temple/light2.light");
  189. spot_lights[0]->setLocalTransform(Transform(Vec3(1.3, 4.3, 3.0), Mat3(Euler(toRad(-20), toRad(20), 0.0)), 1.0));
  190. spot_lights[1] = new SpotLight();
  191. spot_lights[1]->init("maps/temple/light3.light");
  192. spot_lights[1]->setLocalTransform(Transform(Vec3(-2.3, 6.3, 2.9), Mat3(Euler(toRad(-70), toRad(-20), 0.0)), 1.0));
  193. // horse
  194. horse = new MeshNode();
  195. horse->init("meshes/horse/horse.mesh");
  196. //horse->init("models/head/head.mesh");
  197. horse->setLocalTransform(Transform(Vec3(-2, 0, 1), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.5));
  198. // sarge
  199. sarge = new MeshNode();
  200. sarge->init("meshes/sphere/sphere16.mesh");
  201. //sarge->setLocalTransform(Vec3(0, -2.8, 1.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 1.1);
  202. sarge->setLocalTransform(Transform(Vec3(0, 2.0, 2.0), Mat3::getIdentity(), 0.4));
  203. // floor
  204. floor__ = new MeshNode();
  205. floor__->init("maps/temple/Cube.019.mesh");
  206. floor__->setLocalTransform(Transform(Vec3(0.0, -0.19, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.8));
  207. // imp
  208. imp = new SkelModelNode();
  209. imp->init("models/imp/imp.smdl");
  210. imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
  211. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim = Resource::skelAnims.load("models/imp/walk.imp.anim");
  212. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
  213. // particle emitter
  214. partEmitter = new ParticleEmitter;
  215. partEmitter->init(NULL);
  216. partEmitter->getLocalTransform().setOrigin(Vec3(3.0, 0.0, 0.0));
  217. // crate
  218. /*crate = new MeshNode;
  219. crate->init("models/crate0/crate0.mesh");
  220. crate->scaleLspace = 1.0;*/
  221. //
  222. //floor_ = new floor_t;
  223. //floor_->material = Resource::materials.load("materials/default.mtl");
  224. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  225. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  226. app->getScene()->skybox.load(skybox_fnames);
  227. initPhysics();
  228. INFO("Engine initialization ends (" << App::getTicks()-ticks << ")");
  229. }
  230. //======================================================================================================================
  231. // mainLoop =
  232. //======================================================================================================================
  233. void mainLoop()
  234. {
  235. INFO("Entering main loop");
  236. int ticks = App::getTicks();
  237. do
  238. {
  239. int ticks_ = App::getTicks();
  240. I::handleEvents();
  241. float dist = 0.2;
  242. float ang = toRad(3.0);
  243. float scale = 0.01;
  244. // move the camera
  245. static SceneNode* mover = app->getActiveCam();
  246. if(I::keys[SDL_SCANCODE_1]) mover = app->getActiveCam();
  247. if(I::keys[SDL_SCANCODE_2]) mover = point_lights[0];
  248. if(I::keys[SDL_SCANCODE_3]) mover = spot_lights[0];
  249. if(I::keys[SDL_SCANCODE_4]) mover = point_lights[1];
  250. if(I::keys[SDL_SCANCODE_5]) mover = spot_lights[1];
  251. if(I::keys[SDL_SCANCODE_6]) mover = partEmitter;
  252. if(I::keys[SDL_SCANCODE_M] == 1) I::warpMouse = !I::warpMouse;
  253. if(I::keys[SDL_SCANCODE_A]) mover->moveLocalX(-dist);
  254. if(I::keys[SDL_SCANCODE_D]) mover->moveLocalX(dist);
  255. if(I::keys[SDL_SCANCODE_LSHIFT]) mover->moveLocalY(dist);
  256. if(I::keys[SDL_SCANCODE_SPACE]) mover->moveLocalY(-dist);
  257. if(I::keys[SDL_SCANCODE_W]) mover->moveLocalZ(-dist);
  258. if(I::keys[SDL_SCANCODE_S]) mover->moveLocalZ(dist);
  259. if(!I::warpMouse)
  260. {
  261. if(I::keys[SDL_SCANCODE_UP]) mover->rotateLocalX(ang);
  262. if(I::keys[SDL_SCANCODE_DOWN]) mover->rotateLocalX(-ang);
  263. if(I::keys[SDL_SCANCODE_LEFT]) mover->rotateLocalY(ang);
  264. if(I::keys[SDL_SCANCODE_RIGHT]) mover->rotateLocalY(-ang);
  265. }
  266. else
  267. {
  268. float accel = 44.0;
  269. mover->rotateLocalX(ang * I::mouseVelocity.y * accel);
  270. mover->rotateLocalY(-ang * I::mouseVelocity.x * accel);
  271. }
  272. if(I::keys[SDL_SCANCODE_Q]) mover->rotateLocalZ(ang);
  273. if(I::keys[SDL_SCANCODE_E]) mover->rotateLocalZ(-ang);
  274. if(I::keys[SDL_SCANCODE_PAGEUP]) mover->getLocalTransform().getScale() += scale ;
  275. if(I::keys[SDL_SCANCODE_PAGEDOWN]) mover->getLocalTransform().getScale() -= scale ;
  276. if(I::keys[SDL_SCANCODE_K]) app->getActiveCam()->lookAtPoint(point_lights[0]->getWorldTransform().getOrigin());
  277. if(I::keys[SDL_SCANCODE_O] == 1)
  278. {
  279. btRigidBody* body = static_cast<btRigidBody*>(boxes[0]);
  280. //body->getMotionState()->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  281. body->setWorldTransform(toBt(Mat4(Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0)));
  282. //body->clearForces();
  283. body->forceActivationState(ACTIVE_TAG);
  284. }
  285. mover->getLocalTransform().getRotation().reorthogonalize();
  286. //static_cast<btRigidBody*>(dynamicsWorld->getCollisionObjectArray()[1])->getMotionState()->setWorldTransform(toBt(point_lights[0]->transformationWspace));
  287. //dynamicsWorld->getCollisionObjectArray()[3]->setWorldTransform(toBt(point_lights[0]->transformationWspace));
  288. app->getScene()->updateAllControllers();
  289. app->getScene()->updateAllWorldStuff();
  290. //partEmitter->update();
  291. app->getScene()->getPhyWorld()->getDynamicsWorld()->stepSimulation(app->timerTick);
  292. app->getScene()->getPhyWorld()->getDynamicsWorld()->debugDrawWorld();
  293. app->getMainRenderer()->render(*app->getActiveCam());
  294. //map.octree.root->bounding_box.render();
  295. // print some debug stuff
  296. Ui::setColor(Vec4(1.0, 1.0, 1.0, 1.0));
  297. Ui::setPos(-0.98, 0.95);
  298. Ui::setFontWidth(0.03);
  299. Ui::printf("frame:%d fps:%dms\n", app->getMainRenderer()->getFramesNum(), (App::getTicks()-ticks_));
  300. //Ui::print("Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n");
  301. /*Ui::printf("Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  302. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z));*/
  303. if(I::keys[SDL_SCANCODE_ESCAPE]) break;
  304. if(I::keys[SDL_SCANCODE_F11]) app->togleFullScreen();
  305. if(I::keys[SDL_SCANCODE_F12] == 1) app->getMainRenderer()->takeScreenshot("gfx/screenshot.jpg");
  306. /*char str[128];
  307. sprintf(str, "capt/%06d.jpg", R::framesNum);
  308. R::takeScreenshot(str);*/
  309. // std stuff follow
  310. app->swapBuffers();
  311. GL_OK();
  312. if(1)
  313. {
  314. //if(R::framesNum == 10) R::takeScreenshot("gfx/screenshot.tga");
  315. app->waitForNextFrame();
  316. }
  317. else
  318. if(app->getMainRenderer()->getFramesNum() == 5000) break;
  319. }while(true);
  320. INFO("Exiting main loop (" << App::getTicks()-ticks << ")");
  321. }
  322. #include "Arr.h"
  323. //======================================================================================================================
  324. // main =
  325. //======================================================================================================================
  326. int main(int argc, char* argv[])
  327. {
  328. new App(argc, argv);
  329. init();
  330. mainLoop();
  331. INFO("Exiting...");
  332. app->quit(EXIT_SUCCESS);
  333. return 0;
  334. }