a00006.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <title>Code Samples</title>
  6. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  7. <link href="doxygen.css" rel="stylesheet" type="text/css"/>
  8. </head>
  9. <body>
  10. <!-- Generated by Doxygen 1.7.4 -->
  11. <div id="top">
  12. <div id="titlearea">
  13. <table cellspacing="0" cellpadding="0">
  14. <tbody>
  15. <tr style="height: 56px;">
  16. <td id="projectlogo"><img alt="Logo" src="logo-mini.png"/></td>
  17. </tr>
  18. </tbody>
  19. </table>
  20. </div>
  21. <div id="navrow1" class="tabs">
  22. <ul class="tablist">
  23. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  24. <li><a href="modules.html"><span>Modules</span></a></li>
  25. <li><a href="namespaces.html"><span>Namespaces</span></a></li>
  26. <li><a href="annotated.html"><span>Classes</span></a></li>
  27. <li><a href="files.html"><span>Files</span></a></li>
  28. </ul>
  29. </div>
  30. <div id="nav-path" class="navpath">
  31. <ul>
  32. <li class="navelem"><a class="el" href="index.html">OpenGL Mathematics</a> </li>
  33. </ul>
  34. </div>
  35. </div>
  36. <div class="header">
  37. <div class="headertitle">
  38. <div class="title">Code Samples </div> </div>
  39. </div>
  40. <div class="contents">
  41. <div class="textblock"><p>This series of samples only shows various GLM functionality.</p>
  42. <h2><a class="anchor" id="sample1"></a>
  43. Compute a Triangle's Normal</h2>
  44. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;glm/glm.hpp&gt;</span> <span class="comment">// vec3 normalize cross</span>
  45. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> computeNormal(
  46. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; a,
  47. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; b,
  48. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; c)
  49. {
  50. <span class="keywordflow">return</span> <a class="code" href="a00238.html#ga07ff16965f11fa17122ac874ed492276" title="Returns a vector in the same direction as x but with length of 1.">glm::normalize</a>(<a class="code" href="a00238.html#ga63272179a066d755953bcfbe38666257" title="Returns the cross product of x and y.">glm::cross</a>(c - a, b - a));
  51. }
  52. </pre></div><p>A potentially faster, but less accurate alternative:</p>
  53. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;glm/glm.hpp&gt;</span> <span class="comment">// vec3 cross</span>
  54. <span class="preprocessor">#include &lt;glm/gtx/fast_square_root.hpp&gt;</span> <span class="comment">// fastNormalize</span>
  55. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> computeNormal(
  56. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; a,
  57. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; b,
  58. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; c)
  59. {
  60. <span class="keywordflow">return</span> <a class="code" href="a00262.html#ga7e785d24d7bbac63f273baf9af1160da" title="Faster than the common normalize function but less accurate.">glm::fastNormalize</a>(<a class="code" href="a00238.html#ga63272179a066d755953bcfbe38666257" title="Returns the cross product of x and y.">glm::cross</a>(c - a, b - a));
  61. }
  62. </pre></div><h2><a class="anchor" id="sample2"></a>
  63. Matrix Transform</h2>
  64. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;glm/glm.hpp&gt;</span> <span class="comment">//vec3, vec4, ivec4, mat4</span>
  65. <span class="preprocessor">#include &lt;glm/gtc/matrix_transform.hpp&gt;</span> <span class="comment">//translate, rotate, scale, perspective </span>
  66. <span class="preprocessor">#include &lt;glm/gtc/type_ptr.hpp&gt;</span> <span class="comment">//value_ptr</span>
  67. <span class="keywordtype">void</span> setUniformMVP(
  68. GLuint Location,
  69. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; Translate,
  70. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; Rotate)
  71. {
  72. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> Projection =
  73. <a class="code" href="a00244.html#ga1bb3652e43f824d8c1dd5b9e60c80437" title="Creates a matrix for a symetric perspective-view frustum.">glm::perspective</a>(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
  74. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> ViewTranslate = <a class="code" href="a00244.html#ga4683c446c8432476750ade56f2537397" title="Builds a translation 4 * 4 matrix created from a vector of 3 components.">glm::translate</a>(
  75. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a>(1.0f),
  76. Translate);
  77. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> ViewRotateX = <a class="code" href="a00244.html#ga48168ff70412019857ceb28b3b2b1f5e" title="Builds a rotation 4 * 4 matrix created from an axis vector and an angle expressed in degrees...">glm::rotate</a>(
  78. ViewTranslate,
  79. Rotate.y, <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a>(-1.0f, 0.0f, 0.0f));
  80. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> View = <a class="code" href="a00244.html#ga48168ff70412019857ceb28b3b2b1f5e" title="Builds a rotation 4 * 4 matrix created from an axis vector and an angle expressed in degrees...">glm::rotate</a>(
  81. ViewRotateX,
  82. Rotate.x, <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a>(0.0f, 1.0f, 0.0f));
  83. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> Model = <a class="code" href="a00244.html#ga6da77ee2c33d0d33de557a37ff35b197" title="Builds a scale 4 * 4 matrix created from 3 scalars.">glm::scale</a>(
  84. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a>(1.0f),
  85. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a>(0.5f));
  86. <a class="code" href="a00018.html" title="Template for 4 * 4 matrix of floating-point numbers.">glm::mat4</a> MVP = Projection * View * Model;
  87. glUniformMatrix4fv(
  88. Location, 1, GL_FALSE, <a class="code" href="a00247.html#gac21518f95a134dbe3c61460c89264b08" title="Get the const address of the vector content.">glm::value_ptr</a>(MVP));
  89. }
  90. </pre></div><h2><a class="anchor" id="sample3"></a>
  91. Vector Types</h2>
  92. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;glm/glm.hpp&gt;</span> <span class="comment">//vec2</span>
  93. <span class="preprocessor">#include &lt;glm/gtc/type_precision.hpp&gt;</span> <span class="comment">//hvec2, i8vec2, i32vec2</span>
  94. std::size_t <span class="keyword">const</span> VertexCount = 4;
  95. <span class="comment">// Float quad geometry</span>
  96. std::size_t <span class="keyword">const</span> PositionSizeF32 = VertexCount * <span class="keyword">sizeof</span>(<a class="code" href="a00235.html#gae0d1080e37fc58255cac2d521704ec60" title="2 components vector of floating-point numbers.">glm::vec2</a>);
  97. <a class="code" href="a00020.html" title="The basic 2D vector type.">glm::vec2</a> <span class="keyword">const</span> PositionDataF32[VertexCount] =
  98. {
  99. <a class="code" href="a00235.html#gae0d1080e37fc58255cac2d521704ec60" title="2 components vector of floating-point numbers.">glm::vec2</a>(-1.0f,-1.0f),
  100. <a class="code" href="a00235.html#gae0d1080e37fc58255cac2d521704ec60" title="2 components vector of floating-point numbers.">glm::vec2</a>( 1.0f,-1.0f),
  101. <a class="code" href="a00235.html#gae0d1080e37fc58255cac2d521704ec60" title="2 components vector of floating-point numbers.">glm::vec2</a>( 1.0f, 1.0f),
  102. <a class="code" href="a00235.html#gae0d1080e37fc58255cac2d521704ec60" title="2 components vector of floating-point numbers.">glm::vec2</a>(-1.0f, 1.0f)
  103. };
  104. <span class="comment">// Half-float quad geometry</span>
  105. std::size_t <span class="keyword">const</span> PositionSizeF16 = VertexCount * <span class="keyword">sizeof</span>(<a class="code" href="a00240.html#ga2cd0b3fe113dc1aa2ee750a85f77c50a" title="Vector of 2 half-precision floating-point numbers.">glm::hvec2</a>);
  106. <a class="code" href="a00020.html" title="The basic 2D vector type.">glm::hvec2</a> <span class="keyword">const</span> PositionDataF16[VertexCount] =
  107. {
  108. <a class="code" href="a00240.html#ga2cd0b3fe113dc1aa2ee750a85f77c50a" title="Vector of 2 half-precision floating-point numbers.">glm::hvec2</a>(-1.0f, -1.0f),
  109. <a class="code" href="a00240.html#ga2cd0b3fe113dc1aa2ee750a85f77c50a" title="Vector of 2 half-precision floating-point numbers.">glm::hvec2</a>( 1.0f, -1.0f),
  110. <a class="code" href="a00240.html#ga2cd0b3fe113dc1aa2ee750a85f77c50a" title="Vector of 2 half-precision floating-point numbers.">glm::hvec2</a>( 1.0f, 1.0f),
  111. <a class="code" href="a00240.html#ga2cd0b3fe113dc1aa2ee750a85f77c50a" title="Vector of 2 half-precision floating-point numbers.">glm::hvec2</a>(-1.0f, 1.0f)
  112. };
  113. <span class="comment">// 8 bits signed integer quad geometry</span>
  114. std::size_t <span class="keyword">const</span> PositionSizeI8 = VertexCount * <span class="keyword">sizeof</span>(<a class="code" href="a00246.html#ga9ab3f26fa232d5a4d2397d7904eb9069" title="8bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i8vec2</a>);
  115. <a class="code" href="a00020.html" title="The basic 2D vector type.">glm::i8vec2</a> <span class="keyword">const</span> PositionDataI8[VertexCount] =
  116. {
  117. <a class="code" href="a00246.html#ga9ab3f26fa232d5a4d2397d7904eb9069" title="8bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i8vec2</a>(-1,-1),
  118. <a class="code" href="a00246.html#ga9ab3f26fa232d5a4d2397d7904eb9069" title="8bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i8vec2</a>( 1,-1),
  119. <a class="code" href="a00246.html#ga9ab3f26fa232d5a4d2397d7904eb9069" title="8bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i8vec2</a>( 1, 1),
  120. <a class="code" href="a00246.html#ga9ab3f26fa232d5a4d2397d7904eb9069" title="8bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i8vec2</a>(-1, 1)
  121. };
  122. <span class="comment">// 32 bits signed integer quad geometry</span>
  123. std::size_t <span class="keyword">const</span> PositionSizeI32 = VertexCount * <span class="keyword">sizeof</span>(<a class="code" href="a00246.html#ga587a33330386e50f7ff9a870f1b62ab1" title="32bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i32vec2</a>);
  124. <a class="code" href="a00020.html" title="The basic 2D vector type.">glm::i32vec2</a> <span class="keyword">const</span> PositionDataI32[VertexCount] =
  125. {
  126. <a class="code" href="a00246.html#ga587a33330386e50f7ff9a870f1b62ab1" title="32bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i32vec2</a> (-1,-1),
  127. <a class="code" href="a00246.html#ga587a33330386e50f7ff9a870f1b62ab1" title="32bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i32vec2</a> ( 1,-1),
  128. <a class="code" href="a00246.html#ga587a33330386e50f7ff9a870f1b62ab1" title="32bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i32vec2</a> ( 1, 1),
  129. <a class="code" href="a00246.html#ga587a33330386e50f7ff9a870f1b62ab1" title="32bit signed integer vector of 2 components. (from GLM_GTC_type_precision extension)">glm::i32vec2</a> (-1, 1)
  130. };
  131. </pre></div><h2><a class="anchor" id="sample4"></a>
  132. Lighting</h2>
  133. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;glm/glm.hpp&gt;</span> <span class="comment">// vec3 normalize reflect dot pow</span>
  134. <span class="preprocessor">#include &lt;glm/gtx/random.hpp&gt;</span> <span class="comment">// vecRand3</span>
  135. <span class="comment">// vecRand3, generate a random and equiprobable normalized vec3</span>
  136. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> lighting(
  137. intersection <span class="keyword">const</span> &amp; Intersection,
  138. material <span class="keyword">const</span> &amp; Material,
  139. light <span class="keyword">const</span> &amp; Light,
  140. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> <span class="keyword">const</span> &amp; View)
  141. {
  142. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> Color = <a class="code" href="a00235.html#gac422c7058a7c9963b55ad70764c62752" title="3 components vector of floating-point numbers.">glm::vec3</a>(0.0f);
  143. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> LightVertor = <a class="code" href="a00238.html#ga07ff16965f11fa17122ac874ed492276" title="Returns a vector in the same direction as x but with length of 1.">glm::normalize</a>(
  144. Light.position() - Intersection.globalPosition() +
  145. <a class="code" href="a00287.html#ga63249d58e1327a83638f1cf8b421a0de" title="Generate a random normalized 3 component vector. It&#39;s a spherical uniform distribution. (From GLM_GTX_random extension)">glm::vecRand3</a>(0.0f, Light.inaccuracy());
  146. <span class="keywordflow">if</span>(!shadow(
  147. Intersection.globalPosition(),
  148. Light.position(),
  149. LightVertor))
  150. {
  151. <span class="keywordtype">float</span> Diffuse = <a class="code" href="a00238.html#ga38e1f1538ac0fedc9d6ac14910105421" title="Returns the dot product of x and y, i.e., result = x * y.">glm::dot</a>(Intersection.normal(), LightVector);
  152. <span class="keywordflow">if</span>(Diffuse &lt;= 0.0f)
  153. <span class="keywordflow">return</span> Color;
  154. <span class="keywordflow">if</span>(Material.isDiffuse())
  155. Color += Light.color() * Material.diffuse() * Diffuse;
  156. <span class="keywordflow">if</span>(Material.isSpecular())
  157. {
  158. <a class="code" href="a00021.html" title="Basic 3D vector type.">glm::vec3</a> Reflect = <a class="code" href="a00238.html#ga2863d2331eb3752a5a17244c604c1d07" title="For the incident vector I and surface orientation N, returns the reflection direction : result = I - ...">glm::reflect</a>(
  159. -LightVector,
  160. Intersection.normal());
  161. <span class="keywordtype">float</span> Dot = <a class="code" href="a00238.html#ga38e1f1538ac0fedc9d6ac14910105421" title="Returns the dot product of x and y, i.e., result = x * y.">glm::dot</a>(Reflect, View);
  162. <span class="keywordtype">float</span> Base = Dot &gt; 0.0f ? Dot : 0.0f;
  163. <span class="keywordtype">float</span> Specular = <a class="code" href="a00238.html#ga8eb76dfbf0fed3397c0327915279f06f" title="Returns x raised to the y power.">glm::pow</a>(Base, Material.exponent());
  164. Color += Material.specular() * Specular;
  165. }
  166. <span class="keywordflow">return</span> Color;
  167. }
  168. </pre></div> </div></div>
  169. <hr class="footer"/><address class="footer"><small>Generated by&#160;
  170. <a href="http://www.doxygen.org/index.html">
  171. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
  172. </body>
  173. </html>