terrain_main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. //GLint view[4];
  61. //GLdouble proj[16], model[16];
  62. //glGetDoublev(GL_MODELVIEW_MATRIX, model);
  63. //glGetDoublev(GL_PROJECTION_MATRIX, proj);
  64. //glGetIntegerv(GL_VIEWPORT, view);
  65. //int x = event.x;
  66. //int y = event.y;
  67. // Adjust y wndCoord
  68. //y = (625 - y);
  69. //double sX, sY, sZ;
  70. //double eX, eY, eZ;
  71. //gluUnProject(x, y, 0.0f, model, proj, view, &sX, &sY, &sZ);
  72. //gluUnProject(x, y, 1.0f, model, proj, view, &eX, &eY, &eZ);
  73. //Vec3 dir = Vec3(eX, eY, eZ) - Vec3(sX, sY, sZ);
  74. //dir.normalize();
  75. //ray.direction = dir;
  76. mouseLeftPressed = mouse->button_pressed(MB_LEFT);
  77. mouseRightPressed = mouse->button_pressed(MB_RIGHT);
  78. }
  79. void on_load()
  80. {
  81. crown::Renderer* renderer = crown::device()->renderer();
  82. Vec3 start = Vec3(0.0f, 10.0f, 0.0f);
  83. // Add a movable camera
  84. cam = CE_NEW(m_allocator, Camera)(start, 90.0f, 1.6f);
  85. system = CE_NEW(m_allocator, FPSSystem)(cam, 10.0f, 2.5f);
  86. terrain.CreateTerrain(64, 64, 1, 0.0f);
  87. terrain.PlotCircle(4, 4, 4, 2);
  88. terrain.UpdateVertexBuffer(true);
  89. red_north = device()->load("textures/red_north.tga");
  90. red_south = device()->load("textures/red_south.tga");
  91. red_east = device()->load("textures/red_east.tga");
  92. red_west = device()->load("textures/red_west.tga");
  93. red_up = device()->load("textures/red_up.tga");
  94. red_down = device()->load("textures/red_down.tga");
  95. grass = device()->load("textures/grass.tga");
  96. device()->resource_manager()->flush();
  97. TextureResource* grass_texture = (TextureResource*)device()->data(grass);
  98. grass_id = device()->renderer()->create_texture(grass_texture->width(), grass_texture->height(), grass_texture->format(), grass_texture->data());
  99. }
  100. void on_unload()
  101. {
  102. device()->unload(grass);
  103. device()->unload(red_north);
  104. device()->unload(red_south);
  105. device()->unload(red_east);
  106. device()->unload(red_west);
  107. device()->unload(red_up);
  108. device()->unload(red_down);
  109. CE_DELETE(m_allocator, system);
  110. CE_DELETE(m_allocator, cam);
  111. }
  112. void update(float dt)
  113. {
  114. poll_input();
  115. Renderer* renderer = device()->renderer();
  116. renderer->set_clear_color(Color4::LIGHTBLUE);
  117. if (camera_active)
  118. {
  119. system->set_view_by_cursor();
  120. }
  121. system->update(dt);
  122. renderer->set_lighting(false);
  123. renderer->set_texturing(0, false);
  124. ray.set_origin(cam->position());
  125. ray.set_direction(cam->look_at());
  126. /* Render the terrain */
  127. renderer->set_ambient_light(Color4(0.5f, 0.5f, 0.5f, 1.0f));
  128. renderer->set_lighting(true);
  129. renderer->set_light(0, true);
  130. renderer->set_light_params(0, LT_DIRECTION, Vec3(0.6, 0.5f, -2.0f));
  131. renderer->set_light_color(0, Color4::WHITE, Color4::WHITE, Color4(0.6f, 0.6f, 0.6f));
  132. renderer->set_light_attenuation(0, 1, 0, 0);
  133. renderer->set_material_params(Color4(0.3f, 0.3f, 0.3f), Color4(0.8f, 0.8f, 0.8f), Color4::BLACK, Color4::BLACK, 0);
  134. renderer->set_matrix(MT_MODEL, Mat4::IDENTITY);
  135. if (device()->is_loaded(grass))
  136. {
  137. renderer->set_texturing(0, true);
  138. renderer->bind_texture(0, grass_id);
  139. }
  140. //glColor3f(1, 1, 1);
  141. terrain.Render();
  142. /* Test for intersection */
  143. Triangle tri, tri2;
  144. float dist;
  145. if (terrain.TraceRay(ray, tri, tri2, dist))
  146. {
  147. renderer->set_depth_test(false);
  148. Vec3 intersectionPoint = ray.origin() + (ray.direction() * dist);
  149. if (mouseLeftPressed)
  150. {
  151. terrain.ApplyBrush(intersectionPoint, 0.09f);
  152. terrain.UpdateVertexBuffer(true);
  153. }
  154. if (mouseRightPressed)
  155. {
  156. terrain.ApplyBrush(intersectionPoint, -0.09f);
  157. terrain.UpdateVertexBuffer(true);
  158. }
  159. renderer->set_depth_test(true);
  160. }
  161. }
  162. private:
  163. HeapAllocator m_allocator;
  164. FPSSystem* system;
  165. Camera* cam;
  166. Mat4 ortho;
  167. Terrain terrain;
  168. // Resources
  169. ResourceId grass;
  170. ResourceId red_north;
  171. ResourceId red_south;
  172. ResourceId red_east;
  173. ResourceId red_west;
  174. ResourceId red_up;
  175. ResourceId red_down;
  176. TextureId grass_id;
  177. RenderBufferId rb_id;
  178. ResourceId script;
  179. bool optShowSkybox;
  180. bool optShowCrate;
  181. bool optShowTerrain;
  182. bool mouseLeftPressed;
  183. bool mouseRightPressed;
  184. float wheel;
  185. bool camera_active;
  186. Ray ray;
  187. };
  188. MainScene m_scene;
  189. WndCtrl m_ctrl;
  190. extern "C"
  191. {
  192. void init()
  193. {
  194. m_scene.on_load();
  195. }
  196. void shutdown()
  197. {
  198. m_scene.on_unload();
  199. }
  200. void frame(float dt)
  201. {
  202. m_scene.update(dt);
  203. }
  204. }