RubyでOpenGLをやってみるとRuby/Qt/OpenGL で GLSL を使ってみるを参考にRubyでGLSLしてみた。シェーダーでは単純に赤く塗りつぶしてるだけなので立体感が無い状態だけど、そのうちLambertなりPhongあたりをやる予定。
。シェーダーのコンパイル時のエラーメッセージの取得方法がまだわかってなかったり。以下ソースコード
tea.rb
require "rubygems" require "opengl" require "glut" class Teapot def reshape(w,h) GL.Viewport(0,0,w,h) GL.MatrixMode(GL::GL_PROJECTION) GL.LoadIdentity() GLU.Perspective(45.0, w.to_f()/h.to_f(), 0.1, 100.0) end def display() GL.MatrixMode(GL::GL_MODELVIEW) GL.LoadIdentity() GLU.LookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) GL.ClearColor(0.0, 0.0, 0.0, 1.0) GL.Clear(GL::GL_COLOR_BUFFER_BIT | GL::GL_DEPTH_BUFFER_BIT) GL.Rotate(@rotX, 1, 0, 0) GL.Rotate(@rotY, 0, 1, 0) GL.UseProgram(@program); GLUT.SolidTeapot(0.5) GLUT.SwapBuffers() end def mouse(button,state,x,y) if button == GLUT::GLUT_LEFT_BUTTON && state == GLUT::GLUT_DOWN then @start_x = x @start_y = y @drag_flg = true elsif state == GLUT::GLUT_UP then @drag_flg = false end end def motion(x,y) if @drag_flg then dx = x - @start_x dy = y - @start_y @rotY += dx @rotY = @rotY % 360 @rotX += dy @rotX = @rotX % 360 end @start_x = x @start_y = y GLUT.PostRedisplay() end def initialize() @start_x = 0 @start_y = 0 @rotY = 0 @rotX = 0 @drag_flg = false GLUT.InitWindowPosition(100, 100) GLUT.InitWindowSize(300,300) GLUT.Init() GLUT.InitDisplayMode(GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH) GLUT.CreateWindow("Ruby de GLSL") GL.Enable(GL::GL_DEPTH_TEST) GL.FrontFace(GL::GL_CW) GL.Enable(GL::GL_AUTO_NORMAL) GL.Enable(GL::GL_NORMALIZE) GL.Enable(GL::GL_DEPTH_TEST) GL.DepthFunc(GL::GL_LESS) @program = create_program("tea.vert", "tea.frag") GLUT.ReshapeFunc(method(:reshape).to_proc()) GLUT.DisplayFunc(method(:display).to_proc()) GLUT.MouseFunc(method(:mouse).to_proc()) GLUT.MotionFunc(method(:motion).to_proc()) end def create_program(vert_name, frag_name) program = GL.CreateProgram() vert_shader = create_shader(vert_name, GL_VERTEX_SHADER) frag_shader = create_shader(frag_name, GL_FRAGMENT_SHADER) GL.AttachShader(program, vert_shader) GL.AttachShader(program, frag_shader) GL.LinkProgram(program) GL.GetProgramiv(program, GL_LINK_STATUS) GL.DeleteShader(vert_shader) GL.DeleteShader(frag_shader) return program end def create_shader(file_name, type) shader = GL.CreateShader(type) File.open(file_name, "rb") { |file| GL.ShaderSource(shader, file.read()) GL.CompileShader(shader) GL.GetShaderiv(shader, GL_COMPILE_STATUS) } return shader end def start() GLUT.MainLoop() end end Teapot.new().start()
tea.vert
void main(void) { gl_Position = ftransform(); }
tea.frag
void main (void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }