Hatena::ブログ(Diary)

Marginalia

2017-04-14

OpenGL の Ruby ミニライブラリ

もう一度 Ruby で OpenGL してみる - Marginalia
まだ開発中です。

miniopengl.rb

require 'opengl'
require 'glu'
require 'glut'
require 'matrix'
include Gl, Glu, Glut, Math


module MiniOpenGL
  class Tool
    def initialize
      @clear_clr = [0, 0, 0]
      @clear_color_alpha = 0
      @color_alpha = 1
      @buffering = 0
      @depth_buffer = 0
      @depth = 0
    end
    attr_accessor :clear_clr, :clear_color_alpha, :color_alpha, :buffering, :depth_buffer, :depth
    
    def draw(&bk)
      glutDisplayFunc(bk)
    end
    
    def reshape(&bk)
      glutReshapeFunc(bk)
    end
    
    def repeat(msec, value = 0, &bk)
      a = proc do
        yield
        glutTimerFunc(msec, a, value)
      end
      glutTimerFunc(msec, a, value)
    end
    
    def timer(msec, prc, value = 0)
      glutTimerFunc(msec, prc, value)
    end
    
    def block(c)
      glBegin(c)
      yield
      glEnd
    end
    
    def line(x1, y1, x2, y2)
      block(GL_LINES) do
        glVertex2d(x1, y1)
        glVertex2d(x2, y2)
      end
    end
    
    def line3(x1, y1, z1, x2, y2, z2)
      block(GL_LINES) do
        glVertex3d(x1, y1, z1)
        glVertex3d(x2, y2, z2)
      end
    end
    
    def color(r, g, b)
      glColor4f(r, g, b, @color_alpha)
    end
    
    def clear_color(r, g, b)
      @clear_clr = [r, g, b]
    end
    
    def clear
      glClear(GL_COLOR_BUFFER_BIT | @depth_buffer)
    end
    
    alias display glFlush
    alias finish glFinish
    alias redisplay glutPostRedisplay
    alias clr_matrix glLoadIdentity
    alias swap_buffers glutSwapBuffers
    
    def init_modelview
      modelview
      clr_matrix
    end
    
    def init_projection
      projection
      clr_matrix
    end
    
    def modelview
      glMatrixMode(GL_MODELVIEW)
    end
    
    def projection
      glMatrixMode(GL_PROJECTION)
    end
    
    alias mmap glMultMatrixd
    alias mload glLoadMatrixd
    alias translate glTranslated
    alias rotate glRotated
    alias scale glScaled
    alias viewport glViewport
    alias perspective gluPerspective
    alias frustum glFrustum
    alias mpush glPushMatrix
    alias mpop glPopMatrix
    
    def stack
      mpush
      yield
      mpop
    end
    
    alias look_at gluLookAt
    
    def key_in(&bk)
      glutKeyboardFunc(bk)
    end
    
    def draw_vertex(c, dim, vtx)
      glEnableClientState(GL_VERTEX_ARRAY)     #頂点配列の有効化
      glVertexPointer(dim, GL_DOUBLE, 0, vtx)  #頂点配列の定義
      glDrawArrays(c, 0, vtx.size / dim)     #頂点配列の描画指定
    end
    
    def draw_elements(c, dim, vtx, id)
      glEnableClientState(GL_VERTEX_ARRAY)
      glVertexPointer(dim, GL_DOUBLE, 0, vtx)
      glDrawElements(c, id.size, GL_UNSIGNED_BYTE, id)
    end
  end
  
  def self.app(width: 300, height: 300, x: 200, y: 100, buffering: :off, depth_buffer: :off, title: "MiniOpenGL", &bk)
    t = Tool.new
    
    t.buffering = GLUT_DOUBLE if buffering == :on
    if depth_buffer == :on
      t.depth_buffer = GL_DEPTH_BUFFER_BIT
      t.depth = GLUT_DEPTH
    end
    glutInit
    glutInitWindowSize(width, height)
    glutInitWindowPosition(x, y)
    glutCreateWindow(title)
    glutInitDisplayMode(GLUT_RGBA | t.buffering | t.depth)
    
    t.init_modelview
    t.init_projection
    t.clear
    
    t.instance_eval(&bk)
    
    glClearColor(t.clear_clr[0], t.clear_clr[1], t.clear_clr[2], t.clear_color_alpha)
    glutMainLoop
  end
end

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/obelisk2+marginalia/20170414/1492139406