Vala プログラミング

Rust & Vala プログラミング

おながのブログ

Vala + Cogl Cube(2)

f:id:onagat12:20141226003308p:plain

f:id:onagat12:20141226003319p:plain

ポリゴンをマウスの移動で回転させています。

 

プログラム

cube2.vala

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);

   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
   vertices1[0].x = -100;
   vertices1[0].y = -100;
   vertices1[0].z = 100;
   vertices1[0].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

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

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

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

   Cogl.polygon (vertices1, true);

   //Back
   vertices2[0].x = -100;
   vertices2[0].y = -100;
   vertices2[0].z = -100;
   vertices2[0].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices2[1].x = 100;
   vertices2[1].y = -100;
   vertices2[1].z = -100;
   vertices2[1].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices2[2].x = 100;
   vertices2[2].y = 100;
   vertices2[2].z = -100;
   vertices2[2].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices2[3].x = -100;
   vertices2[3].y = 100;
   vertices2[3].z = -100;
   vertices2[3].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   Cogl.polygon (vertices2, true);

   //Left
   vertices3[0].x = -100;
   vertices3[0].y = -100;
   vertices3[0].z = 100;
   vertices3[0].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices3[1].x = -100;
   vertices3[1].y = -100;
   vertices3[1].z = -100;
   vertices3[1].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices3[2].x = -100;
   vertices3[2].y = 100;
   vertices3[2].z = -100;
   vertices3[2].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices3[3].x = -100;
   vertices3[3].y = 100;
   vertices3[3].z = 100;
   vertices3[3].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   Cogl.polygon (vertices3, true);

   //Right
   vertices4[0].x = 100;
   vertices4[0].y = -100;
   vertices4[0].z = 100;
   vertices4[0].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices4[1].x = 100;
   vertices4[1].y = -100;
   vertices4[1].z = -100;
   vertices4[1].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices4[2].x = 100;
   vertices4[2].y = 100;
   vertices4[2].z = -100;
   vertices4[2].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices4[3].x = 100;
   vertices4[3].y = 100;
   vertices4[3].z = 100;
   vertices4[3].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   Cogl.polygon (vertices4, true);

   //Top
   vertices5[0].x = -100;
   vertices5[0].y = -100;
   vertices5[0].z = 100;
   vertices5[0].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices5[1].x = -100;
   vertices5[1].y = -100;
   vertices5[1].z = -100;
   vertices5[1].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices5[2].x = 100;
   vertices5[2].y = -100;
   vertices5[2].z = -100;
   vertices5[2].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   vertices5[3].x = 100;
   vertices5[3].y = -100;
   vertices5[3].z = 100;
   vertices5[3].color.set_from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   Cogl.polygon (vertices5, true);

   //Bottom
   vertices6[0].x = -100;
   vertices6[0].y = 100;
   vertices6[0].z = 100;
   vertices6[0].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices6[1].x = -100;
   vertices6[1].y = 100;
   vertices6[1].z = -100;
   vertices6[1].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices6[2].x = 100;
   vertices6[2].y = 100;
   vertices6[2].z = -100;
   vertices6[2].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   vertices6[3].x = 100;
   vertices6[3].y = 100;
   vertices6[3].z = 100;
   vertices6[3].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

   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 + 1;
      stdout.printf("alpha = %f \n", alpha);
      beta = beta + 1;

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

   stage.show ();

   Clutter.main ();

   return 0;
}

ビルド

コマンドライン

   valac --pkg clutter-1.0 cube2.vala

Valencia-gedit-plugin

Makefile

   SRCS = cube2.vala
   PROGRAM = cube2
   VALAPKGS = --pkg clutter-1.0
   VALAOPTS =
   CFLAGS =

   all: $(PROGRAM)

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

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

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