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.

355 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "BoundBox.h"
  9. #include "Gizmo.h"
  10. #include "Render3D.h"
  11. #include "Resource.h"
  12. #include "materialsystem/IMesh.h"
  13. #include "TextureSystem.h"
  14. #include "camera.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include <tier0/memdbgon.h>
  17. static IEditorTexture* g_pAxisTexture = 0;
  18. static IEditorTexture* g_pRotateHandleTexture = 0;
  19. static IEditorTexture* g_pScaleHandleTexture = 0;
  20. static IEditorTexture* g_pTranslateHandleTexture = 0;
  21. //-----------------------------------------------------------------------------
  22. // Purpose:
  23. //-----------------------------------------------------------------------------
  24. CGizmo::CGizmo(void)
  25. {
  26. static bool bFirst = true;
  27. if (bFirst)
  28. {
  29. g_pAxisTexture = g_Textures.FindActiveTexture("editor/gizmoAxis");
  30. g_pRotateHandleTexture = g_Textures.FindActiveTexture("editor/gizmoRotateHandle");
  31. g_pScaleHandleTexture = g_Textures.FindActiveTexture("editor/gizmoScaleHandle");
  32. g_pTranslateHandleTexture = g_Textures.FindActiveTexture("editor/gizmoTranslateHandle");
  33. bFirst = false;
  34. }
  35. Initialize();
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose:
  39. // Input : x -
  40. // y -
  41. // z -
  42. //-----------------------------------------------------------------------------
  43. CGizmo::CGizmo(float x, float y, float z)
  44. {
  45. Initialize();
  46. m_Position[0] = x;
  47. m_Position[1] = y;
  48. m_Position[2] = z;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. //-----------------------------------------------------------------------------
  53. void CGizmo::Initialize(void)
  54. {
  55. m_Position[0] = m_Position[1] = m_Position[2] = 0;
  56. m_fAxisLength = 100;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. // Input : ViewPoint -
  61. // Origin -
  62. // EndPoint -
  63. // red -
  64. // green -
  65. // blue -
  66. // Output :
  67. //-----------------------------------------------------------------------------
  68. #define GIZMO_AXIS_WIDTH 2
  69. #define GIZMO_HANDLE_WIDTH 4
  70. void CGizmo::DrawGizmoAxis(CRender3D *pRender, Vector& Origin,
  71. Vector& EndPoint, int red, int green, int blue,
  72. unsigned int uAxisHandle)
  73. {
  74. CCamera *pCamera = pRender->GetCamera();
  75. Vector ViewUp;
  76. pCamera->GetViewUp(ViewUp);
  77. Vector ViewPoint;
  78. Vector ViewForward;
  79. pCamera->GetViewPoint(ViewPoint);
  80. VectorSubtract(Origin, ViewPoint, ViewForward);
  81. Vector Axis;
  82. VectorSubtract(EndPoint, Origin, Axis);
  83. CrossProduct(ViewForward, Axis, ViewUp);
  84. VectorNormalize(ViewUp);
  85. Vector Start;
  86. Vector End;
  87. //
  88. // Draw the first segment of the gizmo axis.
  89. //
  90. VectorMA(Origin, 0.1, Axis, Start);
  91. VectorMA(Origin, 0.25, Axis, End);
  92. pRender->BindTexture( g_pAxisTexture );
  93. CMeshBuilder meshBuilder;
  94. CMatRenderContextPtr pRenderContext( MaterialSystemInterface() );
  95. IMesh* pMesh = pRenderContext->GetDynamicMesh( );
  96. meshBuilder.Begin( pMesh, MATERIAL_POLYGON, 4 );
  97. meshBuilder.TexCoord2f(0, 0, 0);
  98. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  99. meshBuilder.Color3ub(red, green, blue);
  100. meshBuilder.AdvanceVertex();
  101. meshBuilder.TexCoord2f(0, 2, 0);
  102. meshBuilder.Position3f(End[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  103. meshBuilder.Color3ub(red, green, blue);
  104. meshBuilder.AdvanceVertex();
  105. meshBuilder.TexCoord2f(0, 2, 1);
  106. meshBuilder.Position3f(End[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  107. meshBuilder.Color3ub(red, green, blue);
  108. meshBuilder.AdvanceVertex();
  109. meshBuilder.TexCoord2f(0, 0, 1);
  110. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  111. meshBuilder.Color3ub(red, green, blue);
  112. meshBuilder.AdvanceVertex();
  113. meshBuilder.End();
  114. pMesh->Draw();
  115. //
  116. // Draw the scale handle.
  117. //
  118. Start = End;
  119. VectorMA(Origin, 0.35, Axis, End);
  120. pRender->BeginRenderHitTarget(this, uAxisHandle + GIZMO_HANDLE_SCALE);
  121. pRender->BindTexture( g_pScaleHandleTexture );
  122. pMesh = pRenderContext->GetDynamicMesh( );
  123. meshBuilder.Begin( pMesh, MATERIAL_POLYGON, 4 );
  124. meshBuilder.TexCoord2f( 0, 0, 0);
  125. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] - ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] - ViewUp[2] * GIZMO_HANDLE_WIDTH);
  126. meshBuilder.Color3ub(red, green, blue);
  127. meshBuilder.AdvanceVertex();
  128. meshBuilder.TexCoord2f( 0, 1, 0);
  129. meshBuilder.Position3f(End[0] - ViewUp[0] * GIZMO_HANDLE_WIDTH, End[1] - ViewUp[1] * GIZMO_HANDLE_WIDTH, End[2] - ViewUp[2] * GIZMO_HANDLE_WIDTH);
  130. meshBuilder.Color3ub(red, green, blue);
  131. meshBuilder.AdvanceVertex();
  132. meshBuilder.TexCoord2f( 0, 1, 1);
  133. meshBuilder.Position3f(End[0] + ViewUp[0] * GIZMO_HANDLE_WIDTH, End[1] + ViewUp[1] * GIZMO_HANDLE_WIDTH, End[2] + ViewUp[2] * GIZMO_HANDLE_WIDTH);
  134. meshBuilder.Color3ub(red, green, blue);
  135. meshBuilder.AdvanceVertex();
  136. meshBuilder.TexCoord2f( 0, 0, 1);
  137. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] + ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] + ViewUp[2] * GIZMO_HANDLE_WIDTH);
  138. meshBuilder.AdvanceVertex();
  139. meshBuilder.End();
  140. pMesh->Draw();
  141. pRender->EndRenderHitTarget();
  142. //
  143. // Draw the second segment of the gizmo axis.
  144. //
  145. Start = End;
  146. VectorMA(Origin, 0.5, Axis, End);
  147. pRender->BindTexture( g_pAxisTexture );
  148. pMesh = pRenderContext->GetDynamicMesh( );
  149. meshBuilder.Begin( pMesh, MATERIAL_POLYGON, 4 );
  150. meshBuilder.TexCoord2f( 0, 0, 0);
  151. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  152. meshBuilder.Color3ub(red, green, blue);
  153. meshBuilder.AdvanceVertex();
  154. meshBuilder.TexCoord2f( 0, 2, 0);
  155. meshBuilder.Position3f(End[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  156. meshBuilder.AdvanceVertex();
  157. meshBuilder.Color3ub(red, green, blue);
  158. meshBuilder.TexCoord2f( 0, 2, 1);
  159. meshBuilder.Position3f(End[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  160. meshBuilder.Color3ub(red, green, blue);
  161. meshBuilder.AdvanceVertex();
  162. meshBuilder.TexCoord2f( 0, 0, 1);
  163. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  164. meshBuilder.Color3ub(red, green, blue);
  165. meshBuilder.AdvanceVertex();
  166. meshBuilder.End();
  167. pMesh->Draw();
  168. //
  169. // Draw the rotate handle.
  170. //
  171. Start = End;
  172. VectorMA(Origin, 0.6, Axis, End);
  173. pRender->BeginRenderHitTarget(this, uAxisHandle + GIZMO_HANDLE_ROTATE);
  174. pRender->BindTexture( g_pRotateHandleTexture );
  175. pMesh = pRenderContext->GetDynamicMesh( );
  176. meshBuilder.Begin( pMesh, MATERIAL_POLYGON, 4 );
  177. meshBuilder.TexCoord2f( 0, 0, 0);
  178. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] - ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] - ViewUp[2] * GIZMO_HANDLE_WIDTH);
  179. meshBuilder.Color3ub(red, green, blue);
  180. meshBuilder.AdvanceVertex();
  181. meshBuilder.TexCoord2f( 0, 1, 0);
  182. meshBuilder.Position3f(End[0] - ViewUp[0] * GIZMO_HANDLE_WIDTH, End[1] - ViewUp[1] * GIZMO_HANDLE_WIDTH, End[2] - ViewUp[2] * GIZMO_HANDLE_WIDTH);
  183. meshBuilder.Color3ub(red, green, blue);
  184. meshBuilder.AdvanceVertex();
  185. meshBuilder.TexCoord2f( 0, 1, 1);
  186. meshBuilder.Position3f(End[0] + ViewUp[0] * GIZMO_HANDLE_WIDTH, End[1] + ViewUp[1] * GIZMO_HANDLE_WIDTH, End[2] + ViewUp[2] * GIZMO_HANDLE_WIDTH);
  187. meshBuilder.Color3ub(red, green, blue);
  188. meshBuilder.AdvanceVertex();
  189. meshBuilder.TexCoord2f( 0, 0, 1);
  190. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] + ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] + ViewUp[2] * GIZMO_HANDLE_WIDTH);
  191. meshBuilder.Color3ub(red, green, blue);
  192. meshBuilder.AdvanceVertex();
  193. meshBuilder.End();
  194. pMesh->Draw();
  195. pRender->EndRenderHitTarget();
  196. //
  197. // Draw the third segment of the gizmo axis.
  198. //
  199. Start = End;
  200. VectorMA(Origin, 0.75, Axis, End);
  201. pRender->BindTexture( g_pAxisTexture );
  202. pMesh = pRenderContext->GetDynamicMesh( );
  203. meshBuilder.Begin( pMesh, MATERIAL_POLYGON, 4 );
  204. meshBuilder.TexCoord2f( 0, 0, 0);
  205. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  206. meshBuilder.Color3ub(red, green, blue);
  207. meshBuilder.AdvanceVertex();
  208. meshBuilder.TexCoord2f( 0, 2, 0);
  209. meshBuilder.Position3f(End[0] - ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] - ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] - ViewUp[2] * GIZMO_AXIS_WIDTH);
  210. meshBuilder.Color3ub(red, green, blue);
  211. meshBuilder.AdvanceVertex();
  212. meshBuilder.TexCoord2f( 0, 2, 1);
  213. meshBuilder.Position3f(End[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, End[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, End[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  214. meshBuilder.Color3ub(red, green, blue);
  215. meshBuilder.AdvanceVertex();
  216. meshBuilder.TexCoord2f( 0, 0, 1);
  217. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_AXIS_WIDTH, Start[1] + ViewUp[1] * GIZMO_AXIS_WIDTH, Start[2] + ViewUp[2] * GIZMO_AXIS_WIDTH);
  218. meshBuilder.Color3ub(red, green, blue);
  219. meshBuilder.AdvanceVertex();
  220. meshBuilder.End();
  221. pMesh->Draw();
  222. //
  223. // Draw the translate handle (arrowhead).
  224. //
  225. Start = End;
  226. pRender->BeginRenderHitTarget(this, uAxisHandle + GIZMO_HANDLE_TRANSLATE);
  227. pRender->BindTexture( g_pTranslateHandleTexture );
  228. pMesh = pRenderContext->GetDynamicMesh( );
  229. meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, 1 );
  230. meshBuilder.TexCoord2f( 0, 0, 0);
  231. meshBuilder.Position3f(Start[0] - ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] - ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] - ViewUp[2] * GIZMO_HANDLE_WIDTH);
  232. meshBuilder.Color3ub(red, green, blue);
  233. meshBuilder.AdvanceVertex();
  234. meshBuilder.TexCoord2f( 0, 1, 0.5);
  235. meshBuilder.Position3f(EndPoint[0], EndPoint[1], EndPoint[2]);
  236. meshBuilder.Color3ub(red, green, blue);
  237. meshBuilder.AdvanceVertex();
  238. meshBuilder.TexCoord2f( 0, 0, 1);
  239. meshBuilder.Position3f(Start[0] + ViewUp[0] * GIZMO_HANDLE_WIDTH, Start[1] + ViewUp[1] * GIZMO_HANDLE_WIDTH, Start[2] + ViewUp[2] * GIZMO_HANDLE_WIDTH);
  240. meshBuilder.Color3ub(red, green, blue);
  241. meshBuilder.AdvanceVertex();
  242. meshBuilder.End();
  243. pMesh->Draw();
  244. pRender->EndRenderHitTarget();
  245. }
  246. //-----------------------------------------------------------------------------
  247. // Purpose:
  248. // Input : *pRender -
  249. //-----------------------------------------------------------------------------
  250. void CGizmo::Render(CRender3D *pRender)
  251. {
  252. Vector XAxis( m_Position[0] + m_fAxisLength, m_Position[1], m_Position[2] );
  253. Vector YAxis( m_Position[0], m_Position[1] + m_fAxisLength, m_Position[2] );
  254. Vector ZAxis( m_Position[0], m_Position[1], m_Position[2] + m_fAxisLength );
  255. static BoundBox UniformScaleBox;
  256. Vector Mins;
  257. Vector Maxs;
  258. Mins[0] = m_Position[0] - m_fAxisLength * 0.1;
  259. Mins[1] = m_Position[1] - m_fAxisLength * 0.1;
  260. Mins[2] = m_Position[2] - m_fAxisLength * 0.1;
  261. Maxs[0] = m_Position[0] + m_fAxisLength * 0.1;
  262. Maxs[1] = m_Position[1] + m_fAxisLength * 0.1;
  263. Maxs[2] = m_Position[2] + m_fAxisLength * 0.1;
  264. UniformScaleBox.ResetBounds();
  265. UniformScaleBox.UpdateBounds(Mins, Maxs);
  266. pRender->BeginRenderHitTarget(this, GIZMO_HANDLE_UNIFORM_SCALE);
  267. //pRender->RenderBox(Mins, Maxs, BoxType_Solid, 200, 200, 200);
  268. pRender->EndRenderHitTarget();
  269. pRender->SetRenderMode( RENDER_MODE_TEXTURED );
  270. DrawGizmoAxis(pRender, m_Position, XAxis, 255, 0, 0, GIZMO_AXIS_X);
  271. DrawGizmoAxis(pRender, m_Position, YAxis, 0, 255, 0, GIZMO_AXIS_Y);
  272. DrawGizmoAxis(pRender, m_Position, ZAxis, 0, 0, 255, GIZMO_AXIS_Z);
  273. pRender->SetRenderMode( RENDER_MODE_DEFAULT );
  274. }