Main.cpp 14 KB

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