AuxGeomSharedDrawFunctions.cpp 54 KB

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