terrain.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "Crown.h"
  2. #include "Terrain.h"
  3. #include <cstdlib>
  4. #include <GL/glew.h>
  5. #include <GL/glu.h>
  6. #include "OS.h"
  7. using namespace crown;
  8. class WndCtrl: public KeyboardListener
  9. {
  10. public:
  11. WndCtrl()
  12. {
  13. GetInputManager()->RegisterKeyboardListener(this);
  14. }
  15. void KeyReleased(const KeyboardEvent& event)
  16. {
  17. if (event.key == KC_ESCAPE)
  18. {
  19. GetDevice()->StopRunning();
  20. }
  21. }
  22. };
  23. void DrawCircle(const Vec3& pos, float radius);
  24. class MainScene: public KeyboardListener, public MouseListener
  25. {
  26. public:
  27. MainScene() :
  28. optShowSkybox(true),
  29. optShowCrate(true),
  30. optShowTerrain(true)
  31. {
  32. GetInputManager()->RegisterKeyboardListener(this);
  33. GetInputManager()->RegisterMouseListener(this);
  34. mouseRightPressed = false;
  35. mouseLeftPressed = false;
  36. }
  37. ~MainScene()
  38. {
  39. }
  40. void KeyReleased(const KeyboardEvent& event)
  41. {
  42. if (event.key == '1')
  43. {
  44. terrain.PlotCircle(2, 2, 2, 2);
  45. }
  46. if (event.key == '2')
  47. {
  48. terrain.PlotCircle(4, 4, 4, 2);
  49. }
  50. if (event.key == '3')
  51. {
  52. terrain.PlotCircle(8, 8, 8, 2);
  53. }
  54. if (event.key == KC_F5)
  55. {
  56. terrain.SaveAsBmp("blah.bmp");
  57. }
  58. if (event.key == KC_SPACE)
  59. {
  60. if (cam)
  61. {
  62. cam->SetActive(!cam->IsActive());
  63. }
  64. }
  65. }
  66. void ButtonPressed(const MouseEvent& event)
  67. {
  68. if (event.button == MB_LEFT)
  69. {
  70. mouseLeftPressed = true;
  71. GLint view[4];
  72. GLdouble proj[16], model[16];
  73. glGetDoublev(GL_MODELVIEW_MATRIX, model);
  74. glGetDoublev(GL_PROJECTION_MATRIX, proj);
  75. glGetIntegerv(GL_VIEWPORT, view);
  76. int x = event.x;
  77. int y = event.y;
  78. // Adjust y wndCoord
  79. y = (625 - y);
  80. double sX, sY, sZ;
  81. double eX, eY, eZ;
  82. gluUnProject(x, y, 0.0f, model, proj, view, &sX, &sY, &sZ);
  83. gluUnProject(x, y, 1.0f, model, proj, view, &eX, &eY, &eZ);
  84. Vec3 dir = Vec3(eX, eY, eZ) - Vec3(sX, sY, sZ);
  85. dir.normalize();
  86. //ray.origin = cam->GetPosition();
  87. ray.direction = dir;
  88. //std::cout << x << " " << y << std::endl;
  89. //std::cout << "Ori: " << ray.origin.ToStr().c_str() << std::endl;
  90. //std::cout << "Dir: " << ray.direction.ToStr().c_str() << std::endl;
  91. }
  92. else if (event.button == MB_RIGHT)
  93. {
  94. mouseRightPressed = true;
  95. }
  96. wheel += event.wheel * 0.25;
  97. }
  98. void ButtonReleased(const MouseEvent& event)
  99. {
  100. if (event.button == MB_LEFT)
  101. {
  102. mouseLeftPressed = false;
  103. }
  104. else if (event.button == MB_RIGHT)
  105. {
  106. mouseRightPressed = false;
  107. }
  108. wheel -= event.wheel * 0.25;
  109. }
  110. void OnLoad()
  111. {
  112. crown::Renderer* renderer = crown::GetDevice()->GetRenderer();
  113. renderer->SetClearColor(Color4::LIGHTBLUE);
  114. // Add a movable camera
  115. cam = new MovableCamera(Vec3::ZERO, true, 90.0f, 1.6f, true, 0.1, 2.5);
  116. if (cam)
  117. {
  118. cam->SetActive(true);
  119. cam->SetSpeed(0.1);
  120. cam->SetFarClipDistance(1000.0f);
  121. }
  122. // Add a skybox
  123. skybox = new Skybox(Vec3::ZERO, true);
  124. if (skybox)
  125. {
  126. skybox->SetFace(SF_NORTH, GetTextureManager()->Load("res/red_north.tga"));
  127. skybox->SetFace(SF_SOUTH, GetTextureManager()->Load("res/red_south.tga"));
  128. skybox->SetFace(SF_EAST, GetTextureManager()->Load("res/red_east.tga"));
  129. skybox->SetFace(SF_WEST, GetTextureManager()->Load("res/red_west.tga"));
  130. skybox->SetFace(SF_UP, GetTextureManager()->Load("res/red_up.tga"));
  131. skybox->SetFace(SF_DOWN, GetTextureManager()->Load("res/red_down.tga"));
  132. }
  133. terrain.CreateTerrain(64, 64, 1, 0.0f);
  134. grass = GetTextureManager()->Load("res/grass.tga");
  135. grass->SetFilter(TF_TRILINEAR);
  136. terrain.PlotCircle(4, 4, 4, 2);
  137. //terrain.ApplyBrush(32, 32, 1.25f);
  138. terrain.UpdateVertexBuffer(true);
  139. }
  140. void RenderScene()
  141. {
  142. Renderer* renderer = GetDevice()->GetRenderer();
  143. cam->Render();
  144. renderer->_SetLighting(false);
  145. renderer->_SetTexturing(0, false);
  146. if (skybox)
  147. {
  148. skybox->Render();
  149. }
  150. if (cam->IsActive())
  151. {
  152. ray.origin = cam->GetPosition();
  153. ray.direction = cam->GetLookAt();
  154. }
  155. /* Render the terrain */
  156. renderer->_SetAmbientLight(Color4(0.5f, 0.5f, 0.5f, 1.0f));
  157. renderer->_SetLighting(true);
  158. renderer->_SetLight(0, true);
  159. renderer->_SetLightParams(0, LT_DIRECTION, Vec3(0.6, 0.5f, -2.0f));
  160. renderer->_SetLightColor(0, Color4::WHITE, Color4::WHITE, Color4(0.6f, 0.6f, 0.6f));
  161. renderer->_SetLightAttenuation(0, 1, 0, 0);
  162. renderer->_SetMaterialParams(Color4(0.3f, 0.3f, 0.3f), Color4(0.8f, 0.8f, 0.8f), Color4::BLACK, Color4::BLACK, 0);
  163. renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  164. renderer->_SetTexturing(0, true);
  165. renderer->SetTexture(0, grass);
  166. renderer->_SetLighting(true);
  167. glColor3f(1, 1, 1);
  168. terrain.Render();
  169. /* Test for intersection */
  170. Triangle tri, tri2;
  171. real dist;
  172. if (terrain.TraceRay(ray, tri, tri2, dist))
  173. {
  174. renderer->_SetDepthTest(false);
  175. Vec3 intersectionPoint = ray.origin + (ray.direction * dist);
  176. if (mouseLeftPressed)
  177. {
  178. terrain.ApplyBrush(intersectionPoint, 0.09f);
  179. terrain.UpdateVertexBuffer(true);
  180. }
  181. if (mouseRightPressed)
  182. {
  183. terrain.ApplyBrush(intersectionPoint, -0.09f);
  184. terrain.UpdateVertexBuffer(true);
  185. }
  186. renderer->_SetDepthTest(true);
  187. }
  188. }
  189. private:
  190. MovableCamera* cam;
  191. Skybox* skybox;
  192. Mat4 ortho;
  193. Terrain terrain;
  194. Texture* grass;
  195. Mesh* cube;
  196. bool optShowSkybox;
  197. bool optShowCrate;
  198. bool optShowTerrain;
  199. bool mouseLeftPressed;
  200. bool mouseRightPressed;
  201. float wheel;
  202. Ray ray;
  203. };
  204. int main(int argc, char** argv)
  205. {
  206. Device* mDevice = GetDevice();
  207. if (!mDevice->Init(argc, argv))
  208. {
  209. return 0;
  210. }
  211. WndCtrl ctrl;
  212. MainScene mainScene;
  213. mainScene.OnLoad();
  214. while (mDevice->IsRunning())
  215. {
  216. os::event_loop();
  217. GetInputManager()->EventLoop();
  218. GetDevice()->GetRenderer()->_BeginFrame();
  219. mainScene.RenderScene();
  220. GetDevice()->GetRenderer()->_EndFrame();
  221. os::swap_buffers();
  222. }
  223. mDevice->Shutdown();
  224. return 0;
  225. }