mirror of
https://github.com/g-truc/glm.git
synced 2024-11-10 04:31:47 +00:00
189 lines
20 KiB
HTML
189 lines
20 KiB
HTML
<?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.2.3/glm-0.9.2.3.zip/download">
|
||
Download GLM 0.9.2.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-0.9.2.pdf">GLM Manual</a></div><div class="menu2"><a href="./api-0.9.2/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.10.6.clean.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="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">Source 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::perspective</span></span></li><li class="code-line"><span class="code-line-content"><span class="keyword">#include </span><span class="string"><glm/gtc/matrix_projection.hpp> </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"><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 <20> 2005 - 2011<a href="http://www.g-truc.net">G-Truc Creation</a></div></td></tr></table></body></html> |