collision.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "Crown.h"
  2. #include <cstdlib>
  3. #include <GL/glu.h>
  4. using namespace Crown;
  5. class WndCtrl: public KeyboardListener
  6. {
  7. public:
  8. WndCtrl()
  9. {
  10. GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  11. }
  12. virtual void KeyReleased(const KeyboardEvent& event)
  13. {
  14. if (event.key == KC_ESCAPE)
  15. {
  16. GetDevice()->StopRunning();
  17. }
  18. }
  19. };
  20. class MainScene: public MouseListener//, public KeyboardListener
  21. {
  22. public:
  23. MainScene()
  24. {
  25. GetDevice()->GetInputManager()->RegisterMouseListener(this);
  26. //GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  27. sphereCount = 0;
  28. }
  29. virtual ~MainScene()
  30. {
  31. if (quadric)
  32. gluDeleteQuadric(quadric);
  33. }
  34. virtual void ButtonPressed(const MouseEvent& event)
  35. {
  36. if (event.button == MB_LEFT)
  37. {
  38. if (sphereCount < 12)
  39. {
  40. sphere[sphereCount].c = cam->GetPosition();
  41. velocity[sphereCount] = cam->GetLookAt() * 0.7f;
  42. sphereCount++;
  43. }
  44. }
  45. if (event.button == MB_RIGHT)
  46. {
  47. sphereCount = 0;
  48. }
  49. }
  50. virtual void OnLoad()
  51. {
  52. Crown::Renderer* renderer = Crown::GetDevice()->GetRenderer();
  53. renderer->SetClearColor(Crown::Color4(0.0f, 0.0f, 0.0f, 1.0f));
  54. // Add a movable camera
  55. cam = new MovableCamera(Vec3::ZERO, Angles::ZERO, true, 90.0f, 1.6f, 0.01, 2.5, true);
  56. if (cam)
  57. {
  58. cam->SetActive(true);
  59. cam->SetSpeed(0.1);
  60. cam->SetFarClipDistance(1000.0f);
  61. cam->SetPosition(Vec3(0, 10, 15));
  62. }
  63. grass = GetTextureManager()->Load("res/test_texture1.tga");
  64. dplane = GetMeshManager()->LoadPlane("3dplane", 128.0f, 128.0f);
  65. ray.origin = Vec3(0, 0, 10);
  66. ray.direction = Vec3(0, 0, -1);
  67. for (int i = 0; i < 12; i++)
  68. {
  69. sphere[i].r = 1.5f;
  70. }
  71. plane[0].n = Vec3(0, 1, 0);
  72. plane[0].d = 0;
  73. plane[1].n = Vec3(0, 0, 1);
  74. plane[1].d = 0;
  75. plane[2].n = Vec3(1, 0, 0);
  76. plane[2].d = 0;
  77. gravity = Vec3(0, -0.003, 0);
  78. quadric = gluNewQuadric();
  79. }
  80. virtual void RenderScene()
  81. {
  82. Renderer* renderer = GetDevice()->GetRenderer();
  83. renderer->_SetLighting(false);
  84. renderer->_SetBlending(false);
  85. renderer->_SetAlphaTest(false);
  86. renderer->_SetBackfaceCulling(false);
  87. renderer->_SetTexturing(0, false);
  88. renderer->_SetTexturing(1, false);
  89. renderer->_SetTexturing(2, false);
  90. cam->Render();
  91. for (int i = 0; i < sphereCount; i++)
  92. {
  93. velocity[i] += gravity;
  94. for (int j = 0; j < 3; j++)
  95. {
  96. real itt;
  97. Vec3 intersectionPoint;
  98. if (Intersection::TestDynamicSpherePlane(sphere[i], velocity[i], plane[j], itt, intersectionPoint))
  99. {
  100. real velLength = velocity[i].GetLength();
  101. velocity[i].Normalize();
  102. Vec3 nuovo = ((2.0f * (-velocity[i].Dot(plane[j].n))) * plane[j].n) + velocity[i];
  103. velocity[i] = nuovo * velLength * 0.7f;
  104. }
  105. }
  106. }
  107. for (int i = 0; i < sphereCount; i++)
  108. {
  109. if (velocity[i] == Vec3::ZERO)
  110. {
  111. velocity[i].Zero();
  112. }
  113. sphere[i].c += velocity[i];
  114. }
  115. if (cam->IsActive())
  116. {
  117. ray.origin = cam->GetPosition();
  118. ray.direction = cam->GetLookAt();
  119. }
  120. renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  121. for (int j = 0; j < sphereCount; j++)
  122. {
  123. // Draw spheres
  124. glColor3f(1, 0, 0);
  125. tr.LoadIdentity();
  126. tr.SetTranslation(sphere[j].c);
  127. renderer->SetMatrix(MT_MODEL, tr);
  128. gluSphere(quadric, sphere[j].r, 64, 64);
  129. }
  130. // tr.SetTranslation(sphere[1].c);
  131. // renderer->SetMatrix(MT_MODEL, tr);
  132. // gluSphere(quadric, sphere[1].r, 64, 64);
  133. // tr.SetTranslation(sphere[2].c);
  134. // renderer->SetMatrix(MT_MODEL, tr);
  135. // gluSphere(quadric, sphere[2].r, 64, 64);
  136. // End Draw spheres
  137. // // Draw velocity segments
  138. // glColor3f(1, 0, 0);
  139. // renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  140. // glBegin(GL_LINES);
  141. // glVertex3fv(sphere[0].c.ToFloatPtr());
  142. // glVertex3fv((sphere[0].c + velocity[0]).ToFloatPtr());
  143. // glEnd();
  144. // glBegin(GL_LINES);
  145. // glVertex3fv(sphere[1].c.ToFloatPtr());
  146. // glVertex3fv((sphere[1].c + velocity[1]).ToFloatPtr());
  147. // glEnd();
  148. // glBegin(GL_LINES);
  149. // glVertex3fv(sphere[2].c.ToFloatPtr());
  150. // glVertex3fv((sphere[2].c + velocity[2]).ToFloatPtr());
  151. // glEnd();
  152. // // End Draw velocity segments
  153. // // Draw end spheres
  154. // glColor3f(1.0, 0.0, 0.0);
  155. // tr.LoadIdentity();
  156. // tr.SetTranslation(sphere[0].c + velocity[0]);
  157. // renderer->SetMatrix(MT_MODEL, tr);
  158. // gluSphere(quadric, sphere[0].r, 64, 64);
  159. // tr.SetTranslation(sphere[1].c + velocity[1]);
  160. // renderer->SetMatrix(MT_MODEL, tr);
  161. // gluSphere(quadric, sphere[1].r, 64, 64);
  162. // tr.SetTranslation(sphere[2].c + velocity[2]);
  163. // renderer->SetMatrix(MT_MODEL, tr);
  164. // gluSphere(quadric, sphere[2].r, 64, 64);
  165. // // End Draw end spheres
  166. // // Draw spheres at collision time
  167. // glColor3f(0.0, 1.0, 0.0);
  168. // real it;
  169. // if (Intersection::TestDynamicSphereSphere(sphere[0], velocity[0], sphere[1], velocity[1], it))
  170. // {
  171. // tr.LoadIdentity();
  172. // tr.SetTranslation(sphere[0].c + (velocity[0] * it));
  173. // renderer->SetMatrix(MT_MODEL, tr);
  174. // gluSphere(quadric, sphere[0].r, 64, 64);
  175. // tr.SetTranslation(sphere[1].c + (velocity[1] * it));
  176. // renderer->SetMatrix(MT_MODEL, tr);
  177. // gluSphere(quadric, sphere[1].r, 64, 64);
  178. // }
  179. // if (Intersection::TestDynamicSpherePlane(sphere[2], velocity[2], plane, it))
  180. // {
  181. // tr.LoadIdentity();
  182. // tr.SetTranslation(sphere[2].c + (velocity[2] * it));
  183. // renderer->SetMatrix(MT_MODEL, tr);
  184. // gluSphere(quadric, sphere[2].r, 64, 64);
  185. // }
  186. // // End Draw spheres at collision time
  187. renderer->_SetTexture(0, grass);
  188. renderer->_SetTexturing(0, true);
  189. renderer->_SetTextureMode(0, grass->GetMode(), grass->GetBlendColor());
  190. renderer->_SetTextureFilter(0, grass->GetFilter());
  191. tr.BuildRotationX(Math::PI / 2.0f);
  192. tr.SetTranslation(Vec3(0, 0, 0));
  193. renderer->SetMatrix(MT_MODEL, tr);
  194. renderer->RenderVertexIndexBuffer(dplane->GetVertexBuffer(), dplane->GetIndexBuffer());
  195. tr.LoadIdentity();
  196. tr.SetTranslation(Vec3(0, 0, 0));
  197. renderer->SetMatrix(MT_MODEL, tr);
  198. renderer->RenderVertexIndexBuffer(dplane->GetVertexBuffer(), dplane->GetIndexBuffer());
  199. tr.BuildRotationY(Math::PI / 2.0f);
  200. tr.SetTranslation(Vec3(0, 0, 0));
  201. renderer->SetMatrix(MT_MODEL, tr);
  202. renderer->RenderVertexIndexBuffer(dplane->GetVertexBuffer(), dplane->GetIndexBuffer());
  203. }
  204. private:
  205. Mesh* dplane;
  206. Mat4 ortho;
  207. Mat4 crateModel;
  208. Mat4 tr;
  209. Texture* grass;
  210. Ray ray;
  211. Plane plane[3];
  212. Sphere sphere[12];
  213. Vec3 velocity[12];
  214. Vec3 gravity;
  215. GLUquadric* quadric;
  216. MovableCamera* cam;
  217. int sphereCount;
  218. };
  219. int main(int argc, char** argv)
  220. {
  221. Device* mDevice = GetDevice();
  222. EventLoop loop;
  223. if (!mDevice->Init(argc, argv))
  224. {
  225. return 0;
  226. }
  227. WndCtrl ctrl;
  228. MainScene scene;
  229. scene.OnLoad();
  230. mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - Collision Test");
  231. while (mDevice->IsRunning())
  232. {
  233. InputManager* mInputManager = GetDevice()->GetInputManager();
  234. if (mInputManager)
  235. {
  236. if (mInputManager->IsMouseAvailable())
  237. {
  238. if (mInputManager->IsMouseAvailable())
  239. {
  240. mInputManager->GetMouse()->EventLoop();
  241. }
  242. if (mInputManager->IsKeyboardAvailable())
  243. {
  244. mInputManager->GetKeyboard()->EventLoop();
  245. }
  246. if (mInputManager->IsTouchAvailable())
  247. {
  248. mInputManager->GetTouch()->EventLoop();
  249. }
  250. }
  251. }
  252. mDevice->GetMainWindow()->EventLoop();
  253. GetDevice()->GetRenderer()->_BeginFrame();
  254. scene.RenderScene();
  255. GetDevice()->GetRenderer()->_EndFrame();
  256. mDevice->GetMainWindow()->Update();
  257. }
  258. mDevice->Shutdown();
  259. mDevice->Shutdown();
  260. return 0;
  261. }