Camera3D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "pch.h"
  2. #include "Camera3D.h"
  3. #include "CppUnitTest.h"
  4. #include "DX12GraphicsApi.h"
  5. #include "Globals.h"
  6. #include "Model3D.h"
  7. #include "RenderThread.h"
  8. #include "Screen.h"
  9. #include "Window.h"
  10. #include "World3D.h"
  11. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  12. namespace FrameworkTests
  13. {
  14. TEST_CLASS (Cam3Tests)
  15. {
  16. public:
  17. TEST_METHOD (TestProjection)
  18. {
  19. Framework::Cam3D cam;
  20. cam.setPosition(Framework::Vec3<float>(0, 0, 0));
  21. cam.setRotation(Framework::Vec3<float>(0, 0, 0));
  22. Framework::Vec3<float> target(0, 1000, 0);
  23. cam.setViewDirection(target);
  24. Framework::Mat4<float> projection = cam.getProjectionMatrix();
  25. Framework::Mat4<float> inverse = cam.getInverseProjectionMatrix();
  26. Framework::Mat4<float> view = cam.getViewMatrix();
  27. Framework::Mat4<float> iView = cam.getInverseViewMatrix();
  28. for (int i = 0; i < 10; i++)
  29. {
  30. for (int j = 0; j < 10; j++)
  31. {
  32. Framework::Vec4<float> origin(0, 0, 0, 1.f);
  33. Framework::Vec4<float> target(
  34. i / 5.f - 1.f, j / 5.f - 1.f, -1.f, 1.f);
  35. Framework::Vec4<float> transformedOrigin = iView * origin;
  36. Framework::Vec4<float> transformedTarget
  37. = iView * (inverse * target);
  38. Framework::Vec4<float> transformedDirection
  39. = transformedTarget - transformedOrigin;
  40. }
  41. }
  42. }
  43. TEST_METHOD (TestRaytracing)
  44. {
  45. Framework::initFramework();
  46. Framework::setDebugDX(1);
  47. Framework::NativeWindow window;
  48. WNDCLASS wc = Framework::F_Normal(GetModuleHandle(0));
  49. wc.lpszClassName = "Factory Craft";
  50. window.create(WS_POPUPWINDOW, wc);
  51. Framework::Monitor m = Framework::getMonitor(0);
  52. window.setBounds(Framework::Point(m.x, m.y),
  53. Framework::Point(m.width, m.height));
  54. window.setDisplayMode(SW_SHOWNORMAL);
  55. window.setPreCloseAction([&window](void* p, void* f) {
  56. Framework::StopMessageLoop(window.getWindowHandle());
  57. });
  58. window.setMouseAction(Framework::_ret1ME);
  59. window.setKeyboardAction(Framework::_ret1TE);
  60. Framework::Screen3D screen(
  61. dynamic_cast<Framework::NativeWindow*>(window.getThis()),
  62. new Framework::DirectX12());
  63. screen.setHandleUserInputsOnTick(0);
  64. window.setScreen(
  65. dynamic_cast<Framework::Screen*>(screen.getThis()));
  66. screen.setFillColor(0);
  67. screen.setTestRend(0);
  68. Framework::Cam3D cam;
  69. cam.setPosition({0.f, 0.f, 0.f});
  70. Framework::Vec3<float> target(0, 1000, 0);
  71. cam.setViewDirection(target);
  72. Framework::World3D world;
  73. Framework::Model3D cube;
  74. Framework::Model3DData* data
  75. = screen.zGraphicsApi()->createModel("cube");
  76. data->setAmbientFactor(0.f);
  77. data->setDiffusFactor(1.f);
  78. data->setSpecularFactor(0.f);
  79. float size = 1;
  80. float left, right, top, bottom;
  81. // Calculate the screen coordinates of the left side of the bitmap.
  82. right = (float)((-size / 2.0));
  83. // Calculate the screen coordinates of the right side of the bitmap.
  84. left = right + (float)size;
  85. // Calculate the screen coordinates of the top of the bitmap.
  86. top = (float)(size / 2.0);
  87. // Calculate the screen coordinates of the bottom of the bitmap.
  88. bottom = top - (float)size;
  89. float front = -size / 2;
  90. float back = front + size;
  91. Framework::Vertex3D* vertecies = new Framework::Vertex3D[24];
  92. for (int i = 0; i < 24; i++)
  93. vertecies[i].knochenId = 0;
  94. // front side
  95. vertecies[0].pos = Framework::Vec3<float>(left, front, top);
  96. vertecies[0].tPos = Framework::Vec2<float>(0.f, 0.f);
  97. vertecies[1].pos = Framework::Vec3<float>(right, front, top);
  98. vertecies[1].tPos = Framework::Vec2<float>(1.f, 0.f);
  99. vertecies[2].pos = Framework::Vec3<float>(left, front, bottom);
  100. vertecies[2].tPos = Framework::Vec2<float>(0.f, 1.f);
  101. vertecies[3].pos = Framework::Vec3<float>(right, front, bottom);
  102. vertecies[3].tPos = Framework::Vec2<float>(1.f, 1.f);
  103. // back side
  104. vertecies[4].pos = Framework::Vec3<float>(right, back, top);
  105. vertecies[4].tPos = Framework::Vec2<float>(0.0f, 0.0f);
  106. vertecies[5].pos = Framework::Vec3<float>(left, back, top);
  107. vertecies[5].tPos = Framework::Vec2<float>(1.0f, 0.0f);
  108. vertecies[6].pos = Framework::Vec3<float>(right, back, bottom);
  109. vertecies[6].tPos = Framework::Vec2<float>(0.0f, 1.0f);
  110. vertecies[7].pos = Framework::Vec3<float>(left, back, bottom);
  111. vertecies[7].tPos = Framework::Vec2<float>(1.0f, 1.0f);
  112. // left side
  113. vertecies[8].pos = Framework::Vec3<float>(left, back, top);
  114. vertecies[8].tPos = Framework::Vec2<float>(0.f, 0.f);
  115. vertecies[9].pos = Framework::Vec3<float>(left, front, top);
  116. vertecies[9].tPos = Framework::Vec2<float>(1.f, 0.f);
  117. vertecies[10].pos = Framework::Vec3<float>(left, back, bottom);
  118. vertecies[10].tPos = Framework::Vec2<float>(0.f, 1.f);
  119. vertecies[11].pos = Framework::Vec3<float>(left, front, bottom);
  120. vertecies[11].tPos = Framework::Vec2<float>(1.f, 1.f);
  121. // right side
  122. vertecies[12].pos = Framework::Vec3<float>(right, front, top);
  123. vertecies[12].tPos = Framework::Vec2<float>(0.0f, 0.0f);
  124. vertecies[13].pos = Framework::Vec3<float>(right, back, top);
  125. vertecies[13].tPos = Framework::Vec2<float>(1.0f, 0.0f);
  126. vertecies[14].pos = Framework::Vec3<float>(right, front, bottom);
  127. vertecies[14].tPos = Framework::Vec2<float>(0.0f, 1.0f);
  128. vertecies[15].pos = Framework::Vec3<float>(right, back, bottom);
  129. vertecies[15].tPos = Framework::Vec2<float>(1.0f, 1.0f);
  130. // top side
  131. vertecies[16].pos = Framework::Vec3<float>(left, back, top);
  132. vertecies[16].tPos = Framework::Vec2<float>(0.f, 0.f);
  133. vertecies[17].pos = Framework::Vec3<float>(right, back, top);
  134. vertecies[17].tPos = Framework::Vec2<float>(1.f, 0.f);
  135. vertecies[18].pos = Framework::Vec3<float>(left, front, top);
  136. vertecies[18].tPos = Framework::Vec2<float>(0.f, 1.f);
  137. vertecies[19].pos = Framework::Vec3<float>(right, front, top);
  138. vertecies[19].tPos = Framework::Vec2<float>(1.f, 1.f);
  139. // botom side
  140. vertecies[20].pos = Framework::Vec3<float>(left, front, bottom);
  141. vertecies[20].tPos = Framework::Vec2<float>(0.0f, 0.0f);
  142. vertecies[21].pos = Framework::Vec3<float>(right, front, bottom);
  143. vertecies[21].tPos = Framework::Vec2<float>(1.0f, 0.0f);
  144. vertecies[22].pos = Framework::Vec3<float>(left, back, bottom);
  145. vertecies[22].tPos = Framework::Vec2<float>(0.0f, 1.0f);
  146. vertecies[23].pos = Framework::Vec3<float>(right, back, bottom);
  147. vertecies[23].tPos = Framework::Vec2<float>(1.0f, 1.0f);
  148. data->setVertecies(vertecies, 24);
  149. // the order of the polygons has to be NORTH (front), EAST (left),
  150. // SOUTH (back), WEST (right), TOP, BOTTOM according to the Area
  151. // definition front side
  152. Framework::Polygon3D* p
  153. = new Framework::Polygon3D(); // looking from (0,0,0) to (1,0,0)
  154. // to see this side
  155. p->indexAnz = 6;
  156. p->indexList = new int[p->indexAnz];
  157. p->indexList[0] = 0;
  158. p->indexList[1] = 1;
  159. p->indexList[2] = 2;
  160. p->indexList[3] = 1;
  161. p->indexList[4] = 3;
  162. p->indexList[5] = 2;
  163. data->addPolygon(p);
  164. // left side
  165. p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,1,0)
  166. // to see this
  167. // side
  168. p->indexAnz = 6;
  169. p->indexList = new int[p->indexAnz];
  170. p->indexList[0] = 0 + 8;
  171. p->indexList[1] = 1 + 8;
  172. p->indexList[2] = 2 + 8;
  173. p->indexList[3] = 1 + 8;
  174. p->indexList[4] = 3 + 8;
  175. p->indexList[5] = 2 + 8;
  176. data->addPolygon(p);
  177. // back side
  178. p = new Framework::Polygon3D(); // looking from (0,0,0) to (-1,0,0)
  179. // to see this
  180. // side
  181. p->indexAnz = 6;
  182. p->indexList = new int[p->indexAnz];
  183. p->indexList[0] = 0 + 4;
  184. p->indexList[1] = 1 + 4;
  185. p->indexList[2] = 2 + 4;
  186. p->indexList[3] = 1 + 4;
  187. p->indexList[4] = 3 + 4;
  188. p->indexList[5] = 2 + 4;
  189. data->addPolygon(p);
  190. // right side
  191. p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,-1,0)
  192. // to see this
  193. // side
  194. p->indexAnz = 6;
  195. p->indexList = new int[p->indexAnz];
  196. p->indexList[0] = 0 + 12;
  197. p->indexList[1] = 1 + 12;
  198. p->indexList[2] = 2 + 12;
  199. p->indexList[3] = 1 + 12;
  200. p->indexList[4] = 3 + 12;
  201. p->indexList[5] = 2 + 12;
  202. data->addPolygon(p);
  203. // top side
  204. p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,0,-1)
  205. // to see this
  206. // side
  207. p->indexAnz = 6;
  208. p->indexList = new int[p->indexAnz];
  209. p->indexList[0] = 0 + 16;
  210. p->indexList[1] = 1 + 16;
  211. p->indexList[2] = 2 + 16;
  212. p->indexList[3] = 1 + 16;
  213. p->indexList[4] = 3 + 16;
  214. p->indexList[5] = 2 + 16;
  215. data->addPolygon(p);
  216. // botom side
  217. p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,0,1)
  218. // to see this
  219. // side
  220. p->indexAnz = 6;
  221. p->indexList = new int[p->indexAnz];
  222. p->indexList[0] = 0 + 20;
  223. p->indexList[1] = 1 + 20;
  224. p->indexList[2] = 2 + 20;
  225. p->indexList[3] = 1 + 20;
  226. p->indexList[4] = 3 + 20;
  227. p->indexList[5] = 2 + 20;
  228. data->addPolygon(p);
  229. data->calculateNormals();
  230. cube.setModelData(data);
  231. cube.setPosition(0, 100, 0);
  232. cube.setSize(50);
  233. cube.setRotation(0.5, 0.5, 0.5);
  234. cube.tick(0.1);
  235. world.addDrawable(
  236. dynamic_cast<Framework::Model3D*>(cube.getThis()));
  237. cam.setWorld(dynamic_cast<Framework::World3D*>(world.getThis()));
  238. cam.addStyle(Framework::Cam3D::Style::Movable
  239. | Framework::Cam3D::Style::Rotatable
  240. | Framework::Cam3D::Style::Zoomable
  241. | Framework::Cam3D::Style::Tick);
  242. screen.addKamera(dynamic_cast<Framework::Cam3D*>(cam.getThis()));
  243. Framework::RenderTh rTh;
  244. rTh.setMaxFps(-1);
  245. rTh.setQuiet(0);
  246. rTh.setScreen(dynamic_cast<Framework::Screen*>(screen.getThis()));
  247. rTh.setTickFunktion([&cube](void* p, void* f, double tick) {
  248. cube.setRotationX(cube.getXRotation() + tick);
  249. cube.setRotationY(cube.getXRotation() + tick / 10);
  250. cube.setRotationZ(cube.getXRotation() + tick / 50);
  251. });
  252. rTh.beginn();
  253. Framework::StartMessageLoop();
  254. rTh.terminate();
  255. }
  256. };
  257. } // namespace FrameworkTests