trash.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "EventBuffer.h"
  2. #include <cstdio>
  3. #include <cstring>
  4. #include "System.h"
  5. #include "Crown.h"
  6. #include <GL/glew.h>
  7. using namespace Crown;
  8. class WndCtrl: public KeyboardListener
  9. {
  10. public:
  11. WndCtrl()
  12. {
  13. GetDevice()->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. int main(int argc, char** argv)
  24. {
  25. if (!GetDevice()->Init(argc, argv))
  26. {
  27. return -1;
  28. }
  29. GetDevice()->GetMainWindow()->SetTitle("System test");
  30. WndCtrl ctrl;
  31. //-------------- WORLD -----------------------------
  32. Triangle tri(Vec3(-1.5, -1.5, 2), Vec3(1.5, -1.5, 2), Vec3(0, 1.5, 2));
  33. Plane triPlane = tri.ToPlane();
  34. Sphere s;
  35. s.c = Vec3(0, 0, 5);
  36. s.r = 0.05f;
  37. GLUquadric* quadric = gluNewQuadric();
  38. //-------------- WORLD -----------------------------
  39. MovableCamera cam(Vec3::ZERO, Angles::ZERO, true, 90.0f, 1.6f, true, 0.1, 2.5);
  40. Device* mDevice = GetDevice();
  41. Timer timer;
  42. Crown::ulong dt = 0;
  43. Crown::ulong sTime = 0;
  44. Crown::ulong eTime = 0;
  45. while (mDevice->IsRunning())
  46. {
  47. dt = eTime - sTime;
  48. sTime = timer.GetMilliseconds();
  49. InputManager* mInputManager = GetDevice()->GetInputManager();
  50. if (mInputManager)
  51. {
  52. if (mInputManager->IsMouseAvailable())
  53. {
  54. if (mInputManager->IsMouseAvailable())
  55. {
  56. mInputManager->GetMouse()->EventLoop();
  57. }
  58. if (mInputManager->IsKeyboardAvailable())
  59. {
  60. mInputManager->GetKeyboard()->EventLoop();
  61. }
  62. if (mInputManager->IsTouchAvailable())
  63. {
  64. mInputManager->GetTouch()->EventLoop();
  65. }
  66. }
  67. }
  68. mDevice->GetMainWindow()->EventLoop();
  69. GetDevice()->GetRenderer()->_BeginFrame();
  70. GetDevice()->GetRenderer()->SetClearColor(Color4::GREY);
  71. GetDevice()->GetRenderer()->_SetLighting(false);
  72. GetDevice()->GetRenderer()->_SetTexturing(0, false);
  73. GetDevice()->GetRenderer()->_SetBackfaceCulling(false);
  74. cam.Render();
  75. GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  76. glColor3f(0, 1, 0);
  77. glBegin(GL_TRIANGLES);
  78. glVertex3fv(tri.v1.ToFloatPtr());
  79. glVertex3fv(tri.v2.ToFloatPtr());
  80. glVertex3fv(tri.v3.ToFloatPtr());
  81. glEnd();
  82. Vec3 sphereVelocity = cam.GetLookAt() * 15.0f;
  83. Vec3 sphereCenter = cam.GetPosition();
  84. s.c = sphereCenter;
  85. real it;
  86. Vec3 intersectionPoint;
  87. if (Intersection::TestDynamicSphereTriangle(s, sphereVelocity, tri, it, intersectionPoint))
  88. {
  89. Mat4 sphereTranslation = Mat4::IDENTITY;
  90. sphereTranslation.SetTranslation(s.c + (sphereVelocity * it));
  91. GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, sphereTranslation);
  92. glColor3f(1, 0, 0);
  93. gluSphere(quadric, s.r, 64, 64);
  94. Renderer* renderer = GetDevice()->GetRenderer();
  95. renderer->_SetDepthTest(false);
  96. renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  97. glColor3f(0, 0, 1);
  98. renderer->_SetPointSize(3.0f);
  99. glBegin(GL_POINTS);
  100. glVertex3fv(intersectionPoint.ToFloatPtr());
  101. glEnd();
  102. renderer->_SetPointSize(1.0f);
  103. renderer->_SetDepthTest(true);
  104. }
  105. GetDevice()->GetRenderer()->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  106. glBegin(GL_LINES);
  107. glVertex3fv(s.c.ToFloatPtr());
  108. glVertex3fv((s.c + sphereVelocity).ToFloatPtr());
  109. glEnd();
  110. GetDevice()->GetRenderer()->_EndFrame();
  111. mDevice->GetMainWindow()->Update();
  112. eTime = timer.GetMilliseconds();
  113. }
  114. mDevice->Shutdown();
  115. }