2
0

intersection.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 Scene, public KeyboardListener, public MouseListener
  21. {
  22. public:
  23. MainScene(uint windowWidth, uint windowHeight)
  24. {
  25. GetDevice()->GetInputManager()->RegisterMouseListener(this);
  26. GetDevice()->GetInputManager()->RegisterKeyboardListener(this);
  27. }
  28. virtual ~MainScene()
  29. {
  30. if (quadric)
  31. gluDeleteQuadric(quadric);
  32. }
  33. virtual void ButtonReleased(const MouseEvent& event)
  34. {
  35. if (event.button == MB_LEFT)
  36. {
  37. GLint view[4];
  38. GLdouble proj[16], model[16];
  39. glGetDoublev(GL_MODELVIEW_MATRIX, model);
  40. glGetDoublev(GL_PROJECTION_MATRIX, proj);
  41. glGetIntegerv(GL_VIEWPORT, view);
  42. int x = event.x;
  43. int y = event.y;
  44. // Adjust y wndCoord
  45. y = (625 - y);
  46. double sX, sY, sZ;
  47. double eX, eY, eZ;
  48. gluUnProject(x, y, 0.0f, model, proj, view, &sX, &sY, &sZ);
  49. gluUnProject(x, y, 1.0f, model, proj, view, &eX, &eY, &eZ);
  50. Vec3 dir = Vec3(eX, eY, eZ) - Vec3(sX, sY, sZ);
  51. dir.Normalize();
  52. ray.origin = cam->GetPosition();
  53. ray.direction = dir;
  54. //std::cout << x << " " << y << std::endl;
  55. //std::cout << "Ori: " << ray.origin.ToStr().c_str() << std::endl;
  56. //std::cout << "Dir: " << ray.direction.ToStr().c_str() << std::endl;
  57. }
  58. }
  59. virtual void KeyPressed(const KeyboardEvent& event)
  60. {
  61. if (event.key == KC_UP)
  62. {
  63. ray.origin += Vec3(0, 0, -0.1);
  64. }
  65. if (event.key == KC_DOWN)
  66. {
  67. ray.origin += Vec3(0, 0, 0.1);
  68. }
  69. if (event.key == KC_LCONTROL)
  70. {
  71. cam->SetActive(!cam->IsActive());
  72. }
  73. }
  74. virtual void OnLoad()
  75. {
  76. Scene::OnLoad();
  77. Crown::Renderer* renderer = Crown::GetDevice()->GetRenderer();
  78. renderer->SetClearColor(Crown::Color4(0.0f, 0.0f, 0.0f, 1.0f));
  79. // Add a movable camera
  80. cam = AddMovableCamera();
  81. if (cam)
  82. {
  83. cam->SetActive(true);
  84. cam->SetSpeed(0.1);
  85. cam->SetFarClipDistance(1000.0f);
  86. //cam->SetPosition(Vec3(25, 0, 15));
  87. }
  88. grid = GetMeshManager()->LoadGrid("grid", 32);
  89. ray.origin = Vec3(0, 0, 10);
  90. ray.direction = Vec3(0, 0, -1);
  91. sphere[0].c = Vec3(0, 0, 0);
  92. sphere[0].r = 2.0f;
  93. sphere[1].c = Vec3(3, -1, -5);
  94. sphere[1].r = 1.0f;
  95. sphere[2].c = Vec3(-5, 3, 2);
  96. sphere[2].r = 3.0f;
  97. quadric = gluNewQuadric();
  98. }
  99. virtual void RenderScene()
  100. {
  101. Renderer* renderer = GetDevice()->GetRenderer();
  102. renderer->_SetLighting(false);
  103. renderer->_SetBlending(false);
  104. renderer->_SetAlphaTest(false);
  105. Scene::RenderScene();
  106. renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  107. renderer->_SetTexturing(0, false);
  108. renderer->_SetTexturing(1, false);
  109. renderer->_SetTexturing(2, false);
  110. if (cam->IsActive())
  111. {
  112. ray.origin = cam->GetPosition();
  113. ray.direction = cam->GetLookAt();
  114. //std::cout << ray.direction.ToStr().c_str() << std::endl;
  115. }
  116. int sphereIndex = -1;
  117. real lastDist = 1000000.0f;
  118. // Check ray-sphere intersection
  119. for (int i = 0; i < 3; i++)
  120. {
  121. real dist;
  122. if (Intersection::TestRaySphere(ray, sphere[i], dist))
  123. {
  124. if (dist <= lastDist)
  125. {
  126. lastDist = dist;
  127. sphereIndex = i;
  128. }
  129. }
  130. }
  131. for (int i = 0; i < 3; i++)
  132. {
  133. if (sphereIndex == i)
  134. {
  135. glColor3f(1, 0, 0);
  136. }
  137. else
  138. {
  139. glColor3f(0, 1, 0);
  140. }
  141. tr.LoadIdentity();
  142. tr.SetTranslation(sphere[i].c);
  143. renderer->SetMatrix(MT_MODEL, tr);
  144. gluSphere(quadric, sphere[i].r, 64, 64);
  145. }
  146. renderer->SetMatrix(MT_MODEL, Mat4::IDENTITY);
  147. glColor3f(0, 0, 1);
  148. glBegin(GL_LINES);
  149. glVertex3fv(ray.origin.ToFloatPtr());
  150. glVertex3fv((ray.origin + ray.direction).ToFloatPtr());
  151. glEnd();
  152. tr.SetTranslation(Vec3(0, -2, 0));
  153. renderer->SetMatrix(MT_MODEL, tr);
  154. renderer->RenderMesh(grid.GetPointer());
  155. }
  156. private:
  157. MeshPtr grid;
  158. Mat4 ortho;
  159. Mat4 crateModel;
  160. Mat4 tr;
  161. Ray ray;
  162. Sphere sphere[3];
  163. GLUquadric* quadric;
  164. MovableCamera* cam;
  165. Mouse* mouse;
  166. Keyboard* keyboard;
  167. InputManager* input;
  168. };
  169. int main(int argc, char** argv)
  170. {
  171. int wndW = 1000;
  172. int wndH = 625;
  173. if (argc == 3)
  174. {
  175. wndW = atoi(argv[1]);
  176. wndH = atoi(argv[2]);
  177. }
  178. Device* mDevice = GetDevice();
  179. if (!mDevice->Init(wndW, wndH, 32, false))
  180. {
  181. return 0;
  182. }
  183. WndCtrl ctrl;
  184. MainScene* mainScene = new MainScene(wndW, wndH);
  185. GetDevice()->GetSceneManager()->SelectNextScene(mainScene);
  186. mDevice->GetMainWindow()->SetTitle("Crown Engine v0.1 - Intersection Test");
  187. mDevice->Run();
  188. mDevice->Shutdown();
  189. return 0;
  190. }