triangleSelector.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright (C) 2008-2012 Christian Stehno, Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. namespace{
  6. class MyEventReceiver : public IEventReceiver
  7. {
  8. public:
  9. // This is the one method that we have to implement
  10. virtual bool OnEvent(const SEvent& event)
  11. {
  12. // Remember whether each key is down or up
  13. if (event.EventType == EET_KEY_INPUT_EVENT)
  14. KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
  15. return false;
  16. }
  17. // This is used to check whether a key is being held down
  18. virtual bool IsKeyDown(EKEY_CODE keyCode) const
  19. {
  20. return KeyIsDown[keyCode];
  21. }
  22. MyEventReceiver()
  23. {
  24. for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
  25. KeyIsDown[i] = false;
  26. }
  27. private:
  28. // We use this array to store the current state of each key
  29. bool KeyIsDown[KEY_KEY_CODES_COUNT];
  30. };
  31. //! Tests using octree selector
  32. bool octree()
  33. {
  34. IrrlichtDevice *device = createDevice (video::EDT_OPENGL, core::dimension2d < u32 > (160, 120));
  35. if (!device)
  36. return true; // No error if device does not exist
  37. video::IVideoDriver* driver = device->getVideoDriver();
  38. scene::ISceneManager* smgr = device->getSceneManager();
  39. stabilizeScreenBackground(driver);
  40. scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
  41. device->getFileSystem()->addFileArchive("../media/map-20kdm2.pk3");
  42. scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
  43. if (q3levelmesh)
  44. {
  45. scene::ISceneNode* q3node = smgr->addOctreeSceneNode(q3levelmesh->getMesh(0));
  46. q3node->setPosition(core::vector3df(-1350,-130,-1400));
  47. scene::ITriangleSelector * selector =
  48. smgr->createOctreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
  49. meta->addTriangleSelector(selector);
  50. selector->drop();
  51. }
  52. scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
  53. camera->setPosition(core::vector3df(-100,50,-150));
  54. camera->updateAbsolutePosition();
  55. camera->setTarget(camera->getAbsolutePosition() + core::vector3df(0, 0, 20));
  56. device->getCursorControl()->setVisible(false);
  57. enum
  58. {
  59. MAX_TRIANGLES = 4096, // Large to test getting all the triangles
  60. BOX_SIZE1 = 300,
  61. BOX_SIZE2 = 50
  62. };
  63. core::triangle3df triangles[MAX_TRIANGLES];
  64. core::vector3df boxPosition(camera->getAbsolutePosition());
  65. video::SMaterial unlit;
  66. unlit.Lighting = false;
  67. unlit.Thickness = 3.f;
  68. unlit.PolygonOffsetSlopeScale= -1.f;
  69. unlit.PolygonOffsetDepthBias = -1.f;
  70. bool result = true;
  71. {
  72. camera->setPosition(core::vector3df(-620,-20,550));
  73. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0));
  74. smgr->drawAll();
  75. core::aabbox3df box(boxPosition.X - BOX_SIZE1, boxPosition.Y - BOX_SIZE1, boxPosition.Z - BOX_SIZE1,
  76. boxPosition.X + BOX_SIZE1, boxPosition.Y + BOX_SIZE1, boxPosition.Z + BOX_SIZE1);
  77. driver->setTransform(video::ETS_WORLD, core::matrix4());
  78. driver->setMaterial(unlit);
  79. driver->draw3DBox(box, video::SColor(255, 0, 255, 0));
  80. if(meta)
  81. {
  82. s32 found;
  83. meta->getTriangles(triangles, MAX_TRIANGLES, found, box);
  84. while(--found >= 0)
  85. driver->draw3DTriangle(triangles[found], video::SColor(255, 255, 0, 0));
  86. }
  87. driver->endScene();
  88. result &= takeScreenshotAndCompareAgainstReference(driver, "-octree_select1.png");
  89. }
  90. {
  91. camera->setPosition(core::vector3df(120,40,50));
  92. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0));
  93. smgr->drawAll();
  94. core::aabbox3df box(boxPosition.X - BOX_SIZE2, boxPosition.Y - BOX_SIZE2, boxPosition.Z - BOX_SIZE2,
  95. boxPosition.X + BOX_SIZE2, boxPosition.Y + BOX_SIZE2, boxPosition.Z + BOX_SIZE2);
  96. driver->setTransform(video::ETS_WORLD, core::matrix4());
  97. driver->setMaterial(unlit);
  98. driver->draw3DBox(box, video::SColor(255, 0, 255, 0));
  99. if(meta)
  100. {
  101. s32 found;
  102. meta->getTriangles(triangles, MAX_TRIANGLES, found, box);
  103. while(--found >= 0)
  104. driver->draw3DTriangle(triangles[found], video::SColor(255, 255, 0, 0));
  105. }
  106. driver->endScene();
  107. result &= takeScreenshotAndCompareAgainstReference(driver, "-octree_select2.png");
  108. }
  109. meta->drop();
  110. device->closeDevice();
  111. device->run();
  112. device->drop();
  113. return result;
  114. }
  115. //! Tests using triangle selector
  116. bool triangle()
  117. {
  118. IrrlichtDevice *device = createDevice (video::EDT_OPENGL, core::dimension2d < u32 > (160, 120));
  119. if (!device)
  120. return true; // No error if device does not exist
  121. MyEventReceiver receiver;
  122. device->setEventReceiver(&receiver);
  123. video::IVideoDriver* driver = device->getVideoDriver();
  124. scene::ISceneManager* smgr = device->getSceneManager();
  125. stabilizeScreenBackground(driver);
  126. scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
  127. scene::IAnimatedMesh * mesh = smgr->getMesh("../media/sydney.md2");
  128. scene::IAnimatedMeshSceneNode * sydney = smgr->addAnimatedMeshSceneNode( mesh );
  129. if (sydney)
  130. {
  131. sydney->setPosition(core::vector3df(15, -10, 15));
  132. sydney->setMaterialFlag(video::EMF_LIGHTING, false);
  133. sydney->setMD2Animation ( scene::EMAT_STAND );
  134. sydney->setAnimationSpeed(0.f);
  135. sydney->setMaterialTexture( 0, driver->getTexture("../media/sydney.bmp") );
  136. scene::ITriangleSelector * selector =
  137. smgr->createTriangleSelector(sydney->getMesh()->getMesh(0), sydney);
  138. meta->addTriangleSelector(selector);
  139. selector->drop();
  140. }
  141. scene::ICameraSceneNode* camera =
  142. smgr->addCameraSceneNodeFPS();
  143. camera->setPosition(core::vector3df(70,0,-30));
  144. camera->updateAbsolutePosition();
  145. camera->setTarget(camera->getAbsolutePosition() + core::vector3df(-20, 0, 20));
  146. device->getCursorControl()->setVisible(false);
  147. enum
  148. {
  149. MAX_TRIANGLES = 5000, // Large to test getting all the triangles
  150. BOX_SIZE = 30
  151. };
  152. core::triangle3df triangles[MAX_TRIANGLES];
  153. core::vector3df boxPosition(0,0,0);
  154. video::SMaterial unlit;
  155. unlit.Lighting = false;
  156. unlit.Thickness = 3.f;
  157. unlit.PolygonOffsetSlopeScale= -1.f;
  158. unlit.PolygonOffsetDepthBias = -1.f;
  159. bool result = true;
  160. {
  161. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0xff00ffff));
  162. smgr->drawAll();
  163. core::aabbox3df box(boxPosition.X - BOX_SIZE, boxPosition.Y - BOX_SIZE, boxPosition.Z - BOX_SIZE,
  164. boxPosition.X + BOX_SIZE, boxPosition.Y + BOX_SIZE, boxPosition.Z + BOX_SIZE);
  165. driver->setTransform(video::ETS_WORLD, core::matrix4());
  166. driver->setMaterial(unlit);
  167. driver->draw3DBox(box, video::SColor(255, 0, 255, 0));
  168. if(meta)
  169. {
  170. s32 found;
  171. meta->getTriangles(triangles, MAX_TRIANGLES, found, box);
  172. while(--found >= 0)
  173. driver->draw3DTriangle(triangles[found], video::SColor(255, 255, 0, 0));
  174. }
  175. driver->endScene();
  176. result &= takeScreenshotAndCompareAgainstReference(driver, "-tri_select1.png");
  177. }
  178. {
  179. boxPosition.Z -= 10.f;
  180. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0xff00ffff));
  181. smgr->drawAll();
  182. core::aabbox3df box(boxPosition.X - BOX_SIZE, boxPosition.Y - BOX_SIZE, boxPosition.Z - BOX_SIZE,
  183. boxPosition.X + BOX_SIZE, boxPosition.Y + BOX_SIZE, boxPosition.Z + BOX_SIZE);
  184. driver->setTransform(video::ETS_WORLD, core::matrix4());
  185. driver->setMaterial(unlit);
  186. driver->draw3DBox(box, video::SColor(255, 0, 255, 0));
  187. if(meta)
  188. {
  189. s32 found;
  190. meta->getTriangles(triangles, MAX_TRIANGLES, found, box);
  191. while(--found >= 0)
  192. driver->draw3DTriangle(triangles[found], video::SColor(255, 255, 0, 0));
  193. }
  194. driver->endScene();
  195. result &= takeScreenshotAndCompareAgainstReference(driver, "-tri_select2.png");
  196. }
  197. {
  198. boxPosition.Z -= 20.f;
  199. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0xff00ffff));
  200. smgr->drawAll();
  201. core::aabbox3df box(boxPosition.X - BOX_SIZE, boxPosition.Y - BOX_SIZE, boxPosition.Z - BOX_SIZE,
  202. boxPosition.X + BOX_SIZE, boxPosition.Y + BOX_SIZE, boxPosition.Z + BOX_SIZE);
  203. driver->setTransform(video::ETS_WORLD, core::matrix4());
  204. driver->setMaterial(unlit);
  205. driver->draw3DBox(box, video::SColor(255, 0, 255, 0));
  206. if(meta)
  207. {
  208. s32 found;
  209. meta->getTriangles(triangles, MAX_TRIANGLES, found, box);
  210. while(--found >= 0)
  211. driver->draw3DTriangle(triangles[found], video::SColor(255, 255, 0, 0));
  212. }
  213. driver->endScene();
  214. result &= takeScreenshotAndCompareAgainstReference(driver, "-tri_select3.png");
  215. }
  216. meta->drop();
  217. device->closeDevice();
  218. device->run();
  219. device->drop();
  220. return result;
  221. }
  222. }
  223. // Tests need not be accurate, as we just need to include at least
  224. // the triangles that match the criteria. But we try to be as close
  225. // as possible of course, to reduce the collision checks done afterwards
  226. bool triangleSelector(void)
  227. {
  228. bool result = true;
  229. result &= octree();
  230. result &= triangle();
  231. return result;
  232. }