Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

659 lines
21 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "uigeometry.h"
  8. #include "materialsystem/imaterialsystem.h"
  9. #include "mathlib/vector.h"
  10. #include "gamegraphic.h"
  11. #include "graphicgroup.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. //
  16. //-----------------------------------------------------------------------------
  17. CGeometry::CGeometry()
  18. {
  19. m_Center.x = 0;
  20. m_Center.y = 0;
  21. m_Scale.x = 1;
  22. m_Scale.y = 1;
  23. m_Rotation = 0;
  24. m_Color.r = 255;
  25. m_Color.g = 255;
  26. m_Color.b = 255;
  27. m_Color.a = 255;
  28. m_TopColor.r = 255;
  29. m_TopColor.g = 255;
  30. m_TopColor.b = 255;
  31. m_TopColor.a = 255;
  32. m_BottomColor.r = 255;
  33. m_BottomColor.g = 255;
  34. m_BottomColor.b = 255;
  35. m_BottomColor.a = 255;
  36. m_bHorizontalGradient = false;
  37. m_SheetSequenceNumber = 0;
  38. m_AnimationRate = 1;
  39. m_Sublayer = -1;
  40. m_bMaintainAspectRatio = true;
  41. m_bVisible = true;
  42. m_AnimStartTime = DMETIME_ZERO;
  43. m_bAnimate = false;
  44. m_bDirtyExtents = false;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Rendering helper
  48. // Calculate a matrix that will transform the points of the geometry, which are
  49. // currently relative to the center, into screen coords.
  50. //-----------------------------------------------------------------------------
  51. void CGeometry::UpdateRenderTransforms( const StageRenderInfo_t &stageRenderInfo, const CGraphicGroup *pGroup )
  52. {
  53. Assert( pGroup );
  54. if ( !pGroup->IsStageGroup() )
  55. {
  56. if ( pGroup->GetVisible() == false )
  57. {
  58. Assert( m_bVisible == pGroup->GetVisible() );
  59. }
  60. }
  61. if ( !m_bVisible )
  62. return;
  63. // Update positions relative to the center, texture coords, and vertex colors
  64. // If the group maintains aspect ratio it will already have handled this in its transform update.
  65. Vector2D center;
  66. bool bUseMaintainedMatrix = m_bMaintainAspectRatio && !pGroup->MaintainAspectRatio();
  67. if ( bUseMaintainedMatrix )
  68. {
  69. // If this is the case we transform the center to screen coords first.
  70. // Then take into account any size scaling in the scalemat
  71. // Note this is not accounting for groups or rotations.
  72. matrix3x4_t screenScalemat;
  73. SetScaleMatrix( stageRenderInfo.parentScale.x, stageRenderInfo.parentScale.y, 1, screenScalemat );
  74. Vector centerVec( m_Center.x, m_Center.y, 0 );
  75. Vector centerInScreen;
  76. VectorTransform( centerVec, screenScalemat, centerInScreen );
  77. // Remove the uniform scale component from the center because it will come back in GetRenderTransform()
  78. float scaleToUse;
  79. if ( stageRenderInfo.parentScale.x > stageRenderInfo.parentScale.y )
  80. {
  81. scaleToUse = stageRenderInfo.parentScale.y;
  82. }
  83. else
  84. {
  85. scaleToUse = stageRenderInfo.parentScale.x;
  86. }
  87. SetScaleMatrix( 1/scaleToUse, 1/scaleToUse, 1, screenScalemat );
  88. Vector tempCenter;
  89. VectorTransform( centerInScreen, screenScalemat, tempCenter );
  90. center.x = tempCenter.x;
  91. center.y = tempCenter.y;
  92. /* old version.
  93. // If this is the case we transform the center to screen coords first.
  94. // Then take into account any size scaling in the scalemat
  95. matrix3x4_t screenScalemat;
  96. SetScaleMatrix( stageRenderInfo.parentScale.x, stageRenderInfo.parentScale.y, 1, screenScalemat );
  97. Vector centerVec( m_Center.x, m_Center.y, 0 );
  98. Vector centerInScreen;
  99. VectorTransform( centerVec, screenScalemat, centerInScreen );
  100. center.x = centerInScreen.x;
  101. center.y = centerInScreen.y;
  102. */
  103. }
  104. else
  105. {
  106. center = m_Center;
  107. }
  108. matrix3x4_t transmat;
  109. Vector position( center.x, center.y, 0 );
  110. SetIdentityMatrix( transmat );
  111. PositionMatrix( position, transmat );
  112. matrix3x4_t scalemat;
  113. SetScaleMatrix( m_Scale.x, m_Scale.y, 1, scalemat );
  114. matrix3x4_t rotmat;
  115. Vector axis( 0, 0, 1 );
  116. MatrixBuildRotationAboutAxis( axis, m_Rotation, rotmat );
  117. matrix3x4_t temp;
  118. MatrixMultiply( rotmat, scalemat, temp );
  119. matrix3x4_t rawToLocal;
  120. MatrixMultiply( transmat, temp, rawToLocal );
  121. matrix3x4_t groupToScreen;
  122. // Use the matrix that doesn't contain any scale changes if we should
  123. pGroup->GetRenderTransform( groupToScreen, bUseMaintainedMatrix );
  124. MatrixMultiply( groupToScreen, rawToLocal, m_RenderToScreen );
  125. if ( m_bDirtyExtents )
  126. {
  127. CalculateExtentsMatrix( stageRenderInfo, pGroup );
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. //
  132. //-----------------------------------------------------------------------------
  133. void CGeometry::UpdateRenderData( CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  134. {
  135. if ( !m_bVisible )
  136. return;
  137. int i = renderGeometryLists[firstListIndex].AddToTail();
  138. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  139. // Now transform our array of positions into local graphic coord system.
  140. int nCount = m_RelativePositions.Count();
  141. for ( int i = 0; i < nCount; ++i )
  142. {
  143. // Position
  144. Vector relativePosition( m_RelativePositions[i].x, m_RelativePositions[i].y, 0 );
  145. Vector screenpos;
  146. VectorTransform( relativePosition, m_RenderToScreen, screenpos );
  147. renderGeometry.m_Positions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );;
  148. // TexCoord
  149. renderGeometry.m_TextureCoords.AddToTail( m_TextureCoords[i] );
  150. // Vertex Color
  151. renderGeometry.m_VertexColors.AddToTail( m_VertexColors[i] );
  152. }
  153. // Triangles
  154. nCount = m_Triangles.Count();
  155. for ( int i = 0; i < nCount; ++i )
  156. {
  157. renderGeometry.m_Triangles.AddToTail( m_Triangles[i] );
  158. }
  159. // Anim Info
  160. renderGeometry.m_SheetSequenceNumber = m_SheetSequenceNumber;
  161. renderGeometry.m_AnimationRate = m_AnimationRate;
  162. renderGeometry.m_bAnimate = m_bAnimate;
  163. renderGeometry.m_AnimStartTime = m_AnimStartTime;
  164. renderGeometry.m_pImageAlias = NULL;
  165. CalculateExtents();
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Calculate the rectangular extents of this graphic.
  169. //-----------------------------------------------------------------------------
  170. void CGeometry::CalculateExtentsMatrix( const StageRenderInfo_t &stageRenderInfo, const CGraphicGroup *pGroup )
  171. {
  172. Assert( pGroup );
  173. // Update positions relative to the center, texture coords, and vertex colors
  174. // If the group maintains aspect ratio it will already have handled this in its transform update.
  175. Vector2D center;
  176. bool bUseMaintainedMatrix = m_bMaintainAspectRatio && !pGroup->MaintainAspectRatio();
  177. if ( bUseMaintainedMatrix )
  178. {
  179. // If this is the case we transform the center to screen coords first.
  180. // Then take into account any size scaling in the scalemat
  181. matrix3x4_t screenScalemat;
  182. SetScaleMatrix( stageRenderInfo.parentScale.x, stageRenderInfo.parentScale.y, 1, screenScalemat );
  183. Vector centerVec( m_Center.x, m_Center.y, 0 );
  184. Vector centerInScreen;
  185. VectorTransform( centerVec, screenScalemat, centerInScreen );
  186. center.x = centerInScreen.x;
  187. center.y = centerInScreen.y;
  188. }
  189. else
  190. {
  191. center = m_Center;
  192. }
  193. matrix3x4_t transmat;
  194. Vector position( center.x, center.y, 0 );
  195. SetIdentityMatrix( transmat );
  196. PositionMatrix( position, transmat );
  197. // TODO: account for scaling anims?
  198. matrix3x4_t scalemat;
  199. SetScaleMatrix( m_Scale.x, m_Scale.y, 1, scalemat );
  200. // Rotation is ignored.
  201. matrix3x4_t rotmat;
  202. Vector axis( 0, 0, 1 );
  203. MatrixBuildRotationAboutAxis( axis, 0, rotmat );
  204. matrix3x4_t temp;
  205. MatrixMultiply( rotmat, scalemat, temp );
  206. matrix3x4_t rawToLocal;
  207. MatrixMultiply( transmat, temp, rawToLocal );
  208. matrix3x4_t groupToScreen;
  209. // Use the matrix that doesn't contain any scale changes if we should
  210. pGroup->GetRenderTransform( groupToScreen, bUseMaintainedMatrix );
  211. MatrixMultiply( groupToScreen, rawToLocal, m_ExtentsMatrix );
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Calculate the rectangular extents of this graphic.
  215. //-----------------------------------------------------------------------------
  216. void CGeometry::CalculateExtents()
  217. {
  218. if ( !m_bDirtyExtents )
  219. return;
  220. // Now transform our array of positions into local graphic coord system.
  221. int nCount = m_RelativePositions.Count();
  222. CUtlVector< Vector2D > screenPositions;
  223. for ( int i = 0; i < nCount; ++i )
  224. {
  225. // Position
  226. Vector relativePosition( m_RelativePositions[i].x, m_RelativePositions[i].y, 0 );
  227. Vector screenpos;
  228. VectorTransform( relativePosition, m_ExtentsMatrix, screenpos );
  229. screenPositions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );;
  230. }
  231. // left most position is x.
  232. m_Extents.m_TopLeft.x = INT_MAX;
  233. for ( int i = 0; i < screenPositions.Count(); ++i )
  234. {
  235. if ( m_Extents.m_TopLeft.x > screenPositions[i].x )
  236. m_Extents.m_TopLeft.x = screenPositions[i].x;
  237. }
  238. // top most position is y.
  239. m_Extents.m_TopLeft.y = INT_MAX;
  240. for ( int i = 0; i < screenPositions.Count(); ++i )
  241. {
  242. if ( m_Extents.m_TopLeft.y > screenPositions[i].y )
  243. m_Extents.m_TopLeft.y = screenPositions[i].y;
  244. }
  245. // right most position is x
  246. m_Extents.m_BottomRight.x = INT_MIN;
  247. for ( int i = 0; i < screenPositions.Count(); ++i )
  248. {
  249. if ( m_Extents.m_BottomRight.x < screenPositions[i].x )
  250. m_Extents.m_BottomRight.x = screenPositions[i].x;
  251. }
  252. // bottom most position is y
  253. m_Extents.m_BottomRight.y = INT_MIN;
  254. for ( int i = 0; i < screenPositions.Count(); ++i )
  255. {
  256. if ( m_Extents.m_BottomRight.y < screenPositions[i].y )
  257. m_Extents.m_BottomRight.y = screenPositions[i].y;
  258. }
  259. m_bDirtyExtents = false;
  260. }
  261. //-----------------------------------------------------------------------------
  262. // Return the rectangular bounds of this object
  263. //-----------------------------------------------------------------------------
  264. void CGeometry::GetBounds( Rect_t &bounds )
  265. {
  266. bounds.x = m_Extents.m_TopLeft.x;
  267. bounds.y = m_Extents.m_TopLeft.x;
  268. bounds.width = m_Extents.m_BottomRight.x - m_Extents.m_TopLeft.x;
  269. bounds.height = m_Extents.m_BottomRight.y - m_Extents.m_TopLeft.y;
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Purpose: Set the vertex colors of the graphic using the base color and the gradient colors.
  273. //-----------------------------------------------------------------------------
  274. void CGeometry::SetResultantColor( color32 parentColor )
  275. {
  276. SetResultantColor( true, parentColor );
  277. SetResultantColor( false, parentColor );
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose: Set the vertex colors of the graphic using the base color and the gradient colors.
  281. //-----------------------------------------------------------------------------
  282. void CGeometry::SetResultantColor( bool bTop, color32 parentColor )
  283. {
  284. if ( bTop )
  285. {
  286. color32 localColor;
  287. localColor.r = (int)( (float)m_TopColor.r * (float)(m_Color.r/255.0) );
  288. localColor.g = (int)( (float)m_TopColor.g * (float)(m_Color.g/255.0) );
  289. localColor.b = (int)( (float)m_TopColor.b * (float)(m_Color.b/255.0) );
  290. localColor.a = (int)( (float)m_TopColor.a * (float)(m_Color.a/255.0) );
  291. color32 resultantColor;
  292. resultantColor.r = (int)( (float)localColor.r * (float)(parentColor.r/255.0) );
  293. resultantColor.g = (int)( (float)localColor.g * (float)(parentColor.g/255.0) );
  294. resultantColor.b = (int)( (float)localColor.b * (float)(parentColor.b/255.0) );
  295. resultantColor.a = (int)( (float)localColor.a * (float)(parentColor.a/255.0) );
  296. SetTopVerticesColor( resultantColor );
  297. }
  298. else
  299. {
  300. color32 localColor;
  301. localColor.r = (int)( (float)m_BottomColor.r * (float)(m_Color.r/255.0) );
  302. localColor.g = (int)( (float)m_BottomColor.g * (float)(m_Color.g/255.0) );
  303. localColor.b = (int)( (float)m_BottomColor.b * (float)(m_Color.b/255.0) );
  304. localColor.a = (int)( (float)m_BottomColor.a * (float)(m_Color.a/255.0) );
  305. color32 resultantColor;
  306. resultantColor.r = (int)( (float)localColor.r * (float)(parentColor.r/255.0) );
  307. resultantColor.g = (int)( (float)localColor.g * (float)(parentColor.g/255.0) );
  308. resultantColor.b = (int)( (float)localColor.b * (float)(parentColor.b/255.0) );
  309. resultantColor.a = (int)( (float)localColor.a * (float)(parentColor.a/255.0) );
  310. SetBottomVerticesColor( resultantColor );
  311. }
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Note corner colors will be stomped if the base graphic's color changes.
  315. //-----------------------------------------------------------------------------
  316. void CGeometry::SetTopVerticesColor( color32 c )
  317. {
  318. if ( m_bHorizontalGradient )
  319. {
  320. for ( int i = 0; i < m_VertexColors.Count(); i += 4 )
  321. {
  322. m_VertexColors[i] = c;
  323. m_VertexColors[i+3] = c;
  324. }
  325. }
  326. else
  327. {
  328. for ( int i = 0; i < m_VertexColors.Count(); i += 4 )
  329. {
  330. m_VertexColors[i] = c;
  331. m_VertexColors[i+1] = c;
  332. }
  333. }
  334. }
  335. //-----------------------------------------------------------------------------
  336. // Note corner colors will be stomped if the base graphic's color changes.
  337. //-----------------------------------------------------------------------------
  338. void CGeometry::SetBottomVerticesColor( color32 c )
  339. {
  340. if ( m_bHorizontalGradient )
  341. {
  342. for ( int i = 0; i < m_VertexColors.Count(); i += 4 )
  343. {
  344. m_VertexColors[i+1] = c;
  345. m_VertexColors[i+2] = c;
  346. }
  347. }
  348. else
  349. {
  350. for ( int i = 0; i < m_VertexColors.Count(); i += 4 )
  351. {
  352. m_VertexColors[i + 2] = c;
  353. m_VertexColors[i + 3] = c;
  354. }
  355. }
  356. }
  357. //-----------------------------------------------------------------------------
  358. //
  359. //-----------------------------------------------------------------------------
  360. void CGeometry::DrawExtents( CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex, color32 extentLineColor )
  361. {
  362. if ( !m_bVisible )
  363. return;
  364. float lineWidth = 2.0;
  365. // Time to invent some render data to draw this thing.
  366. {
  367. int i = renderGeometryLists[firstListIndex].AddToTail();
  368. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  369. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_TopLeft.y ) );
  370. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 0) );
  371. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  372. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_TopLeft.y ) );
  373. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 0) );
  374. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  375. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_TopLeft.y + lineWidth ) );
  376. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 1) );
  377. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  378. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_TopLeft.y + lineWidth ) );
  379. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 1) );
  380. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  381. // Triangles
  382. CTriangle tri;
  383. tri.m_PointIndex[0] = 0;
  384. tri.m_PointIndex[1] = 1;
  385. tri.m_PointIndex[2] = 2;
  386. renderGeometry.m_Triangles.AddToTail( tri );
  387. tri.m_PointIndex[0] = 0;
  388. tri.m_PointIndex[1] = 2;
  389. tri.m_PointIndex[2] = 3;
  390. renderGeometry.m_Triangles.AddToTail( tri );
  391. // Anim Info
  392. renderGeometry.m_SheetSequenceNumber = 0;
  393. renderGeometry.m_AnimationRate = m_AnimationRate;
  394. renderGeometry.m_bAnimate = false;
  395. renderGeometry.m_AnimStartTime = m_AnimStartTime;
  396. renderGeometry.m_pImageAlias = NULL;
  397. }
  398. {
  399. // Time to invent some render data to draw this thing.
  400. int i = renderGeometryLists[firstListIndex].AddToTail();
  401. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  402. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x - lineWidth, m_Extents.m_TopLeft.y ) );
  403. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 0) );
  404. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  405. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_TopLeft.y ) );
  406. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 0) );
  407. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  408. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_BottomRight.y ) );
  409. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 1) );
  410. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  411. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x - lineWidth, m_Extents.m_BottomRight.y ) );
  412. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 1) );
  413. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  414. // Triangles
  415. CTriangle tri;
  416. tri.m_PointIndex[0] = 0;
  417. tri.m_PointIndex[1] = 1;
  418. tri.m_PointIndex[2] = 2;
  419. renderGeometry.m_Triangles.AddToTail( tri );
  420. tri.m_PointIndex[0] = 0;
  421. tri.m_PointIndex[1] = 2;
  422. tri.m_PointIndex[2] = 3;
  423. renderGeometry.m_Triangles.AddToTail( tri );
  424. // Anim Info
  425. renderGeometry.m_SheetSequenceNumber = 0;
  426. renderGeometry.m_AnimationRate = m_AnimationRate;
  427. renderGeometry.m_bAnimate = false;
  428. renderGeometry.m_AnimStartTime = m_AnimStartTime;
  429. renderGeometry.m_pImageAlias = NULL;
  430. }
  431. {
  432. int i = renderGeometryLists[firstListIndex].AddToTail();
  433. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  434. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_BottomRight.y - lineWidth ) );
  435. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 0) );
  436. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  437. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_BottomRight.y - lineWidth ) );
  438. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 0) );
  439. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  440. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_BottomRight.x, m_Extents.m_BottomRight.y ) );
  441. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 1) );
  442. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  443. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_BottomRight.y ) );
  444. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 1) );
  445. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  446. // Triangles
  447. CTriangle tri;
  448. tri.m_PointIndex[0] = 0;
  449. tri.m_PointIndex[1] = 1;
  450. tri.m_PointIndex[2] = 2;
  451. renderGeometry.m_Triangles.AddToTail( tri );
  452. tri.m_PointIndex[0] = 0;
  453. tri.m_PointIndex[1] = 2;
  454. tri.m_PointIndex[2] = 3;
  455. renderGeometry.m_Triangles.AddToTail( tri );
  456. // Anim Info
  457. renderGeometry.m_SheetSequenceNumber = 0;
  458. renderGeometry.m_AnimationRate = m_AnimationRate;
  459. renderGeometry.m_bAnimate = false;
  460. renderGeometry.m_AnimStartTime = m_AnimStartTime;
  461. renderGeometry.m_pImageAlias = NULL;
  462. }
  463. {
  464. // Time to invent some render data to draw this thing.
  465. int i = renderGeometryLists[firstListIndex].AddToTail();
  466. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  467. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_TopLeft.y ) );
  468. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 0) );
  469. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  470. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x + lineWidth, m_Extents.m_TopLeft.y ) );
  471. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 0) );
  472. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  473. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x + lineWidth, m_Extents.m_BottomRight.y ) );
  474. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 1) );
  475. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  476. renderGeometry.m_Positions.AddToTail( Vector2D( m_Extents.m_TopLeft.x, m_Extents.m_BottomRight.y ) );
  477. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 1) );
  478. renderGeometry.m_VertexColors.AddToTail( extentLineColor );
  479. // Triangles
  480. CTriangle tri;
  481. tri.m_PointIndex[0] = 0;
  482. tri.m_PointIndex[1] = 1;
  483. tri.m_PointIndex[2] = 2;
  484. renderGeometry.m_Triangles.AddToTail( tri );
  485. tri.m_PointIndex[0] = 0;
  486. tri.m_PointIndex[1] = 2;
  487. tri.m_PointIndex[2] = 3;
  488. renderGeometry.m_Triangles.AddToTail( tri );
  489. // Anim Info
  490. renderGeometry.m_SheetSequenceNumber = 0;
  491. renderGeometry.m_AnimationRate = m_AnimationRate;
  492. renderGeometry.m_bAnimate = false;
  493. renderGeometry.m_AnimStartTime = m_AnimStartTime;
  494. renderGeometry.m_pImageAlias = NULL;
  495. }
  496. }
  497. //-----------------------------------------------------------------------------
  498. //
  499. //-----------------------------------------------------------------------------
  500. int CRenderGeometry::GetTriangleCount()
  501. {
  502. return m_Triangles.Count();
  503. }
  504. int CRenderGeometry::GetVertexCount()
  505. {
  506. return m_Positions.Count();
  507. }
  508. DmeTime_t CRenderGeometry::GetAnimStartTime()
  509. {
  510. return m_AnimStartTime;
  511. }
  512. //-----------------------------------------------------------------------------
  513. // Return true if the point x, y lies inside the triangle.
  514. // Triangle points should be supplied in clockwise dir.
  515. //-----------------------------------------------------------------------------
  516. bool PointTriangleHitTest( Vector2D tringleVert0, Vector2D tringleVert1, Vector2D tringleVert2, Vector2D point )
  517. {
  518. // Compute vectors
  519. Vector2D v0 = tringleVert2 - tringleVert0;
  520. Vector2D v1 = tringleVert1 - tringleVert0;
  521. Vector2D v2 = point - tringleVert0;
  522. // Compute dot products
  523. float dot00 = v0.Dot( v0 );
  524. float dot01 = v0.Dot( v1 );
  525. float dot02 = v0.Dot( v2 );
  526. float dot11 = v1.Dot( v1 );
  527. float dot12 = v1.Dot( v2 );
  528. // Compute barycentric coordinates
  529. float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  530. float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  531. float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  532. // Check if point is in triangle
  533. return ( u > 0 ) && ( v > 0 ) && ( u + v < 1 );
  534. }