main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "gmath.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 "mesh_node.h"
  24. #include "skel_model_node.h"
  25. #include "mesh_node.h"
  26. #include "skel_anim.h"
  27. #include "mesh_skel_ctrl.h"
  28. #include "skel_anim_ctrl.h"
  29. #include "skel_node.h"
  30. #include "light_props.h"
  31. #include "btBulletCollisionCommon.h"
  32. #include "btBulletDynamicsCommon.h"
  33. #include "BulletDebuger.h"
  34. // map (hard coded)
  35. camera_t* main_cam;
  36. mesh_node_t* floor__,* sarge,* horse;
  37. skel_model_node_t* imp;
  38. point_light_t* point_lights[10];
  39. spot_light_t* spot_lights[2];
  40. class floor_t: public camera_t
  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.f);
  103. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  104. bool isDynamic = (mass != 0.f);
  105. btVector3 localInertia(0,0,0);
  106. if (isDynamic)
  107. colShape->calculateLocalInertia(mass,localInertia);
  108. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  109. float start_y = START_POS_Y;
  110. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  111. for (int k=0;k<ARRAY_SIZE_Y;k++)
  112. {
  113. for (int i=0;i<ARRAY_SIZE_X;i++)
  114. {
  115. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  116. {
  117. startTransform.setOrigin(SCALING*btVector3(
  118. btScalar(2.0*i + start_x),
  119. btScalar(20+2.0*k + start_y),
  120. btScalar(2.0*j + start_z)));
  121. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  122. btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
  123. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  124. btRigidBody* body = new btRigidBody(rbInfo);
  125. body->setActivationState(ISLAND_SLEEPING);
  126. dynamicsWorld->addRigidBody(body);
  127. body->setActivationState(ISLAND_SLEEPING);
  128. }
  129. }
  130. }
  131. }
  132. dynamicsWorld->setDebugDrawer(&debugDrawer);
  133. }
  134. //=====================================================================================================================================
  135. // Init =
  136. //=====================================================================================================================================
  137. void Init()
  138. {
  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. main_cam = new camera_t( r::aspect_ratio*ToRad(60.0), ToRad(60.0), 0.5, 100.0 );
  149. main_cam->MoveLocalY( 3.0 );
  150. main_cam->MoveLocalZ( 5.7 );
  151. main_cam->MoveLocalX( -0.3 );
  152. // lights
  153. point_lights[0] = new point_light_t();
  154. point_lights[0]->Init( "maps/temple/light0.light" );
  155. point_lights[0]->SetLocalTransformation( vec3_t( -1.0, 2.4, 1.0 ), mat3_t::GetIdentity(), 1.0 );
  156. point_lights[1] = new point_light_t();
  157. point_lights[1]->Init( "maps/temple/light1.light" );
  158. point_lights[1]->SetLocalTransformation( vec3_t( 2.5, 1.4, 1.0 ), mat3_t::GetIdentity(), 1.0 );
  159. spot_lights[0] = new spot_light_t();
  160. spot_lights[0]->Init( "maps/temple/light2.light" );
  161. spot_lights[0]->SetLocalTransformation( vec3_t( 1.3, 4.3, 3.0 ), mat3_t( euler_t(ToRad(-20), ToRad(20), 0.0) ), 1.0 );
  162. spot_lights[1] = new spot_light_t();
  163. spot_lights[1]->Init( "maps/temple/light3.light" );
  164. spot_lights[1]->SetLocalTransformation( vec3_t( -2.3, 6.3, 2.9 ), mat3_t( euler_t(ToRad(-70), ToRad(-20), 0.0) ), 1.0 );
  165. // horse
  166. horse = new mesh_node_t();
  167. horse->Init( "meshes/horse/horse.mesh" );
  168. horse->SetLocalTransformation( vec3_t( -2, 0, 1 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.5 );
  169. // sarge
  170. sarge = new mesh_node_t();
  171. sarge->Init( "meshes/sphere/sphere16.mesh" );
  172. //sarge->SetLocalTransformation( vec3_t( 0, -2.8, 1.0 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 1.1 );
  173. sarge->SetLocalTransformation( vec3_t( 0, 2.0, 2.0 ), mat3_t::GetIdentity(), 0.4 );
  174. // floor
  175. floor__ = new mesh_node_t();
  176. floor__->Init( "maps/temple/Cube.019.mesh" );
  177. floor__->SetLocalTransformation( vec3_t(0.0, -0.19, 0.0), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.8 );
  178. // imp
  179. imp = new skel_model_node_t();
  180. imp->Init( "models/imp/imp.smdl" );
  181. imp->SetLocalTransformation( vec3_t( 0.0, 2.11, 0.0 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.7 );
  182. imp->mesh_nodes[0]->mesh_skel_ctrl->skel_node->skel_anim_ctrl->skel_anim = rsrc::skel_anims.Load( "models/imp/walk.imp.anim" );
  183. imp->mesh_nodes[0]->mesh_skel_ctrl->skel_node->skel_anim_ctrl->step = 0.8;
  184. //
  185. floor_ = new floor_t;
  186. //floor_->material = rsrc::materials.Load( "materials/default.mtl" );
  187. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  188. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  189. scene::skybox.Load( skybox_fnames );
  190. PRINT( "Engine initialization ends (" << app::GetTicks()-ticks << ")" );
  191. cerr.flush();
  192. }
  193. //=====================================================================================================================================
  194. // main =
  195. //=====================================================================================================================================
  196. int main( int /*argc*/, char* /*argv*/[] )
  197. {
  198. app::PrintAppInfo();
  199. Init();
  200. PRINT( "Entering main loop" );
  201. int ticks = app::GetTicks();
  202. do
  203. {
  204. int ticks_ = app::GetTicks();
  205. i::HandleEvents();
  206. r::PrepareNextFrame();
  207. float dist = 0.2;
  208. float ang = ToRad(3.0);
  209. float scale = 0.01;
  210. // move the camera
  211. static node_t* mover = main_cam;
  212. if( i::keys[ SDLK_1 ] ) mover = main_cam;
  213. if( i::keys[ SDLK_2 ] ) mover = point_lights[0];
  214. if( i::keys[ SDLK_3 ] ) mover = spot_lights[0];
  215. if( i::keys[ SDLK_4 ] ) mover = point_lights[1];
  216. if( i::keys[ SDLK_5 ] ) mover = spot_lights[1];
  217. if( i::keys[ SDLK_m ] == 1 ) i::warp_mouse = !i::warp_mouse;
  218. if( i::keys[SDLK_a] ) mover->MoveLocalX( -dist );
  219. if( i::keys[SDLK_d] ) mover->MoveLocalX( dist );
  220. if( i::keys[SDLK_LSHIFT] ) mover->MoveLocalY( dist );
  221. if( i::keys[SDLK_SPACE] ) mover->MoveLocalY( -dist );
  222. if( i::keys[SDLK_w] ) mover->MoveLocalZ( -dist );
  223. if( i::keys[SDLK_s] ) mover->MoveLocalZ( dist );
  224. if( !i::warp_mouse )
  225. {
  226. if( i::keys[SDLK_UP] ) mover->RotateLocalX( ang );
  227. if( i::keys[SDLK_DOWN] ) mover->RotateLocalX( -ang );
  228. if( i::keys[SDLK_LEFT] ) mover->RotateLocalY( ang );
  229. if( i::keys[SDLK_RIGHT] ) mover->RotateLocalY( -ang );
  230. }
  231. else
  232. {
  233. float accel = 44.0;
  234. mover->RotateLocalX( ang * i::mouse_velocity.y * accel );
  235. mover->RotateLocalY( -ang * i::mouse_velocity.x * accel );
  236. }
  237. if( i::keys[SDLK_q] ) mover->RotateLocalZ( ang );
  238. if( i::keys[SDLK_e] ) mover->RotateLocalZ( -ang );
  239. if( i::keys[SDLK_PAGEUP] ) mover->scale_lspace += scale ;
  240. if( i::keys[SDLK_PAGEDOWN] ) mover->scale_lspace -= scale ;
  241. if( i::keys[SDLK_k] ) main_cam->LookAtPoint( point_lights[0]->translation_wspace );
  242. mover->rotation_lspace.Reorthogonalize();
  243. scene::UpdateAllControllers();
  244. scene::UpdateAllWorldStuff();
  245. dynamicsWorld->stepSimulation( 1 );
  246. r::Render( *main_cam );
  247. //map.octree.root->bounding_box.Render();
  248. // print some debug stuff
  249. ui::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
  250. ui::SetPos( -0.98, 0.95 );
  251. ui::SetFontWidth( 0.03 );
  252. ui::Printf( "frame:%d time:%dms\n", r::frames_num, app::GetTicks()-ticks_ );
  253. //ui::Print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
  254. ui::Printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translation_wspace.x, mover->translation_wspace.y, mover->translation_wspace.z,
  255. ToDegrees(euler_t(mover->rotation_wspace).x), ToDegrees(euler_t(mover->rotation_wspace).y), ToDegrees(euler_t(mover->rotation_wspace).z) );
  256. if( i::keys[SDLK_ESCAPE] ) break;
  257. if( i::keys[SDLK_F11] ) app::TogleFullScreen();
  258. if( i::keys[SDLK_F12] == 1 ) r::TakeScreenshot("gfx/screenshot.jpg");
  259. /*char str[128];
  260. if( r::frames_num < 1000 )
  261. sprintf( str, "capt/%06d.jpg", r::frames_num );
  262. else
  263. sprintf( str, "capt2/%06d.jpg", r::frames_num );
  264. r::TakeScreenshot(str);*/
  265. // std stuff follow
  266. SDL_GL_SwapBuffers();
  267. r::PrintLastError();
  268. if( 1 )
  269. {
  270. //if( r::frames_num == 10 ) r::TakeScreenshot("gfx/screenshot.tga");
  271. app::WaitForNextFrame();
  272. }
  273. else
  274. if( r::frames_num == 5000 ) break;
  275. }while( true );
  276. PRINT( "Exiting main loop (" << app::GetTicks()-ticks << ")" );
  277. PRINT( "Exiting..." );
  278. app::QuitApp( EXIT_SUCCESS );
  279. return 0;
  280. }