AuxGeomSharedDrawFunctions.cpp 55 KB

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