21/02/2005 - 02/04/2014 OpenGL Mathematics (GLM)

OpenGL Mathematics (GLM) is a C++ mathematics library for 3D software based on the OpenGL Shading Language (GLSL) specification.

The goal of the project is to provide to 3D programmers math classes and functions that miss in C++ when we use to program with GLSL or any high level GPU language. With GLM, the idea is to have a library that works the same way that GLSL which imply a strict following of GLSL specification for the implementation.

However, this project isn't limited by GLSL features. An extension system based on GLSL extensions development conventions allows to extend GLSL capabilities.

GLM is release under MIT license and available for all version of GCC from version 3.4 and Visual Studio from version 8.0 as a platform independent library.

Raytracer based on GLMRaytracer based on GLMAnimation based on GLMAnimation based on GLM
GLM sample, compute a triangle normal:
  • #include <glm/glm.hpp>
  • void computeNormal(triangle & Triangle)
  • {
  • glm::vec3 const & a = Triangle.Position[0];
  • glm::vec3 const & b = Triangle.Position[1];
  • glm::vec3 const & c = Triangle.Position[2];
  • Triangle.Normal = glm::normalize(glm::cross(c - a, b - a));
  • }
GLM sample, matrix transform:
  • // glm::vec3, glm::vec4, glm::ivec4, glm::mat4
  • #include <glm/glm.hpp>
  • // glm::perspective
  • #include <glm/gtc/matrix_projection.hpp>
  • // glm::translate, glm::rotate, glm::scale
  • #include <glm/gtc/matrix_transform.hpp>
  • // glm::value_ptr
  • #include <glm/gtc/type_ptr.hpp>
  • {
  • glm::mat4 Projection =
  • glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
  • glm::mat4 ViewTranslate = glm::translate(
  • glm::mat4(1.0f),
  • glm::vec3(0.0f, 0.0f, -Translate));
  • glm::mat4 ViewRotateX = glm::rotate(
  • ViewTranslate,
  • Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));
  • glm::mat4 View = glm::rotate(
  • ViewRotateX,
  • Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));
  • glm::mat4 Model = glm::scale(
  • glm::mat4(1.0f),
  • glm::vec3(0.5f));
  • glm::mat4 MVP = Projection * View * Model;
  • glUniformMatrix4fv(
  • LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP));
  • }
GLM sample, vector types:
  • #include <glm/glm.hpp>
  • #include <glm/gtx/type_precision.hpp>
  • std::size_t const VertexCount = 4;
  • // Float quad geometry
  • std::size_t const PositionSizeF32 = VertexCount * sizeof(glm::vec2);
  • glm::vec2 const PositionDataF32[VertexCount] =
  • {
  • glm::vec2(-1.0f,-1.0f),
  • glm::vec2( 1.0f,-1.0f),
  • glm::vec2( 1.0f, 1.0f),
  • glm::vec2(-1.0f, 1.0f)
  • };
  • // Half-float quad geometry
  • std::size_t const PositionSizeF16 = VertexCount * sizeof(glm::hvec2);
  • glm::hvec2 const PositionDataF16[VertexCount] =
  • {
  • glm::hvec2(-1.0f, -1.0f),
  • glm::hvec2( 1.0f, -1.0f),
  • glm::hvec2( 1.0f, 1.0f),
  • glm::hvec2(-1.0f, 1.0f)
  • };
  • // 8 bits signed integer quad geometry
  • std::size_t const PositionSizeI8 = VertexCount * sizeof(glm::i8vec2);
  • glm::i8vec2 const PositionDataI8[VertexCount] =
  • {
  • glm::i8vec2(-1,-1),
  • glm::i8vec2( 1,-1),
  • glm::i8vec2( 1, 1),
  • glm::i8vec2(-1, 1)
  • };
  • // 32 bits signed integer quad geometry
  • std::size_t const PositionSizeI32 = VertexCount * sizeof(glm::i32vec2);
  • glm::i32vec2 const PositionDataI32[VertexCount] =
  • {
  • glm::i32vec2 (-1,-1),
  • glm::i32vec2 ( 1,-1),
  • glm::i32vec2 ( 1, 1),
  • glm::i32vec2 (-1, 1)
  • };
GLM sample, lighting:
  • #include <glm/glm.hpp>
  • #include <glm/gtx/random.hpp>
  • glm::vec3 lighting
  • (
  • intersection const & Intersection,
  • material const & Material,
  • light const & Light,
  • glm::vec3 const & View
  • )
  • {
  • glm::vec3 Color = glm::vec3(0.0f);
  • glm::vec3 LightVertor = glm::normalize(
  • Light.position() - Intersection.globalPosition() +
  • glm::vecRand3(0.0f, Light.inaccuracy());
  • if(!shadow(
  • Intersection.globalPosition(),
  • Light.position(),
  • LightVertor))
  • {
  • float Diffuse = glm::dot(Intersection.normal(), LightVector);
  • if(Diffuse <= 0.0f)
  • return Color;
  • if(Material.isDiffuse())
  • Color += Light.color() * Material.diffuse() * Diffuse;
  • if(Material.isSpecular())
  • {
  • glm::vec3 Reflect = glm::reflect(
  • glm::normalize(-LightVector),
  • glm::normalize(Intersection.normal()));
  • float Dot = glm::dot(Reflect, View);
  • float Base = Dot > 0.0f ? Dot : 0.0f;
  • float Specular = glm::pow(Base, Material.exponent());
  • Color += Material.specular() * Specular;
  • }
  • }
Releases:
Posts:
Copyright Christophe Riccio 2002-2013 all rights reserved
Designed for Chrome 9, Firefox 4, Opera 11 and Safari 5