Main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "particles.h"
  13. #include "Texture.h"
  14. #include "Mesh.h"
  15. #include "Light.h"
  16. #include "collision.h"
  17. #include "Material.h"
  18. #include "Resource.h"
  19. #include "Scene.h"
  20. #include "Scanner.h"
  21. #include "skybox.h"
  22. #include "map.h"
  23. #include "MeshNode.h"
  24. #include "SkelModelNode.h"
  25. #include "MeshNode.h"
  26. #include "SkelAnim.h"
  27. #include "MeshSkelNodeCtrl.h"
  28. #include "SkelAnimCtrl.h"
  29. #include "SkelNode.h"
  30. #include "LightProps.h"
  31. #include "BulletDebuger.h"
  32. #include "PhyCommon.h"
  33. #include "Parser.h"
  34. App* app;
  35. // map (hard coded)
  36. MeshNode* floor__,* sarge,* horse,* crate;
  37. SkelModelNode* imp;
  38. PointLight* point_lights[10];
  39. SpotLight* spot_lights[2];
  40. class floor_t: public Camera
  41. {
  42. public:
  43. void render()
  44. {
  45. R::Dbg::renderCube( true, 1.0 );
  46. }
  47. void renderDepth()
  48. {
  49. R::Dbg::renderCube( true, 1.0 );
  50. }
  51. }* floor_;
  52. // Physics
  53. BulletDebuger debugDrawer;
  54. #define ARRAY_SIZE_X 5
  55. #define ARRAY_SIZE_Y 5
  56. #define ARRAY_SIZE_Z 5
  57. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  58. #define SCALING 1.
  59. #define START_POS_X -5
  60. #define START_POS_Y -5
  61. #define START_POS_Z -3
  62. void initPhysics()
  63. {
  64. btDiscreteDynamicsWorld* dynamicsWorld = app->scene->getPhyWorld()->getDynamicsWorld();
  65. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  66. btTransform groundTransform;
  67. groundTransform.setIdentity();
  68. groundTransform.setOrigin(btVector3(0,-50,0));
  69. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  70. {
  71. btScalar mass(0.);
  72. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  73. bool isDynamic = (mass != 0.f);
  74. btVector3 localInertia(0,0,0);
  75. if (isDynamic)
  76. groundShape->calculateLocalInertia(mass,localInertia);
  77. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  78. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  79. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  80. btRigidBody* body = new btRigidBody(rbInfo);
  81. //add the body to the dynamics world
  82. dynamicsWorld->addRigidBody(body);
  83. }
  84. {
  85. //create a few dynamic rigidbodies
  86. // Re-using the same collision is better for memory usage and performance
  87. btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
  88. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  89. /// Create Dynamic Objects
  90. btTransform startTransform;
  91. startTransform.setIdentity();
  92. btScalar mass(1.0);
  93. btVector3 localInertia(0,0,0);
  94. colShape->calculateLocalInertia(mass,localInertia);
  95. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  96. float start_y = START_POS_Y;
  97. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  98. btRigidBody* body;
  99. for (int k=0;k<ARRAY_SIZE_Y;k++)
  100. {
  101. for (int i=0;i<ARRAY_SIZE_X;i++)
  102. {
  103. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  104. {
  105. startTransform.setOrigin(SCALING*btVector3(
  106. btScalar(2.0*i + start_x),
  107. btScalar(20+2.0*k + start_y),
  108. btScalar(2.0*j + start_z)));
  109. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  110. MeshNode* crate = new MeshNode;
  111. crate->init( "models/crate0/crate0.mesh" );
  112. crate->scaleLspace = 1.11;
  113. MotionState* myMotionState = new MotionState( startTransform, crate);
  114. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  115. //btRigidBody* body = new btRigidBody(rbInfo);
  116. body = new btRigidBody(rbInfo);
  117. //if( i=2 ) body->setActivationState(ISLAND_SLEEPING);
  118. //body->setActivationState(ISLAND_SLEEPING);
  119. //body->setGravity( btVector3( 0.0, -1.0, 0.0 ) );
  120. dynamicsWorld->addRigidBody(body);
  121. }
  122. }
  123. }
  124. }
  125. //dynamicsWorld->setDebugDrawer(&debugDrawer);
  126. }
  127. #include "ShaderPrePreprocessor.h"
  128. //=====================================================================================================================================
  129. // init =
  130. //=====================================================================================================================================
  131. void init()
  132. {
  133. PRINT( "Engine initializing..." );
  134. srand( unsigned(time(NULL)) );
  135. mathSanityChecks();
  136. app = new App;
  137. app->initWindow();
  138. uint ticks = app->getTicks();
  139. R::init();
  140. Ui::init();
  141. app->scene = new Scene;
  142. // camera
  143. app->activeCam = new Camera( R::aspectRatio*toRad(60.0), toRad(60.0), 0.5, 200.0 );
  144. app->activeCam->moveLocalY( 3.0 );
  145. app->activeCam->moveLocalZ( 5.7 );
  146. app->activeCam->moveLocalX( -0.3 );
  147. // lights
  148. point_lights[0] = new PointLight();
  149. point_lights[0]->init( "maps/temple/light0.light" );
  150. point_lights[0]->setLocalTransformation( Vec3( -1.0, 2.4, 1.0 ), Mat3::getIdentity(), 1.0 );
  151. point_lights[1] = new PointLight();
  152. point_lights[1]->init( "maps/temple/light1.light" );
  153. point_lights[1]->setLocalTransformation( Vec3( 2.5, 1.4, 1.0 ), Mat3::getIdentity(), 1.0 );
  154. spot_lights[0] = new SpotLight();
  155. spot_lights[0]->init( "maps/temple/light2.light" );
  156. spot_lights[0]->setLocalTransformation( Vec3( 1.3, 4.3, 3.0 ), Mat3( Euler(toRad(-20), toRad(20), 0.0) ), 1.0 );
  157. spot_lights[1] = new SpotLight();
  158. spot_lights[1]->init( "maps/temple/light3.light" );
  159. spot_lights[1]->setLocalTransformation( Vec3( -2.3, 6.3, 2.9 ), Mat3( Euler(toRad(-70), toRad(-20), 0.0) ), 1.0 );
  160. // horse
  161. horse = new MeshNode();
  162. horse->init( "meshes/horse/horse.mesh" );
  163. horse->setLocalTransformation( Vec3( -2, 0, 1 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.5 );
  164. // sarge
  165. sarge = new MeshNode();
  166. sarge->init( "meshes/sphere/sphere16.mesh" );
  167. //sarge->setLocalTransformation( Vec3( 0, -2.8, 1.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 1.1 );
  168. sarge->setLocalTransformation( Vec3( 0, 2.0, 2.0 ), Mat3::getIdentity(), 0.4 );
  169. // floor
  170. floor__ = new MeshNode();
  171. floor__->init( "maps/temple/Cube.019.mesh" );
  172. floor__->setLocalTransformation( Vec3(0.0, -0.19, 0.0), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.8 );
  173. // imp
  174. imp = new SkelModelNode();
  175. imp->init( "models/imp/imp.smdl" );
  176. imp->setLocalTransformation( Vec3( 0.0, 2.11, 0.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.7 );
  177. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim = Rsrc::skelAnims.load( "models/imp/walk.imp.anim" );
  178. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
  179. // crate
  180. /*crate = new MeshNode;
  181. crate->init( "models/crate0/crate0.mesh" );
  182. crate->scaleLspace = 1.0;*/
  183. //
  184. //floor_ = new floor_t;
  185. //floor_->material = Rsrc::materials.load( "materials/default.mtl" );
  186. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  187. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  188. app->scene->skybox.load( skybox_fnames );
  189. initPhysics();
  190. PRINT( "Engine initialization ends (" << App::getTicks()-ticks << ")" );
  191. }
  192. //=====================================================================================================================================
  193. // main =
  194. //=====================================================================================================================================
  195. int main( int /*argc*/, char* /*argv*/[] )
  196. {
  197. App::printAppInfo();
  198. init();
  199. PRINT( "Entering main loop" );
  200. int ticks = App::getTicks();
  201. do
  202. {
  203. int ticks_ = App::getTicks();
  204. I::handleEvents();
  205. R::prepareNextFrame();
  206. float dist = 0.2;
  207. float ang = toRad(3.0);
  208. float scale = 0.01;
  209. // move the camera
  210. static Node* mover = app->activeCam;
  211. if( I::keys[ SDLK_1 ] ) mover = app->activeCam;
  212. if( I::keys[ SDLK_2 ] ) mover = point_lights[0];
  213. if( I::keys[ SDLK_3 ] ) mover = spot_lights[0];
  214. if( I::keys[ SDLK_4 ] ) mover = point_lights[1];
  215. if( I::keys[ SDLK_5 ] ) mover = spot_lights[1];
  216. if( I::keys[ SDLK_m ] == 1 ) I::warpMouse = !I::warpMouse;
  217. if( I::keys[SDLK_a] ) mover->moveLocalX( -dist );
  218. if( I::keys[SDLK_d] ) mover->moveLocalX( dist );
  219. if( I::keys[SDLK_LSHIFT] ) mover->moveLocalY( dist );
  220. if( I::keys[SDLK_SPACE] ) mover->moveLocalY( -dist );
  221. if( I::keys[SDLK_w] ) mover->moveLocalZ( -dist );
  222. if( I::keys[SDLK_s] ) mover->moveLocalZ( dist );
  223. if( !I::warpMouse )
  224. {
  225. if( I::keys[SDLK_UP] ) mover->rotateLocalX( ang );
  226. if( I::keys[SDLK_DOWN] ) mover->rotateLocalX( -ang );
  227. if( I::keys[SDLK_LEFT] ) mover->rotateLocalY( ang );
  228. if( I::keys[SDLK_RIGHT] ) mover->rotateLocalY( -ang );
  229. }
  230. else
  231. {
  232. float accel = 44.0;
  233. mover->rotateLocalX( ang * I::mouseVelocity.y * accel );
  234. mover->rotateLocalY( -ang * I::mouseVelocity.x * accel );
  235. }
  236. if( I::keys[SDLK_q] ) mover->rotateLocalZ( ang );
  237. if( I::keys[SDLK_e] ) mover->rotateLocalZ( -ang );
  238. if( I::keys[SDLK_PAGEUP] ) mover->scaleLspace += scale ;
  239. if( I::keys[SDLK_PAGEDOWN] ) mover->scaleLspace -= scale ;
  240. if( I::keys[SDLK_k] ) app->activeCam->lookAtPoint( point_lights[0]->translationWspace );
  241. mover->rotationLspace.reorthogonalize();
  242. //static_cast<btRigidBody*>(dynamicsWorld->getCollisionObjectArray()[1])->getMotionState()->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  243. //dynamicsWorld->getCollisionObjectArray()[3]->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  244. app->scene->updateAllControllers();
  245. app->scene->updateAllWorldStuff();
  246. app->scene->getPhyWorld()->getDynamicsWorld()->stepSimulation( app->timerTick );
  247. app->scene->getPhyWorld()->getDynamicsWorld()->debugDrawWorld();
  248. R::render( *app->activeCam );
  249. //map.octree.root->bounding_box.render();
  250. // print some debug stuff
  251. Ui::setColor( Vec4(1.0, 1.0, 1.0, 1.0) );
  252. Ui::setPos( -0.98, 0.95 );
  253. Ui::setFontWidth( 0.03 );
  254. Ui::printf( "frame:%d fps:%dms\n", R::framesNum, (App::getTicks()-ticks_) );
  255. //Ui::print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
  256. Ui::printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  257. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z) );
  258. if( I::keys[SDLK_ESCAPE] ) break;
  259. if( I::keys[SDLK_F11] ) app->togleFullScreen();
  260. if( I::keys[SDLK_F12] == 1 ) R::takeScreenshot("gfx/screenshot.jpg");
  261. /*char str[128];
  262. sprintf( str, "capt/%06d.jpg", R::framesNum );
  263. R::takeScreenshot(str);*/
  264. // std stuff follow
  265. SDL_GL_SwapBuffers();
  266. R::printLastError();
  267. if( 1 )
  268. {
  269. //if( R::framesNum == 10 ) R::takeScreenshot("gfx/screenshot.tga");
  270. app->waitForNextFrame();
  271. }
  272. else
  273. if( R::framesNum == 5000 ) break;
  274. }while( true );
  275. PRINT( "Exiting main loop (" << App::getTicks()-ticks << ")" );
  276. PRINT( "Exiting..." );
  277. app->quitApp( EXIT_SUCCESS );
  278. return 0;
  279. }