AuxGeomSharedDrawFunctions.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  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. }