planeMatrix.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. using namespace io;
  9. using namespace gui;
  10. // There's all sorts of minor and inevitable FP accuracy errors here, so use a sloppy comparison.
  11. static bool sloppyComparePlanes(const plane3df plane1, const plane3df plane2)
  12. {
  13. return(equals(plane1.D, plane2.D, 0.001f) &&
  14. equals(plane1.Normal.X, plane2.Normal.X, 0.001f) &&
  15. equals(plane1.Normal.Y, plane2.Normal.Y, 0.001f) &&
  16. equals(plane1.Normal.Z, plane2.Normal.Z, 0.001f));
  17. }
  18. static bool transformPlane(const vector3df & point, const vector3df & normal,
  19. const matrix4 & matrix, const plane3df & expected)
  20. {
  21. plane3df plane(point, vector3df(normal).normalize());
  22. logTestString("\n Pre N:(%.3ff,%.3ff,%.3ff), D:%.3ff\n",
  23. plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.D);
  24. matrix.transformPlane(plane);
  25. logTestString(" Post N:(%.3ff,%.3ff,%.3ff), D:%.3ff\n",
  26. plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.D);
  27. logTestString("Expected N:(%.3ff,%.3ff,%.3ff), D:%.3ff\n",
  28. expected.Normal.X, expected.Normal.Y, expected.Normal.Z, expected.D);
  29. if(!plane.Normal.equals(vector3df(plane.Normal).normalize()))
  30. {
  31. logTestString("Plane normal no longer normalized after transformation\n");
  32. assert_log(false);
  33. return false;
  34. }
  35. if(!sloppyComparePlanes(plane, expected))
  36. {
  37. logTestString("Unexpected result\n");
  38. assert_log(false);
  39. return false;
  40. }
  41. return true;
  42. }
  43. static bool drawScaledOctree(void)
  44. {
  45. bool result = false;
  46. IrrlichtDevice *device = createDevice(video::EDT_BURNINGSVIDEO, dimension2d<u32>(160, 120), 32);
  47. if (!device)
  48. return false;
  49. video::IVideoDriver* driver = device->getVideoDriver();
  50. ISceneManager * smgr = device->getSceneManager();
  51. bool added = device->getFileSystem()->addFileArchive("../media/map-20kdm2.pk3");
  52. assert_log(added);
  53. if(added)
  54. {
  55. ISceneNode * node = smgr->addOctreeSceneNode(smgr->getMesh("20kdm2.bsp")->getMesh(0), 0, -1, 1024);
  56. assert_log(node);
  57. if (node)
  58. {
  59. node->setMaterialFlag(EMF_LIGHTING, false);
  60. node->setPosition(core::vector3df(-1300,-820,-1249));
  61. node->setScale(core::vector3df(1, 5, 1));
  62. (void)smgr->addCameraSceneNode(0, core::vector3df(0,0,0), core::vector3df(40,100,30));
  63. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,255,255,0));
  64. smgr->drawAll();
  65. driver->endScene();
  66. result = takeScreenshotAndCompareAgainstReference(driver, "-planeMatrix-scaledClip.png");
  67. }
  68. }
  69. device->closeDevice();
  70. device->run();
  71. device->drop();
  72. return result;
  73. }
  74. // Test the ability to transform a plane with a matrix.
  75. bool planeMatrix(void)
  76. {
  77. matrix4 rotationMatrix;
  78. rotationMatrix.setRotationDegrees(vector3df(90, 0, 0));
  79. matrix4 translationMatrix;
  80. translationMatrix.setTranslation(vector3df(0, 3, 0));
  81. matrix4 scaleMatrix;
  82. scaleMatrix.setScale(vector3df(1, 2, 3));
  83. bool success = true;
  84. matrix4 matrix = rotationMatrix;
  85. logTestString("\nRotation matrix\n%02.02f %02.02f %02.02f %02.02f"
  86. "\n%02.02f %02.02f %02.02f %02.02f"
  87. "\n%02.02f %02.02f %02.02f %02.02f"
  88. "\n%02.02f %02.02f %02.02f %02.02f\n",
  89. matrix[0], matrix[1], matrix[2], matrix[3],
  90. matrix[4], matrix[5], matrix[6], matrix[7],
  91. matrix[8], matrix[9], matrix[10], matrix[11],
  92. matrix[12], matrix[13], matrix[14], matrix[15]);
  93. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-.707f, 0.f, -.707f), 0.f));
  94. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(.707f, 0.f, .707f), 0.f));
  95. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-.707f, 0.f, .707f), 0.f));
  96. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(.707f, 0.f, -.707f), 0.f));
  97. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-.707f, 0.f, -.707f), .707f));
  98. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(.707f, 0.f, .707f), -.707f));
  99. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-.707f, 0.f, .707f), -.707f));
  100. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(.707f, 0.f, -.707f), .707f));
  101. matrix = translationMatrix;
  102. logTestString("\nTranslation matrix\n%02.02f %02.02f %02.02f %02.02f"
  103. "\n%02.02f %02.02f %02.02f %02.02f"
  104. "\n%02.02f %02.02f %02.02f %02.02f"
  105. "\n%02.02f %02.02f %02.02f %02.02f\n",
  106. matrix[0], matrix[1], matrix[2], matrix[3],
  107. matrix[4], matrix[5], matrix[6], matrix[7],
  108. matrix[8], matrix[9], matrix[10], matrix[11],
  109. matrix[12], matrix[13], matrix[14], matrix[15]);
  110. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.707f,0.000f), 2.121f));
  111. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.707f,0.000f), -2.121f));
  112. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.707f,0.000f), -2.121f));
  113. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.707f,0.000f), 2.121f));
  114. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.707f,0.000f), 2.828f));
  115. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.707f,0.000f), -2.828f));
  116. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.707f,0.000f), -2.828f));
  117. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.707f,0.000f), 2.828f));
  118. matrix = scaleMatrix;
  119. logTestString("\nScale matrix\n%02.02f %02.02f %02.02f %02.02f"
  120. "\n%02.02f %02.02f %02.02f %02.02f"
  121. "\n%02.02f %02.02f %02.02f %02.02f"
  122. "\n%02.02f %02.02f %02.02f %02.02f\n",
  123. matrix[0], matrix[1], matrix[2], matrix[3],
  124. matrix[4], matrix[5], matrix[6], matrix[7],
  125. matrix[8], matrix[9], matrix[10], matrix[11],
  126. matrix[12], matrix[13], matrix[14], matrix[15]);
  127. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f).normalize(), -0.000f));
  128. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f).normalize(), -0.000f));
  129. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f).normalize(), -0.000f));
  130. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f).normalize(), -0.000f));
  131. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f).normalize(), 0.894f));
  132. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f).normalize(), -0.894f));
  133. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f).normalize(), -0.894f));
  134. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f).normalize(), 0.894f));
  135. matrix = rotationMatrix * translationMatrix;
  136. logTestString("\nRotation * translation matrix\n%02.02f %02.02f %02.02f %02.02f"
  137. "\n%02.02f %02.02f %02.02f %02.02f"
  138. "\n%02.02f %02.02f %02.02f %02.02f"
  139. "\n%02.02f %02.02f %02.02f %02.02f\n",
  140. matrix[0], matrix[1], matrix[2], matrix[3],
  141. matrix[4], matrix[5], matrix[6], matrix[7],
  142. matrix[8], matrix[9], matrix[10], matrix[11],
  143. matrix[12], matrix[13], matrix[14], matrix[15]);
  144. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.707f).normalize(), 2.121f));
  145. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.707f).normalize(), -2.121f));
  146. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.707f).normalize(), -2.121f));
  147. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.707f).normalize(), 2.121f));
  148. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.707f).normalize(), 2.828f));
  149. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.707f).normalize(), -2.828f));
  150. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.707f).normalize(), -2.828f));
  151. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.707f).normalize(), 2.828f));
  152. matrix = rotationMatrix * scaleMatrix;
  153. logTestString("\nRotation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  154. "\n%02.02f %02.02f %02.02f %02.02f"
  155. "\n%02.02f %02.02f %02.02f %02.02f"
  156. "\n%02.02f %02.02f %02.02f %02.02f\n",
  157. matrix[0], matrix[1], matrix[2], matrix[3],
  158. matrix[4], matrix[5], matrix[6], matrix[7],
  159. matrix[8], matrix[9], matrix[10], matrix[11],
  160. matrix[12], matrix[13], matrix[14], matrix[15]);
  161. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f).normalize(), -0.000f));
  162. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f).normalize(), -0.000f));
  163. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f).normalize(), -0.000f));
  164. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f).normalize(), -0.000f));
  165. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f).normalize(), 0.894f));
  166. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f).normalize(), -0.894f));
  167. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f).normalize(), -0.894f));
  168. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f).normalize(), 0.894f));
  169. matrix = translationMatrix * scaleMatrix;
  170. logTestString("\nTranslation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  171. "\n%02.02f %02.02f %02.02f %02.02f"
  172. "\n%02.02f %02.02f %02.02f %02.02f"
  173. "\n%02.02f %02.02f %02.02f %02.02f\n",
  174. matrix[0], matrix[1], matrix[2], matrix[3],
  175. matrix[4], matrix[5], matrix[6], matrix[7],
  176. matrix[8], matrix[9], matrix[10], matrix[11],
  177. matrix[12], matrix[13], matrix[14], matrix[15]);
  178. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f).normalize(), 1.3416f));
  179. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f).normalize(), -1.3416f));
  180. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f).normalize(), -1.3416f));
  181. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f).normalize(), 1.3416f));
  182. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f).normalize(), 2.236f));
  183. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f).normalize(), -2.236f));
  184. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f).normalize(), -2.236f));
  185. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f).normalize(), 2.236f));
  186. matrix = rotationMatrix * translationMatrix * scaleMatrix;
  187. logTestString("\nRotation * translation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  188. "\n%02.02f %02.02f %02.02f %02.02f"
  189. "\n%02.02f %02.02f %02.02f %02.02f"
  190. "\n%02.02f %02.02f %02.02f %02.02f\n",
  191. matrix[0], matrix[1], matrix[2], matrix[3],
  192. matrix[4], matrix[5], matrix[6], matrix[7],
  193. matrix[8], matrix[9], matrix[10], matrix[11],
  194. matrix[12], matrix[13], matrix[14], matrix[15]);
  195. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f).normalize(), 1.3416f));
  196. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f).normalize(), -1.3416f));
  197. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f).normalize(), -1.3416f));
  198. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f).normalize(), 1.3416f));
  199. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f).normalize(), 2.236f));
  200. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f).normalize(), -2.236f));
  201. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f).normalize(), -2.236f));
  202. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f).normalize(), 2.236f));
  203. success &= drawScaledOctree();
  204. return success;
  205. }