Vala プログラミング

WebGPU プログラミング

おなが@京都先端科学大

Vala OpenGL ( ValaGL MatrixMath.vala )

ValaGL MatrixMath.vala の演算

Mat4 ( 4x4 行列)

Mat4 の定義
struct Mat4
   メンバー
   float data[16]
      Mat4 各要素
      m11(data[0]), m12(data[4]), m13(data[8]), m14(data[12])
      m21(data[1]), m22(data[5]), m23(data[9]), m24(data[13])
      m31(data[2]), m32(data[6]), m33(data[10]), m348data[14])
      m41(data[3]), m42(data[7]), m43(data[11]), m44(data[15])

   メソッド
   *生成
   Mat4()
      要素が 0 値の Mat4 を生成
      (Creates a new matrix, zero initialized.)
   Mat4.from_data (GLfloat a11, GLfloat a12, GLfloat a13, GLfloat a14,
                             GLfloat a21, GLfloat a22, GLfloat a23, GLfloat a24,
                             GLfloat a31, GLfloat a32, GLfloat a33, GLfloat a34,
                             GLfloat a41, GLfloat a42, GLfloat a43, GLfloat a44)
      要素が、以下のようになる Mat4 を生成
      m11 = a11, m12 = a12, m13 = a13, m14 = a14
      m21 = a21, m22 = a22, m23 = a23, m24 = a24
      m31 = a31, m32 = a32, m33 = a33, m34 = a34
      m41 = a41, m42 = a42, m43 = a43, m44 = a44
      (Creates a matrix whose contents are the copy of the given data.
       Warning: the data are specified in column-first-index order,
       which is different from the internal storage format(row-first-
       index).)
   Mat4.from_vec_mul (ref Vec4 a, ref Vec4 b)
      2つの Vec4 a, b から Mat4 を生成(a * bT の計算、bT は b の
      転置ベクトル)
      m11 = a.x * b.x, m12 = a.x * b.y, m13 = a.x * b.z, m14 = a.x * b.w
      m21 = a.y * b.x, m22 = a.y * b.y, m23 = a.y * b.z, m24 = a.y * b.w
      m31 = a.z * b.x, m32 = a.z * b.y, m33 = a.z * b.z, m34 = a.z * b.w
      m41 = a.w * b.x, m42 = a.w * b.y, m43 = a.w * b.z, m44 = a.w * b.w
      (Given two vectors ``a`` and ``b``, computes a matrix equal to
       ``a * bT``.)
   Mat4.from_array (GLfloat[] data)
      要素が配列 data で与えられる Mat4 を生成
      m11 = data[0], m12 = data[4], m13 = data[8], m14 = data[12]
      m21 = data[1], m22 = data[5], m23 = data[9], m24 = data[13]
      m31 = data[2], m32 = data[6], m33 = data[10], m34 = data[14]
      m41 = data[3], m42 = data[7], m43 = data[11], m44 = data[15]
      (Creates a matrix whose contents are the copy of the given
       array,assumed to have at least 16 elements.)
   Mat4.identity ()
      単位行列を生成
      m11 = 1, m12 = 0, m13 = 0, m14 = 0
      m21 = 0, m22 = 1, m23 = 0, m24 = 0
      m31 = 0, m32 = 0, m33 = 1, m34 = 0
      m41 = 0, m42 = 0, m43 = 0, m44 = 1
      (Creates an identity matrix.)
   Mat4.expand (ref Mat3 mat3)
      Mat3行列 mat3(A) を拡張して、Mat4行列を生成
      m11 = a11, m12 = a12, m13 = a13, m14 = 0
      m21 = a21, m22 = a22, m23 = a23, m24 = 0
      m31 = a31, m32 = a32, m33 = a33, m34 = 0
      m41 = 0, m42 = 0, m43 = 0, m44 = 1
      (Creates an expansion of the given 3x3 matrix into 4x4.)

  *ベクトル演算
   void add (ref Mat4 other)
      this(インスタンス)と other 行列との和(要素間の和)
      (Adds the given matrix, component-wise.)
   void sub (ref Mat4 other)
      this(インスタンス)と other 行列との差(要素間の差)
      this から other 行列を引く
      (Subtracts the given matrix, component-wise.)
   void mul (GLfloat factor)
      this(インスタンス)の各要素をfactor倍する
      (Multiplies the matrix by the given scalar, component-wise.)
   void div (GLfloat factor)
      this(インスタンス)の各要素をfactorで割る
      (Divides the matrix by the given scalar, component-wise.)

   void mul_mat (ref Mat4 other)
      this(インスタンス、Mat4)と other 行列(Mat4)の積
    ( this × other )
      (Multiplies the given matrix using the linear algebra definition of
       matrix multiplication.)
   Vec4 mul_vec (ref Vec4 vec)
      this(インスタンス, Mat4)と vec ベクトル(Vec4) の積
      ( this × vec )
      (Multiplies this matrix by the given vector and returns the result
       as a new vector.)

   Mat4 transposed ()
      this(インスタンス, Mat4)の転置行列を生成
      (Returns a new matrix that is the transposition of this matrix.)
   GLfloat det ()
      this(インスタンス, Mat4)の行列式を計算
      (Computes the determinant of this matrix.)
   Mat4 inverted (out bool success)
      this(インスタンス, Mat4)の逆行列を生成
      逆行列が存在しない場合、success に "false" が設定される。
      逆行列が存在する場合、success に "true" が設定され、
      逆行列が生成される。
      (Returns a new matrix that is the inversion of this matrix.
       @param success Set to ``false`` if the matrix cannot be inverted
            (its determinant is zero) and ``true`` otherwise.
       @return The inverted matrix if the matrix was not successfully
            inverted, otherwise the return value is undefined.)

*Mat4 の生成・演算は、Mat3 とほぼ同様です。