凡人の日々の経過を記録
カレンダー
カテゴリー
リンク
最新記事
(03/21)
(03/11)
(03/05)
(03/02)
(02/06)
ブログ内検索
コガネモチ
どこぞのページにRuby+QtだとOpenGLが簡単にできると書いてあったので、試してみた。
-------------------- #!/usr/bin/env ruby require 'Qt4' require 'opengl' class MyGLWidget < Qt::GLWidget @@light_position = [0.25, 1.0, 0.25, 0.0] @@light_deffuse = [1.0, 1.0, 1.0] @@light_ambient = [0.25, 0.25, 0.25] @@light_specular = [1.0, 1.0, 1.0] @@diffuse = [1.0, 0.0, 0.0] # R,G,B @@ambient = [0.25, 0.25, 0.25] @@specular = [1.0, 1.0, 1.0] @@shininess = [32.0] @@vertices = [ -0.5, -0.5, -0.5, -0.5, -0.5, +0.5, +0.5, -0.5, +0.5, +0.5, -0.5, -0.5, -0.5, +0.5, -0.5, -0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, -0.5, -0.5, -0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, -0.5, +0.5, -0.5, -0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, -0.5, +0.5, -0.5, -0.5, -0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, -0.5, +0.5, -0.5, +0.5, -0.5, -0.5, +0.5, -0.5, +0.5, +0.5, +0.5, +0.5, +0.5, +0.5, -0.5 ].pack("f*") @@normals = [ +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f ].pack("f*") @@normals = [ +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, -1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0, +1.0, +0.0, +0.0 ].pack("f*") @@indices = [ 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 15, 14, 12, 14, 13, 16, 17, 18, 16, 18, 19, 20, 23, 22, 20, 22, 21 ].pack("i*") def initialize super end def initializeGL @vbuf = GL.GenBuffers 2 @ibuf = GL.GenBuffers 1 GL.BindBuffer GL_ARRAY_BUFFER, @vbuf[0] GL.BufferData GL_ARRAY_BUFFER, @@vertices.length, @@vertices, GL_STATIC_DRAW GL.BindBuffer GL_ARRAY_BUFFER, @vbuf[1] GL.BufferData GL_ARRAY_BUFFER, @@normals.length, @@normals, GL_STATIC_DRAW GL.BindBuffer GL_ELEMENT_ARRAY_BUFFER, @ibuf[0] GL.BufferData GL_ELEMENT_ARRAY_BUFFER, @@indices.length, @@indices, GL_STATIC_DRAW GL.BindBuffer GL_ARRAY_BUFFER, 0 GL.BindBuffer GL_ELEMENT_ARRAY_BUFFER, 0 GL.Enable GL::GL_DEPTH_TEST GL.Enable GL::GL_NORMALIZE GL.Enable GL::GL_CULL_FACE GL.Enable GL::GL_LIGHTING GL.Enable GL::GL_LIGHT0 GL.ClearColor 0.0, 0.0, 0.0, 1.0 GL.ClearDepth 1.0 end def resizeGL width, height GL.Viewport 0, 0, width, height GL.MatrixMode GL_PROJECTION GL.LoadIdentity GLU.Perspective 45.0, width.to_f/height.to_f, 1, 100 end def paintGL GL.Clear GL::COLOR_BUFFER_BIT | GL::GL_DEPTH_BUFFER_BIT GL.Lightfv GL::GL_LIGHT0, GL::GL_POSITION, @@light_position GL.Lightfv GL::GL_LIGHT0, GL::GL_DIFFUSE, @@light_deffuse GL.Lightfv GL::GL_LIGHT0, GL::GL_AMBIENT, @@light_ambient GL.Lightfv GL::GL_LIGHT0, GL::GL_SPECULAR, @@light_specular GL.Materialfv GL::FRONT, GL::GL_DIFFUSE, @@diffuse GL.Materialfv GL::FRONT, GL::GL_AMBIENT, @@ambient GL.Materialfv GL::FRONT, GL::GL_SPECULAR, @@specular GL.Materialfv GL::FRONT, GL::GL_SHININESS, @@shininess GL.MatrixMode GL_MODELVIEW GL.LoadIdentity GLU.LookAt 3, 4, 5, # from 0, 0, 0, # to 0, 1, 0 # up GL.BindBuffer GL_ELEMENT_ARRAY_BUFFER, @ibuf[0] GL.BindBuffer GL_ARRAY_BUFFER, @vbuf[0] GL.VertexPointer 3, GL_FLOAT, 0, 0 GL.BindBuffer GL_ARRAY_BUFFER, @vbuf[1] GL.NormalPointer GL_FLOAT, 0, 0 GL.EnableClientState GL_VERTEX_ARRAY GL.EnableClientState GL_NORMAL_ARRAY GL.DrawElements GL_TRIANGLES, @@indices.size/3, GL_UNSIGNED_INT, 0 GL.DisableClientState GL_VERTEX_ARRAY GL.DisableClientState GL_NORMAL_ARRAY end end app = Qt::Application.new(ARGV) widget = MyGLWidget.new widget.show app.exec ----------------------- RubyのGraphライブラリを見つけた。
さっそく使ってみた。 <インストール> % sudo gem install rgl % gem env ・・・ - INSTALLATION DIRECTORY: /var/lib/gems/1.8 ・・・ % cd /var/lib/gems/1.8/gems/grb-xxx % sudo ruby install.rb あと、dotファイルを画像に変換するためにgraphvizもいれておく % sudo apt-get install graphviz <サンプル> #!/usr/bin/env ruby require 'rgl/adjacency' require 'rgl/dot' dg=RGL::DirectedAdjacencyGraph[1,2, 2,3, 2,4, 4,5, 6,4, 1,6] dg.write_to_graphic_file('jpg') 補足 dg.write_to_graphic_file('svg') とすることで、SVG(XML)画像を作成することも可能。
Powered by 忍者ブログ
Design by © まめの
Copyright © [ ずくのない凡人の日記 ] All Rights Reserved. http://bambooflow.blog.shinobi.jp/ |