Main.cpp 12 KB

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