planeMatrix.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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: (%.3ff,%.3ff,%.3ff), %.3ff\n",
  23. plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.D);
  24. matrix.transformPlane(plane);
  25. logTestString(" Post: (%.3ff,%.3ff,%.3ff), %.3ff\n",
  26. plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.D);
  27. logTestString("Expected: (%.3ff,%.3ff,%.3ff), %.3ff\n",
  28. expected.Normal.X, expected.Normal.Y, expected.Normal.Z, expected.D);
  29. if(!sloppyComparePlanes(plane, expected))
  30. {
  31. logTestString("Unexpected result\n");
  32. assert_log(false);
  33. return false;
  34. }
  35. return true;
  36. }
  37. static bool drawScaledOctree(void)
  38. {
  39. bool result = false;
  40. IrrlichtDevice *device = createDevice(video::EDT_BURNINGSVIDEO, dimension2d<u32>(160, 120), 32);
  41. if (!device)
  42. return false;
  43. video::IVideoDriver* driver = device->getVideoDriver();
  44. ISceneManager * smgr = device->getSceneManager();
  45. bool added = device->getFileSystem()->addFileArchive("../media/map-20kdm2.pk3");
  46. assert_log(added);
  47. if(added)
  48. {
  49. ISceneNode * node = smgr->addOctreeSceneNode(smgr->getMesh("20kdm2.bsp")->getMesh(0), 0, -1, 1024);
  50. assert_log(node);
  51. if (node)
  52. {
  53. node->setMaterialFlag(EMF_LIGHTING, false);
  54. node->setPosition(core::vector3df(-1300,-820,-1249));
  55. node->setScale(core::vector3df(1, 5, 1));
  56. (void)smgr->addCameraSceneNode(0, core::vector3df(0,0,0), core::vector3df(40,100,30));
  57. driver->beginScene(true, true, video::SColor(255,255,255,0));
  58. smgr->drawAll();
  59. driver->endScene();
  60. result = takeScreenshotAndCompareAgainstReference(driver, "-planeMatrix-scaledClip.png");
  61. }
  62. }
  63. device->closeDevice();
  64. device->run();
  65. device->drop();
  66. return result;
  67. }
  68. // Test the ability to transform a plane with a matrix.
  69. bool planeMatrix(void)
  70. {
  71. matrix4 rotationMatrix;
  72. rotationMatrix.setRotationDegrees(vector3df(90, 0, 0));
  73. matrix4 translationMatrix;
  74. translationMatrix.setTranslation(vector3df(0, 3, 0));
  75. matrix4 scaleMatrix;
  76. scaleMatrix.setScale(vector3df(1, 2, 3));
  77. bool success = true;
  78. matrix4 matrix = rotationMatrix;
  79. logTestString("\nRotation matrix\n%02.02f %02.02f %02.02f %02.02f"
  80. "\n%02.02f %02.02f %02.02f %02.02f"
  81. "\n%02.02f %02.02f %02.02f %02.02f"
  82. "\n%02.02f %02.02f %02.02f %02.02f\n",
  83. matrix[0], matrix[1], matrix[2], matrix[3],
  84. matrix[4], matrix[5], matrix[6], matrix[7],
  85. matrix[8], matrix[9], matrix[10], matrix[11],
  86. matrix[12], matrix[13], matrix[14], matrix[15]);
  87. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-.707f, 0.f, -.707f), 0.f));
  88. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(.707f, 0.f, .707f), 0.f));
  89. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-.707f, 0.f, .707f), 0.f));
  90. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(.707f, 0.f, -.707f), 0.f));
  91. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-.707f, 0.f, -.707f), .707f));
  92. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(.707f, 0.f, .707f), -.707f));
  93. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-.707f, 0.f, .707f), -.707f));
  94. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(.707f, 0.f, -.707f), .707f));
  95. matrix = translationMatrix;
  96. logTestString("\nTranslation matrix\n%02.02f %02.02f %02.02f %02.02f"
  97. "\n%02.02f %02.02f %02.02f %02.02f"
  98. "\n%02.02f %02.02f %02.02f %02.02f"
  99. "\n%02.02f %02.02f %02.02f %02.02f\n",
  100. matrix[0], matrix[1], matrix[2], matrix[3],
  101. matrix[4], matrix[5], matrix[6], matrix[7],
  102. matrix[8], matrix[9], matrix[10], matrix[11],
  103. matrix[12], matrix[13], matrix[14], matrix[15]);
  104. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.707f,0.000f), 2.121f));
  105. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.707f,0.000f), -2.121f));
  106. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.707f,0.000f), -2.121f));
  107. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.707f,0.000f), 2.121f));
  108. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.707f,0.000f), 2.828f));
  109. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.707f,0.000f), -2.828f));
  110. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.707f,0.000f), -2.828f));
  111. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.707f,0.000f), 2.828f));
  112. matrix = scaleMatrix;
  113. logTestString("\nScale matrix\n%02.02f %02.02f %02.02f %02.02f"
  114. "\n%02.02f %02.02f %02.02f %02.02f"
  115. "\n%02.02f %02.02f %02.02f %02.02f"
  116. "\n%02.02f %02.02f %02.02f %02.02f\n",
  117. matrix[0], matrix[1], matrix[2], matrix[3],
  118. matrix[4], matrix[5], matrix[6], matrix[7],
  119. matrix[8], matrix[9], matrix[10], matrix[11],
  120. matrix[12], matrix[13], matrix[14], matrix[15]);
  121. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f), -0.000f));
  122. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f), -0.000f));
  123. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f), -0.000f));
  124. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f), -0.000f));
  125. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f), 0.707f));
  126. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f), -0.707f));
  127. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f), -0.707f));
  128. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f), 0.707f));
  129. matrix = rotationMatrix * translationMatrix;
  130. logTestString("\nRotation * translation matrix\n%02.02f %02.02f %02.02f %02.02f"
  131. "\n%02.02f %02.02f %02.02f %02.02f"
  132. "\n%02.02f %02.02f %02.02f %02.02f"
  133. "\n%02.02f %02.02f %02.02f %02.02f\n",
  134. matrix[0], matrix[1], matrix[2], matrix[3],
  135. matrix[4], matrix[5], matrix[6], matrix[7],
  136. matrix[8], matrix[9], matrix[10], matrix[11],
  137. matrix[12], matrix[13], matrix[14], matrix[15]);
  138. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.707f), 2.121f));
  139. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.707f), -2.121f));
  140. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.707f), -2.121f));
  141. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.707f), 2.121f));
  142. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.707f), 2.828f));
  143. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.707f), -2.828f));
  144. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.707f), -2.828f));
  145. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.707f), 2.828f));
  146. matrix = rotationMatrix * scaleMatrix;
  147. logTestString("\nRotation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  148. "\n%02.02f %02.02f %02.02f %02.02f"
  149. "\n%02.02f %02.02f %02.02f %02.02f"
  150. "\n%02.02f %02.02f %02.02f %02.02f\n",
  151. matrix[0], matrix[1], matrix[2], matrix[3],
  152. matrix[4], matrix[5], matrix[6], matrix[7],
  153. matrix[8], matrix[9], matrix[10], matrix[11],
  154. matrix[12], matrix[13], matrix[14], matrix[15]);
  155. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f), -0.000f));
  156. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f), -0.000f));
  157. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f), -0.000f));
  158. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f), -0.000f));
  159. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f), 0.707f));
  160. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f), -0.707f));
  161. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f), -0.707f));
  162. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f), 0.707f));
  163. matrix = translationMatrix * scaleMatrix;
  164. logTestString("\nTranslation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  165. "\n%02.02f %02.02f %02.02f %02.02f"
  166. "\n%02.02f %02.02f %02.02f %02.02f"
  167. "\n%02.02f %02.02f %02.02f %02.02f\n",
  168. matrix[0], matrix[1], matrix[2], matrix[3],
  169. matrix[4], matrix[5], matrix[6], matrix[7],
  170. matrix[8], matrix[9], matrix[10], matrix[11],
  171. matrix[12], matrix[13], matrix[14], matrix[15]);
  172. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f), 1.061f));
  173. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f), -1.061f));
  174. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f), -1.061f));
  175. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f), 1.061f));
  176. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,-0.354f,0.000f), 1.768f));
  177. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,0.354f,0.000f), -1.768f));
  178. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,0.354f,0.000f), -1.768f));
  179. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,-0.354f,0.000f), 1.768f));
  180. matrix = rotationMatrix * translationMatrix * scaleMatrix;
  181. logTestString("\nRotation * translation * scale matrix\n%02.02f %02.02f %02.02f %02.02f"
  182. "\n%02.02f %02.02f %02.02f %02.02f"
  183. "\n%02.02f %02.02f %02.02f %02.02f"
  184. "\n%02.02f %02.02f %02.02f %02.02f\n",
  185. matrix[0], matrix[1], matrix[2], matrix[3],
  186. matrix[4], matrix[5], matrix[6], matrix[7],
  187. matrix[8], matrix[9], matrix[10], matrix[11],
  188. matrix[12], matrix[13], matrix[14], matrix[15]);
  189. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f), 1.061f));
  190. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f), -1.061f));
  191. success &= transformPlane(vector3df(0, 0, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f), -1.061f));
  192. success &= transformPlane(vector3df(0, 0, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f), 1.061f));
  193. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, -1, 0), matrix, plane3df(vector3df(-0.707f,0.000f,-0.354f), 1.768f));
  194. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, 1, 0), matrix, plane3df(vector3df(0.707f,-0.000f,0.354f), -1.768f));
  195. success &= transformPlane(vector3df(0, 1, 0), vector3df(-1, 1, 0), matrix, plane3df(vector3df(-0.707f,-0.000f,0.354f), -1.768f));
  196. success &= transformPlane(vector3df(0, 1, 0), vector3df(1, -1, 0), matrix, plane3df(vector3df(0.707f,0.000f,-0.354f), 1.768f));
  197. success &= drawScaledOctree();
  198. return success;
  199. }