bambooflow Note

GLM

最終更新:

bambooflow

- view
メンバー限定 登録/ログイン

GLM - OpenGL Mathematics


GLMは3DグラフィックスのためのC++ライブラリです。
GLSLのAPIと類似しており扱いやすくなっています。





ダウンロード


http://glm.g-truc.net/


簡単な使い方


次のヘッダをインクルードします。
#include <glm/glm.hpp>


#include <stdio.h>
#include <glm/glm.hpp>
 
int main(int argc, char* argv[])
{
  glm::vec3 pos( 1.0, 2.0, 3.0);
 
  printf("x=%f, y=%f, z=%f\n", pos.x, pos.y, pos.z);
 
  return 0;
}
 

vec3へのアクセスはいろいろ準備されています。
たとえば配列だと、pos[0],pos[1],pos[2]で参照できます。

GLMの扱いやすいところは、GLSLとAPIが似ていることと、swizzleが用意されていること。

ヘッダファイル


よく使いそうなヘッダファイルは次のとおり。

#include <glm/glm.hpp>
// for translate, rotate, scale
#include <glm/matrix_transform.hpp>
// for ortho, frustum, perspective, project
#include <glm/matrix_projection.hpp>
// for lookAt, proj2D, proj3D, etc.
#include <glm/gtx/transform2.hpp>
// for value_ptr
#include <glm/gtc/type_ptr.hpp>
 


基本


Identity(単位行列)


#include <glm/glm.hpp>
 
glm::mat4 ident = glm::mat4(1.0);
 
glLoadIdentity


Matrix Transform



translate行列(移動)


#include <glm/gtc/matrix_transform.hpp>

template <typename T>  
tmat4x4<T> translate(
  tmat4x4<T> const & m, // 元マトリックス
  tvec3<T> const & v    // 位置(x,y,z)
);

  • 使い方例
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
 
float angle;
glm::vec3 pos(0.0, 3.0, 0.0); // 位置y=3.0
glm::mat4 a;
 ・・・
glm::mat4 b = glm::translate(a, pos);
 


rotate行列(回転)


#include <glm/gtc/matrix_transform.hpp>

template <typename T>  
tmat4x4<T> rotate(
  tmat4x4<T> const & m,  // 元マトリックス
  T const & angle,       // 回転角(radian)
  tvec3<T> const & v     // 回転軸(x,y,z)
); 

  • 使い方例
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
 
float angle;
glm::vec3 axis(0.0, 1.0, 0.0); // y軸で回転
glm::mat4 a;
 ・・・
glm::mat4 b = glm::rotate(a, angle, axis);
 


scale行列(拡大縮小)


#include <glm/gtc/matrix_transform.hpp>

template <typename T>
tmat4x4<T> scale(
  tmat4x4<T> const & m,  // 元マトリックス
  tvec3<T> const & v     // スケール(x,y,z)
); 

  • 使い方例
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
 
glm::vec3 size(0.5, 0.5, 0.5); // スケール: 1/2に縮小
glm::mat4 a;
 ・・・
glm::mat4 b = glm::rotate(a, size);
 


Matrix Projection



ortho


#include <glm/glm.hpp>
#include <glm/gtc/matrix_projection.hpp>
 

frustum


#include <glm/glm.hpp>
#include <glm/gtc/matrix_projection.hpp>
 

perspective


#include <glm/glm.hpp>
#include <glm/gtc/matrix_projection.hpp>
 
glm::mat4 projectionMatrix;
 
void reshape(int w, int h)
{
  glViewport(0, 0, w, h);
 
  projectionMatrix = glm::perspective(30.0f, (float)w/(float)h, 1.0f, 100.0f);
}
 

Matrix Transform2


lookAt


template <typename T>  
tmat4x4<T> lookAt(
    tvec3<T> const & eye, 
    tvec3<T> const & center, 
    tvec3<T> const & up
);


#include <glm/glm.hpp>
#include <glm/gtx/transform2.hpp>
 
glm::mat4 viewMatrix;
 
viewMatrix = glm::lookAt(
    glm::vec3(3.0f, 4.0f, 5.0f), // eye pos
    glm::vec3(0.0f, 0.0f, 0.0f), // center pos
    glm::vec3(0.0f, 1.0f, 0.0f)  // up
);  
 


Quaternion


使い方


#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
 
glm::quat a, b, c;
 
// Quatanionの乗算
c = glm::cross( a, b );
 
// Quotanionから行列を求める
glm::mat4 r = glm::mat4_cast( c );
 



フルにSwizzleを使う


たとえば、.x, .y, .zや .t, .sといったものはデフォルトで扱えます。
でも.xyzやxyzwなどは設定が必要になります。

#include  <glm/setup.hpp>
#define GLM_SWIZZLE  GLM_SWIZZLE_FULL
#include  <glm/glm.hpp>
 

タグ:

OpenGL GLM GLSL
記事メニュー
目安箱バナー