1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html><head><title>OpenGL Mathematics: Code</title><meta http-equiv="Content-Language" content="en" /><meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" /><meta name="copyright" content="G-Truc Creation" /><link href="./common/style.css" rel="stylesheet" media="screen, print, embossed" type="text/css" /><script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20182250-1']);
_gaq.push(['_setDomainName', '.g-truc.net']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></head><body><table><tr><td class="menu"><div class="menu1"><br /><div><a href="./goodies/logo1920x1200.png"><img class="menu-img" src="./common/logo.png" alt="GLM Logo" /></a></div><br /><div><a class="menu" href="https://sourceforge.net/projects/ogl-math/files/glm-0.9.3.3/glm-0.9.3.3.zip/download">
Download GLM 0.9.3.3</a></div></div><br /><div class="menu2"><a href="./index.html">Front page</a></div><div class="menu2"><a href="./download.html">Downloads</a></div><div class="menu2"><a href="http://www.opengl.org/sdk/libs/GLM/">OpenGL SDK page</a></div><br /><div class="menu2"><a href="./glm.pdf">GLM Manual</a></div><div class="menu2"><a href="./api/index.html">GLM API</a></div><div class="menu2"><a href="./code.html">Code samples</a></div><div class="menu2"><a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.11.pdf">GLSL Specification</a></div><div class="menu2"><a href="http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=postlist&Board=10&page=1">OpenGL.org Toolkits forum</a></div><br /><div class="menu2"><a href="https://sourceforge.net/apps/trac/ogl-math/newticket">Report a bug</a></div><div class="menu2"><a href="https://sourceforge.net/projects/ogl-math/">SourceForge page</a></div><div class="menu2"><a href="http://www.g-truc.net/project-0016.html#menu">G-Truc Creation page</a></div><div class="menu2"><a href="https://github.com/Groovounet/glm">Browse Git repository</a></div><div class="menu2"><a href="https://github.com/Groovounet/glm/zipball/master">Master branch snapshot</a></div><div class="menu2"><a href="https://github.com/Groovounet/glm/zipball/0.9.3">0.9.3 branch snapshot</a></div><div class="menu2"><a href="https://github.com/Groovounet/glm/zipball/0.9.4">0.9.4 branch snapshot</a></div><br /><br /><div class="menu2"><a href="http://www.g-truc.net"><img class="menu-img" src="./common/g-truc.png" alt="G-Truc" /></a></div><br /></td><td class="page"><div class="title1"><img src="./common/title.png" alt="OpenGL Mathematics" /></div><div class="title3">GLSL + Optional features = OpenGL Mathematics (GLM)<br />A C++ mathematics library for graphics programming<br /></div><br /><br /><span xmlns="http://www.w3.org/1999/xhtml" class="code-title">Compute a triangle normal:</span><ul xmlns="http://www.w3.org/1999/xhtml" class="code-list"><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/glm.hpp></span></span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content"><span class="keyword">void </span> computeNormal(triangle & Triangle)
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span><span class="keyword">const </span> & a = Triangle.Position[0];
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span><span class="keyword">const </span> & b = Triangle.Position[1];
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span><span class="keyword">const </span> & c = Triangle.Position[2];
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
Triangle.Normal = glm::normalize(glm::cross(c - a, b - a));
</span></li><li class="code-line"><span class="code-line-content">
}
</span></li></ul><span xmlns="http://www.w3.org/1999/xhtml" class="code-title">Matrix transform:</span><ul xmlns="http://www.w3.org/1999/xhtml" class="code-list"><li class="code-line"><span class="code-line-content"><span class="comment">// glm::vec3, glm::vec4, glm::ivec4, glm::mat4</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/glm.hpp> </span></span></li><li class="code-line"><span class="code-line-content"><span class="comment">// glm::translate, glm::rotate, glm::scale, glm::perspective</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/gtc/matrix_transform.hpp> </span></span></li><li class="code-line"><span class="code-line-content"><span class="comment">// glm::value_ptr</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/gtc/type_ptr.hpp> </span></span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">mat4</span> Projection =
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">mat4</span> ViewTranslate = glm::translate(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
glm::<span class="userword">mat4</span>(1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
glm::<span class="userword">vec3</span>(0.0f, 0.0f, -Translate));
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">mat4</span> ViewRotateX = glm::rotate(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
ViewTranslate,
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
Rotate.y, glm::<span class="userword">vec3</span>(-1.0f, 0.0f, 0.0f));
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">mat4</span> View = glm::rotate(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
ViewRotateX,
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
Rotate.x, glm::<span class="userword">vec3</span>(0.0f, 1.0f, 0.0f));
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::mat4 Model = glm::scale(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
glm::<span class="userword">mat4</span>(1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
glm::<span class="userword">vec3</span>(0.5f));
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">mat4</span> MVP = Projection * View * Model;
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glUniformMatrix4fv(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP));
</span></li><li class="code-line"><span class="code-line-content">
}
</span></li></ul><span xmlns="http://www.w3.org/1999/xhtml" class="code-title">Vector types:</span><ul xmlns="http://www.w3.org/1999/xhtml" class="code-list"><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/glm.hpp></span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/gtx/type_precision.hpp></span></span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content">
std::size_t <span class="keyword">const </span>VertexCount = 4;
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content"><span class="comment">// Float quad geometry</span></span></li><li class="code-line"><span class="code-line-content">
std::size_t <span class="keyword">const </span>PositionSizeF32 = VertexCount * <span class="keyword">sizeof</span>(glm::<span class="userword">vec2</span>);
</span></li><li class="code-line"><span class="code-line-content">
glm::<span class="userword">vec2 </span><span class="keyword">const </span>PositionDataF32[VertexCount] =
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec2</span>(-1.0f,-1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec2</span>( 1.0f,-1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec2</span>( 1.0f, 1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec2</span>(-1.0f, 1.0f)
</span></li><li class="code-line"><span class="code-line-content">
};
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content"><span class="comment">// Half-float quad geometry</span></span></li><li class="code-line"><span class="code-line-content">
std::size_t <span class="keyword">const </span>PositionSizeF16 = VertexCount * <span class="keyword">sizeof</span>(glm::<span class="userword">hvec2</span>);
</span></li><li class="code-line"><span class="code-line-content">
glm::<span class="userword">hvec2 </span><span class="keyword">const </span>PositionDataF16[VertexCount] =
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">hvec2</span>(-1.0f, -1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">hvec2</span>( 1.0f, -1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">hvec2</span>( 1.0f, 1.0f),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">hvec2</span>(-1.0f, 1.0f)
</span></li><li class="code-line"><span class="code-line-content">
};
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content"><span class="comment">// 8 bits signed integer quad geometry</span></span></li><li class="code-line"><span class="code-line-content">
std::size_t <span class="keyword">const </span>PositionSizeI8 = VertexCount * <span class="keyword">sizeof</span>(glm::<span class="userword">i8vec2</span>);
</span></li><li class="code-line"><span class="code-line-content">
glm::<span class="userword">i8vec2 </span><span class="keyword">const </span> PositionDataI8[VertexCount] =
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i8vec2</span>(-1,-1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i8vec2</span>( 1,-1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i8vec2</span>( 1, 1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i8vec2</span>(-1, 1)
</span></li><li class="code-line"><span class="code-line-content">
};
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content"><span class="comment">// 32 bits signed integer quad geometry</span></span></li><li class="code-line"><span class="code-line-content">
std::size_t <span class="keyword">const </span>PositionSizeI32 = VertexCount * <span class="keyword">sizeof</span>(glm::<span class="userword">i32vec2</span>);
</span></li><li class="code-line"><span class="code-line-content">
glm::<span class="userword">i32vec2 </span><span class="keyword">const </span>PositionDataI32[VertexCount] =
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i32vec2 </span>(-1,-1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i32vec2 </span>( 1,-1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i32vec2 </span>( 1, 1),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">i32vec2 </span>(-1, 1)
</span></li><li class="code-line"><span class="code-line-content">
};
</span></li></ul><span xmlns="http://www.w3.org/1999/xhtml" class="code-title">Lighting:</span><ul xmlns="http://www.w3.org/1999/xhtml" class="code-list"><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/glm.hpp></span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/gtx/random.hpp></span></span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content">
glm::<span class="userword">vec3 </span> lighting
</span></li><li class="code-line"><span class="code-line-content">
(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
intersection<span class="keyword"> const </span>& Intersection,
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
material<span class="keyword"> const </span>& Material,
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
light<span class="keyword"> const </span>& Light,
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span>const & View
</span></li><li class="code-line"><span class="code-line-content">
)
</span></li><li class="code-line"><span class="code-line-content">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span> Color = glm::<span class="userword">vec3</span>(0.0f);
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
glm::<span class="userword">vec3 </span>LightVertor = glm::normalize(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
Light.position() - Intersection.globalPosition() +
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px">
glm::vecRand3(0.0f, Light.inaccuracy());
</span></li><li class="code-line"><span class="code-line-content" /></li><li class="code-line"><span class="code-line-content" style="padding-left:32px"><span class="keyword">if</span>(!shadow(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
Intersection.globalPosition(),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
Light.position(),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
LightVertor))
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px"><span class="keyword">float</span> Diffuse = glm::dot(Intersection.normal(), LightVector);
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px" /></li><li class="code-line"><span class="code-line-content" style="padding-left:64px"><span class="keyword">if</span>(Diffuse <= 0.0f)
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px"><span class="keyword">return</span> Color;
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px"><span class="keyword">if</span>(Material.isDiffuse())
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px">
Color += Light.color() * Material.diffuse() * Diffuse;
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px" /></li><li class="code-line"><span class="code-line-content" style="padding-left:64px"><span class="keyword">if</span>(Material.isSpecular())
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
{
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px">
glm::<span class="userword">vec3 </span>Reflect = glm::reflect(
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:128px">
glm::normalize(-LightVector),
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:128px">
glm::normalize(Intersection.normal()));
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px"><span class="keyword">float</span> Dot = glm::dot(Reflect, View);
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px"><span class="keyword">float</span> Base = Dot > 0.0f ? Dot : 0.0f;
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px"><span class="keyword">float</span> Specular = glm::pow(Base, Material.exponent());
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:96px">
Color += Material.specular() * Specular;
</span></li><li class="code-line"><span class="code-line-content" style="padding-left:64px">
}
</span></li><li class="code-line"><span class="code-line-content">
}
</span></li></ul><div class="title3">Copyright 2005 - 2012<a href="http://www.g-truc.net">G-Truc Creation</a></div></td></tr></table></body></html>
|