Vala プログラミング

WebGPU プログラミング

おなが@京都先端科学大

Vala + Cogl Cube(4)

f:id:onagat12:20141226010905p:plain

f:id:onagat12:20141226010913p:plain

ポリゴンの法線ベクトルと光線方向のベクトル(z方向)の内積を作り、

ポリゴン面の色の強さに設定しています。(上はマウスで回転している様子)

 

プログラム

private static float alpha;
private static float beta;

void paint_cb () {
   Cogl.push_matrix ();
   Cogl.translate (300, 200, -200);
   Cogl.rotate (alpha, 1, 0, 0);
   stdout.printf("alpha = %f \n", alpha);
   Cogl.rotate (beta, 0, 1, 0);
   stdout.printf("beta = %f \n", beta);

   Cogl.TextureVertex vertices1[4];
   Cogl.TextureVertex vertices2[4];
   Cogl.TextureVertex vertices3[4];
   Cogl.TextureVertex vertices4[4];
   Cogl.TextureVertex vertices5[4];
   Cogl.TextureVertex vertices6[4];

   Cogl.set_depth_test_enabled (true);

   //Front
   var x1 = Math.cos(beta / 180.0f * Math.PI) * Math.cos(alpha / 180.0f * Math.PI);
   var front_color = Cogl.Color.from_4f ((float)x1, 0.0f, 0.0f, 1.0f);

   vertices1[0].x = -100;
   vertices1[0].y = -100;
   vertices1[0].z = 100;
   vertices1[0].color = front_color;

   vertices1[1].x = 100;
   vertices1[1].y = -100;
   vertices1[1].z = 100;
   vertices1[1].color = front_color;

   vertices1[2].x = 100;
   vertices1[2].y = 100;
   vertices1[2].z = 100;
   vertices1[2].color= front_color;

   vertices1[3].x = -100;
   vertices1[3].y = 100;
   vertices1[3].z = 100;
   vertices1[3].color= front_color;

   Cogl.polygon (vertices1, true);

   //Back
   var x2 = -Math.cos(beta / 180.0f * Math.PI) * Math.cos(alpha / 180.0f * Math.PI);
   var back_color = Cogl.Color.from_4f ((float)x2, 0.0f, 0.0f, 1.0f);

   vertices2[0].x = -100;
   vertices2[0].y = -100;
   vertices2[0].z = -100;
   vertices2[0].color = back_color;

   vertices2[1].x = 100;
   vertices2[1].y = -100;
   vertices2[1].z = -100;
   vertices2[1].color = back_color;

   vertices2[2].x = 100;
   vertices2[2].y = 100;
   vertices2[2].z = -100;
   vertices2[2].color = back_color;

   vertices2[3].x = -100;
   vertices2[3].y = 100;
   vertices2[3].z = -100;
   vertices2[3].color = back_color;

   Cogl.polygon (vertices2, true);

   //Left
   var x3 = Math.sin(beta / 180.0f * Math.PI) * Math.cos(alpha / 180.0f * Math.PI);
   var left_color = Cogl.Color.from_4f ((float)x3, 0.0f, 0.0f, 1.0f);

   vertices3[0].x = -100;
   vertices3[0].y = -100;
   vertices3[0].z = 100;
   vertices3[0].color = left_color;

   vertices3[1].x = -100;
   vertices3[1].y = -100;
   vertices3[1].z = -100;
   vertices3[1].color = left_color;

   vertices3[2].x = -100;
   vertices3[2].y = 100;
   vertices3[2].z = -100;
   vertices3[2].color = left_color;

   vertices3[3].x = -100;
   vertices3[3].y = 100;
   vertices3[3].z = 100;
   vertices3[3].color = left_color;

   Cogl.polygon (vertices3, true);

   //Right
   var x4 = -Math.sin(beta / 180.0f * Math.PI) * Math.cos(alpha / 180.0f * Math.PI);
   var right_color = Cogl.Color.from_4f ((float)x4, 0.0f, 0.0f, 1.0f);

   vertices4[0].x = 100;
   vertices4[0].y = -100;
   vertices4[0].z = 100;
   vertices4[0].color = right_color;

   vertices4[1].x = 100;
   vertices4[1].y = -100;
   vertices4[1].z = -100;
   vertices4[1].color = right_color;

   vertices4[2].x = 100;
   vertices4[2].y = 100;
   vertices4[2].z = -100;
   vertices4[2].color = right_color;

   vertices4[3].x = 100;
   vertices4[3].y = 100;
   vertices4[3].z = 100;
   vertices4[3].color = right_color;

   Cogl.polygon (vertices4, true);

   //Top
   var x5 = -Math.sin(alpha / 180.0f * Math.PI);
   var top_color = Cogl.Color.from_4f ((float)x5, 0.0f, 0.0f, 1.0f);

   vertices5[0].x = -100;
   vertices5[0].y = -100;
   vertices5[0].z = 100;
   vertices5[0].color = top_color;

   vertices5[1].x = -100;
   vertices5[1].y = -100;
   vertices5[1].z = -100;
   vertices5[1].color = top_color;

   vertices5[2].x = 100;
   vertices5[2].y = -100;
   vertices5[2].z = -100;
   vertices5[2].color = top_color;

   vertices5[3].x = 100;
   vertices5[3].y = -100;
   vertices5[3].z = 100;
   vertices5[3].color = top_color;

   Cogl.polygon (vertices5, true);

   //Bottom
   var x6 = Math.sin(alpha / 180.0f * Math.PI);
   var bottom_color = Cogl.Color.from_4f ((float)x6, 0.0f, 0.0f, 1.0f);

   vertices6[0].x = -100;
   vertices6[0].y = 100;
   vertices6[0].z = 100;
   vertices6[0].color = bottom_color;

   vertices6[1].x = -100;
   vertices6[1].y = 100;
   vertices6[1].z = -100;
   vertices6[1].color = bottom_color;

   vertices6[2].x = 100;
   vertices6[2].y = 100;
   vertices6[2].z = -100;
   vertices6[2].color = bottom_color;

   vertices6[3].x = 100;
   vertices6[3].y = 100;
   vertices6[3].z = 100;
   vertices6[3].color = bottom_color;

  Cogl.polygon (vertices6, true);

  Cogl.pop_matrix();
}

int main (string[] args)
{
   Clutter.init (ref args);

   var stage = Clutter.Stage.get_default ();
   stage.background_color = Clutter.Color () { alpha = 255 };

   var coglbox = new Clutter.Actor ();
   stage.add_actor (coglbox);

   coglbox.paint.connect (paint_cb);

   stage.motion_event.connect ((evt) => {
      GLib.message ("Motion event - Stage");
      stdout.printf ("x0 = %f y0 = %f\n", evt.x, evt.y);
      alpha = alpha + 0.5f;
      //stdout.printf("alpha = %f \n", alpha);
      beta = beta + 0.5f;

      coglbox.queue_redraw ();
      return true;
   });

   stage.show ();

   Clutter.main ();

   return 0;
}

 

ビルド

コマンドライン

 valac --pkg clutter-1.0 -X -lm cube4.vala

 Cの数値ライブラリを使用しますので、"-X -lm" のオプションを付けています。

Valencia-gedit-plugin

Makefile

 SRCS = cube4.vala
 PROGRAM = cube4
 VALAPKGS = --pkg clutter-1.0
 VALAOPTS =
 CFLAGS = -X -lm

 all: $(PROGRAM)

 $(PROGRAM): $(SRCS)
 (tab) valac $(VALAOPTS) $(VALAPKGS) $(CFLAGS) -o $(PROGRAM) $(SRCS)

 run: $(PROGRAM)
 (tab) ./$(PROGRAM)

 clean:
 (tab) rm -f $(PROGRAM)