terrain_main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "Crown.h"
  2. #include "Terrain.h"
  3. #include "FPSSystem.h"
  4. #include "Game.h"
  5. using namespace crown;
  6. class WndCtrl: public KeyboardListener
  7. {
  8. public:
  9. WndCtrl()
  10. {
  11. device()->input_manager()->register_keyboard_listener(this);
  12. }
  13. void key_released(const KeyboardEvent& event)
  14. {
  15. if (event.key == KC_ESCAPE)
  16. {
  17. device()->stop();
  18. }
  19. }
  20. };
  21. class MainScene
  22. {
  23. public:
  24. MainScene() :
  25. optShowSkybox(true),
  26. optShowCrate(true),
  27. optShowTerrain(true),
  28. camera_active(true)
  29. {
  30. mouseRightPressed = false;
  31. mouseLeftPressed = false;
  32. }
  33. ~MainScene()
  34. {
  35. }
  36. void poll_input()
  37. {
  38. Keyboard* keyb = device()->keyboard();
  39. Mouse* mouse = device()->mouse();
  40. if (keyb->key_pressed(KC_1))
  41. {
  42. terrain.PlotCircle(2, 2, 2, 2);
  43. }
  44. if (keyb->key_pressed(KC_2))
  45. {
  46. terrain.PlotCircle(4, 4, 4, 2);
  47. }
  48. if (keyb->key_pressed(KC_3))
  49. {
  50. terrain.PlotCircle(8, 8, 8, 2);
  51. }
  52. if (keyb->key_pressed(KC_F5))
  53. {
  54. device()->reload(grass);
  55. }
  56. if (keyb->key_pressed(KC_SPACE))
  57. {
  58. camera_active = !camera_active;
  59. }
  60. mouseLeftPressed = mouse->button_pressed(MB_LEFT);
  61. mouseRightPressed = mouse->button_pressed(MB_RIGHT);
  62. }
  63. void on_load()
  64. {
  65. crown::Renderer* renderer = crown::device()->renderer();
  66. Vec3 start = Vec3(0.0f, 10.0f, 0.0f);
  67. // Add a movable camera
  68. cam = CE_NEW(m_allocator, Camera)(start, 90.0f, 1.6f);
  69. system = CE_NEW(m_allocator, FPSSystem)(cam, 10.0f, 2.5f);
  70. terrain.CreateTerrain(64, 64, 1, 0.0f);
  71. terrain.PlotCircle(4, 4, 4, 2);
  72. terrain.UpdateVertexBuffer(true);
  73. // red_north = device()->load("textures/red_north.tga");
  74. // red_south = device()->load("textures/red_south.tga");
  75. // red_east = device()->load("textures/red_east.tga");
  76. // red_west = device()->load("textures/red_west.tga");
  77. // red_up = device()->load("textures/red_up.tga");
  78. // red_down = device()->load("textures/red_down.tga");
  79. grass = device()->load("textures/grass.tga");
  80. device()->resource_manager()->flush();
  81. TextureResource* grass_texture = (TextureResource*)device()->data(grass);
  82. grass_id = device()->renderer()->create_texture(grass_texture->width(), grass_texture->height(), grass_texture->format(), grass_texture->data());
  83. }
  84. void on_unload()
  85. {
  86. device()->unload(grass);
  87. device()->unload(red_north);
  88. device()->unload(red_south);
  89. device()->unload(red_east);
  90. device()->unload(red_west);
  91. device()->unload(red_up);
  92. device()->unload(red_down);
  93. CE_DELETE(m_allocator, system);
  94. CE_DELETE(m_allocator, cam);
  95. }
  96. void update(float dt)
  97. {
  98. poll_input();
  99. Renderer* renderer = device()->renderer();
  100. renderer->set_clear_color(Color4::LIGHTBLUE);
  101. if (camera_active)
  102. {
  103. system->set_view_by_cursor();
  104. }
  105. system->update(dt);
  106. renderer->set_texturing(0, false);
  107. ray.set_origin(cam->position());
  108. ray.set_direction(cam->look_at());
  109. /* Render the terrain */
  110. renderer->set_ambient_light(Color4(0.5f, 0.5f, 0.5f, 1.0f));
  111. renderer->set_matrix(MT_MODEL, Mat4::IDENTITY);
  112. if (device()->is_loaded(grass))
  113. {
  114. renderer->set_texturing(0, true);
  115. renderer->bind_texture(0, grass_id);
  116. }
  117. terrain.Render();
  118. /* Test for intersection */
  119. Triangle tri, tri2;
  120. float dist;
  121. if (terrain.TraceRay(ray, tri, tri2, dist))
  122. {
  123. renderer->set_depth_test(false);
  124. Vec3 intersectionPoint = ray.origin() + (ray.direction() * dist);
  125. if (mouseLeftPressed)
  126. {
  127. terrain.ApplyBrush(intersectionPoint, 0.09f);
  128. terrain.UpdateVertexBuffer(true);
  129. }
  130. if (mouseRightPressed)
  131. {
  132. terrain.ApplyBrush(intersectionPoint, -0.09f);
  133. terrain.UpdateVertexBuffer(true);
  134. }
  135. renderer->set_depth_test(true);
  136. }
  137. }
  138. private:
  139. HeapAllocator m_allocator;
  140. FPSSystem* system;
  141. Camera* cam;
  142. Mat4 ortho;
  143. Terrain terrain;
  144. // Resources
  145. ResourceId grass;
  146. ResourceId red_north;
  147. ResourceId red_south;
  148. ResourceId red_east;
  149. ResourceId red_west;
  150. ResourceId red_up;
  151. ResourceId red_down;
  152. TextureId grass_id;
  153. RenderBufferId rb_id;
  154. VertexShaderId vs_id;
  155. PixelShaderId ps_id;
  156. GPUProgramId gpu_program_id;
  157. ResourceId script;
  158. bool optShowSkybox;
  159. bool optShowCrate;
  160. bool optShowTerrain;
  161. bool mouseLeftPressed;
  162. bool mouseRightPressed;
  163. float wheel;
  164. bool camera_active;
  165. Ray ray;
  166. };
  167. MainScene m_scene;
  168. WndCtrl m_ctrl;
  169. namespace crown
  170. {
  171. void init()
  172. {
  173. m_scene.on_load();
  174. }
  175. void shutdown()
  176. {
  177. m_scene.on_unload();
  178. }
  179. void frame(float dt)
  180. {
  181. m_scene.update(dt);
  182. }
  183. }