Vala プログラミング

WebGPU プログラミング

おなが@京都先端科学大

Vala言語とOpenGL (2)

f:id:onagat12:20150301012106p:plain

GtkウィンドウにOpenGLを描画していることが分かるように、ボタン(Button)を
ウィンドウに追加しました。
また、Stageのmotion_eventシグナルを使って、Stage上でマウスを動かすと
三角形が回転するようにしました。コールバック関数には、無名関数を使っています。

gtkcluttergl2.vala
using Gtk;
using GtkClutter;
using Clutter;

public class MainWindow : Gtk.Window
{
    private Clutter.Actor coglbox;
    private static float alpha;

    public MainWindow ()

    {
         title = "GtkClutterGL";
         set_default_size (300, 300);
         destroy.connect (Gtk.main_quit);

         var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
         var box2 = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
         var button = new Gtk.Button.with_label ("Quit");
         button.clicked.connect (Gtk.main_quit);

         vbox.pack_start (box2, true, true, 0);
         vbox.pack_start (button, false, false, 0);
         add (vbox);

         var widget = new Embed ();
         box2.pack_start (widget, true, true, 0);

         var stage = widget.get_stage ();
         stage.width = stage.height = 300;
         stage.background_color = Clutter.Color () { alpha = 255 };

         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 ("x = %f y = %f\n", evt.x, evt.y);
              alpha = alpha + 0.5f;
              coglbox.queue_redraw ();
              return true;
        });
     }

    private void paint_cb ()

    {
         Cogl.TextureVertex vertices1[3];

         Cogl.push_matrix ();
         Cogl.translate (150, 150, 0);
         Cogl.rotate (alpha, 0, 0, 1);

         vertices1[0].x = 0;
         vertices1[0].y = -150;
         vertices1[0].z = 0;
         vertices1[0].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

         vertices1[1].x = -150;
         vertices1[1].y = 150;
         vertices1[1].z = 0;
         vertices1[1].color.set_from_4f (0.0f, 1.0f, 0.0f, 1.0f);

         vertices1[2].x = 150;
         vertices1[2].y = 150;
         vertices1[2].z = 0;
         vertices1[2].color.set_from_4f (0.0f, 0.0f, 1.0f, 1.0f);

         Cogl.polygon (vertices1, true);

         Cogl.pop_matrix();
     }

}

void main (string[] args)
{
     GtkClutter.init (ref args);
     var window = new MainWindow ();
     window.show_all ();
     Gtk.main ();
}
ビルド
valac --pkg gtk+-3.0 --pkg clutter-gtk-1.0 gtkcluttergl2.vala

説明
1 メインのウィンドウに縦方向のBox(vbox)を配置します。その中に、Box(box2)と
  ボタン(button)を配置します。また、box2にEmbedウィジェを貼り付けます。
       var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
          var box2 = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
          var button = new Gtk.Button.with_label ("Quit");
          button.clicked.connect (Gtk.main_quit);
          vbox.pack_start (box2, true, true, 0);
          vbox.pack_start (button, false, false, 0);
          add (vbox);

          var widget = new Embed ();
          box2.pack_start (widget, true, true, 0);
2 Stageのmotion_eventシグナルを使った三角形の回転
          stage.motion_event.connect ((evt) => {
               GLib.message ("Motion event - Stage");
               stdout.printf ("x = %f y = %f\n", evt.x, evt.y);
               alpha = alpha + 0.5f;
               coglbox.queue_redraw ();
               return true;
           });
  paint_cbコールバック関数(coglboxアクターのコールバック関数)
     ここでopenglのrotate関数で回転させる。
           Cogl.rotate (alpha, 0, 0, 1);