Vala プログラミング

読者です 読者をやめる 読者になる 読者になる

Rust & Vala プログラミング

おながのブログ

Vala + Cogl Cube(3)

f:id:onagat12:20141226004949p:plain

Materialを設定し、材質の色(diffuse, ambient, specular)を設定しています。

いろいろ調べましたが、光源の設定、法線ベクトルの設定が分かりません。

OpenGLライブラリを使用しています。

 

プログラム

cube3.vala

using GL;

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

   var material = new Cogl.Material ();
   Cogl.set_source (material);

   Cogl.begin_gl ();
      glEnable (GL_LIGHTING);
   Cogl.end_gl ();

   var diffuse_color = Cogl.Color.from_4f (1.0f, 0.0f, 0.0f, 1.0f);
   var ambient_color = Cogl.Color.from_4f (0.8f, 0.8f, 0.8f, 1.0f);
   var specular_color = Cogl.Color.from_4f (1.0f, 1.0f, 1.0f, 1.0f);

   material.set_diffuse (diffuse_color);
   material.set_ambient (ambient_color);
   material.set_specular (specular_color);
   material.set_shininess (50.0f);

   //var emission_color = Cogl.Color.from_4f (0.8f, 0.0f, 0.0f, 1.0f);
   //material.set_emission (emission_color);

   //Front
   vertices1[0].x = -100;
   vertices1[0].y = -100;
   vertices1[0].z = 100;

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

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

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

   Cogl.polygon (vertices1, false);

   //Back
   vertices2[0].x = -100;
   vertices2[0].y = -100;
   vertices2[0].z = -100;

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

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

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

   Cogl.polygon (vertices2, false);

   //Left
   vertices3[0].x = -100;
   vertices3[0].y = -100;
   vertices3[0].z = 100;

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

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

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

   Cogl.polygon (vertices3, false);

   //Right
   vertices4[0].x = 100;
   vertices4[0].y = -100;
   vertices4[0].z = 100;

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

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

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

   Cogl.polygon (vertices4, false);

   //Top
   vertices5[0].x = -100;
   vertices5[0].y = -100;
   vertices5[0].z = 100;

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

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

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

   Cogl.polygon (vertices5, false);

   //Bottom
   vertices6[0].x = -100;
   vertices6[0].y = 100;
   vertices6[0].z = 100;

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

   vertices6[2].x = 100; 

   vertices6[2].y = 100;
   vertices6[2].z = -100;

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

   Cogl.polygon (vertices6, false);

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

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

   stage.show ();

   Clutter.main ();

   return 0;
}

 

ビルド

コマンドライン

   valac --vapidir=../vapi --pkg clutter-1.0 --pkg gl cube3.vala

   (vapiフォルダには、gl.vapiファイルが入っている。)

Valencia-gedit-plugin

Makefile

 SRCS = cube3.vala
 PROGRAM = cube3
 VALAPKGS = --pkg clutter-1.0 --pkg gl
 VALAOPTS = --vapidir=../vapi
 CFLAGS =

 all: $(PROGRAM)

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

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

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