main.cpp 11 KB

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