Source

gltut / glm-0.9.0.0 / doc / code.html

Full commit
<?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" /></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.0.0/glm-0.9.0.0.zip/download">
			Download GLM 0.9.0.0</a></div></div><br /><div class="menu2"><a href="./index.html">News</a></div><div class="menu2"><a href="./about.html">About</a></div><div class="menu2"><a href="./download.html">Downloads</a></div><div class="menu2"><a href="./goodies.html">Goodies</a></div><br /><div class="menu2"><a href="./glm-manual.pdf">GLM Manual</a></div><div class="menu2"><a href="./html/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.00.8.clean.pdf">GLSL Specification</a></div><div class="menu2"><a href="http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=postlist&amp;Board=10&amp;page=1">OpenGL.org Toolkits forum</a></div><br /><div class="menu2"><a href="https://sourceforge.net/projects/ogl-math/">SF.net page</a></div><div class="menu2"><a href="https://sourceforge.net/apps/trac/ogl-math">Bug report</a></div><div class="menu2"><a href="http://ogl-math.git.sourceforge.net/git/gitweb.cgi?p=ogl-math/ogl-math;a=summary">Browse Git repository</a></div><div class="menu2"><a href="http://ogl-math.git.sourceforge.net/git/gitweb.cgi?p=ogl-math/ogl-math;a=snapshot;h=HEAD;sf=tgz">GIT HEAD snapshot</a></div><br /><br /><div class="menu2"><a href="http://www.g-truc.net"><img class="menu-img" src="./common/g-truc.jpg" alt="G-Truc" /></a></div><div class="menu2"><a href="http://www.sourceforge.net"><img class="menu-img" src="./common/sourceforge.gif" alt="SourceForge" /></a></div><div class="menu2"><a href="http://www.opengl.org"><img class="menu-img" src="./common/opengl.jpg" alt="OpenGL" /></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 3D graphics.<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">&lt;glm/glm.hpp&gt;</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 &amp; 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> &amp; 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> &amp; 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> &amp; 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">&lt;glm/glm.hpp&gt; </span></span></li><li class="code-line"><span class="code-line-content"><span class="comment">// glm::perspective</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string">&lt;glm/gtc/matrix_projection.hpp&gt; </span></span></li><li class="code-line"><span class="code-line-content"><span class="comment">// glm::translate, glm::rotate, glm::scale</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string">&lt;glm/gtc/matrix_transform.hpp&gt; </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">&lt;glm/gtc/type_ptr.hpp&gt; </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">&lt;glm/glm.hpp&gt;</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string">&lt;glm/gtx/type_precision.hpp&gt;</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">&lt;glm/glm.hpp&gt;</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string">&lt;glm/gtx/random.hpp&gt;</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>&amp; Intersection,
			</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
				material<span class="keyword"> const </span>&amp; Material,
			</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
				light<span class="keyword"> const </span>&amp; Light,
			</span></li><li class="code-line"><span class="code-line-content" style="padding-left:32px">
				glm::<span class="userword">vec3 </span>const &amp; 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 &lt;= 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 &gt; 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 - 2010 <a href="http://www.g-truc.net">G-Truc Creation</a></div></td></tr></table></body></html>