terrain_main.cpp 5.2 KB

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