main.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "hud.h"
  11. #include "handlers.h"
  12. #include "particles.h"
  13. #include "primitives.h"
  14. #include "texture.h"
  15. #include "mesh.h"
  16. #include "shaders.h"
  17. #include "lights.h"
  18. #include "collision.h"
  19. #include "smodel.h"
  20. #include "spatial.h"
  21. #include "material.h"
  22. #include "resource.h"
  23. #include "scene.h"
  24. #include "scanner.h"
  25. #include "skybox.h"
  26. #include "map.h"
  27. #include "model.h"
  28. #include "shader_parser.h"
  29. camera_t main_cam;
  30. mesh_t imp, mcube, floor__, sarge;
  31. smodel_t mdl;
  32. skeleton_anim_t walk_anim;
  33. point_light_t point_lights[10];
  34. spot_light_t projlights[2];
  35. map_t map;
  36. class sphere_t: public mesh_t
  37. {
  38. public:
  39. sphere_t()
  40. {
  41. translation_lspace = vec3_t( 0.0, 0.0, 0.0 );
  42. scale_lspace = 2.5;
  43. }
  44. void Render()
  45. {
  46. glPushMatrix();
  47. r::MultMatrix( transformation_wspace );
  48. //r::dbg::RenderSphere( 1.0, 16.0 );
  49. r::dbg::RenderCube( false, 1.0 );
  50. glPopMatrix();
  51. }
  52. void RenderDepth()
  53. {
  54. glPushMatrix();
  55. r::MultMatrix( transformation_wspace );
  56. //r::dbg::RenderSphere( 1.0, 16.0 );
  57. r::dbg::RenderCube( false, 1.0 );
  58. glPopMatrix();
  59. }
  60. } sphere;
  61. /*
  62. =======================================================================================================================================
  63. Init =
  64. =======================================================================================================================================
  65. */
  66. void Init()
  67. {
  68. #if defined( _DEBUG_ )
  69. PRINT( "Engine initializing (Debug)..." );
  70. #else
  71. PRINT( "Engine initializing (Release)..." );
  72. #endif
  73. srand( unsigned(time(NULL)) );
  74. MathSanityChecks();
  75. hndl::InitWindow( r::w, r::h, "AnKi Engine" );
  76. uint ticks = hndl::GetTicks();
  77. r::Init();
  78. hud::Init();
  79. main_cam = camera_t( r::aspect_ratio*ToRad(60.0), ToRad(60.0), 0.5, 100.0 );
  80. main_cam.MoveLocalY( 3.0 );
  81. main_cam.MoveLocalZ( 5.7 );
  82. main_cam.MoveLocalX( -0.3 );
  83. // main_cam.translation_lspace = vec3_t(2.0, 2.0, 0.0);
  84. // main_cam.RotateLocalY( ToRad(75) );
  85. // main_cam.RotateLocalX( ToRad(-30) );
  86. main_cam.camera_data_user_class_t::SetName("main_cam");
  87. point_lights[0].SetSpecularColor( vec3_t( 0.4, 0.4, 0.4) );
  88. point_lights[0].SetDiffuseColor( vec3_t( 1.0, 0.0, 0.0)*1 );
  89. point_lights[0].translation_lspace = vec3_t( -1.0, 2.4, 1.0 );
  90. point_lights[0].radius = 2.0;
  91. point_lights[1].SetSpecularColor( vec3_t( 0.0, 0.0, 1.0)*4 );
  92. point_lights[1].SetDiffuseColor( vec3_t( 3.0, 0.1, 0.1) );
  93. point_lights[1].translation_lspace = vec3_t( 2.5, 1.4, 1.0 );
  94. point_lights[1].radius = 3.0;
  95. projlights[0].camera.SetAll( ToRad(60), ToRad(60), 0.1, 20.0 );
  96. projlights[0].texture = rsrc::textures.Load( "gfx/lights/flashlight.tga" );
  97. projlights[0].texture->TexParameter( GL_TEXTURE_MAX_ANISOTROPY_EXT, 0 );
  98. projlights[0].SetSpecularColor( vec3_t( 1.0, 1.0, 1.0) );
  99. projlights[0].SetDiffuseColor( vec3_t( 1.0, 1.0, 1.0)*3.0 );
  100. projlights[0].translation_lspace = vec3_t( 1.3, 4.3, 3.0 );
  101. projlights[0].rotation_lspace.RotateYAxis(ToRad(20));
  102. projlights[0].rotation_lspace.RotateXAxis(ToRad(-30));
  103. projlights[0].casts_shadow = true;
  104. // projlights[0].translation_lspace = vec3_t( 2.36, 1.14, 9.70 );
  105. // projlights[0].rotation_lspace = euler_t( ToRad(-27.13), ToRad(38.13), ToRad(18.28) );
  106. projlights[1].camera.SetAll( ToRad(60), ToRad(60), 0.1, 20.0 );
  107. projlights[1].texture = rsrc::textures.Load( "gfx/lights/impflash.tga" );
  108. projlights[1].SetSpecularColor( vec3_t( 1.0, 1.0, 0.0) );
  109. projlights[1].SetDiffuseColor( vec3_t( 1.0, 1.0, 1.0) );
  110. projlights[1].translation_lspace = vec3_t( -2.3, 6.3, 2.9 );
  111. projlights[1].rotation_lspace.RotateYAxis(ToRad(-20));
  112. projlights[1].rotation_lspace.RotateXAxis(ToRad(-70));
  113. projlights[1].casts_shadow = true;
  114. /*imp.Load( "models/imp/imp.mesh" );
  115. //imp.Load( "maps/temple/column.mesh" );
  116. imp.translation_lspace = vec3_t( 0.0, 2.11, 0.0 );
  117. imp.scale_lspace = 0.7;
  118. imp.rotation_lspace.RotateXAxis( -PI/2 );*/
  119. mcube.Load( "meshes/horse/horse.mesh" );
  120. mcube.translation_lspace = vec3_t( -2, 0, 1 );
  121. mcube.scale_lspace = 0.5;
  122. mcube.rotation_lspace.RotateXAxis(ToRad(-90));
  123. /*floor__.Load( "maps/temple/floor.mesh" );
  124. floor__.translation_lspace = vec3_t(0.0, -0.2, 0.0);*/
  125. sarge.Load( "meshes/sarge/sarge.mesh" );
  126. sarge.scale_lspace = 0.1;
  127. sarge.RotateLocalX(ToRad(-90));
  128. sarge.translation_lspace = vec3_t(0, -2.8, 1.0);
  129. mdl.Init( rsrc::model_datas.Load( "models/imp/imp.smdl" ) );
  130. mdl.translation_lspace = vec3_t( 0.0, 2.11, 0.0 );
  131. mdl.scale_lspace = 0.7;
  132. mdl.rotation_lspace.RotateXAxis( -PI/2 );
  133. walk_anim.Load( "models/imp/walk.imp.anim" );
  134. mdl.Play( &walk_anim, 0, 0.8, smodel_t::START_IMMEDIATELY );
  135. sphere.material = rsrc::materials.Load( "materials/volfog.mtl" );
  136. scene::smodels.Register( &mdl );
  137. scene::meshes.Register( &sarge );
  138. //scene::Register( &imp );
  139. scene::meshes.Register( &mcube );
  140. scene::cameras.Register( &main_cam );
  141. scene::lights.Register( &point_lights[0] );
  142. scene::lights.Register( &point_lights[1] );
  143. scene::lights.Register( &projlights[0] );
  144. scene::lights.Register( &projlights[1] );
  145. scene::meshes.Register( &sphere );
  146. //map.Load( "maps/temple/temple.map" );
  147. //map.CreateOctree();
  148. const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
  149. "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
  150. scene::skybox.Load( skybox_fnames );
  151. PRINT( "Engine initialization ends (" << hndl::GetTicks()-ticks << ")" );
  152. }
  153. //=====================================================================================================================================
  154. // main =
  155. //=====================================================================================================================================
  156. int main( int /*argc*/, char* /*argv*/[] )
  157. {
  158. Init();
  159. //===================================================================================================================================
  160. // MAIN LOOP =
  161. //===================================================================================================================================
  162. PRINT( "Entering main loop" );
  163. int ticks = hndl::GetTicks();
  164. do
  165. {
  166. i::HandleEvents();
  167. r::PrepareNextFrame();
  168. float dist = 0.2;
  169. float ang = ToRad(3.0);
  170. float scale = 0.01;
  171. // move the camera
  172. static object_t* mover = &main_cam;
  173. if( i::keys[ SDLK_1 ] ) mover = &main_cam;
  174. if( i::keys[ SDLK_2 ] ) mover = &point_lights[0];
  175. if( i::keys[ SDLK_3 ] ) mover = &projlights[0];
  176. if( i::keys[ SDLK_4 ] ) mover = &point_lights[1];
  177. if( i::keys[ SDLK_5 ] ) mover = &projlights[1];
  178. if( i::keys[ SDLK_m ] == 1 ) i::warp_mouse = !i::warp_mouse;
  179. if( i::keys[SDLK_a] ) mover->MoveLocalX( -dist );
  180. if( i::keys[SDLK_d] ) mover->MoveLocalX( dist );
  181. if( i::keys[SDLK_LSHIFT] ) mover->MoveLocalY( dist );
  182. if( i::keys[SDLK_SPACE] ) mover->MoveLocalY( -dist );
  183. if( i::keys[SDLK_w] ) mover->MoveLocalZ( -dist );
  184. if( i::keys[SDLK_s] ) mover->MoveLocalZ( dist );
  185. if( !i::warp_mouse )
  186. {
  187. if( i::keys[SDLK_UP] ) mover->RotateLocalX( ang );
  188. if( i::keys[SDLK_DOWN] ) mover->RotateLocalX( -ang );
  189. if( i::keys[SDLK_LEFT] ) mover->RotateLocalY( ang );
  190. if( i::keys[SDLK_RIGHT] ) mover->RotateLocalY( -ang );
  191. }
  192. else
  193. {
  194. float accel = 44.0;
  195. mover->RotateLocalX( ang * i::mouse_velocity.y * accel );
  196. mover->RotateLocalY( -ang * i::mouse_velocity.x * accel );
  197. }
  198. if( i::keys[SDLK_q] ) mover->RotateLocalZ( ang );
  199. if( i::keys[SDLK_e] ) mover->RotateLocalZ( -ang );
  200. if( i::keys[SDLK_PAGEUP] ) mover->scale_lspace += scale ;
  201. if( i::keys[SDLK_PAGEDOWN] ) mover->scale_lspace -= scale ;
  202. if( i::keys[SDLK_k] ) main_cam.LookAtPoint( point_lights[0].translation_wspace );
  203. mover->rotation_lspace.Reorthogonalize();
  204. scene::InterpolateAllModels();
  205. scene::UpdateAllWorldStuff();
  206. r::Render( main_cam );
  207. //map.octree.root->bounding_box.Render();
  208. // print some debug stuff
  209. hud::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
  210. hud::SetPos( -0.98, 0.95 );
  211. hud::SetFontWidth( 0.03 );
  212. hud::Printf( "frame:%d time:%dms\n", r::frames_num, StopBench() );
  213. //hud::Print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
  214. hud::Printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translation_wspace.x, mover->translation_wspace.y, mover->translation_wspace.z,
  215. ToDegrees(euler_t(mover->rotation_wspace).x), ToDegrees(euler_t(mover->rotation_wspace).y), ToDegrees(euler_t(mover->rotation_wspace).z) );
  216. if( i::keys[SDLK_ESCAPE] ) break;
  217. if( i::keys[SDLK_F11] ) hndl::TogleFullScreen();
  218. if( i::keys[SDLK_F12] == 1 ) r::TakeScreenshot("gfx/screenshot.jpg");
  219. /*char str[128];
  220. sprintf( str, "capt/%05d.jpg", r::frames_num );
  221. r::TakeScreenshot(str);*/
  222. // std stuff follow
  223. SDL_GL_SwapBuffers();
  224. r::PrintLastError();
  225. //if( r::frames_num == 10 ) r::TakeScreenshot("gfx/screenshot.tga");
  226. hndl::WaitForNextFrame();
  227. //if( r::frames_num == 5000 ) break;
  228. }while( true );
  229. PRINT( "Exiting main loop (" << hndl::GetTicks()-ticks << ")" );
  230. PRINT( "Exiting..." );
  231. hndl::QuitApp( EXIT_SUCCESS );
  232. return 0;
  233. }