TransformComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //========================================================================
  2. // TransformComponent.cpp - Component for managing transforms on actors
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. #include "../Unix/GameCodeStd.h"
  39. #include "TransformComponent.h"
  40. #include "../Graphics3D/Geometry.h"
  41. #include "../Utilities/String.h"
  42. #include <glm/gtx/string_cast.hpp>
  43. const char* TransformComponent::g_Name = "TransformComponent";
  44. bool TransformComponent::VInit(TiXmlElement* pData)
  45. {
  46. GCC_ASSERT(pData);
  47. // [mrmike] - this was changed post-press - because changes to the TransformComponents can come in partial definitions,
  48. // such as from the editor, its better to grab the current values rather than clear them out.
  49. // glm::vec3 yawPitchRoll = m_transform.GetYawPitchRoll();
  50. // yawPitchRoll.x = glm::degrees(yawPitchRoll.x);
  51. // yawPitchRoll.y = glm::degrees(yawPitchRoll.y);
  52. // yawPitchRoll.z = glm::degrees(yawPitchRoll.z);
  53. glm::vec3 yawPitchRoll;
  54. glm::vec3 position = geometry::GetPosition(m_transform);
  55. TiXmlElement* pPositionElement = pData->FirstChildElement("Position");
  56. if (pPositionElement)
  57. {
  58. double x = 0.0;
  59. double y = 0.0;
  60. double z = 0.0;
  61. pPositionElement->Attribute("x", &x);
  62. pPositionElement->Attribute("y", &y);
  63. pPositionElement->Attribute("z", &z);
  64. position = glm::vec3(x, y, z);
  65. }
  66. TiXmlElement* pOrientationElement = pData->FirstChildElement("YawPitchRoll");
  67. if (pOrientationElement)
  68. {
  69. double yaw = 0.0;
  70. double pitch = 0.0;
  71. double roll = 0.0;
  72. pOrientationElement->Attribute("x", &yaw);
  73. pOrientationElement->Attribute("y", &pitch);
  74. pOrientationElement->Attribute("z", &roll);
  75. yawPitchRoll = glm::vec3(yaw, pitch, roll);
  76. }
  77. glm::mat4 translation = geometry::BuildTranslation(position);
  78. glm::mat4 rotation = geometry::BuildYawPitchRoll(
  79. glm::radians(yawPitchRoll.x),
  80. glm::radians(yawPitchRoll.y),
  81. glm::radians(yawPitchRoll.z)
  82. );
  83. m_transform = rotation * translation;
  84. return true;
  85. }
  86. TiXmlElement* TransformComponent::VGenerateXml(void)
  87. {
  88. TiXmlElement* pBaseElement = GCC_NEW TiXmlElement(VGetName());
  89. // initial transform -> position
  90. TiXmlElement *pPosition = GCC_NEW TiXmlElement("Position");
  91. glm::vec3 pos(geometry::GetPosition(m_transform));
  92. pPosition->SetAttribute("x", ToStr(pos.x).c_str());
  93. pPosition->SetAttribute("y", ToStr(pos.y).c_str());
  94. pPosition->SetAttribute("z", ToStr(pos.z).c_str());
  95. // initial transform -> LookAt
  96. TiXmlElement* pDirection = GCC_NEW TiXmlElement("YawPitchRoll");
  97. glm::vec3 orient(geometry::GetYawPitchRoll(m_transform));
  98. orient.x = glm::degrees(orient.x);
  99. orient.y = glm::degrees(orient.y);
  100. orient.z = glm::degrees(orient.z);
  101. pDirection->SetAttribute("x", ToStr(orient.x).c_str());
  102. pDirection->SetAttribute("y", ToStr(orient.y).c_str());
  103. pDirection->SetAttribute("z", ToStr(orient.z).c_str());
  104. pBaseElement->LinkEndChild(pDirection);
  105. return pBaseElement;
  106. }