challenge ポリゴンを表示するプログラム

適当なポリゴンを表示させて、描画するプログラムを書いてください。
ポリゴンは回転させてください。

2D処理だけなら、標準ライブラリで大体いけますが、
3D処理は追加でライブラリを利用すると思うので、
何のライブラリを利用したのか書いてください。

Posted feedbacks - Other

プログラムと呼ぶのかはわかりませんが、マウスでぐりぐりっと回転できます.
VRMLビューアが必要です.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#VRML V2.0 utf8
Shape {
  geometry IndexedFaceSet {
    coord Coordinate {
      point [ 0 0 0.3, 0 2 0, 1.73 -1 0, -1.73 -1 0 ]
    }
    coordIndex [
     0 1 2 -1
     0 2 3 -1
     0 3 1 -1
    ]
    solid FALSE
  }
  appearance Appearance {
    material Material {}
  }
}

Ruby/SDLとOpenGLでベタに書いてみました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'sdl'
require 'opengl'

vertices = [
  [[1.0, 0.0, 0.0], [-1.0, -1.0, 0.0]],
  [[0.0, 1.0, 0.0], [ 1.0, -1.0, 0.0]],
  [[0.0, 0.0, 1.0], [ 1.0,  1.0, 0.0]],
  [[1.0, 1.0, 1.0], [-1.0,  1.0, 0.0]]
]

SDL.init(SDL::INIT_VIDEO)
SDL::Screen.open(600, 600, 16, SDL::OPENGL)

GL.ClearColor(0.0, 0.0, 0.2, 1.0);
GL::Ortho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0)
GL::MatrixMode(GL::MODELVIEW);

loop {
  GL.Clear(GL::COLOR_BUFFER_BIT);
  
  GL::Rotate(1.0, 2.0, 3.0, 1.0);
  
  GL::Begin(GL::QUADS)
  vertices.each {|v|
    GL::Color(v[0])
    GL::Vertex(v[1])
  }  
  GL::End()
  
  SDL::GL.swap_buffers
  
  evt = SDL::Event.poll
  case evt
  when SDL::Event::Quit
    exit
  when SDL::Event::KeyUp
    exit if evt.sym == SDL::Key::ESCAPE
  end
}

Processing で。 可視化が本分の Processing で、 python より長いのがちょっと残念ですが。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
float theta = 0.0;

void setup() {
  size(200, 200, P3D);
}

void draw() {
  background(0);
  lights();
  translate(width/2, height/2, 0);
  rotateX(PI/3+theta);
  rotateY(theta);
  box(100);
  theta += PI/256;
}

Index

Feed

Other

Link

Pathtraq

loading...