AuxGeomSharedDrawFunctions.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include "AuxGeomSharedDrawFunctions.h"
  8. #include <AzCore/base.h>
  9. #include <AzCore/Math/Color.h>
  10. #include <AzCore/Math/Vector3.h>
  11. #include <AzCore/Math/Aabb.h>
  12. #include <AzCore/Math/Obb.h>
  13. #include <AzCore/Casting/numeric_cast.h>
  14. namespace AtomSampleViewer
  15. {
  16. using namespace AZ;
  17. using namespace RPI;
  18. using namespace Colors;
  19. // Create some semi-transparent colors
  20. const AZ::Color BlackAlpha (0.0f, 0.0f, 0.0f, 0.5f);
  21. const AZ::Color WhiteAlpha (1.0f, 1.0f, 1.0f, 0.5f);
  22. const AZ::Color RedAlpha (1.0f, 0.0f, 0.0f, 0.5f);
  23. const AZ::Color GreenAlpha (0.0f, 1.0f, 0.0f, 0.5f);
  24. const AZ::Color BlueAlpha (0.0f, 0.0f, 1.0f, 0.5f);
  25. const AZ::Color YellowAlpha (0.5f, 0.5f, 0.0f, 0.5f);
  26. const AZ::Color CyanAlpha (0.0f, 0.5f, 0.5f, 0.5f);
  27. const AZ::Color MagentaAlpha (0.5f, 0.0f, 0.5f, 0.5f);
  28. const AZ::Color LightGray (0.8f, 0.8f, 0.8, 1.0f);
  29. const AZ::Color DarkGray (0.2f, 0.2f, 0.2, 1.0f);
  30. void DrawBackgroundBox(AZ::RPI::AuxGeomDrawPtr auxGeom)
  31. {
  32. // Draw a big cube using DrawTriangles to create a background for the other tests.
  33. // Use triangles rather than an AABB because triangles have back-face culling disabled.
  34. // All other test geometries are drawn inside this big cube.
  35. float cubeHalfWidth = 80.0f;
  36. float left = -cubeHalfWidth;
  37. float right = cubeHalfWidth;
  38. float top = cubeHalfWidth;
  39. float bottom = -cubeHalfWidth;
  40. float front = cubeHalfWidth;
  41. float back = -cubeHalfWidth;
  42. const uint32_t NumCubePoints = 8;
  43. AZ::Vector3 cubePoints[NumCubePoints] =
  44. {
  45. AZ::Vector3(left, front, top), AZ::Vector3(right, front, top), AZ::Vector3(right, front, bottom), AZ::Vector3(left, front, bottom),
  46. AZ::Vector3(left, back, top), AZ::Vector3(right, back, top), AZ::Vector3(right, back, bottom), AZ::Vector3(left, back, bottom),
  47. };
  48. const uint32_t NumCubeIndicies = 36;
  49. uint32_t cubeIndicies[NumCubeIndicies] =
  50. {
  51. 0, 1, 2, 2, 3, 0, // front face
  52. 4, 5, 6, 6, 7, 4, // back face (no back-face culling)
  53. 0, 3, 7, 7, 4, 0, // left
  54. 1, 2, 6, 6, 5, 1, // right
  55. 0, 1, 5, 5, 4, 0, // top
  56. 2, 3, 7, 7, 6, 2, // bottom
  57. };
  58. // Make the cube dark gray on the top face, blending to light gray on the bottom face
  59. AZ::Color cubeColors[NumCubePoints] = { DarkGray, DarkGray, LightGray, LightGray, DarkGray, DarkGray, LightGray, LightGray };
  60. // Draw as opaque cube with multiple colors and shared vertices
  61. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  62. drawArgs.m_verts = cubePoints;
  63. drawArgs.m_vertCount = NumCubePoints;
  64. drawArgs.m_indices = cubeIndicies;
  65. drawArgs.m_indexCount = NumCubeIndicies;
  66. drawArgs.m_colors = cubeColors;
  67. drawArgs.m_colorCount = NumCubePoints;
  68. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Opaque;
  69. auxGeom->DrawTriangles(drawArgs);
  70. }
  71. void DrawThreeGridsOfPoints(AZ::RPI::AuxGeomDrawPtr auxGeom)
  72. {
  73. AZ::u8 pointSize = 10; // DX12 API ignores point size, works on Vulkan
  74. // draw three grids of points
  75. const uint32_t NumPlanePointsPerAxis = 16; // must be even
  76. const uint32_t NumPlanePoints = NumPlanePointsPerAxis * NumPlanePointsPerAxis;
  77. float gridHalfWidth = 0.1f;
  78. float gridSpacing = gridHalfWidth/aznumeric_cast<float>(NumPlanePointsPerAxis/2);
  79. AZ::Vector3 origin(0.0f, 0.0f, 2.0f);
  80. ///////////////////////////////////////////////////////////////////////
  81. // 1st grid of points is in plane of x = 0, draw in red
  82. float x, y, z;
  83. y = -gridHalfWidth;
  84. for (int yIndex = 0; yIndex < NumPlanePointsPerAxis; ++yIndex, y += gridSpacing)
  85. {
  86. z = -gridHalfWidth;
  87. for (int zIndex = 0; zIndex <= NumPlanePointsPerAxis; ++zIndex, z += gridSpacing)
  88. {
  89. AZ::Vector3 vert = origin + AZ::Vector3(0.0f, y, z);
  90. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  91. drawArgs.m_verts = &vert;
  92. drawArgs.m_vertCount = 1;
  93. drawArgs.m_colors = &Red;
  94. drawArgs.m_colorCount = 1;
  95. drawArgs.m_size = pointSize;
  96. auxGeom->DrawPoints(drawArgs);
  97. }
  98. }
  99. ///////////////////////////////////////////////////////////////////////
  100. // 2nd grid of points is in plane of y = 0, draw in green with one draw call
  101. AZ::Vector3 planePoints[NumPlanePointsPerAxis * NumPlanePointsPerAxis];
  102. uint32_t pointIndex = 0;
  103. x = -gridHalfWidth;
  104. for (int xIndex = 0; xIndex < NumPlanePointsPerAxis; ++xIndex, x += gridSpacing)
  105. {
  106. z = -gridHalfWidth;
  107. for (int zIndex = 0; zIndex < NumPlanePointsPerAxis; ++zIndex, z += gridSpacing)
  108. {
  109. planePoints[pointIndex++] = origin + AZ::Vector3(x, 0.0f, z);
  110. }
  111. }
  112. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  113. drawArgs.m_verts = planePoints;
  114. drawArgs.m_vertCount = NumPlanePoints;
  115. drawArgs.m_colors = &Green;
  116. drawArgs.m_colorCount = 1;
  117. drawArgs.m_size = pointSize;
  118. auxGeom->DrawPoints(drawArgs);
  119. ///////////////////////////////////////////////////////////////////////
  120. // 3rd grid of points is in plane of z = 0, draw in multiple colors with one draw call
  121. AZ::Color pointColors[NumPlanePointsPerAxis * NumPlanePointsPerAxis];
  122. pointIndex = 0;
  123. float opacity = 0.0f;
  124. x = -gridHalfWidth;
  125. for (int xIndex = 0; xIndex < NumPlanePointsPerAxis; ++xIndex, x += gridSpacing)
  126. {
  127. y = -gridHalfWidth;
  128. for (int yIndex = 0; yIndex < NumPlanePointsPerAxis; ++yIndex, y += gridSpacing)
  129. {
  130. planePoints[pointIndex] = origin + AZ::Vector3(x, y, 0.0f);
  131. pointColors[pointIndex] = AZ::Color(0.0f, 0.0f, 1.0f, opacity);
  132. ++pointIndex;
  133. opacity += 1.0f / NumPlanePoints;
  134. }
  135. }
  136. drawArgs.m_verts = planePoints;
  137. drawArgs.m_vertCount = NumPlanePoints;
  138. drawArgs.m_colors = pointColors;
  139. drawArgs.m_colorCount = NumPlanePoints;
  140. drawArgs.m_size = pointSize;
  141. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  142. auxGeom->DrawPoints(drawArgs);
  143. }
  144. void DrawAxisLines(AZ::RPI::AuxGeomDrawPtr auxGeom)
  145. {
  146. // draw a line for each axis with triangles indicating direction and spheres on the ends
  147. AZ::u8 lineWidth = 1; // currently we don't support width on lines
  148. ///////////////////////////////////////////////////////////////////////
  149. // Draw three lines for the axes
  150. float axisLength = 30.0f;
  151. AZ::Vector3 verts[3] = {AZ::Vector3( -axisLength, 0, 0 ), AZ::Vector3( axisLength, 0, 0 )};
  152. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  153. drawArgs.m_verts = verts;
  154. drawArgs.m_vertCount = 2;
  155. drawArgs.m_colors = &Red;
  156. drawArgs.m_colorCount = 1;
  157. drawArgs.m_size = lineWidth;
  158. auxGeom->DrawLines(drawArgs);
  159. verts[0] = AZ::Vector3( 0, -axisLength, 0 ); verts[1] = AZ::Vector3( 0, axisLength, 0 );
  160. drawArgs.m_colors = &Green;
  161. auxGeom->DrawLines(drawArgs);
  162. verts[0] = AZ::Vector3( 0, 0, -axisLength ); verts[1] = AZ::Vector3( 0, 0, axisLength );
  163. drawArgs.m_colors = &Blue;
  164. auxGeom->DrawLines(drawArgs);
  165. ///////////////////////////////////////////////////////////////////////
  166. // Next, draw a couple of triangles on each axis to indicate increasing direction
  167. float triLength = 1.0f;
  168. float triHalfWidth = 0.3f;
  169. float start = 2.5f;
  170. verts[0] = AZ::Vector3(start + triLength, 0, 0); verts[1] = AZ::Vector3(start, -triHalfWidth, 0); verts[2] = AZ::Vector3(start, triHalfWidth, 0);
  171. drawArgs.m_colors = &Red;
  172. drawArgs.m_vertCount = 3;
  173. auxGeom->DrawTriangles(drawArgs);
  174. verts[1] = AZ::Vector3(start, 0, triHalfWidth); verts[2] = AZ::Vector3(start, 0, -triHalfWidth);
  175. auxGeom->DrawTriangles(drawArgs);
  176. verts[0] = AZ::Vector3(0, start + triLength, 0); verts[1] = AZ::Vector3(0, start, -triHalfWidth); verts[2] = AZ::Vector3(0, start, triHalfWidth);
  177. drawArgs.m_colors = &Green;
  178. auxGeom->DrawTriangles(drawArgs);
  179. verts[1] = AZ::Vector3(triHalfWidth, start, 0); verts[2] = AZ::Vector3(-triHalfWidth, start, 0);
  180. auxGeom->DrawTriangles(drawArgs);
  181. verts[0] = AZ::Vector3(0, 0, start + triLength); verts[1] = AZ::Vector3(-triHalfWidth, 0, start); verts[2] = AZ::Vector3(triHalfWidth, 0, start);
  182. drawArgs.m_colors = &Blue;
  183. auxGeom->DrawTriangles(drawArgs);
  184. verts[1] = AZ::Vector3(0, triHalfWidth, start); verts[2] = AZ::Vector3(0, -triHalfWidth, start);
  185. auxGeom->DrawTriangles(drawArgs);
  186. }
  187. void DrawLines(AZ::RPI::AuxGeomDrawPtr auxGeom)
  188. {
  189. float halfLength = 0.25f;
  190. AZ::Vector3 xVec(halfLength, 0.0f, 0.0f);
  191. AZ::Vector3 yVec(0.0f, halfLength, 0.0f);
  192. AZ::Vector3 zVec(0.0f, 0.0f, halfLength);
  193. ///////////////////////////////////////////////////////////////////////
  194. // Draw a 3d cross all one color using DrawLines (opaque)
  195. {
  196. AZ::Vector3 center(-1.0f, 1.0f, 0.0f);
  197. AZ::Vector3 points[] = { center - xVec, center + xVec, center - zVec, center + zVec, center - yVec, center + yVec };
  198. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  199. drawArgs.m_verts = points;
  200. drawArgs.m_vertCount = 6;
  201. drawArgs.m_colors = &Black;
  202. drawArgs.m_colorCount = 1;
  203. auxGeom->DrawLines(drawArgs);
  204. }
  205. ///////////////////////////////////////////////////////////////////////
  206. // Draw a 3d cross all one color using DrawLines (translucent)
  207. {
  208. AZ::Vector3 center(-2.0f, 1.0f, 0.0f);
  209. AZ::Vector3 points[] = { center - xVec, center + xVec, center - zVec, center + zVec, center - yVec, center + yVec };
  210. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  211. drawArgs.m_verts = points;
  212. drawArgs.m_vertCount = 6;
  213. drawArgs.m_colors = &BlackAlpha;
  214. drawArgs.m_colorCount = 1;
  215. auxGeom->DrawLines(drawArgs);
  216. }
  217. ///////////////////////////////////////////////////////////////////////
  218. // Draw a 3d cross in three colors using DrawLines (opaque)
  219. {
  220. AZ::Vector3 center(-1.0f, 2.0f, 0.0f);
  221. AZ::Vector3 points[] = { center - xVec, center + xVec, center - zVec, center + zVec, center - yVec, center + yVec };
  222. AZ::Color colors[] = { Red, Yellow, Green, Cyan, Blue, Magenta };
  223. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  224. drawArgs.m_verts = points;
  225. drawArgs.m_vertCount = 6;
  226. drawArgs.m_colors = colors;
  227. drawArgs.m_colorCount = 6;
  228. auxGeom->DrawLines(drawArgs);
  229. }
  230. ///////////////////////////////////////////////////////////////////////
  231. // Draw a 3d cross in three colors using DrawLines (translucent)
  232. {
  233. AZ::Vector3 center(-2.0f, 2.0f, 0.0f);
  234. AZ::Vector3 points[] = { center - xVec, center + xVec, center - zVec, center + zVec, center - yVec, center + yVec };
  235. AZ::Color colors[] = { RedAlpha, Yellow, GreenAlpha, Cyan, BlueAlpha, Magenta };
  236. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  237. drawArgs.m_verts = points;
  238. drawArgs.m_vertCount = 6;
  239. drawArgs.m_colors = colors;
  240. drawArgs.m_colorCount = 6;
  241. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  242. auxGeom->DrawLines(drawArgs);
  243. }
  244. ///////////////////////////////////////////////////////////////////////
  245. // draw a wireframe pyramid using 5 points and 16 indices in one color (opaque)
  246. {
  247. AZ::Vector3 baseCenter(-1.0f, 3.0f, 0.0f);
  248. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec, baseCenter + zVec };
  249. uint32_t indices[] = { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 4, 2, 4, 3, 4 };
  250. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  251. drawArgs.m_verts = points;
  252. drawArgs.m_vertCount = 5;
  253. drawArgs.m_indices = indices;
  254. drawArgs.m_indexCount = 16;
  255. drawArgs.m_colors = &Black;
  256. drawArgs.m_colorCount = 1;
  257. auxGeom->DrawLines(drawArgs);
  258. }
  259. ///////////////////////////////////////////////////////////////////////
  260. // draw a wireframe pyramid using 5 points and 16 indices in one color (translucent)
  261. {
  262. AZ::Vector3 baseCenter(-2.0f, 3.0f, 0.0f);
  263. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec, baseCenter + zVec };
  264. uint32_t indices[] = { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 4, 2, 4, 3, 4 };
  265. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  266. drawArgs.m_verts = points;
  267. drawArgs.m_vertCount = 5;
  268. drawArgs.m_indices = indices;
  269. drawArgs.m_indexCount = 16;
  270. drawArgs.m_colors = &BlackAlpha;
  271. drawArgs.m_colorCount = 1;
  272. auxGeom->DrawLines(drawArgs);
  273. }
  274. ///////////////////////////////////////////////////////////////////////
  275. // draw a wireframe pyramid using 5 points and 16 indices in many colors (opaque)
  276. {
  277. AZ::Vector3 baseCenter(-1.0f, 4.0f, 0.0f);
  278. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec, baseCenter + zVec };
  279. uint32_t indices[] = { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 4, 2, 4, 3, 4 };
  280. AZ::Color colors[] = { Red, Green, Blue, Yellow, Black };
  281. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  282. drawArgs.m_verts = points;
  283. drawArgs.m_vertCount = 5;
  284. drawArgs.m_indices = indices;
  285. drawArgs.m_indexCount = 16;
  286. drawArgs.m_colors = colors;
  287. drawArgs.m_colorCount = 5;
  288. auxGeom->DrawLines(drawArgs);
  289. }
  290. ///////////////////////////////////////////////////////////////////////
  291. // draw a wireframe pyramid using 5 points and 16 indices in many colors (translucent)
  292. {
  293. AZ::Vector3 baseCenter(-2.0f, 4.0f, 0.0f);
  294. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec, baseCenter + zVec };
  295. uint32_t indices[] = { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 4, 2, 4, 3, 4 };
  296. AZ::Color colors[] = { RedAlpha, GreenAlpha, BlueAlpha, YellowAlpha, Black };
  297. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  298. drawArgs.m_verts = points;
  299. drawArgs.m_vertCount = 5;
  300. drawArgs.m_indices = indices;
  301. drawArgs.m_indexCount = 16;
  302. drawArgs.m_colors = colors;
  303. drawArgs.m_colorCount = 5;
  304. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  305. auxGeom->DrawLines(drawArgs);
  306. }
  307. ///////////////////////////////////////////////////////////////////////
  308. // draw a closed square using a polyline using 4 points in one color (opaque)
  309. {
  310. AZ::Vector3 baseCenter(-1.0f, 5.0f, 0.0f);
  311. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  312. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  313. drawArgs.m_verts = points;
  314. drawArgs.m_vertCount = 4;
  315. drawArgs.m_colors = &Black;
  316. drawArgs.m_colorCount = 1;
  317. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Closed);
  318. }
  319. ///////////////////////////////////////////////////////////////////////
  320. // draw a closed square using a polyline using 4 points in one color (translucent)
  321. {
  322. AZ::Vector3 baseCenter(-2.0f, 5.0f, 0.0f);
  323. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  324. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  325. drawArgs.m_verts = points;
  326. drawArgs.m_vertCount = 4;
  327. drawArgs.m_colors = &BlackAlpha;
  328. drawArgs.m_colorCount = 1;
  329. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Closed);
  330. }
  331. ///////////////////////////////////////////////////////////////////////
  332. // draw an open square using a polyline using 4 points in one color (opaque)
  333. {
  334. AZ::Vector3 baseCenter(-1.0f, 6.0f, 0.0f);
  335. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  336. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  337. drawArgs.m_verts = points;
  338. drawArgs.m_vertCount = 4;
  339. drawArgs.m_colors = &Black;
  340. drawArgs.m_colorCount = 1;
  341. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Open);
  342. }
  343. ///////////////////////////////////////////////////////////////////////
  344. // draw an open square using a polyline using 4 points in one color (translucent)
  345. {
  346. AZ::Vector3 baseCenter(-2.0f, 6.0f, 0.0f);
  347. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  348. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  349. drawArgs.m_verts = points;
  350. drawArgs.m_vertCount = 4;
  351. drawArgs.m_colors = &BlackAlpha;
  352. drawArgs.m_colorCount = 1;
  353. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Open);
  354. }
  355. ///////////////////////////////////////////////////////////////////////
  356. // draw a closed square using a polyline using 4 points in many colors (opaque)
  357. {
  358. AZ::Vector3 baseCenter(-1.0f, 7.0f, 0.0f);
  359. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  360. AZ::Color colors[] = { Red, Green, Blue, Yellow };
  361. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  362. drawArgs.m_verts = points;
  363. drawArgs.m_vertCount = 4;
  364. drawArgs.m_colors = colors;
  365. drawArgs.m_colorCount = 4;
  366. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Closed);
  367. }
  368. ///////////////////////////////////////////////////////////////////////
  369. // draw a closed square using a polyline using 4 points in many colors (translucent)
  370. {
  371. AZ::Vector3 baseCenter(-2.0f, 7.0f, 0.0f);
  372. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  373. AZ::Color colors[] = { RedAlpha, GreenAlpha, BlueAlpha, YellowAlpha };
  374. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  375. drawArgs.m_verts = points;
  376. drawArgs.m_vertCount = 4;
  377. drawArgs.m_colors = colors;
  378. drawArgs.m_colorCount = 4;
  379. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  380. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Closed);
  381. }
  382. ///////////////////////////////////////////////////////////////////////
  383. // draw an open square using a polyline using 4 points in many colors (opaque)
  384. {
  385. AZ::Vector3 baseCenter(-1.0f, 8.0f, 0.0f);
  386. AZ::Vector3 points[] = { baseCenter - xVec - yVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  387. AZ::Color colors[] = { Red, Green, Blue, Yellow };
  388. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  389. drawArgs.m_verts = points;
  390. drawArgs.m_vertCount = 4;
  391. drawArgs.m_colors = colors;
  392. drawArgs.m_colorCount = 4;
  393. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Open);
  394. }
  395. ///////////////////////////////////////////////////////////////////////
  396. // draw an open square using a polyline using 4 points in many colors (translucent)
  397. {
  398. AZ::Vector3 baseCenter(-2.0f, 8.0f, 0.0f);
  399. AZ::Vector3 points[] = { baseCenter - xVec - zVec, baseCenter + xVec - yVec, baseCenter + xVec + yVec, baseCenter - xVec + yVec };
  400. AZ::Color colors[] = { RedAlpha, GreenAlpha, BlueAlpha, YellowAlpha };
  401. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  402. drawArgs.m_verts = points;
  403. drawArgs.m_vertCount = 4;
  404. drawArgs.m_colors = colors;
  405. drawArgs.m_colorCount = 4;
  406. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  407. auxGeom->DrawPolylines(drawArgs, AuxGeomDraw::PolylineEnd::Open);
  408. }
  409. }
  410. void DrawTriangles(AZ::RPI::AuxGeomDrawPtr auxGeom)
  411. {
  412. // Draw a mixture of opaque and translucent triangles to test distance sorting of primitives
  413. float width = 2.0f;
  414. float left = -5.0f;
  415. float right = left + width;
  416. float bottom = 2.0f;
  417. float top = bottom + width;
  418. float spacing = 1.0f;
  419. float yStart = -10.0f;
  420. float y = yStart;
  421. ///////////////////////////////////////////////////////////////////////
  422. AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  423. AZ::Vector3 verts[3] = {AZ::Vector3(left, y, top), AZ::Vector3(right, y, top), AZ::Vector3(right, y, bottom)};
  424. drawArgs.m_verts = verts;
  425. drawArgs.m_vertCount = 3;
  426. drawArgs.m_colors = &Black;
  427. drawArgs.m_colorCount = 1;
  428. auxGeom->DrawTriangles(drawArgs);
  429. ///////////////////////////////////////////////////////////////////////
  430. y += spacing;
  431. verts[0] = AZ::Vector3(right, y, top); verts[1] = AZ::Vector3(right, y, bottom); verts[2] = AZ::Vector3(left, y, bottom);
  432. drawArgs.m_colors = &BlackAlpha;
  433. auxGeom->DrawTriangles(drawArgs);
  434. ///////////////////////////////////////////////////////////////////////
  435. y += spacing;
  436. verts[0] = AZ::Vector3(left, y, top); verts[1] = AZ::Vector3(right, y, top); verts[2] = AZ::Vector3(right, y, bottom);
  437. drawArgs.m_colors = &Red;
  438. auxGeom->DrawTriangles(drawArgs);
  439. ///////////////////////////////////////////////////////////////////////
  440. y += spacing;
  441. verts[0] = AZ::Vector3(right, y, top); verts[1] = AZ::Vector3(right, y, bottom); verts[2] = AZ::Vector3(left, y, bottom);
  442. drawArgs.m_colors = &RedAlpha;
  443. auxGeom->DrawTriangles(drawArgs);
  444. ///////////////////////////////////////////////////////////////////////
  445. y += spacing;
  446. verts[0] = AZ::Vector3(left, y, top); verts[1] = AZ::Vector3(right, y, top); verts[2] = AZ::Vector3(right, y, bottom);
  447. drawArgs.m_colors = &Green;
  448. auxGeom->DrawTriangles(drawArgs);
  449. ///////////////////////////////////////////////////////////////////////
  450. y += spacing;
  451. verts[0] = AZ::Vector3(right, y, top); verts[1] = AZ::Vector3(right, y, bottom); verts[2] = AZ::Vector3(left, y, bottom);
  452. drawArgs.m_colors = &GreenAlpha;
  453. auxGeom->DrawTriangles(drawArgs);
  454. ///////////////////////////////////////////////////////////////////////
  455. y += spacing;
  456. verts[0] = AZ::Vector3(left, y, top); verts[1] = AZ::Vector3(right, y, top); verts[2] = AZ::Vector3(right, y, bottom);
  457. drawArgs.m_colors = &Blue;
  458. auxGeom->DrawTriangles(drawArgs);
  459. ///////////////////////////////////////////////////////////////////////
  460. y += spacing;
  461. verts[0] = AZ::Vector3(right, y, top); verts[1] = AZ::Vector3(right, y, bottom); verts[2] = AZ::Vector3(left, y, bottom);
  462. drawArgs.m_colors = &BlueAlpha;
  463. auxGeom->DrawTriangles(drawArgs);
  464. ///////////////////////////////////////////////////////////////////////
  465. // do the same thing but in 2 AuxGeom draw calls - one for the opaque and one for the translucent
  466. // Note that this will mean that the 5 translucent depth sort together and not separately
  467. left = -8.0f;
  468. right = left + width;
  469. const uint32_t NumPoints = 12;
  470. AZ::Vector3 opaquePoints[NumPoints] =
  471. {
  472. AZ::Vector3(left, yStart + spacing * 0, top), AZ::Vector3(right, yStart + spacing * 0, top), AZ::Vector3(right, yStart + spacing * 0, bottom),
  473. AZ::Vector3(left, yStart + spacing * 2, top), AZ::Vector3(right, yStart + spacing * 2, top), AZ::Vector3(right, yStart + spacing * 2, bottom),
  474. AZ::Vector3(left, yStart + spacing * 4, top), AZ::Vector3(right, yStart + spacing * 4, top), AZ::Vector3(right, yStart + spacing * 4, bottom),
  475. AZ::Vector3(left, yStart + spacing * 6, top), AZ::Vector3(right, yStart + spacing * 6, top), AZ::Vector3(right, yStart + spacing * 6, bottom),
  476. };
  477. AZ::Vector3 transPoints[NumPoints] =
  478. {
  479. AZ::Vector3(right, yStart + spacing * 1, top), AZ::Vector3(right, yStart + spacing * 1, bottom), AZ::Vector3(left, yStart + spacing * 1, bottom),
  480. AZ::Vector3(right, yStart + spacing * 3, top), AZ::Vector3(right, yStart + spacing * 3, bottom), AZ::Vector3(left, yStart + spacing * 3, bottom),
  481. AZ::Vector3(right, yStart + spacing * 5, top), AZ::Vector3(right, yStart + spacing * 5, bottom), AZ::Vector3(left, yStart + spacing * 5, bottom),
  482. AZ::Vector3(right, yStart + spacing * 7, top), AZ::Vector3(right, yStart + spacing * 7, bottom), AZ::Vector3(left, yStart + spacing * 7, bottom),
  483. };
  484. AZ::Color opaqueColors[NumPoints] = {
  485. Black, Black, Black,
  486. Red, Red, Red,
  487. Green, Green, Green,
  488. Blue, Blue, Blue,
  489. };
  490. AZ::Color transColors[NumPoints] = {
  491. BlackAlpha, BlackAlpha, BlackAlpha,
  492. RedAlpha, RedAlpha, RedAlpha,
  493. GreenAlpha, GreenAlpha, GreenAlpha,
  494. BlueAlpha, BlueAlpha, BlueAlpha,
  495. };
  496. ///////////////////////////////////////////////////////////////////////
  497. // opaque triangles
  498. drawArgs.m_verts = opaquePoints;
  499. drawArgs.m_vertCount = NumPoints;
  500. drawArgs.m_colors = opaqueColors;
  501. drawArgs.m_colorCount = NumPoints;
  502. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Opaque;
  503. auxGeom->DrawTriangles(drawArgs);
  504. ///////////////////////////////////////////////////////////////////////
  505. // translucent triangles
  506. drawArgs.m_verts = transPoints;
  507. drawArgs.m_colors = transColors;
  508. drawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  509. auxGeom->DrawTriangles(drawArgs);
  510. ///////////////////////////////////////////////////////////////////////
  511. // Draw cubes using indexed draws to test shared vertices
  512. left = -12.0f;
  513. right = left + width;
  514. float front = yStart;
  515. float back = front + width;
  516. const uint32_t NumCubePoints = 8;
  517. AZ::Vector3 cubePoints[NumCubePoints] =
  518. {
  519. AZ::Vector3(left, front, top), AZ::Vector3(right, front, top), AZ::Vector3(right, front, bottom), AZ::Vector3(left, front, bottom),
  520. AZ::Vector3(left, back, top), AZ::Vector3(right, back, top), AZ::Vector3(right, back, bottom), AZ::Vector3(left, back, bottom),
  521. };
  522. const uint32_t NumCubeIndicies = 36;
  523. uint32_t cubeIndicies[NumCubeIndicies] =
  524. {
  525. 0, 1, 2, 2, 3, 0, // front face
  526. 4, 5, 6, 6, 7, 4, // back face (no back-face culling)
  527. 0, 3, 7, 7, 4, 0, // left
  528. 1, 2, 6, 6, 5, 1, // right
  529. 0, 1, 5, 5, 4, 0, // top
  530. 2, 3, 7, 7, 6, 2, // bottom
  531. };
  532. AZ::Color cubeColors[NumCubePoints] = { Red, Green, Blue, Black, RedAlpha, GreenAlpha, BlueAlpha, BlackAlpha };
  533. ///////////////////////////////////////////////////////////////////////
  534. // Opaque cube all one color
  535. AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments indexedDrawArgs;
  536. indexedDrawArgs.m_verts = cubePoints;
  537. indexedDrawArgs.m_vertCount = NumCubePoints;
  538. indexedDrawArgs.m_indices = cubeIndicies;
  539. indexedDrawArgs.m_indexCount = NumCubeIndicies;
  540. indexedDrawArgs.m_colors = &Red;
  541. indexedDrawArgs.m_colorCount = 1;
  542. auxGeom->DrawTriangles(indexedDrawArgs);
  543. ///////////////////////////////////////////////////////////////////////
  544. // Move all the points along the positive Y axis and draw another cube
  545. AZ::Vector3 offset(0.0f, 4.0f, 0.0f);
  546. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  547. {
  548. cubePoints[pointIndex] += offset;
  549. }
  550. // Translucent cube all one color
  551. indexedDrawArgs.m_colors = &RedAlpha;
  552. auxGeom->DrawTriangles(indexedDrawArgs);
  553. ///////////////////////////////////////////////////////////////////////
  554. // Move all the points along the positive Z axis and draw another cube
  555. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  556. {
  557. cubePoints[pointIndex] += offset;
  558. }
  559. // Opaque cube with multiple colors
  560. indexedDrawArgs.m_colors = cubeColors;
  561. indexedDrawArgs.m_colorCount = NumCubePoints;
  562. indexedDrawArgs.m_opacityType = AuxGeomDraw::OpacityType::Opaque;
  563. auxGeom->DrawTriangles(indexedDrawArgs);
  564. ///////////////////////////////////////////////////////////////////////
  565. // Move all the points along the positive Z axis and draw another cube
  566. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  567. {
  568. cubePoints[pointIndex] += offset;
  569. }
  570. // Translucent cube with multiple colors
  571. indexedDrawArgs.m_opacityType = AuxGeomDraw::OpacityType::Translucent;
  572. auxGeom->DrawTriangles(indexedDrawArgs);
  573. }
  574. void DrawShapes(AZ::RPI::AuxGeomDrawPtr auxGeom)
  575. {
  576. // Draw a mixture of opaque and translucent shapes to test distance sorting of objects
  577. const int numRows = 4;
  578. AZ::Color opaqueColors[numRows] = { Red, Green, Blue, Yellow };
  579. AZ::Color translucentColors[numRows] = { RedAlpha, GreenAlpha, BlueAlpha, YellowAlpha };
  580. const int numDrawStyles = 4;
  581. AuxGeomDraw::DrawStyle drawStyles[numDrawStyles] = { AuxGeomDraw::DrawStyle::Point, AuxGeomDraw::DrawStyle::Line, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DrawStyle::Shaded };
  582. AZ::Vector3 direction[numRows] =
  583. {
  584. AZ::Vector3( 0.0f, -1.0f, 0.0f),
  585. AZ::Vector3(-1.0f, 0.0f, 0.0f),
  586. AZ::Vector3( 1.0f, 1.0f, 0.0f),
  587. AZ::Vector3(-1.0f, 1.0f, -1.0f)
  588. };
  589. float radius = 1.0f;
  590. float height = 1.0f;
  591. float x = 5.0f;
  592. float y = -8.0f;
  593. float z = 2.0f;
  594. Vector3 translucentOffset(2.0f, 0.0f, 0.5f);
  595. Vector3 shapeOffset(0.0f, 0.0f, 3.0f);
  596. Vector3 styleOffset(8.0f, 0.0f, 0.0f);
  597. float colorYOffset = -8.0f;
  598. auxGeom->SetPointSize(5.0f);
  599. // Each row is drawn in a different color (colors changing along the Z axis)
  600. // Within each row we draw every shape in all three draw styles, both opaque and translucent
  601. for (int rowIndex = 0; rowIndex < numRows; ++rowIndex)
  602. {
  603. Vector3 basePosition = Vector3(x, y, z);
  604. // Spread the draw style out along the X axis
  605. for (int style = 0; style < numDrawStyles; ++style)
  606. {
  607. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  608. // Draw Spheres
  609. // Two spheres, one opaque, one translucent, One DepthTest On & DepthWrite off, One DepthTest Off & DepthWrite On
  610. Vector3 shapePosition = basePosition;
  611. auxGeom->DrawSphere(shapePosition, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  612. auxGeom->DrawSphere(shapePosition + 1.0f * translucentOffset, radius, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  613. // Two spheres, One DepthTest On & DepthWrite off, One DepthTest Off & DepthWrite On
  614. shapePosition += shapeOffset;
  615. auxGeom->DrawSphere(shapePosition, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  616. auxGeom->DrawSphere(shapePosition + translucentOffset, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::Off, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  617. // Three spheres, One no face cull, One front face cull, One back face cull
  618. shapePosition += shapeOffset;
  619. auxGeom->DrawSphere(shapePosition, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None);
  620. auxGeom->DrawSphere(shapePosition + translucentOffset, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Front);
  621. auxGeom->DrawSphere(shapePosition + 2.0f * translucentOffset, radius, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  622. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  623. // Draw Cones
  624. // Two cones, one opaque, one translucent
  625. shapePosition += 2.0f * shapeOffset;
  626. auxGeom->DrawCone(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  627. auxGeom->DrawCone(shapePosition + translucentOffset, direction[rowIndex], radius, height, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  628. // Two cones, One DepthTest On & DepthWrite off, One DepthTest Off & DepthWrite On
  629. shapePosition += shapeOffset;
  630. auxGeom->DrawCone(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  631. auxGeom->DrawCone(shapePosition + translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::Off, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  632. // Three cones, One no face cull, One front face cull, One back face cull
  633. shapePosition += shapeOffset;
  634. auxGeom->DrawCone(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None);
  635. auxGeom->DrawCone(shapePosition + translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Front);
  636. auxGeom->DrawCone(shapePosition + 2.0f * translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  637. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  638. // Draw Cylinders
  639. // Two cylinders, one opaque, one translucent
  640. shapePosition += 2.0f * shapeOffset;
  641. auxGeom->DrawCylinder(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  642. auxGeom->DrawCylinder(shapePosition + translucentOffset, direction[rowIndex], radius, height, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  643. // Two cylinders, One DepthTest On & DepthWrite off, One DepthTest Off & DepthWrite On
  644. shapePosition += shapeOffset;
  645. auxGeom->DrawCylinder(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::Back);
  646. auxGeom->DrawCylinder(shapePosition + translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::Off, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  647. // Three cylinders, One no face cull, One front face cull, One back face cull
  648. shapePosition += shapeOffset;
  649. auxGeom->DrawCylinder(shapePosition, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None);
  650. auxGeom->DrawCylinder(shapePosition + translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Front);
  651. auxGeom->DrawCylinder(shapePosition + 2.0f * translucentOffset, direction[rowIndex], radius, height, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::Back);
  652. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  653. basePosition += styleOffset;
  654. }
  655. // move along Z axis for next color
  656. y += colorYOffset;
  657. radius *= 0.75f;
  658. height *= 1.25f;
  659. }
  660. }
  661. void DrawBoxes(AZ::RPI::AuxGeomDrawPtr auxGeom, float x)
  662. {
  663. // Draw a mixture of opaque and translucent boxes
  664. const int numRows = 4;
  665. AZ::Color opaqueColors[numRows] = { Red, Green, Blue, Yellow };
  666. AZ::Color translucentColors[numRows] = { RedAlpha, GreenAlpha, BlueAlpha, YellowAlpha };
  667. const int numStyles = 4;
  668. AuxGeomDraw::DrawStyle drawStyles[numStyles] = { AuxGeomDraw::DrawStyle::Point, AuxGeomDraw::DrawStyle::Line, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DrawStyle::Shaded };
  669. // The size of the box for each row
  670. AZ::Vector3 halfExtents[numRows] =
  671. {
  672. AZ::Vector3( 0.5f, 2.0f, 1.0f),
  673. AZ::Vector3( 1.0f, 1.5f, 1.0f),
  674. AZ::Vector3( 2.0f, 0.5f, 0.5f),
  675. AZ::Vector3( 0.1f, 1.0f, 2.0f)
  676. };
  677. // The euler rotations for each row that has a rotation
  678. AZ::Vector3 rotation[numRows] =
  679. {
  680. AZ::Vector3( 90.0f, 0.0f, 90.0f),
  681. AZ::Vector3( 90.0f, 0.0f, 0.0f),
  682. AZ::Vector3( 0.0f, 0.0f, -45.0f),
  683. AZ::Vector3( 0.0f, 45.0f, -45.0f)
  684. };
  685. float y = 5.0f;
  686. float z = 2.0f;
  687. Vector3 translucentOffset(2.0f, 0.0f, 0.5f);
  688. Vector3 typeOffset(0.0f, 0.0f, 3.0f);
  689. Vector3 styleOffset(8.0f, 0.0f, 0.0f);
  690. // each translucent box is positioned like its opaque partner but with this additional scale
  691. Vector3 transScale(1.5f, 0.5f, 1.0f);
  692. float colorYOffset = 8.0f;
  693. auxGeom->SetPointSize(10.0f);
  694. // Each row is drawn in a different color (colors changing along the Z axis)
  695. // Within each row we draw every box type in all three draw styles, both opaque and translucent
  696. for (int rowIndex = 0; rowIndex < numRows; ++rowIndex)
  697. {
  698. Vector3 basePosition = Vector3(x, y, z);
  699. AZ::Transform rotationTransform;
  700. rotationTransform.SetFromEulerDegrees(rotation[rowIndex]);
  701. AZ::Quaternion rotationQuaternion = rotationTransform.GetRotation();
  702. AZ::Matrix3x3 rotationMatrix = AZ::Matrix3x3::CreateFromTransform(rotationTransform);
  703. // Spread the draw style out along the X axis
  704. for (int style = 0; style < numStyles; ++style)
  705. {
  706. Vector3 boxPosition = basePosition;
  707. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  708. // Layer 0: AABBs with no transform
  709. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  710. AZ::Aabb aabb = AZ::Aabb::CreateCenterHalfExtents(basePosition, halfExtents[rowIndex]);
  711. auxGeom->DrawAabb(aabb, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  712. AZ::Vector3 scaledHalfExtents = halfExtents[rowIndex] * transScale;
  713. for (int index = 0; index < 3; ++index)
  714. aabb = AZ::Aabb::CreateCenterHalfExtents(basePosition + translucentOffset, scaledHalfExtents);
  715. auxGeom->DrawAabb(aabb, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off);
  716. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  717. // Layer 1: AABBs with transforms that rotate and scale
  718. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  719. AZ::Matrix3x4 transform = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation(rotationMatrix, basePosition + typeOffset);
  720. aabb = AZ::Aabb::CreateCenterHalfExtents(AZ::Vector3::CreateZero(), halfExtents[rowIndex]);
  721. auxGeom->DrawAabb(aabb, transform, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  722. // for the transparent version apply an additional scale to the transform
  723. transform = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation(rotationMatrix, basePosition + typeOffset + translucentOffset);
  724. transform.MultiplyByScale(transScale);
  725. auxGeom->DrawAabb(aabb, transform, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off);
  726. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  727. // Layer 2: OBB with all position, rotation scale defined in the OBB itself
  728. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  729. // position the OOB using Obb::SetPosition and rotate it using Obb::SetAxis
  730. AZ::Obb obb = AZ::Obb::CreateFromAabb(aabb);
  731. obb.SetPosition(basePosition + 2 * typeOffset);
  732. obb.SetRotation(rotationQuaternion);
  733. auxGeom->DrawObb(obb, AZ::Matrix3x4::CreateIdentity(), opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  734. // for the transparent version apply an additional scale to the transform
  735. obb.SetPosition(basePosition + 2 * typeOffset + translucentOffset);
  736. for (int index = 0; index < 3; ++index)
  737. {
  738. obb.SetHalfLength(index, obb.GetHalfLength(index) * transScale.GetElement(index));
  739. }
  740. auxGeom->DrawObb(obb, AZ::Matrix3x4::CreateIdentity(), translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  741. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  742. // Layer 3: OBB with rotation and scale defined in the OBB itself but position passed to DrawObb separately
  743. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  744. // position the OOB using position param to DrawObb, rotate it using Obb::SetAxis
  745. obb = AZ::Obb::CreateFromAabb(aabb);
  746. obb.SetRotation(rotationQuaternion);
  747. auxGeom->DrawObb(obb, basePosition + 3 * typeOffset, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  748. // For the translucent version apply the additional scale in the half extents
  749. for (int index = 0; index < 3; ++index)
  750. {
  751. obb.SetHalfLength(index, obb.GetHalfLength(index) * transScale.GetElement(index));
  752. }
  753. auxGeom->DrawObb(obb, basePosition + 3 * typeOffset + translucentOffset, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off);
  754. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  755. // Layer 4: OBB with rotation and scale and translation defined in the transform passed to DrawObb
  756. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  757. // position and rotate the OOB using transform param to DrawObb
  758. obb = AZ::Obb::CreateFromAabb(aabb);
  759. transform = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation(rotationMatrix, basePosition + 4 * typeOffset);
  760. auxGeom->DrawObb(obb, transform, opaqueColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On);
  761. // for the transparent version apply an additional scale to the transform
  762. transform = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation(rotationMatrix, basePosition + 4 * typeOffset + translucentOffset);
  763. transform.MultiplyByScale(transScale);
  764. auxGeom->DrawObb(obb, transform, translucentColors[rowIndex], drawStyles[style], AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off);
  765. basePosition += styleOffset;
  766. }
  767. // move along Z axis for next color
  768. y += colorYOffset;
  769. }
  770. }
  771. void DrawManyPrimitives(AZ::RPI::AuxGeomDrawPtr auxGeom)
  772. {
  773. // Draw a grid of 300 x 200 quads (as triangle pairs - no shared verts)
  774. const float y = 20.0f;
  775. const float xOrigin = -30.0f;
  776. const float zOrigin = 0.0f;
  777. const float width = 0.1f;
  778. const float height = 0.1f;
  779. // we will draw 300 by 200 (60,000) quads as triangle pairs = 120,000 triangles = 360,000 vertices
  780. const int widthInQuads = 300;
  781. const int heightInQuads = 200;
  782. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  783. drawArgs.m_vertCount = 3;
  784. drawArgs.m_colorCount = 1;
  785. for (int xIndex = 0; xIndex < widthInQuads; ++xIndex)
  786. {
  787. for (int zIndex = 0; zIndex < heightInQuads; ++zIndex)
  788. {
  789. const float xMin = xOrigin + xIndex * width;
  790. const float xMax = xMin + width;
  791. const float zMin = zOrigin + zIndex * height;
  792. const float zMax = zMin + height;
  793. AZ::Color color(static_cast<float>(xIndex) / (widthInQuads - 1), static_cast<float>(zIndex) / (heightInQuads - 1), 0.0f, 1.0f);
  794. AZ::Vector3 verts[3] = {AZ::Vector3(xMin, y, zMax), AZ::Vector3(xMax, y, zMax), AZ::Vector3(xMax, y, zMin)};
  795. drawArgs.m_verts = verts;
  796. drawArgs.m_colors = &color;
  797. auxGeom->DrawTriangles(drawArgs);
  798. AZStd::swap(verts[0], verts[2]);
  799. verts[1] = AZ::Vector3(xMin, y, zMin);
  800. auxGeom->DrawTriangles(drawArgs);
  801. }
  802. }
  803. }
  804. void DrawDepthTestPrimitives(AZ::RPI::AuxGeomDrawPtr auxGeom)
  805. {
  806. float width = 2.0f;
  807. float left = -20.0f;
  808. float right = left + width;
  809. float bottom = -1.0f;
  810. float top = bottom + width;
  811. float yStart = -1.0f;
  812. //Draw opaque cube using DrawTriangles
  813. right = left + width;
  814. float front = yStart;
  815. float back = front + width;
  816. const uint32_t NumCubePoints = 8;
  817. AZ::Vector3 cubePoints[NumCubePoints] =
  818. {
  819. AZ::Vector3(left, front, top), AZ::Vector3(right, front, top), AZ::Vector3(right, front, bottom), AZ::Vector3(left, front, bottom),
  820. AZ::Vector3(left, back, top), AZ::Vector3(right, back, top), AZ::Vector3(right, back, bottom), AZ::Vector3(left, back, bottom),
  821. };
  822. const uint32_t NumCubeIndicies = 36;
  823. uint32_t cubeIndicies[NumCubeIndicies] =
  824. {
  825. 0, 1, 2, 2, 3, 0, // front face
  826. 4, 5, 6, 6, 7, 4, // back face (no back-face culling)
  827. 0, 3, 7, 7, 4, 0, // left
  828. 1, 2, 6, 6, 5, 1, // right
  829. 0, 1, 5, 5, 4, 0, // top
  830. 2, 3, 7, 7, 6, 2, // bottom
  831. };
  832. ///////////////////////////////////////////////////////////////////////
  833. // Opaque cube all one color, depth write & depth test on
  834. AZ::RPI::AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  835. drawArgs.m_verts = cubePoints;
  836. drawArgs.m_vertCount = NumCubePoints;
  837. drawArgs.m_indices = cubeIndicies;
  838. drawArgs.m_indexCount = NumCubeIndicies;
  839. drawArgs.m_colors = &Red;
  840. drawArgs.m_colorCount = 1;
  841. drawArgs.m_depthTest = AuxGeomDraw::DepthTest::On;
  842. drawArgs.m_depthWrite = AuxGeomDraw::DepthWrite::On;
  843. auxGeom->DrawTriangles(drawArgs);
  844. AZ::Vector3 offset = AZ::Vector3(-5.0f, 0.0f, 0.0f);
  845. AZ::Vector3 scale = AZ::Vector3(1.0f, 3.0f, 3.0f);
  846. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  847. {
  848. cubePoints[pointIndex] *= scale;
  849. cubePoints[pointIndex] += offset;
  850. }
  851. ///////////////////////////////////////////////////////////////////////
  852. // Opaque cube all one color, depth write off
  853. drawArgs.m_colors = &Green;
  854. drawArgs.m_depthTest = AuxGeomDraw::DepthTest::On;
  855. drawArgs.m_depthWrite = AuxGeomDraw::DepthWrite::Off;
  856. auxGeom->DrawTriangles(drawArgs);
  857. offset = AZ::Vector3(-5.0f, 1.0f, 0.0f);
  858. scale = AZ::Vector3(1.0f, 0.3333f, 0.3333f);
  859. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  860. {
  861. cubePoints[pointIndex] *= scale;
  862. cubePoints[pointIndex] += offset;
  863. }
  864. ///////////////////////////////////////////////////////////////////////
  865. // Opaque cube all one color, depth Test off
  866. drawArgs.m_colors = &Blue;
  867. drawArgs.m_depthTest = AuxGeomDraw::DepthTest::Off;
  868. drawArgs.m_depthWrite = AuxGeomDraw::DepthWrite::On;
  869. auxGeom->DrawTriangles(drawArgs);
  870. offset = AZ::Vector3(-5.0f, 1.0f, 0.0f);
  871. for (int pointIndex = 0; pointIndex < NumCubePoints; ++pointIndex)
  872. {
  873. cubePoints[pointIndex] += offset;
  874. }
  875. ///////////////////////////////////////////////////////////////////////
  876. // Opaque cube all one color, depth Test on, depth Write on
  877. drawArgs.m_colors = &Yellow;
  878. drawArgs.m_depthTest = AuxGeomDraw::DepthTest::On;
  879. drawArgs.m_depthWrite = AuxGeomDraw::DepthWrite::On;
  880. auxGeom->DrawTriangles(drawArgs);
  881. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  882. // Repeat with AABB shapes
  883. float radius = width / 2.0f;
  884. AZ::Vector3 basePosition = AZ::Vector3(-20.0f + radius, -7.0f, 0.0f); // adjust x pos by +radius to account for AABB's being centered relative to the coordinate.
  885. AZ::Aabb aabb = AZ::Aabb::CreateCenterRadius(basePosition, radius);
  886. auxGeom->DrawAabb(aabb, Red, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None, -1);
  887. aabb = AZ::Aabb::CreateCenterHalfExtents(basePosition + 1.0f * offset, AZ::Vector3(radius, 3.0f * radius, 3.0f * radius));
  888. auxGeom->DrawAabb(aabb, Green, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::Off, AuxGeomDraw::FaceCullMode::None, -1);
  889. aabb = AZ::Aabb::CreateCenterRadius(basePosition + 2.0f * offset, radius);
  890. auxGeom->DrawAabb(aabb, Blue, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::Off, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None, -1);
  891. aabb = AZ::Aabb::CreateCenterRadius(basePosition + 3.0f * offset, radius);
  892. auxGeom->DrawAabb(aabb, Yellow, AuxGeomDraw::DrawStyle::Solid, AuxGeomDraw::DepthTest::On, AuxGeomDraw::DepthWrite::On, AuxGeomDraw::FaceCullMode::None, -1);
  893. }
  894. void Draw2DWireRect(AZ::RPI::AuxGeomDrawPtr auxGeom, const AZ::Color& color, float z)
  895. {
  896. float vertOffset = z * 0.45f;
  897. AZ::Vector3 verts[4] = {
  898. AZ::Vector3(0.5f - vertOffset, 0.5f - vertOffset, z),
  899. AZ::Vector3(0.5f - vertOffset, 0.5f + vertOffset, z),
  900. AZ::Vector3(0.5f + vertOffset, 0.5f + vertOffset, z),
  901. AZ::Vector3(0.5f + vertOffset, 0.5f - vertOffset, z) };
  902. int32_t viewProjOverrideIndex = auxGeom->GetOrAdd2DViewProjOverride();
  903. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  904. drawArgs.m_verts = verts;
  905. drawArgs.m_vertCount = 4;
  906. drawArgs.m_colors = &color;
  907. drawArgs.m_colorCount = 1;
  908. drawArgs.m_viewProjectionOverrideIndex = viewProjOverrideIndex;
  909. auxGeom->DrawPolylines(drawArgs, AZ::RPI::AuxGeomDraw::PolylineEnd::Closed);
  910. }
  911. }