Main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include "ParticleEmitter.h"
  35. #include "PhyCharacter.h"
  36. App* app;
  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. BulletDebuger debugDrawer;
  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->scaleLspace = 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. }
  122. //=====================================================================================================================================
  123. // init =
  124. //=====================================================================================================================================
  125. void init()
  126. {
  127. PRINT( "Engine initializing..." );
  128. srand( unsigned(time(NULL)) );
  129. mathSanityChecks();
  130. app = new App;
  131. app->initWindow();
  132. uint ticks = app->getTicks();
  133. R::init();
  134. Ui::init();
  135. // camera
  136. Camera* cam = new Camera( R::aspectRatio*toRad(60.0), toRad(60.0), 0.5, 200.0 );
  137. cam->moveLocalY( 3.0 );
  138. cam->moveLocalZ( 5.7 );
  139. cam->moveLocalX( -0.3 );
  140. app->setActiveCam( cam );
  141. // lights
  142. point_lights[0] = new PointLight();
  143. point_lights[0]->init( "maps/temple/light0.light" );
  144. point_lights[0]->setLocalTransformation( Vec3( -1.0, 2.4, 1.0 ), Mat3::getIdentity(), 1.0 );
  145. point_lights[1] = new PointLight();
  146. point_lights[1]->init( "maps/temple/light1.light" );
  147. point_lights[1]->setLocalTransformation( Vec3( 2.5, 1.4, 1.0 ), Mat3::getIdentity(), 1.0 );
  148. spot_lights[0] = new SpotLight();
  149. spot_lights[0]->init( "maps/temple/light2.light" );
  150. spot_lights[0]->setLocalTransformation( Vec3( 1.3, 4.3, 3.0 ), Mat3( Euler(toRad(-20), toRad(20), 0.0) ), 1.0 );
  151. spot_lights[1] = new SpotLight();
  152. spot_lights[1]->init( "maps/temple/light3.light" );
  153. spot_lights[1]->setLocalTransformation( Vec3( -2.3, 6.3, 2.9 ), Mat3( Euler(toRad(-70), toRad(-20), 0.0) ), 1.0 );
  154. // horse
  155. horse = new MeshNode();
  156. horse->init( "meshes/horse/horse.mesh" );
  157. horse->setLocalTransformation( Vec3( -2, 0, 1 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.5 );
  158. // sarge
  159. sarge = new MeshNode();
  160. sarge->init( "meshes/sphere/sphere16.mesh" );
  161. //sarge->setLocalTransformation( Vec3( 0, -2.8, 1.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 1.1 );
  162. sarge->setLocalTransformation( Vec3( 0, 2.0, 2.0 ), Mat3::getIdentity(), 0.4 );
  163. // floor
  164. floor__ = new MeshNode();
  165. floor__->init( "maps/temple/Cube.019.mesh" );
  166. floor__->setLocalTransformation( Vec3(0.0, -0.19, 0.0), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.8 );
  167. // imp
  168. imp = new SkelModelNode();
  169. imp->init( "models/imp/imp.smdl" );
  170. imp->setLocalTransformation( Vec3( 0.0, 2.11, 0.0 ), Mat3( Euler(-M::PI/2, 0.0, 0.0) ), 0.7 );
  171. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim = Rsrc::skelAnims.load( "models/imp/walk.imp.anim" );
  172. imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
  173. // particle emitter
  174. partEmitter = new ParticleEmitter;
  175. partEmitter->init( NULL );
  176. partEmitter->translationLspace = Vec3( 3.0, 0.0, 0.0 );
  177. // crate
  178. /*crate = new MeshNode;
  179. crate->init( "models/crate0/crate0.mesh" );
  180. crate->scaleLspace = 1.0;*/
  181. //
  182. //floor_ = new floor_t;
  183. //floor_->material = Rsrc::materials.load( "materials/default.mtl" );
  184. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  185. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  186. app->getScene()->skybox.load( skybox_fnames );
  187. initPhysics();
  188. PRINT( "Engine initialization ends (" << App::getTicks()-ticks << ")" );
  189. }
  190. //=====================================================================================================================================
  191. // main =
  192. //=====================================================================================================================================
  193. int main( int /*argc*/, char* /*argv*/[] )
  194. {
  195. App::printAppInfo();
  196. init();
  197. PRINT( "Entering main loop" );
  198. int ticks = App::getTicks();
  199. do
  200. {
  201. int ticks_ = App::getTicks();
  202. I::handleEvents();
  203. R::prepareNextFrame();
  204. float dist = 0.2;
  205. float ang = toRad(3.0);
  206. float scale = 0.01;
  207. // move the camera
  208. static Node* mover = app->getActiveCam();
  209. if( I::keys[ SDLK_1 ] ) mover = app->getActiveCam();
  210. if( I::keys[ SDLK_2 ] ) mover = point_lights[0];
  211. if( I::keys[ SDLK_3 ] ) mover = spot_lights[0];
  212. if( I::keys[ SDLK_4 ] ) mover = point_lights[1];
  213. if( I::keys[ SDLK_5 ] ) mover = spot_lights[1];
  214. if( I::keys[ SDLK_6 ] ) mover = partEmitter;
  215. if( I::keys[ SDLK_m ] == 1 ) I::warpMouse = !I::warpMouse;
  216. if( I::keys[SDLK_a] ) mover->moveLocalX( -dist );
  217. if( I::keys[SDLK_d] ) mover->moveLocalX( dist );
  218. if( I::keys[SDLK_LSHIFT] ) mover->moveLocalY( dist );
  219. if( I::keys[SDLK_SPACE] ) mover->moveLocalY( -dist );
  220. if( I::keys[SDLK_w] ) mover->moveLocalZ( -dist );
  221. if( I::keys[SDLK_s] ) mover->moveLocalZ( dist );
  222. if( !I::warpMouse )
  223. {
  224. if( I::keys[SDLK_UP] ) mover->rotateLocalX( ang );
  225. if( I::keys[SDLK_DOWN] ) mover->rotateLocalX( -ang );
  226. if( I::keys[SDLK_LEFT] ) mover->rotateLocalY( ang );
  227. if( I::keys[SDLK_RIGHT] ) mover->rotateLocalY( -ang );
  228. }
  229. else
  230. {
  231. float accel = 44.0;
  232. mover->rotateLocalX( ang * I::mouseVelocity.y * accel );
  233. mover->rotateLocalY( -ang * I::mouseVelocity.x * accel );
  234. }
  235. if( I::keys[SDLK_q] ) mover->rotateLocalZ( ang );
  236. if( I::keys[SDLK_e] ) mover->rotateLocalZ( -ang );
  237. if( I::keys[SDLK_PAGEUP] ) mover->scaleLspace += scale ;
  238. if( I::keys[SDLK_PAGEDOWN] ) mover->scaleLspace -= scale ;
  239. if( I::keys[SDLK_k] ) app->getActiveCam()->lookAtPoint( point_lights[0]->translationWspace );
  240. if( I::keys[SDLK_o] == 1 )
  241. {
  242. btRigidBody* body = static_cast<btRigidBody*>( boxes[0] );
  243. //body->getMotionState()->setWorldTransform( toBt( Mat4( Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0 ) ) );
  244. body->setWorldTransform( toBt( Mat4( Vec3(0.0, 10.0, 0.0), Mat3::getIdentity(), 1.0 ) ) );
  245. //body->clearForces();
  246. body->forceActivationState( ACTIVE_TAG );
  247. }
  248. mover->rotationLspace.reorthogonalize();
  249. //static_cast<btRigidBody*>(dynamicsWorld->getCollisionObjectArray()[1])->getMotionState()->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  250. //dynamicsWorld->getCollisionObjectArray()[3]->setWorldTransform( toBt(point_lights[0]->transformationWspace) );
  251. app->getScene()->updateAllControllers();
  252. app->getScene()->updateAllWorldStuff();
  253. //partEmitter->update();
  254. app->getScene()->getPhyWorld()->getDynamicsWorld()->stepSimulation( app->timerTick );
  255. app->getScene()->getPhyWorld()->getDynamicsWorld()->debugDrawWorld();
  256. R::render( *app->getActiveCam() );
  257. //map.octree.root->bounding_box.render();
  258. // print some debug stuff
  259. Ui::setColor( Vec4(1.0, 1.0, 1.0, 1.0) );
  260. Ui::setPos( -0.98, 0.95 );
  261. Ui::setFontWidth( 0.03 );
  262. Ui::printf( "frame:%d fps:%dms\n", R::framesNum, (App::getTicks()-ticks_) );
  263. //Ui::print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
  264. Ui::printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translationWspace.x, mover->translationWspace.y, mover->translationWspace.z,
  265. toDegrees(Euler(mover->rotationWspace).x), toDegrees(Euler(mover->rotationWspace).y), toDegrees(Euler(mover->rotationWspace).z) );
  266. if( I::keys[SDLK_ESCAPE] ) break;
  267. if( I::keys[SDLK_F11] ) app->togleFullScreen();
  268. if( I::keys[SDLK_F12] == 1 ) R::takeScreenshot("gfx/screenshot.jpg");
  269. /*char str[128];
  270. sprintf( str, "capt/%06d.jpg", R::framesNum );
  271. R::takeScreenshot(str);*/
  272. // std stuff follow
  273. SDL_GL_SwapBuffers();
  274. R::printLastError();
  275. if( 1 )
  276. {
  277. //if( R::framesNum == 10 ) R::takeScreenshot("gfx/screenshot.tga");
  278. app->waitForNextFrame();
  279. }
  280. else
  281. if( R::framesNum == 5000 ) break;
  282. }while( true );
  283. PRINT( "Exiting main loop (" << App::getTicks()-ticks << ")" );
  284. PRINT( "Exiting..." );
  285. app->quitApp( EXIT_SUCCESS );
  286. return 0;
  287. }