Geometry.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma once
  2. //========================================================================
  3. // Geometry.h : Collection of code for 3D math and 3D shapes
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. //========================================================================
  40. // Content References in Game Coding Complete 3rd Edition
  41. //
  42. // class glm::vec3 - Chapter 14, page 456
  43. // class Vec4 - Chapter 14, page 457
  44. // class Mat4x4 - Chapter 14, page 466
  45. // class Quaternion - Chapter 14, page 471
  46. // class Plane - Chapter 14, page 473
  47. // class Frustum - Chapter 14, page 474
  48. //
  49. #include <cmath>
  50. #include <glm/glm.hpp>
  51. #include <glm/gtc/matrix_transform.hpp>
  52. #include <iostream>
  53. #include <glm/gtx/string_cast.hpp>
  54. const float GCC_PI = 3.14159265358979f;
  55. const float GCC_2PI = 2 * GCC_PI;
  56. extern glm::vec3 g_Up;
  57. extern glm::vec3 g_Right;
  58. extern glm::vec3 g_Forward;
  59. extern glm::vec4 g_Up4;
  60. extern glm::vec4 g_Right4;
  61. extern glm::vec4 g_Forward4;
  62. namespace geometry
  63. {
  64. inline glm::vec3 GetPosition(glm::mat4 matrix)
  65. {
  66. return glm::vec3(matrix[3]);
  67. }
  68. inline void SetPosition(glm::mat4 &matrix, glm::vec3 position)
  69. {
  70. matrix[3] = glm::vec4(position, matrix[3].w);
  71. }
  72. inline glm::vec3 GetDirection(glm::mat4 m)
  73. {
  74. // zero out position so we just have rotation component
  75. SetPosition(m, glm::vec3(0.0f));
  76. glm::vec4 forward = m * g_Forward4;
  77. return glm::vec3(forward);
  78. }
  79. inline glm::mat4 BuildRotationX(float const radians)
  80. {
  81. return glm::rotate(glm::mat4(1.0f), radians, glm::vec3(1.0f, 0.0f, 0.0f));
  82. }
  83. inline glm::mat4 BuildRotationY(float const radians)
  84. {
  85. return glm::rotate(glm::mat4(1.0f), radians, glm::vec3(0.0f, 1.0f, 0.0f));
  86. }
  87. inline glm::mat4 BuildRotationZ(float const radians)
  88. {
  89. return glm::rotate(glm::mat4(1.0f), radians, glm::vec3(0.0f, 0.0f, 1.0f));
  90. }
  91. inline glm::mat4 BuildTranslation(glm::vec3 const translation)
  92. {
  93. return glm::translate(glm::mat4(1.0f), translation);
  94. }
  95. inline glm::mat4 BuildYawPitchRoll(float const yawRadians, float const pitchRadians, float const rollRadians)
  96. {
  97. // [munnellg] taken from page 277 of 3d math primer for graphics and game development
  98. float cy = cos(yawRadians);
  99. float sy = sin(yawRadians);
  100. float cp = cos(pitchRadians);
  101. float sp = sin(pitchRadians);
  102. float cr = cos(rollRadians);
  103. float sr = sin(rollRadians);
  104. glm::mat4 rotation = glm::mat4(
  105. cy * cr + sy * sp * sr, sr * cp, -sy * cr + cy * sp * sr, 0.0f,
  106. -cy * sr + sy * sp * cr, cr * cp, sr * sy + cy * sp * cr, 0.0f,
  107. sy * cp, -sp, cy * cp, 0.0f,
  108. 0, 0, 0, 1.0f
  109. );
  110. return rotation;
  111. }
  112. inline glm::vec3 GetYawPitchRoll(const glm::mat4 &orientation)
  113. {
  114. float yaw, pitch, roll;
  115. pitch = std::asin(-orientation[2][1]);
  116. double threshold = 0.001; // Hardcoded constant - burn him, he's a witch
  117. double test = std::cos(pitch);
  118. if (test > threshold)
  119. {
  120. roll = std::atan2(orientation[0][1], orientation[1][1]);
  121. yaw = std::atan2(orientation[2][0], orientation[2][2]);
  122. }
  123. else
  124. {
  125. roll = std::atan2(-orientation[1][0], orientation[0][0]);
  126. yaw = 0.0f;
  127. }
  128. return glm::vec3(yaw, pitch, roll);
  129. }
  130. };
  131. ////////////////////////////////////////////////////
  132. //
  133. // D3D9 Vertex Type Definitions
  134. //
  135. // TRANSFORMED_VERTEX Description
  136. // UNTRANSFORMED_VERTEX Description
  137. // UNTRANSFORMED_LIT_VERTEX Description
  138. // D3D9Vertex_UnlitColored Description
  139. // D3D9Vertex_ColoredTextured Description
  140. // D3D9Vertex_Colored Description
  141. //
  142. // Note: There's been a slight change from the book in this code.
  143. // Instead of #define D3DFVF_BlahBlah they are static constants;
  144. // find them at the top of Geometry.cpp
  145. //
  146. // See Game Coding Complete, 3rd Edition, Chapter 13, page 440-444
  147. ////////////////////////////////////////////////////
  148. struct SimpleVertex
  149. {
  150. glm::vec3 Pos;
  151. glm::vec3 Normal;
  152. };
  153. struct VertexUnlitTextured
  154. {
  155. glm::vec3 Pos;
  156. glm::vec3 Normal;
  157. glm::vec2 Uv;
  158. };
  159. struct VertexUnlitTexturedColored
  160. {
  161. glm::vec3 Pos;
  162. glm::vec3 Normal;
  163. glm::vec3 Diffuse;
  164. glm::vec2 Uv;
  165. };
  166. struct VertexPositionColored
  167. {
  168. glm::vec3 Pos;
  169. glm::vec3 Diffuse;
  170. };
  171. ////////////////////////////////////////////////////
  172. //
  173. // Plane Definition
  174. //
  175. ////////////////////////////////////////////////////
  176. class Plane
  177. {
  178. };
  179. ////////////////////////////////////////////////////
  180. //
  181. // Frustum Definition
  182. //
  183. ////////////////////////////////////////////////////
  184. class Frustum
  185. {
  186. public:
  187. enum Side { Near, Far, Top, Right, Bottom, Left, NumPlanes };
  188. Plane m_Planes[NumPlanes]; // planes of the frustum in camera space
  189. glm::vec3 m_NearClip[4]; // verts of the near clip planes in camera space
  190. glm::vec3 m_FarClip[4]; // verts of the far clip plane in camera space
  191. float m_Fov; // field of view in radians
  192. float m_Aspect; // aspect ratio - width divided by height
  193. float m_Near; // near clipping distance
  194. float m_Far; // far clipping distance
  195. public:
  196. Frustum();
  197. bool Inside(const glm::vec3 &point) const;
  198. bool Inside(const glm::vec3 &point, const float radius) const;
  199. const Plane &Get(Side side) { return m_Planes[side]; }
  200. void SetFOV(float fov) { m_Fov=fov; Init(m_Fov, m_Aspect, m_Near, m_Far); }
  201. void SetAspect(float aspect) { m_Aspect=aspect; Init(m_Fov, m_Aspect, m_Near, m_Far); }
  202. void SetNear(float near) { m_Near=near; Init(m_Fov, m_Aspect, m_Near, m_Far); }
  203. void SetFar(float far) { m_Far=far; Init(m_Fov, m_Aspect, m_Near, m_Far); }
  204. void Init(const float fov, const float aspect, const float near, const float far);
  205. void Render();
  206. };