From: Bill Kelly Date: 2006-02-21T10:07:25+09:00 Subject: Re: can opengl work with an object method? ------=_NextPart_000_0BAE_01C6363F.CC189340 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit Hi, From: "anne001" > >I started with robot.rb in the sample folder of opengl-0.32g > > It comes with a display proc: > display = Proc.new {} > > which is called by a glut function > GLUT.DisplayFunc(display); > > When I place the exact same code in a method > class Node > def traversetree > end > end > > upperarm = Node.new > GLUT.DisplayFunc(upperarm.traversetree); > > displays the image in a different size, and does not respond to resize > or keyboard keys > > Any option? I have a very simplistic Ruby wrapper for GLUT... It's small so I've attached it to this email. You can subclass GLUTWindow, and just override the methods you want to add custom handlers for, for ex. reshape_func, keyboard_func, display_func, draw_scene. This example just overrides draw_scene... require 'opengl' require 'glut' require 'ftgl' case RUBY_PLATFORM when /mswin32/ DEFAULT_FONT = "C:/WINNT/Fonts/arial.ttf" when /darwin/ DEFAULT_FONT = "./demo/arial.ttf" # ??????????? else DEFAULT_FONT = "/usr/share/fonts/truetype/arial.ttf" end class MyWindow < GLUTWindow def initialize(left, top, wid, hgt, title, parent=nil) super font_filename = DEFAULT_FONT @font = FTGL::PixmapFont.new(font_filename) font_size = 24 @font.SetFaceSize(font_size) end def draw_scene super # GLUTWindow#draw_scene will clear the screen for us GL.PolygonMode(GL::FRONT_AND_BACK, GL::FILL) GL.RasterPos(100, 50) @font.Render("hello world") end end GLUT.Init GLUT.InitDisplayMode(GLUT::DOUBLE | GLUT::RGB) w = MyWindow.new(100, 100, 800, 600, "hello") GLUT.MainLoop The above example uses the 'ftgl' true-type-font extension[1], but that's not required to use the GLUTWindow wrapper itself. Hope this helps, Bill [1] http://rubyforge.org/projects/ruby-ftgl/ ------=_NextPart_000_0BAE_01C6363F.CC189340 Content-Type: application/octet-stream; name="glut-window.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="glut-window.rb" #!/usr/bin/env ruby # # # Usage example: # GLUT.Init # GLUT.InitDisplayMode(GLUT::DOUBLE | GLUT::RGB) # # main1 = GLUTWindow.new(100, 100, 800, 600, "foobar") # m1s1 = main1.new_child(10, 10, 320, 240, "foobar child1") # m1s2 = main1.new_child(400, 10, 320, 240, "foobar child2") # # main2 = GLUTWindow.new(100, 100, 800, 600, "xyzzy") # m2s1 = main2.new_child(10, 10, 320, 240, "xyzzy child1") # m2s2 = main2.new_child(400, 10, 320, 240, "xyzzy child2") # # GLUT.MainLoop # require 'opengl' require 'glut' # require 'ftgl' class GLUTWindow def initialize(left, top, wid, hgt, title, parent=nil) @title = title if parent @win = GLUT.CreateSubWindow(parent, left, top, wid, hgt) else GLUT.InitWindowSize(wid, hgt) GLUT.InitWindowPosition(left, top) @win = GLUT.CreateWindow(title) end @width, @height = 1, 1 # set by reshape_func @clear_color = [0.0, 0.0, 0.0] GLUT.DisplayFunc(build_display_func) GLUT.ReshapeFunc(build_reshape_func) GLUT.KeyboardFunc(build_keyboard_func) end def new_child(left, top, wid, hgt, title) GLUTWindow.new(left, top, wid, hgt, title, @win) end protected def draw_scene c = @clear_color GL.ClearColor(c[0], c[1], c[2], 0.0) GL.Clear(GL::COLOR_BUFFER_BIT) end def display_func draw_scene GLUT.SwapBuffers end def reshape_func(w, h) puts "reshape: win=#@win w=#{w} h=#{h}" @width, @height = w, h do_ortho end def keyboard_func(key, x, y) case key when 27 exit(0) end end def do_ortho w, h = @width, @height # Use the whole window. GL.Viewport(0, 0, w, h) # We are going to do some 2-D orthographic drawing. GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity size = ((w >= h) ? w : h) / 2.0 if w <= h aspect = h.to_f / w GL.Ortho(-size, size, -size*aspect, size*aspect, -100000.0, 100000.0) else aspect = w.to_f / h GL.Ortho(-size*aspect, size*aspect, -size, size, -100000.0, 100000.0) end # Make the world and window coordinates coincide so that 1.0 in # model space equals one pixel in window space. GL.Scaled(aspect, aspect, 1.0) # Now determine where to draw things. GL.MatrixMode(GL::MODELVIEW) GL.LoadIdentity end def build_display_func Proc.new {display_func} end def build_reshape_func Proc.new {|w, h| reshape_func(w, h)} end def build_keyboard_func Proc.new {|key, x, y| keyboard_func(key, x, y)} end end ------=_NextPart_000_0BAE_01C6363F.CC189340--