Vala プログラミング

WebGPU プログラミング

おなが@京都先端科学大

Vala OpenGL ( ValaGL MatrixMath.vala)

ValaGL MatrixMath.vala の演算
Vec4 ( 4次元ベクトル)

Vec4の定義
struct Vec4
*4次元ベクトル
   メンバー
   GLfloat data[4]

  メソッド
  *生成
   Vec4()
       要素が 0 値の Vec4 を生成 (Creates a new vector, zero initialized.)
   Vec4.from_data (GLfloat x, GLfloat y, GLfloat z, GLfloat w)
      要素が {x, y, z, w} の Vec4 を生成
       (Creates a vector whose contents are the copy of the given data.)
   Vec4.from_array (GLfloat[] data)
      要素が配列 data で与えられる Vec4 を生成
      (Creates a vector whose contents are the copy of the given array.)
   Vec4.expand (ref Vec3 vec3, GLfloat w)
      要素が {vec3.x, vec3.y, vec3.z, w} の Vec4 を生成
      (Expands a 3x3 vector plus scalar into a 4x4 vector.)

  *要素間の演算
   void add (ref Vec4 other)
       this(インスタンス)と other ベクトルとの和
      (Adds the given vector, component-wise.)
   void sub (ref Vec4 other)
       this(インスタンス)から other ベクトルを引く
      (Subtracts the given vector, component-wise.)
   void mul_vec (ref Vec4 other)
       this(インスタンス)と other ベクトルとの積
      (Multiplies the given vector, component-wise.)
   void div_vec (ref Vec4 other)
       this(インスタンス)を other ベクトルで割る
     (Divides the given vector, component-wise.)

  *ベクトル演算
   GLfloat dot_product (ref Vec4 other)
       this(インスタンス)と other ベクトルの内積
      (Computes the dot product of this vector and the other vector.)
   void mul (GLfloat factor)
       this(インスタンス)の各要素を factor 倍する
     (Multiplies the vector by the given scalar.)
  void div (GLfloat factor)
       this(インスタンス)の各要素を factor で割る
      (Divides the vector by the given scalar.)
  GLfloat norm ()
       this(インスタンス)ベクトルの大きさを求める
      (Computes the norm of this vector.)
   void normalize ()
       this(インスタンス)ベクトルを規格化する
      (Normalizes this vector, dividing it by its norm.
       If the norm is zero, the result is undefined.)

  *アクセッサー(ゲッター)
   GLfloat x
       data[0] へのアクセッサー
      (Convenience accessor for data[0].)
  GLfloat y
       data[1] へのアクセッサー
     (Convenience accessor for data[1].)
  GLfloat z
      data[2] へのアクセッサー
      (Convenience accessor for data[2].)
  GLfloat w
      data[3] へのアクセッサー
      (Convenience accessor for data[3].)

1 ベクトルの生成
## prog1.vala
using ValaGL.Core;

void main () {
    var a = Vec4.from_data (1.0f, 2.0f, 3.0f, 4.0f);
    // a.x(data[0])= 1.0, a.y(data[1])= 2.0
    // a.z(data[2])= 3.0, a.w(data[3])= 4.0
    stdout.printf ("Vec4 a x:%f y:%f z:%f w:%f\n", a.data[0], a.data[1], a.data[2], a.data[3]);
    stdout.printf ("Vec4 a x:%f y:%f z:%f w:%f\n", a.x, a.y, a.z, a.w);
    stdout.printf("\n");

   var c = Vec3.from_data (5.0f, 6.0f, 7.0f);
    stdout.printf ("Vec3 c x:%f y:%f z:%f\n", c.x, c.y, c.z);
   var b = Vec4.expand (ref c, 8.0f);
    // b.x = 5.0, b.y = 6.0, b.z = 7.0, b.w = 8.0
    stdout.printf ("Vec4 b x:%f y:%f z:%f w:%f\n", b.x, b.y, b.z, b.w);
}

実行結果
Vec4 a x:1.000000 y:2.000000 z:3.000000 w:4.000000
Vec4 a x:1.000000 y:2.000000 z:3.000000 w:4.000000

Vec3 c x:5.000000 y:6.000000 z:7.000000
Vec4 b x:5.000000 y:6.000000 z:7.000000 w:8.000000


2 ベクトル演算
## prog2.vala
using ValaGL.Core;

void main () {
    var a = Vec4.from_data (1.0f, 2.0f, 3.0f, 4.0f);
    stdout.printf ("Vec4 a x:%f y:%f z:%f w:%f\n", a.x, a.y, a.z, a.w);

   var b = Vec4.from_data (8.0f, 7.0f, 6.0f, 5.0f);
    stdout.printf ("Vec4 b x:%f y:%f z:%f w:%f\n", b.x, b.y, b.z, b.w);
    stdout.printf("\n");

   float s = a.dot_product (ref b);
    // s = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w
    stdout.printf("s: %f\n", s);
    stdout.printf("\n");

   float a_norm = a.norm ();
    // a_norm = SQRT(a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w)
    stdout.printf("a_norm: %f\n", a_norm);
    stdout.printf("\n");

   a.normalize ();
    // a.x = a.x/a.norm(), a.y = a.y/a.norm()
    // a.z = a.z/a.norm(), a.w = a.w/a.norm()
    stdout.printf("a x:%f y:%f z:%f w:%f\n", a.x, a.y, a.z, a.w);
    a_norm = a.norm ();
    stdout.printf("a_norm: %f\n", a_norm);
}

実行結果
Vec4 a x:1.000000 y:2.000000 z:3.000000 w:4.000000
Vec4 b x:8.000000 y:7.000000 z:6.000000 w:5.000000

s: 60.000000

a_norm: 5.477226

a x:0.182574 y:0.365148 z:0.547723 w:0.730297
a_norm: 1.000000


ビルド
valac --vapidir=./ --pkg glepoxy -X -lm main.vala MatrixMath.vala